Nightwatch入门(四):浏览器驱动

本节包含大多数主要浏览器入门指南和有关如何配置单个webdriver实现以使用Nightwatch的设置说明。
此处描述的各个驱动程序通常是独立的应用程序,用于通过WebDriver HTTP API与浏览器进行交互。您可以直接或通过Selenium Server运行它们。

GeckoDriver

概述
GeckoDriver是一个独立的应用程序,用于与基于Gecko的浏览器(如Firefox)进行交互。它是用Rust编写的,由Mozilla维护。
从Firefox 48开始,GeckoDriver是自动化Firefox的唯一方法,不再支持以前属于Selenium的传统FirefoxDriver。在内部,它将HTTP调用转换为Marionette,即Mozilla内置于Firefox中的自动化协议。
下载
对于各种平台,可以在GitHub上的GeckoDriver版本页面上下载二进制文件。 建议Selenium 2.x用户使用v0.9版本,而Selenium 3用户应使用最新版本。

运用
如果您通过Selenium Server使用GeckoDriver,只需将cli参数“webdriver.gecko.driver”设置为指向二进制文件的位置即可。
例如:

{
  "selenium" : {
    "start_process" : true,
    "server_path" : "./bin/selenium-server-standalone-3.{VERSION}.jar",
    "log_path" : "",
    "port" : 4444,
    "cli_args" : {
      "webdriver.gecko.driver" : "./bin/geckodriver"
    }
  }
}

GeckoDriver也可以用作独立应用程序。使用步骤记录在GitHub上:
https://github.com/mozilla/geckodriver#usage.
命令行用法

$ ./bin/geckodriver-0.10 -help
geckodriver 0.10.0

USAGE:
    geckodriver-0.10 [FLAGS] [OPTIONS]

FLAGS:
        --connect-existing    Connect to an existing Firefox instance
    -h, --help                Prints help information
        --no-e10s             Start Firefox without multiprocess support (e10s) enabled
    -V, --version             Prints version information
    -v                        Set the level of verbosity. Pass once for debug level logging and twice for trace level logging

OPTIONS:
    -b, --binary            Path to the Firefox binary, if no binary capability provided
        --log                Set Gecko log level [values: fatal, error, warn, info, config, debug, trace]
        --marionette-port     Port to use to connect to gecko (default: random free port)
        --host                Host ip to use for WebDriver server (default: 127.0.0.1)
    -p, --port                Port to use for WebDriver server (default: 4444)

Firefox功能
GeckoDriver支持名为firefoxOptions的功能,该功能采用特定于Firefox的首选项值。有关详细信息,请访问GeckoDriver GitHub页面:https://github.com/mozilla/geckodriver#firefox-capabilities。
Firefox档案
如上所述,可以通过在firefoxOptions字典中设置profile属性来指定firefox配置文件。这可以是配置文件目录的base64编码的zip,它可以用于安装扩展或自定义证书。
实施状况
GeckoDriver尚未完成功能,这意味着它尚未完全符合WebDriver标准或与Selenium完全兼容。可以在Marionette MDN页面上跟踪实施状态。

ChromeDriver

概述
ChromeDriver是一个独立的服务器,它为Chromium实现了W3C WebDriver线路协议。 ChromeDriver适用于Android上的Chrome和桌面版Chrome(Mac,Linux,Windows和ChromeOS)。
下载
可以在ChromeDriver下载页面上为各种平台下载二进制文件。
Selenium服务器用法
如果您通过Selenium Server使用ChromeDriver,只需将cli参数“webdriver.chrome.driver”设置为指向二进制文件的位置即可。

{
  "selenium" : {
    "start_process" : true,
    "server_path" : "./bin/selenium-server-standalone-3.{VERSION}.jar",
    "log_path" : "",
    "port" : 4444,
    "cli_args" : {
      "webdriver.chrome.driver" : "./bin/chromedriver"
    }
  }
}

独立使用
如果您只针对Chrome运行测试,则运行ChromeDriver独立版会更容易,速度更快。此外,它不依赖于Java。
这需要更多配置:
1)首先,禁用Selenium Server(如果适用):

{
  "selenium" : {
    "start_process" : false
  }
}

2)配置端口和默认路径前缀。
ChromeDriver默认在端口9515上运行。我们还需要清除default_path_prefix,因为它默认设置为/ wd / hub,这是selenium正在使用的内容。

{
  "test_settings" : {
    "default" : {
      "selenium_port"  : 9515,
      "selenium_host"  : "localhost",
      "default_path_prefix" : "",

      "desiredCapabilities": {
        "browserName": "chrome",
        "chromeOptions" : {
          "args" : ["--no-sandbox"] },
        "acceptSslCerts": true
      }
    }
  }
}

