如何执行点击/点按操作势必/坐标在python脚本使用appium Android的UI所述特定的(

2019-10-21 14:17发布

我使用appium写在python脚本测试用例的Android应用程序。 我想在一个特定的范围/坐标进行点击/点按操作。 但我不能够执行任何it.Can一个告诉我该怎么做。

类firstTest(unittest.TestCase生成):

def tearDown(self):
    "Tear down the test"
    self.driver.quit()

def test_single_player_mode(self):
    time.sleep(5)
    element = self.driver.find_element_by_name("Enter your Email ID or User name")
    element.send_keys("username")
    element = self.driver.find_element_by_name("Let's get started!")
    element.click()
    time.sleep(5)

直到“让我们开始它工作正常。 它经过UI,我没有任何元素名称或ID。 我只是特别势必单击该元素。 我想在束缚[918154] [1086,324]资源ID点击上面的约束是“com.abc.android.cap:ID / friends_selection_list”请告诉我如何以后做的事。

Answer 1:

我希望你有content-description了这个元素。 在这种情况下,你可以使用它,就可以挖掘tap在方法TouchAction类。

ele = self.driver.find_element_by_accessibility_id('content-description of your element')
action = TouchAction(self.driver)
action.tap(ele).perform()


Answer 2:

U可以使用XPath该元素的,寻找的XPathü将需要appium检查,这是在MAC OSX唯一支持的,所以你必须使用Mac和为了得到它的工作配置Android SDK和Appium的MAC。 代码将是这样的:

 #-*- coding: utf-8 -*-
__author__ = 'chetan.krishna'
import os
import time
import unittest
from time import sleep
from appium import webdriver
from pylab import *
from teamcity import is_running_under_teamcity
from teamcity.unittestpy import TeamcityTestRunner
import logging

# Returns absolute path relative to this file
PATH = lambda p: os.path.abspath(
    os.path.join(os.path.dirname(__file__), p)
)


class AvavntiAndroidTests(unittest.TestCase):
    def setUp(self):
        desired_caps = {}
# Specify platform below(Android, iOS)
        desired_caps['platformName'] = 'Android'
# Specify OS version(Settings->About phone -> android version)
        desired_caps['platformVersion'] = '4.4.4'
# Obtain the Device name from Adb[For Android](Terminal Command: "adb devices")
        desired_caps['deviceName'] = '4d0081004c8741a9'

        desired_caps['noReset'] = False
# Specify the path to Application
        desired_caps["app"] = PATH('AvantiMarket_v1.4.apk')
# Wait for email login activity to appear
        desired_caps["appWaitActivity"]= ('com.android.avantimarket.ui.activity.EmailLoginActivity')
        self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

    def tearDown(self):
# end the session
        self.driver.quit()


    def test_Avanti(self):
# wait for the login screen to appear
        self.driver.implicitly_wait(20)
# set values for plotting pass and fail results
        nPass = 0
        nFail = 0
        print('Checking login for registered user')
    # Logging in as indiaone@avantilab.org
elf.driver.implicitly_wait('20')
        print('Trying to login with abc@abc.org')
        self.driver.find_element_by_xpath('//UIAApplication[1]/UIAWindow[2]/UIATextField[1]').send_keys('abc@abc.org.org')
        self.driver.find_element_by_xpath('//UIAApplication[1]/UIAWindow[2]/UIASecureTextField[1]').send_keys('12345678')
        self.driver.hide_keyboard()
        self.driver.implicitly_wait(10)
        self.driver.find_element_by_xpath('//UIAApplication[1]/UIAWindow[2]/UIAButton[3]').click()
        time.sleep(20)
        self.driver.implicitly_wait(99)
        try:
            self.driver.find_element_by_xpath('//UIAApplication[1]/UIAWindow[2]/UIASecureTextField[1]')
        except :
            print('Login failed, please check credentials and network')
        else:
            print('Login successful, creating pin')

        self.driver.find_element_by_xpath('//UIAApplication[1]/UIAWindow[2]/UIASecureTextField[1]').send_keys('1')
        self.driver.find_element_by_xpath('//UIAApplication[1]/UIAWindow[2]/UIASecureTextField[2]').send_keys('1')
        self.driver.find_element_by_xpath('//UIAApplication[1]/UIAWindow[2]/UIASecureTextField[3]').send_keys('1')
        self.driver.find_element_by_xpath('//UIAApplication[1]/UIAWindow[2]/UIASecureTextField[4]').send_keys('1')
        self.driver.find_element_by_xpath('//UIAApplication[1]/UIAWindow[2]/UIASecureTextField[5]').send_keys('1')
        self.driver.find_element_by_id('com.android.avantimarket:id/m_emailTextField').send_keys('abc@abc.org.org')
        self.driver.back()
        nPass= nPass+1
        self.driver.implicitly_wait(20)
        self.driver.find_element_by_id('com.android.avantimarket:id/m_passwordTextField').send_keys('12345678')
        nPass= nPass+1
        self.driver.back()
        self.driver.implicitly_wait(10)
        self.driver.find_element_by_name('SIGN IN').click()
        self.driver.implicitly_wait(30)
        time.sleep(5)
# validating for successful login
        try:
            self.driver.find_element_by_id('com.android.avantimarket:id/create_pin_fields_container')
            print('Login successful')
            nPass= nPass+1
        except:
            print('Login failed')
            nFail = nFail + 1
        else:
# Creating pin required for login
            print('Creating Pin for user')
        self.driver.find_element_by_id('com.android.avantimarket:id/create_pin_fields_container').send_keys('1111')
        self.driver.find_element_by_id('com.android.avantimarket:id/reEnter_pin_fields_container').send_keys('1111')
        self.driver.back()
        self.driver.implicitly_wait(20)
        self.driver.find_element_by_id('com.android.avantimarket:id/m_saveButton').click()
        self.driver.implicitly_wait(10)
        self.driver.find_element_by_id('com.android.avantimarket:id/btn_cominsoon_Yes').click()
        self.driver.implicitly_wait(10)
        time.sleep(8)


Answer 3:

你可以做到这一点触摸动作 。

action = TouchAction(self.driver)
action.tap(element=None, x=100, y=600, count=2).perform()

或者是这样的:

self.driver.tap([(100,600)], 500)

其中,所述第一变量是高达5×的列表,y坐标(例如self.driver.tap([(495,757), (200,500)], 500)最后一个号码是抽头的持续时间。



文章来源: How to perform click/tap operation to the perticular bound/co-ordinates in python script for Android UI using appium