五大前提:
在以下文章中有说明,可以用于参考:
【App自动化测试】(十四)Android WebView测试方法中的 2.3.1 前提一:chromedriver安装
在以下文章中有说明,可以用于参考:
【App自动化测试】(十四)Android WebView测试方法中的 2.3.2 前提二:chromedriver版本选择正确
需要配置的capability参数:
browserName:chrome(新版本)/ browser(老版本)注意:还可使用标准的
appPackage和appActivity,写明浏览器的包名和activity的名字。但是使用browserName是最简单的
chromedriverExecutableDir:指定 chromedriver 可执行文件集合的目录chromedriverExecutableDir的示例图:可使用不同名字的后缀进行chromedriver的版本区分
showChromedriverLog:让appium 日志展示 chromedriver 的日志方便排查appium chromedriver 自动发现机制中常用的appium capability参数参数:
chromedriverExecutableDir: 指定 chromedriver 可执行文件集合的目录
chromedriverChromeMappingFile: 允许显式指定版本对应关系
showChromedriverLog: 让appium 日志展示 chromedriver 的日志方便排查appium capability参数设置 ——python:
def setup(self):
desired_caps = {
"platformName":"Android",
"deviceName":"emulator-5554",
"appPackage": "com.example.android.apis",
"appActivity":".ApiDemos",
"chromedriverExecutableDir":"E:\chromedriver_webview_test",
"chromedriverChromeMappingFile":"E:\chromedriver_webview_test\mapping.json",
"showChromedriverLog":True
}
self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
# 使用隐式等待
self.driver.implicitly_wait(10)
说明:
练习的时候使用了三款模拟器:Avd Android6.0、Avd Android7.0、MUMU.
Avd Android6.0、Avd Android7.0在使用edge inspect远程调试的时候,出现解析混乱,dom无法正常加载的情况。不知道是不是因为webview版本较低导致的。MUMU模拟器则正常。
"""
模拟器:MUMU
浏览器:MUMU自带浏览器
备注:
使用脚本访问百度并进行搜索的话会导致无法打开网址;但手动访问并搜索正常
相关问题解释帖子:https://ceshiren.com/t/topic/16195/4
"""
from appium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
class TestBrowser:
def setup_class(self):
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '6.0'
desired_caps['deviceName'] = '127.0.0.1:7555'
#python appium client 2.x 会默认使用w3c,因此此处使用chromeOptions参数吧w3c
desired_caps['chromeOptions'] = {'w3c': False}
#方式一:
desired_caps['browserName']= 'browser'
#方式二:
#还可使用标准的`appPackage`和`appActivity`,写明浏览器的包名和activity的名字。
#desired_caps['appPackage'] = 'com.android.browser'
#desired_caps['appActivity'] = 'com.android.browser.BrowserActivity'
#设置chromedriver所在路径
desired_caps['chromedriverExecutableDir']='E:\\chromedriver_webview_test'
#开启chromedriverlog
desired_caps['showChromedriverLog']=True
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_browser(self):
self.driver.get('https://www.sogou.com/')
#使用方式二`appPackage`和`appActivity`话,需要切换上下文,要不然会报错
#print(self.driver.contexts)
#WebDriverWait(self.driver, 5).until(lambda driver: len(self.driver.contexts) > 1)
#self.driver.switch_to.context('WEBVIEW_com.android.browser')
self.driver.find_element(By.ID,'keyword').click()
self.driver.find_element(By.ID,'keyword').send_keys("webview")
self.driver.find_element(By.CLASS_NAME,'qbtn').click()
assert_text=self.driver.find_element(By.XPATH,'//*[@class="vrResult"]//a/em').text
assert assert_text=="WebView"