1、手机或者模拟器连接上之后,打开要测试的app,开始获取应用的包名等信息:(逍遥模拟器的命令:adb connect 127.0.0.1:21503)
1)使用adb命令来获取当前打开应用的包名
adb shell dumpsys window | findstr mCurrentFocus
返回:
其中: com.sankuai.meituan是包名,com.sankuai.meituan.activity.MainActivity是当前打开的activity类名
查询包名的另一种方法:
adb shell "logcat|grep -i displayed.*.meituan"
2、开始写代码:
1)在robotframework上写代码
1-1)发现写的跑不通,先用appium-inspector录制一下,再改改
1-2)生成的代码如下:
from appium import webdriver
caps = {}
caps["platformName"] = "android"
caps["appium:deviceName"] = "demo"
caps["appium:appPackage"] = "com.sankuai.meituan"
caps["appium:appActivity"] = "com.meituan.android.pt.homepage.activity.MainActivity"
caps["appium:ensureWebviewsHavePages"] = True
caps["appium:nativeWebScreenshot"] = True
caps["appium:newCommandTimeout"] = 3600
caps["appium:connectHardwareKeyboard"] = True
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", caps)
贴到pycharm上运行一下可以了,接下来把它改到robotframework上
1-3)关键字和用例
关键字上导入AppiumLibrary;
用例上导入关键字:
运行了一下,正常打开了app
3、接下来就开始干吧:
还是使用appium 桌面端定位元素得到的脚本如下:
from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy
import time
caps = {}
caps["platformName"] = "android"
caps["appium:deviceName"] = "demo"
caps["appium:appPackage"] = "com.sankuai.meituan"
caps["appium:appActivity"] = "com.meituan.android.pt.homepage.activity.MainActivity"
caps['autoGrantPermissions']="true"
#caps["appium:ensureWebviewsHavePages"] = True
#caps["appium:nativeWebScreenshot"] = True
#caps["appium:newCommandTimeout"] = 3600
#caps["appium:connectHardwareKeyboard"] = True
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", caps)
driver.implicitly_wait(5)
driver.find_element(MobileBy.ID, "com.sankuai.meituan:id/permission_agree_btn").click()
time.sleep(2)
driver.find_element(MobileBy.ID, "com.sankuai.meituan:id/btn_open_location").click()
time.sleep(2)
#driver.find_element(MobileBy.ID, "com.sankuai.meituan:id/btn_ok").click()
time.sleep(4)
driver.find_element(MobileBy.XPATH,"//*[@content-desc='外卖']").click()
time.sleep(2)
driver.find_element(MobileBy.XPATH,"//*[@class='android.widget.TextView' and @index=0]").click()
time.sleep(2)
if driver.find_element(MobileBy.XPATH,"//*[@content-desc='关闭']"):
driver.find_element(MobileBy.XPATH,"//*[@content-desc='关闭']").click()
time.sleep(2)
driver.find_element(MobileBy.ID,"com.sankuai.meituan:id/txt_search_normal").send_keys("蛋糕")
time.sleep(2)
转化成rf脚本:
在这个过程中本来要使用uiautomatorviewer.bat定位元素的,结果发现这个一直报错:
网上各种方法都试了没用,最后发现这个uiautomatorviewer.bat和我appium server不兼容;只要appium server开启了这个uiautomatorviewer.bat就开始报错