多架构支持
纯webview
纯webview测试(只测试浏览器)的环境准备
手机端
被测浏览器:(不可以是第三方浏览器)safari for ios and chrome,chromium,or browser for Android
PC端
安装chrome浏览器或者chromium,并且能登录 https://www.google.com/ 下载对应手机浏览器对应的driver – 国内镜像地址:https://npm.taobao.org/mirrors/chromedriver/ – appium github: https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/web/chromedriver.md – https://blog.csdn.net/huilan_same/article/details/51896672(网上看到的一个链接) – 浏览迷网迷查看手机浏览器内核版本:https://liulanmi.com/labs/core.html
客户端代码:
desire_cap – “browserName”:“Browser” 或者 “browserName”:“Chrome” 这个是指定的浏览器 – “chromedriverExecutable”:r"c:\chrome\chromedriver.exe" 这个是指定的chromedriver的路径 – 如何查找app的版本:adb shell pm dump com.android.browser | grep version 不是所有的手机都可以,亲测在appium的日志中可以查看到 – chromedriver的默认地址:/Applications/Appium.app/Contents/Resources/app/node_modules/appium/node_modules/appium-chromedriver/chromedriver/mac
实战
在PC端Chrome中输入 chrome://inspect
实例:手机浏览器打开百度,并输入“appium”
import time
from appium import webdriver
class TestWebview :
def setup ( self) :
desired_caps = { }
desired_caps[ 'platformName' ] = 'Android'
desired_caps[ 'deviceName' ] = 'true'
desired_caps[ 'browserName' ] = 'Browser'
desired_caps[ 'chromedriverExecutable' ] = '/Users/yuanmeng/software/app/chromedriver_2.18'
desired_caps[ 'noReset' ] = "true"
desired_caps[ 'automationName' ] = "uiautomator2"
self. driver = webdriver. Remote( "http://127.0.0.1:4723/wd/hub" , desired_caps)
self. driver. implicitly_wait( 10 )
def teardown ( self) :
pass
def test_browser ( self) :
self. driver. get( "http://m.baidu.com" )
self. driver. find_element_by_css_selector( '#index-kw' ) . send_keys( 'appium' )
time. sleep( 10 )
混合webview
如何判断页面是webview
断网查看 看进度条 看顶部是否有关闭按钮 下拉刷新,页面是否刷新 下拉刷新的时候是否会有网页提供方 用工具查看
webview
是android系统提供能显示页面的系统控件(特殊的view) < android4.4 webview底层实现webkit内部 =android4.4 采用chromium作为webview底层支持,支持html5,css3,js webaudio:图形化的界面收听音频 webGL:页面3d效果的渲染 webRTC:直播等等,美颜
前提条件
PC端
浏览器能访问 https://www.google.com/ chromedriver下载对应的版本 – https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/web/chromedriver.md
手机端
代码
appPackage,appActivity desirecapability里面添加 chromedriverExecutable:driver路径 有一些webview可以被uiautomatorview查找到,但都不推荐,可能会出现兼容性的问题,比如text的显示字符串会不一样 如何查找当前webview的网页 – adb shell – logcat | grep http – 就能找到访问的http了
遇到的坑
设备
Android模拟器6.0默认支持webview操作(mumu不可以,genimotion和sdk自带的emulator可以) 其他模拟器和物理机需要打开app内开关(webview调试开关)
PC浏览器定位元素
chrome浏览器-62版本才可以更好的看见webview的内部,其他的版本都有一些bug 换成chromium浏览器可以避免很多坑,展示效果和速度要chrome要快
代码
有的设备可以使用find_element_acessibility_id(), 不同的设备渲染的页面不同,兼容性不适合 switch_to.context() 切换不同的context,切换原生和webview switch_to.window() 切换不同的窗口句柄,切换webview的不同的窗口
查看webview版本
adb shell list package|grep webview adb shell pm dump com.android.webview|grep version
例子1:进行上下文切换
import time
from appium import webdriver
class TestWebviewWebApp :
def setup ( self) :
desired_caps = { }
desired_caps[ 'platformName' ] = 'Android'
desired_caps[ 'deviceName' ] = 'true'
desired_caps[ 'appPackage' ] = 'com.example.android.apis'
desired_caps[ 'appActivity' ] = '.ApiDemos'
desired_caps[ 'chromedriverExecutable' ] = '/Users/yuanmeng/software/app/chromedriver_74'
desired_caps[ 'noReset' ] = "true"
self. driver = webdriver. Remote( "http://127.0.0.1:4723/wd/hub" , desired_caps)
self. driver. implicitly_wait( 10 )
def teardown ( self) :
self. driver. quit( )
def test_context ( self) :
self. driver. find_element_by_android_uiautomator(
'new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text("Views").instance(0));'
) . click( )
print ( self. driver. contexts)
self. driver. find_element_by_android_uiautomator(
'new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text("WebView").instance(0));'
) . click( )
print ( self. driver. contexts)
self. driver. switch_to. context( self. driver. contexts[ - 1 ] )
print ( self. driver. page_source)
time. sleep( 3 )
例子2:切window
import time
from appium import webdriver
from appium. webdriver. common. mobileby import MobileBy
from selenium. webdriver. support import expected_conditions
from selenium. webdriver. support. wait import WebDriverWait
class TestWebviewHybirdAppXueqiu :
def setup ( self) :
desired_caps = { }
desired_caps[ 'platformName' ] = 'Android'
desired_caps[ 'deviceName' ] = 'true'
desired_caps[ 'appPackage' ] = 'com.xueqiu.android'
desired_caps[ 'appActivity' ] = '.view.WelcomeActivityAlias'
desired_caps[ 'chromedriverExecutable' ] = '/Users/yuanmeng/software/app/chromedriver_74'
desired_caps[ 'noReset' ] = "true"
self. driver = webdriver. Remote( "http://127.0.0.1:4723/wd/hub" , desired_caps)
self. driver. implicitly_wait( 10 )
def teardown ( self) :
self. driver. quit( )
def test_open_account ( self) :
print ( self. driver. contexts)
self. driver. find_element_by_xpath( "//*[@text='交易']" ) . click( )
print ( self. driver. contexts)
self. driver. switch_to. context( self. driver. contexts[ - 1 ] )
print ( self. driver. window_handles)
open_account_locator = ( MobileBy. CSS_SELECTOR, '.trade_home_onsale-container_Pgr' )
WebDriverWait( self. driver, 15 ) . until(
expected_conditions. element_to_be_clickable( open_account_locator) )
self. driver. find_element( * open_account_locator) . click( )
print ( self. driver. window_handles)
window_handles = self. driver. window_handles
self. driver. switch_to. window( window_handles[ - 1 ] )
submit_button_locator = ( MobileBy. CSS_SELECTOR, '.form > div[class="btn-submit"]' )
WebDriverWait( self. driver, 40 ) . until(
expected_conditions. element_to_be_clickable( submit_button_locator) )
self. driver. find_element( MobileBy. CSS_SELECTOR, '#phone-number' ) . send_keys( '12345678901' )
time. sleep( 10 )