robotframework:appium切换webview后,在webview里滑动屏幕
问题:
在用robot写手机淘宝app的自动化时,打开手机淘宝后,点击天猫国际,跳转到天猫国际页面,天猫国际页面是H5,
需要切换到对应的webview,切换到webview后,点击美妆菜单,跳转到美妆页面,美妆页面也是H5,
与天猫国际页面的webview一样,如果需要在webview里滑动页面,例如向下滑动到某个元素出现在屏幕上,直接在webview中使用swipe是不行的,
因为swipe只能在NATIVE_APP中滑动,不能在webview中执行。
解决:
在美妆页面中后,先切换回NATIVE_APP,执行swipe滑动到指定元素,滑动完后,再重新切换回对应的webview。
代码示例:
*** Settings *** Suite Setup Suite Teardown Library AppiumLibrary *** Variables *** *** Test Cases *** 手机淘宝 Open Application http://localhost:4723/wd/hub platformName=Android platformVersion=19 deviceName=127.0.0.1:21503 app=${CURDIR}${/}shoujitaobao_178.apk appPackage=com.taobao.taobao ... appActivity=com.taobao.tao.welcome.Welcome unicodeKeyboard=True resetKeyboard=True Wait Until Page Contains Element xpath=//android.widget.TextView[@text=\"天猫国际\"] #等待手机淘宝打开完成 ${width} Get Window Width ${height} Get Window Height ${x1}= Evaluate int(${width} * 0.5) ${y1}= Evaluate int(${height} * 0.75) ${y2}= Evaluate int(${height} * 0.25) Click Element xpath=//android.widget.TextView[@text=\"天猫国际\"] #点击天猫国际 @{contexts} Get Contexts #获取contexts Log Many @{contexts} #打印contexts ${cur_context} Get Current Context #获取当前contexts log 当前context log ${cur_context} #打印当前contexts log 开始切换 Switch To Context WEBVIEW_com.taobao.taobao #切换contexts log 切换成功 ${cur_context} Get Current Context log ${cur_context} Page Should Contain Text 美妆 Click Element xpath=//*[@id="J_5132393350"]/div/div/div/a[2] #点击美妆菜单 Switch To Context NATIVE_APP Switch To Context WEBVIEW_com.taobao.taobao Page Should Contain Text 明星面膜 ${cur_context_1} Get Current Context #获取当前contexts log 当前context log ${cur_context_1} #打印当前contexts Comment ${page} Get Source Comment log ${page} Comment Click Element xpath=//*[@id="J_1456082907"]/div/div/div[1]/a[1]/div[1]/img Switch To Context NATIVE_APP Swipe ${x1} ${y1} ${x1} ${y2} 1000 sleep 5 Swipe ${x1} ${y1} ${x1} ${y2} 1000 sleep 5 [Teardown] Close All Applications 手机淘宝滑动 Open Application http://localhost:4723/wd/hub platformName=Android platformVersion=19 deviceName=127.0.0.1:21503 app=${CURDIR}${/}shoujitaobao_178.apk appPackage=com.taobao.taobao ... appActivity=com.taobao.tao.welcome.Welcome unicodeKeyboard=True resetKeyboard=True Wait Until Page Contains Element xpath=//android.widget.TextView[@text=\"天猫国际\"] #等待手机淘宝打开完成 ${width} Get Window Width ${height} Get Window Height ${x1}= Evaluate int(${width} * 0.5) ${y1}= Evaluate int(${height} * 0.75) ${y2}= Evaluate int(${height} * 0.25) Swipe ${x1} ${y1} ${x1} ${y2} 1000 sleep 5 [Teardown] Close All Applications *** Keywords ***
截图: