簡體   English   中英

如何使用appium對Android UI的python腳本中的垂直邊界/坐標執行點擊/點擊操作

[英]How to perform click/tap operation to the perticular bound/co-ordinates in python script for Android UI using appium

我正在使用appium在python腳本中為Android應用程序編寫測試用例。 我想在特定范圍/坐標中執行點擊/點擊操作。 但我無法執行。任何人都可以告訴我該怎么做。

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)

直到開始,一切正常。 在用戶界面中之后,我沒有任何元素名稱或ID。 我只需要單擊該元素即可。 我想單擊綁定[918,154] [1086,324]的資源ID為“ com.abc.android.cap:id / friends_selection_list”,請告訴我該怎么做。

希望您對此元素有content-description 在這種情況下,您可以使用它並在TouchAction類中tap方法來點擊它。

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

U可以使用該元素的Xpath來查找xpath,您將需要appium檢查器(僅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)

您可以通過“ 觸摸動作”來實現。

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) 。最后一個數字是抽頭的持續時間。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM