2015-01-28 24 views
0
  • Тестирование простого калькулятора на Android-устройстве.
  • Пробовал много методов, чтобы найти элемент, но не удалось.
  • Информация, которую я могу получить от UI Automator Viewer, - это имя класса, название кнопки, координаты. Я просто хочу найти кнопку «7» и нажать ее.

коды:Как найти элементы с Appium и Python?

import unittest, time, os 
from appium import webdriver 
from time import sleep 




class Android_Maaii(unittest.TestCase): 
    "Class to run tests against the ATP WTA app" 
    def setUp(self): 
     "Setup for the test" 
     PATH = lambda p: os.path.abspath(os.path.join(os.path.dirname(__file__), p)) 
     desired_caps = {} 
     desired_caps['platformName'] = 'Android' 
     desired_caps['platformVersion'] = '4.2.2' 
     desired_caps['deviceName'] = 'Galaxy S4' 
     desired_caps['app'] = PATH('C:\Python34\MyCalculator.apk')   
     # Since the app is already installed launching it using package and activity name 
     desired_caps['appPackage'] = 'com.example.mythirdapp' 
     desired_caps['appActivity'] = '.MainActivity' 
     # Adding appWait Activity since the activity name changes as the focus shifts to the ATP WTA app's first page 
     desired_caps['appWaitActivity'] = 'com.example.mythirdapp.MainActivity' 
     self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) 

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

    def test_maaii(self): 
     #"Testing the ATP WTA app " 
     self.driver.implicitly_wait(30) 
     time.sleep(5) 

     print ("Slept for 5 sec...") 

     element = self.driver.find_element_by_name('7') 

#---START OF SCRIPT 
if __name__ == '__main__': 
    suite = unittest.TestLoader().loadTestsFromTestCase(Android_Maaii) 
    unittest.TextTestRunner(verbosity=2).run(suite) 

Error Report:

ERROR: test_maaii (__main__.Android_Maaii) 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
    File "C:\Python34\testfiles\MaaiiTest_new.py", line 59, in test_maaii 
    element = self.driver.find_element_by_name('7') 
    File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 302, in find_element_by_name 
    return self.find_element(by=By.NAME, value=name) 
    File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 662, in find_element 
    {'using': by, 'value': value})['value'] 
    File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 173, in execute 
    self.error_handler.check_response(response) 
    File "C:\Python34\lib\site-packages\appium\webdriver\errorhandler.py", line 29, in check_response 
    raise wde 
    File "C:\Python34\lib\site-packages\appium\webdriver\errorhandler.py", line 24, in check_response 
    super(MobileErrorHandler, self).check_response(response) 
    File "C:\Python34\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 166, in check_response 
    raise exception_class(message, screen, stacktrace) 
selenium.common.exceptions.WebDriverException: Message: 'An unknown server-side error occurred while processing the command.' 

====================================================================== 
ERROR: test_maaii (__main__.Android_Maaii) 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
    File "C:\Python34\testfiles\MaaiiTest_new.py", line 31, in tearDown 
    self.driver.quit() 
    File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 455, in quit 
    self.execute(Command.QUIT) 
    File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 173, in execute 
    self.error_handler.check_response(response) 
    File "C:\Python34\lib\site-packages\appium\webdriver\errorhandler.py", line 24, in check_response 
    super(MobileErrorHandler, self).check_response(response) 
    File "C:\Python34\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 138, in check_response 
    if 'message' in value: 
TypeError: Type str doesn't support the buffer API 

---------------------------------------------------------------------- 
Ran 1 test in 47.363s 

FAILED (errors=2) 

Что такое реальная проблема? Я пробовал все методы поиска элементов, но не смог.

ответ

3

Вы можете использовать uiautomatorviewer присутствует в Android SDK, чтобы получить идентификатор каждый элемент, то вы можете использовать идентификатор элемента. Затем используйте

self.driver.implicitly_wait(10) 
self.driver.find_element_by_id("com.android.calculator2:id/digit7").click() 
self.driver.implicitly_wait(10) 

enter image description here

+0

не видел ресурсы идентификатора, потому что мое устройство только Android 4.2.2. –

+0

Кажется ар i level> 18, тогда можно получить идентификатор ресурса. Могу ли я получить ваш скайп или другой контакт? Срочно о помощи. –

0

Я 0 в Python, я использую Java, но я думаю, что у меня была такая же проблема в последнее время. Я мог найти все элементы, действовать с ними, но не мог нажать кнопку. Чтобы найти элементы, используйте Appium Inspector.

А для отдыха - This - моя проблема, уже решена. Посмотрите решение под.

надеюсь, что это поможет. ;)

1

Попробуйте использовать resource_id элемента вместо текста. Если вы просмотрите в программе просмотра automator, вы увидите следующие свойства для элемента. Например, номер 7 калькулятора имеет, com.android.calculator2: id/digit7 как resource_id.

Так что использование может использовать это в вашем коде, как: элемент = self.driver.find_element_by_id ("com.android.calculator2: идентификатор/Digit7)

Смежные вопросы