appium-chromium-driver
是Appium
的驱动,用于基于Chromium
浏览器(例如Chrome
)的自动化测试。对基于Chromium
浏览器的测试已经有ChromeDrvier
,那为何还需要appium-chromium-driver
,事实上,本驱动就是基于``ChromeDrvier做了一些对
ChromeDriver的封装实现,基础框架和技术与
ChromeDriver`完全一致,但是,它有如下几个优势:
-
当
Appium
环境部署好后,只需要安装该驱动即可自动化测试基于Chromium
的浏览器; -
不需要下载特定的
ChromeDrvier
版本,本驱动会负责自动下载与浏览器匹配的ChromeDrvier
版本; -
可以利用
Appium
插件和特性的生态系统优势,ChromeDrvier
是不支持的。
备注:appium-chromium-driver
是Appium
基于ChromeDriver
的实现,而ChromeDriver
则是遵循W3C
的WebDriver
规范的实现,WebDriver
规范衍生于Selenium WebDriver
。可以参考ChromeDriver
、Selenium WebDriver
、WebDriver
参考的简介。
安装
假设已经安装Appium
服务端(版本2.0
及以上),如果还未安装,可以参考Appium
环境搭建。此外假设你已经安装了Chrome
浏览器(或其他基于Chromium
的浏览器)。
利用如下命令可以查看已经安装的驱动列表信息:
appium driver list
可以看到chromium
驱动还未安装,在命令行执行如下命令安装appium-chromium-driver
驱动:
appium driver install chromium
使用
要启动一个连接到该驱动的会话,客户端需要提供至少如下三个能力参数:
能力名称 | 具体描述 |
---|---|
platformName |
必选,根据实际的系统,可能是如下值的一个:macOS 、Linux 或Windows 。 |
browserName |
必选,该名称用于选择要启动的WebDriver 驱动,例如设置为chrome (或chromium )用于启动ChromeDriver ,设置为MicrosoftEdge (或msedge )用于启动MsedgeDriver 。 |
appium:automationName |
必选,对于本驱动,该值只能是Chromium 。 |
下面的例子展示了,如何使用上面三个参数就能进行简单的Chrome
浏览器自动化测试,与Selenium
不同,它无需指定ChromeDriver
,Appium
会自动根据本设备的Chrome
浏览器版本选择(下载)对应的ChromeDriver
。也可以指定Chrome
和ChromeDriver
,具体见更多使用的示例
。
from appium.webdriver.appium_service import AppiumService
from appium.options.common.base import AppiumOptions
# 开启服务端
APPIUM_HOST = '127.0.0.1'
APPIUM_PORT = 4723
@pytest.fixture(scope="session")
def start_appium_service():
server = AppiumService()
server.start(args=['--address', APPIUM_HOST, '-p', str(APPIUM_PORT)], timeout_ms=60000)
yield server
server.stop()
def create_appium_session_by_chromedriver(custom_opts = None):
options = AppiumOptions()
options.set_capability("appium:automationName", "Chromium")
if custom_opts is not None:
options.load_capabilities(custom_opts)
return webdriver.Remote(f'http://{APPIUM_HOST}:{APPIUM_PORT}', options=options)
def test_chrome_on_pc(start_appium_service):
custom_opts = {
"platformName": "Linux",
"browserName": "chrome",
}
driver = create_appium_session_by_chromedriver(custom_opts)
url = "https://gitee.com/"
driver.get(url=url)
assert "Gitee" in driver.title
driver.quit()
能力集
除了所有ChromeDrvier
支持的能力集外,本驱动还支持如下能力,主要是用于对WebDriver
(ChromeDriver
或MsedgeDriver
)的管理:
备注:对于Chrome
或Chromium
,ChromeDriver
本身支持的能力集可以通过以goog:chromeOptions
为键的字典加入到能力集中;对于MSEdge
浏览器,可以通过以ms:edgeOptions
为键的字典加入到能力集中。见更多使用的示例
。
能力名称 | 具体描述 | 默认值 |
---|---|---|
appium:chromedriverPort |
可选,指定启动WebDriver 的端口。 |
9515 |
appium:executable |
可选,WebDriver 二进制可执行文件的绝对路径。如果设置了该值,那么本驱动会使用这个路径指定的WebDriver ,而不是本驱动自身的WebDriver 。 |
无 |
appium:executableDir |
可选,存放WebDriver 二进制可执行文件的目录,可以有多个可执行文件。如果设置了该目录,那么本驱动会搜索该目录,寻找与浏览器想匹配的WebDriver 版本。 |
无 |
appium:verbose |
可选,如果设置为true ,则会在启动WebDriver 的时候,传入参数--verbose 。 |
false |
appium:logPath |
可选,如果设置了,则作为--log-path 的参数,指定WebDriver 的日志记录到该路径下。 |
无 |
appium:disableBuildCheck |
可选,设置为true ,则会在启动WebDriver 的时候,传入参数--disable-build-check 。 |
false |
appium:autodownloadEnabled |
可选,设置为false 以便禁止自动下载Chromedriver 驱动。 |
true |
appium:useSystemExecutable |
可选,设置为true ,会使用本驱动捆绑安装的WebDriver 版本,而不是基于被测浏览器版本,去尝试下载一个新的WebDriver 版本。 |
false |
更多使用的示例
-
指定使用具体版本的
ChromeDriver
和Chrome
通过
"appium:executable": path
指定ChromeDriver
的位置,通过"goog:chromeOptions": {"binary": path}
指定Chrome
位置。
from appium.webdriver.appium_service import AppiumService
from appium.options.common.base import AppiumOptions
# 开启服务端
APPIUM_HOST = '127.0.0.1'
APPIUM_PORT = 4723
@pytest.fixture(scope="session")
def start_appium_service():
server = AppiumService()
server.start(args=['--address', APPIUM_HOST, '-p', str(APPIUM_PORT)], timeout_ms=60000)
yield server
server.stop()
def create_appium_session_by_chromedriver(custom_opts = None):
options = AppiumOptions()
options.set_capability("appium:automationName", "Chromium")
if custom_opts is not None:
options.load_capabilities(custom_opts)
return webdriver.Remote(f'http://{APPIUM_HOST}:{APPIUM_PORT}', options=options)
def test_chrome_on_pc_specify_chrome_and_driver(start_appium_service):
chromedriver_path = "/home/chrometest/chromedriver/chromedriver" # 指定ChromeDriver的路径
chrome_path = "/home/chrometest/chrome/chrome" # 指定Chrome浏览器的路径
chromeOptions = {"binary": chrome_path, }
custom_opts = {
"platformName": "Linux",
"browserName": "chrome",
"appium:executable": chromedriver_path,
"goog:chromeOptions": chromeOptions
}
driver = create_appium_session_by_chromedriver(custom_opts)
url = "https://gitee.com/"
driver.get(url=url)
assert "Gitee" in driver.title
driver.quit()
-
测试
Android
设备侧的Chrome
浏览器(使用appium-chromium-driver
)要求:
1)
Android
设备已经连接或模拟器(AVD
)已经打开2)
Appium
服务端机器上的ChromeDriver
与Android
设备(或模拟器)上的Chrome
浏览器版本匹配,请参考version-selection说明:
通过
"goog:chromeOptions": {"androidPackage": "com.android.chrome"}
指定打开Android
设备上Chrome
。platformName
仍然是Appium
服务端机器使用的操作系统。from appium.webdriver.appium_service import AppiumService from appium.options.common.base import AppiumOptions # 开启服务端 APPIUM_HOST = '127.0.0.1' APPIUM_PORT = 4723 @pytest.fixture(scope="session") def start_appium_service(): server = AppiumService() server.start(args=['--address', APPIUM_HOST, '-p', str(APPIUM_PORT)], timeout_ms=60000) yield server server.stop() def create_appium_session_by_chromedriver(custom_opts = None): options = AppiumOptions() options.set_capability("appium:automationName", "Chromium") if custom_opts is not None: options.load_capabilities(custom_opts) return webdriver.Remote(f'http://{APPIUM_HOST}:{APPIUM_PORT}', options=options) def test_chrome_on_android(start_appium_service): chromedriver_path = "/home/chrometest/chromedriver/chromedriver" # 指定ChromeDriver的路径 chromeOptions = {"androidPackage": "com.android.chrome"} custom_opts = { "platformName": "Linux", "browserName": "chrome", "appium:executable": chromedriver_path, "goog:chromeOptions": chromeOptions } driver = create_appium_session_by_chromedriver(custom_opts) url = "https://gitee.com/" driver.get(url=url) assert "Gitee" in driver.title driver.quit()```
网友评论