【问题标题】:How to perform click/tap operation to the perticular bound/co-ordinates in python script for Android UI using appium如何使用appium对Android UI的python脚本中的特定绑定/坐标执行单击/点击操作
【发布时间】:2015-03-08 13:38:50
【问题描述】:

我正在使用 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)

直到'让我们开始它工作正常。在 UI 中之后,我没有任何元素名称或 ID。我只有特别绑定单击该元素。我想点击绑定的[918,154][1086,324]资源ID,上面绑定的是“com.abc.android.cap:id/friends_selection_list”请告诉我之后怎么做。

【问题讨论】:

  • 你找到解决方案了吗?
  • 不,我没有得到任何解决方案
  • 我还有一个问题,你能提出建议吗?请查看此链接stackoverflow.com/questions/27898661/…
  • 查看我的评论...

标签: android python selenium appium


【解决方案1】:

我希望你有这个元素的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()

【讨论】:

  • surya,我想在该视图上执行单击/点击操作,有 5 个按钮具有相同的描述 ID。我只有每个按钮的坐标/边界。所以请告诉我如何在特定坐标/边界上执行点击/单击
  • 好的。在这种情况下,使用查找多个元素的方法,然后从该列表中获取所需的按钮以单击它。也就是说,您需要使用“self.driver.find_elements_by_accessibility_id”。我建议,只有在其他选项都不起作用时才使用基于协调的方法。
  • 是的,surya 我已经尝试了所有选项。所以我认为唯一的选择是只有坐标
  • 而且它没有内容描述ID。所以我只有最后一个选择,那就是坐标
【解决方案2】:

你可以使用该元素的 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)

【讨论】:

    【解决方案3】:

    您可以通过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)。最后一个数字是点击的持续时间。

    【讨论】:

      猜你喜欢
      • 2019-08-08
      • 1970-01-01
      • 1970-01-01
      • 2013-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-25
      相关资源
      最近更新 更多