Appium如何自动判断浏览器驱动

问题:有的测试机chrome是这个版本,有的是另一个版本,怎么能让自动判断去跑呢??

解决办法:使用appium的chromedriverExecutableDir和chromedriverChromeMappingFile

切忌使用chromedriverExecutableDir和chromedriverChromeMappingFile时,一定不要再加chromedriverExecutable

具体appium的caps有哪些可以参考:https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/caps.md

第一步:

python代码(复制以后不要忘记改路径为你自己的):

import pytest
from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions


class TestAppiumBrowser:
    def setup(self):
        """
        初始化driver
        :return:
        """
        caps = {}
        caps['platformName'] = 'android'
        caps['platformVersion'] = '6.0'
        caps['deviceName'] = 'emulator-5554'
        caps['browserName'] = 'Browser'
        caps['unicodeKeyBoard'] = 'true'
        caps['resetKeyBoard'] = 'true'
        # 切忌使用chromedriverExecutableDir和chromedriverChromeMappingFile时,一定不要再加chromedriverExecutable
        caps['chromedriverExecutableDir'] = '/放置你的那一堆chromedriver的路径/chromedriver/'
        caps['chromedriverChromeMappingFile'] = '/mapping.json文件的路径/mapping.json'
        self.driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", caps)
        self.driver.implicitly_wait(10)

    @pytest.mark.parametrize('search_value',["appium","软件测试","selenium"])
    def test_browser(self, search_value):
        """
        测试输入不同的关键字(使用pytest的关键字驱动),验证搜索结果条目>1条
        :param search_value:
        :return:
        """
        # 打开 http://m.baidu.com
        self.driver.get("http://m.baidu.com")
        # 百度搜索框的位置:id=index-kw
        search_input_locator = (By.ID,'index-kw')
        # 百度一下按钮的位置:id=index-bn(这里使用的MobileBy其实继承了By,用法一样,扩展一下用例的技能覆盖点)
        search_click_locator = (MobileBy.ID,'index-bn')
        # 显示等待,等待直到搜索输入框出现
        WebDriverWait(self.driver, 10).until(expected_conditions.visibility_of_element_located(search_input_locator))
        # 输入框输入参数
        self.driver.find_element(*search_input_locator).send_keys(search_value)
        # 点击【百度一下】
        self.driver.find_element(*search_click_locator).click()
        # 获取当前搜索结果页的总条目
        result_elements = self.driver.find_elements(By.CSS_SELECTOR,'.c-gap-bottom-small')
        # 断言条目数大于1
        assert len(result_elements) > 1

    def teardown(self):
        """
        最终退出driver
        :return:
        """
        self.driver.quit()

 

第二步:创建mapping.json文件,内容举例如下:

{
  "2.24": "52.0.2743",
  "2.7": "57.0.2987.110"
}

  

可以看看chromedriver的文件夹:

很简单,就是把各种chromedriver放进来,名字随便起,appium回自动根据mapping里配置的来取对应版本的chromedriver

原文地址:https://www.cnblogs.com/lybolg/p/13992076.html