51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 1487|回复: 1
打印 上一主题 下一主题

Appium在Android平台实战训练

[复制链接]
  • TA的每日心情
    郁闷
    2022-8-29 14:43
  • 签到天数: 1 天

    连续签到: 1 天

    [LV.1]测试小兵

    跳转到指定楼层
    1#
    发表于 2018-2-23 15:25:46 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    1、获取控件的工具


    SDK/tools/uiautomatorviewer.bat
    双击打开,如下:

    注意:这个工具只能是在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上:

    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()
    点击过程,有一个控件没有找到,写脚本要保证相应的控件存在。


    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有帐号?(注-册)加入51Testing

    x
    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-9-21 00:42 , Processed in 0.062612 second(s), 23 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表