51Testing软件测试论坛

标题: Appium在Android平台实战训练 [打印本页]

作者: 恭喜发财dife    时间: 2018-2-23 15:25
标题: Appium在Android平台实战训练
1、获取控件的工具


SDK/tools/uiautomatorviewer.bat
双击打开,如下:
[attach]110914[/attach]
注意:这个工具只能是在Android4.2以上的系统上使用。

读出的信息有:

resource-id
text
index
class
package
...
2、实现一些基本操作的代码


以下代码实现了启动搜狗浏览器,点击“菜单”,然后再点击“设置”,进入设置页面后,滑动到页面最下方,
之后点击“下载路径更改”这样的一系列操作。
  1. #coding=utf-8
  2. from appium import webdriver
  3. import os
  4. import unittest
  5. from appium import webdriver
  6. from time import sleep
  7. import time
  8. import urllib2


  9. # Returns abs path relative to this file and notcwd
  10. #PATH = lambda p: os.path.abspath(
  11. #    os.path.join(os.path.dirname(__file__),p)
  12. #)
  13. #点击设置中“下载路径更改”,验证直接进入路径设置界面,不会弹出路径选择框
  14. desired_caps = {}
  15. desired_caps['platformName'] = 'Android'
  16. desired_caps["platformVersion"] = '6.0.1'
  17. desired_caps["deviceName"] ='0815f8085adc2b04'
  18. #desired_caps["deviceName"] ='4d005802acc22185'
  19. #desired_caps["deviceName"] ='0123456789'
  20. desired_caps["appPackage"] ='sogou.mobile.explorer'
  21. #desired_caps['appActivity'] = '.BrowserActivity'
  22. desired_caps['appActivity'] = '.NoDisplayActivity'
  23. driver = webdriver.Remote('http://localhost:4723/wd/hub',desired_caps)
  24. time.sleep(10)
  25. #btnSkip =driver.find_element_by_id('sogou.mobile.explorer:id/btn_skip')
  26. #print len(btnSkip)
  27. #if len(btnSkip) > 0:
  28.    #driver.find_element_by_id('sogou.mobile.explorer:id/btn_skip').click()
  29. driver.find_element_by_id('sogou.mobile.explorer:id/toolbar_menu').click()
  30. time.sleep(5)
  31. driver.find_element_by_name('设置').click()
  32. time.sleep(5)
  33. #滑动到底,找到下载路径更改sogou.mobile.explorer:id/set_download_path
  34. driver.swipe(700, 2435, 700, 200,1000)
  35. time.sleep(5)
  36. driver.find_element_by_id('sogou.mobile.explorer:id/set_download_path').click()
  37. time.sleep(5)
复制代码
3、点击物理按键


有时候我们遇到一些需要点击手机物理返回按键,或者home键等操作,总结如下:
adb shell input keyevent <key code number or name>
点击home键:
adb shell input keyevent 3
点击back键:
adb shell input keyevent 4
4、运行过程中报错整理


【报错一】
Appium的log上:
[attach]110915[/attach]
python工具上:
C:\Python27\python.exeD:/python/appium/download/downloadmodule/downloadpath.py
Traceback (most recent call last):
  File"D:/python/appium/download/downloadmodule/downloadpath.py", line 24,in <module>
    driver = webdriver.Remote('http://localhost:4723/wd/hub',desired_caps)
  File "build\bdist.win32\egg\appium\webdriver\webdriver.py",line 36, in __init__
  File"C:\Python27\lib\site-packages\selenium-2.50.0-py2.7.egg\selenium\webdriver\remote\
webdriver.py",line 87, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File"C:\Python27\lib\site-packages\selenium-2.50.0-py2.7.egg\selenium\webdriver\remote\
webdriver.py",line 141, in start_session
    'desiredCapabilities': desired_capabilities,
  File "C:\Python27\lib\site-packages\selenium-2.50.0-py2.7.egg\selenium\webdriver\remote\
webdriver.py",line 201, in execute
    self.error_handler.check_response(response)
  File"C:\Python27\lib\site-packages\selenium-2.50.0-py2.7.egg\selenium\webdriver\remote\
errorhandler.py",line 188, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: A new session could notbe created.
(Original error: sogou.mobile.explorer/.NoDisplayActivity neverstarted. Current: sogou.mobile.
explorer/.BrowserActivity)


Process finished with exit code 1

解决方案:
(1)将设备信息和系统信息修改正确即可,由于我信息填写的还是之前用的手机,后期换了
手机,忘记修改相应的信息,故其中的手机设置信息很重要。
(2)手机的控件没有刷新,需要用UI AutomatorViewer刷新一下
(3)退出浏览器,且重启Appium

【报错二】
python报错:
C:\Python27\python.exe D:/python/appium/download/downloadmodule/downloadpath.py
Traceback (most recent call last):
  File"D:/python/appium/download/downloadmodule/downloadpath.py", line 27,in <module>
   driver.find_element_by_id('sogou.mobile.explorer:id/btn_skip')
  File "C:\Python27\lib\site-packages\selenium-2.50.0-py2.7.egg\selenium\webdriver\remote\
webdriver.py",line 234, in find_element_by_id
    return self.find_element(by=By.ID, value=id_)
  File"C:\Python27\lib\site-packages\selenium-2.50.0-py2.7.egg\selenium\webdriver\remote\
webdriver.py",line 712, in find_element
    {'using': by, 'value': value})['value']
  File"C:\Python27\lib\site-packages\selenium-2.50.0-py2.7.egg\selenium\webdriver\remote\
webdriver.py",line 201, in execute
    self.error_handler.check_response(response)
  File "build\bdist.win32\egg\appium\webdriver\errorhandler.py",line 29, in check_response
selenium.common.exceptions.NoSuchElementException:Message: An element could not be
located on the page using the given searchparameters.

解决方案:
这是由于
driver.find_element_by_id('sogou.mobile.explorer:id/btn_skip').click()
driver.find_element_by_id('sogou.mobile.explorer:id/toolbar_menu').click()
点击过程,有一个控件没有找到,写脚本要保证相应的控件存在。



作者: 梦想家    时间: 2018-2-28 16:06





欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2