Selenium Webdriver Desired Capab

作者: 苦叶子 | 来源:发表于2017-04-06 10:16 被阅读5053次

    前言

    我们每一个的selenium测试都应该在指定的环境下运行,这个环境可以是web浏览器、移动设备、移动模拟器等等。

    那怎么来指定我们的selenium测试脚本运行环境呢?

    本次分享主要解决这个问题!!!

    在python版本的webdrive中,DesiredCapabilities类为我们提供了解决方案,通过这个类,我们能够配置webdriver在指定的环境执行我们的测试脚本。

    我们先看一张实际测试过程中会应用到的结构图,基于selenium grid进行分布式测试:

    selenium_grid.png

    在上图中,我们可以使用windows下ie、firefox或linux下ie、firefox进行测试,甚至可以设置使用andriod或ios设备下浏览器进行测试。

    那怎么来进行设置呢?

    我们先看一下DesiredCapabilities的源码

    源码分析

    DesiredCapabilities类代码在C:\Python27\Lib\site-packages\selenium\webdriver\common\desired_capabilities.py。

    注:笔者的python安装在C盘下。

    class DesiredCapabilities(object):
        """
        Set of default supported desired capabilities.
    
        Use this as a starting point for creating a desired capabilities object for
        requesting remote webdrivers for connecting to selenium server or selenium grid.
    
        Usage Example::
    
            from selenium import webdriver
    
            selenium_grid_url = "http://198.0.0.1:4444/wd/hub"
    
            # Create a desired capabilities object as a starting point.
            capabilities = DesiredCapabilities.FIREFOX.copy()
            capabilities['platform'] = "WINDOWS"
            capabilities['version'] = "10"
    
            # Instantiate an instance of Remote WebDriver with the desired capabilities.
            driver = webdriver.Remote(desired_capabilities=capabilities,
                                      command_executor=selenium_grid_url)
    
        Note: Always use '.copy()' on the DesiredCapabilities object to avoid the side
        effects of altering the Global class instance.
    
        """
    
        FIREFOX = {
            "browserName": "firefox",
            "version": "",
            "platform": "ANY",
            "javascriptEnabled": True,
            "marionette": True,
        }
    
        INTERNETEXPLORER = {
            "browserName": "internet explorer",
            "version": "",
            "platform": "WINDOWS",
            "javascriptEnabled": True,
        }
    
        EDGE = {
            "browserName": "MicrosoftEdge",
            "version": "",
            "platform": "WINDOWS"
        }
    
        CHROME = {
            "browserName": "chrome",
            "version": "",
            "platform": "ANY",
            "javascriptEnabled": True,
        }
    
        OPERA = {
            "browserName": "opera",
            "version": "",
            "platform": "ANY",
            "javascriptEnabled": True,
        }
    
        SAFARI = {
            "browserName": "safari",
            "version": "",
            "platform": "MAC",
            "javascriptEnabled": True,
        }
    
        HTMLUNIT = {
            "browserName": "htmlunit",
            "version": "",
            "platform": "ANY",
        }
    
        HTMLUNITWITHJS = {
            "browserName": "htmlunit",
            "version": "firefox",
            "platform": "ANY",
            "javascriptEnabled": True,
        }
    
        IPHONE = {
            "browserName": "iPhone",
            "version": "",
            "platform": "MAC",
            "javascriptEnabled": True,
        }
    
        IPAD = {
            "browserName": "iPad",
            "version": "",
            "platform": "MAC",
            "javascriptEnabled": True,
        }
    
        ANDROID = {
            "browserName": "android",
            "version": "",
            "platform": "ANDROID",
            "javascriptEnabled": True,
        }
    
        PHANTOMJS = {
            "browserName": "phantomjs",
            "version": "",
            "platform": "ANY",
            "javascriptEnabled": True,
        }
    
    

    翻译下:

    Set of default supported desired capabilities.
    desired capabilities默认支持的设置。
    
    Use this as a starting point for creating a desired capabilities object for
    requesting remote webdrivers for connecting to selenium server or selenium grid.
    使用该类为selenium server或selenium grid启动一个desired capabilities配置
    

    对配置项进行解释

    FIREFOX = {
        "browserName": "firefox",  # 浏览器名称
        "version": "",             # 操作系统版本
        "platform": "ANY",         # 平台,这里可以是windows、linux、andriod等等
        "javascriptEnabled": True, # 是否启用js
        "marionette": True,        # 这个值没找对应的说明^_^  不解释了
    }
    

    DesiredCapabilities示例

    from selenium import webdriver
    
    # 本地启动selenium grid
    selenium_grid_url = "http://127.0.0.1:4444/wd/hub"
    
    # 创建一个DesiredCapabilities实例
    capabilities = DesiredCapabilities.FIREFOX.copy()
    capabilities['platform'] = "WINDOWS"  # 指定操作系统
    capabilities['version'] = "10"   # 指定操作系统版本
    
    # 连接到远程服务进行自动化测试
    driver = webdriver.Remote(desired_capabilities=capabilities,
                              command_executor=selenium_grid_url)
    

    总结

    这里的本质就是基于selenium grid 构建分布式自动化测试,而selenium grid根据测试脚本构建的DesiredCapabilities参数来决定将您的测试脚本分发到哪台机器或设备进行测试。

    相关文章

      网友评论

      • 程一川: 我从网上找到的对marionette的解释:marionette是python客户端允许你远程控制基于gecko的浏览器或设备运行一个marionette服务器,包括桌面Firefox和Firefox OS。该参数为Firefox特有。
        苦叶子:666666666666

      本文标题:Selenium Webdriver Desired Capab

      本文链接:https://www.haomeiwen.com/subject/jehxattx.html