浏览器驱动程序设置

这个部分包含了入门的指南,主要的浏览器和设置说明如何配置单独的webdriver来实现来与Nightwatch一起工作。这里描述的单独驱动程序通常是独立的应用程序,它们通过WebDriver HTTP API与浏览器进行交互。你可以直接运行它们,也可以通过Selenium服务器运行它们。

GeckoDriver(Firefox)

GeckoDriver是一个独立的应用程序,用于与基于Firefox的浏览器进行交互,比如Firefox。

从Firefox 48开始,GeckoDriver作为自动化Firefox的唯一方式,传统的FirefoxDriver已经不再支持它了。在内部,它将HTTP调用转换为Marionette,Mozilla的自动化协议内置到Firefox中。

Download

在GitHub上,可以下载GeckoDriver Releases,用于各种平台。

Selenium 2.x用户建议使用v0.1.9,而Selenium 3的用户应该使用最新版本。

Usage

如果你通过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

Command line 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更多信息。

Firefox Profile

可以通过在firefoxOptions字典中设置配置文件属性来指定firefox配置文件,这可以是配置文件目录的base64编码zip,它可以用于安装扩展或定制证书。

实施状况

GeckoDriver目前还没有完整的特性,这意味着它还没有完全符合WebDriver标准或与Selenium的完全兼容性。可以在Marionette MDN页面上跟踪实现状态。

ChromeDriver

ChromeDriver是一个独立的服务器,它实现了用于Chromium的W3C WebDriver有线协议。ChromeDriver可以在Android和桌面上的Chrome浏览器上使用(Mac、Linux、Windows和ChromeOS)。

Download

ChromeDriver下载页面,用于各种平台。

Selenium Server 用法

如果您正在使用Selenium服务器使用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"
    }
  }
}

Standalone 用法

如果你只是对Chrome进行测试,那么运行ChromeDriver的独立运行会更容易也会更快一些,并且它也不依赖于Java。

这些需要一些配置:

1) 首先,禁用Selenium服务器,如果适用的话:

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

2) 配置端口和默认路径前缀

ChromeDriver默认在端口9515上运行,我们还需要清除默认路径前缀,因为它是默认设置为/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

你可以使用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协议,它支持win10和之前的版本。

Download

Microsoft WebDriver 下载页面。

Selenium Server 用法

如果你使用的是Microsoft WebDriver通过Selenium服务器,只需设置cli参数“webdriver.edge.driver”指向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
      }
    }
  }
}

Standalone 用法

如果你只是对Edge运行你的测试,那么运行EdgeDriver的独立运行速度会稍微快一些。它同样也不依赖于Java。

这需要更多的配置,你需要启动/停止EdgeDriver:

1) 首先,禁用Selenium服务器:

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

2) 配置端口和默认路径前缀。

默认情况下,Microsoft WebDriver在端口9515上运行。我们还需要清除默认路径前缀,因为它是默认设置为/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的主页上跟踪实现状态。

results matching ""

    No results matching ""