3)启动ChromeDriver服务器
管理ChromeDriver进程的最简单方法是使用chromedriver NPM软件包,它是针对二进制文件的第三方包装程序。这将抽象出chromedriver二进制文件的下载,并使管理启动和停止过程变得容易。
您可以将其添加到外部全局文件中,如下所示:

var chromedriver = require('chromedriver');
module.exports = {
  before : function(done) {
    chromedriver.start();

    done();
  },

  after : function(done) {
    chromedriver.stop();

    done();
  }
};

使用固定的ChromeDriver版本
在某些情况下,您可能需要使用特定版本的ChromeDriver。例如,CI服务器运行较旧版本的Chrome。然后,您将需要旧版ChromeDriver。
这是你的全局文件可能是这样的:

var chromedriver = require('chromedriver');
var path = require('path');
var driverInstanceCI;

function isRunningInCI() {
  return this.test_settings.globals.integration;
}

function startChromeDriver() {
  if (isRunningInCI.call(this)) {
    var location = path.join(__dirname, '../bin/chromedriver-linux64-2.17');
    driverInstanceCI = require('child_process').execFile(location, []);
    return;
  }

  chromedriver.start();
}

function stopChromeDriver() {
  if (isRunningInCI.call(this)) {
    driverInstanceCI && driverInstanceCI.kill();
    return;
  }

  chromedriver.stop();
}

module.exports = {
  'ci-server' : {
    integration : true
  },

  before : function(done) {
    startChromeDriver.call(this);

    done();
  },

  after : function(done) {
    stopChromeDriver.call(this);

    done();
  }
};

然后使用(在CI服务器上)运行测试:

$ ./node_modules/.bin/nightwatch --env ci-server

ChromeOptions
您可以在desiredCapabilities下使用chromeOptions字典指定Chrome选项或开关。有关支持的功能和选项的填充列表,请参阅ChromeDriver网站。
命令行用法

$ ./bin/chromedriver -h
Usage: ./bin/chromedriver [OPTIONS]

Options
  --port=PORT                     port to listen on
  --adb-port=PORT                 adb server port
  --log-path=FILE                 write server log to file instead of stderr, increases log level to INFO
  --verbose                       log verbosely
  --version                       print the version number and exit
  --silent                        log nothing
  --url-base                      base URL path prefix for commands, e.g. wd/url
  --port-server                   address of server to contact for reserving a port
  --whitelisted-ips               comma-separated whitelist of remote IPv4 addresses which are allowed to connect to ChromeDriver

Microsoft WebDriver

概述
Microsoft WebDriver是一个独立的服务器,它为Edge浏览器实现W3C WebDriver有线协议。它受Windows 10及更高版本的支持。
下载
可以在Microsoft WebDriver主页上下载二进制文件。
Selenium服务器用法
如果您通过Selenium Server使用Microsoft WebDriver,只需将cli参数webdriver.edge.driver设置为指向二进制文件的位置即可。例如。:

{
  "selenium" : {
    "start_process" : true,
    "server_path" : "bin/selenium-server-standalone-3.{VERSION}.jar",
    "log_path" : "",
    "port" : 4444,
    "cli_args" : {
      "webdriver.edge.driver" : "bin/MicrosoftWebDriver.exe"
    }
  },
  "test_settings" : {
    "default" : {
      "selenium_port"  : 4444,
      "selenium_host"  : "localhost",

      "desiredCapabilities": {
        "browserName": "MicrosoftEdge",
        "acceptSslCerts": true
      }
    }
  }
}

独立使用
如果您只是针对Edge运行测试,那么运行EdgeDriver独立版可能会稍快一些。此外,它不依赖于Java。
这需要更多配置,您需要启动/停止EdgeDriver:
1)首先,禁用Selenium Server(如果适用):

{
  "selenium" : {
    "start_process" : false
  }
}

2)配置端口和默认路径前缀。
Microsoft WebDriver默认在端口9515上运行。我们还需要清除default_path_prefix,因为它默认设置为/ wd / hub,这是selenium正在使用的内容。

{
  "test_settings" : {
    "default" : {
      "selenium_port"  : 17556,
      "selenium_host"  : "localhost",
      "default_path_prefix" : "",

      "desiredCapabilities": {
        "browserName": "MicrosoftEdge",
        "acceptSslCerts": true
      }
    }
  }
}

3)启动MicrosoftWebDriver服务器
从Windows CMD提示符,只需CD到MicrosoftWebDriver.exe二进制文件所在的文件夹并运行:

C:\nightwatch\bin>MicrosoftWebDriver.exe
[13:44:49.515] - Listening on http://localhost:17556/

完整的命令行用法:

C:\nightwatch\bin>MicrosoftWebDriver.exe -h
Usage:
 MicrosoftWebDriver.exe --host= --port= --package= --verbose

实施状况
EdgeDriver尚未完成功能,这意味着它尚未完全符合WebDriver标准或与Selenium完全兼容。可以在Microsoft WebDriver主页上跟踪实施状态。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值