如何使用appium对Android UI的python脚本中的特定边界/坐标执行单击/点击操作

时间:2015-01-10 07:48:43

标签: android python selenium appium

我正在使用appium在android应用程序的python脚本中编写测试用例。我想在特定的边界/坐标中执行点击/点击操作。但是我无法执行它。任何人都可以告诉我该怎么做。

class 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)

Till'让我们开始它工作正常。在用户界面中,我没有任何元素名称或ID。我只有特定的界限才能点击该元素。我想点击绑定[918,154] [1086,324]上面绑定的资源ID是" com.abc.android.cap:id / friends_selection_list"请告诉我怎么做。

3 个答案:

答案 0 :(得分:0)

我希望你有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()

答案 1 :(得分:0)

你可以使用该元素的Xpath,为了找到xpath你将需要appium inspector,这只在MAC OSX中受支持,因此你必须使用mac并在MAC上配置Android SDK和Appium才能使其运行。 代码将是这样的:

 #-*- 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)

答案 2 :(得分:0)

您可以使用Touch Actions实现此目的。

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

或者像这样:

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

第一个变量是最多5 x,y坐标的列表(例如self.driver.tap([(495,757), (200,500)], 500)。最后一个数字是点击的持续时间。