Selenium-Webdriver NodeJS等同于DesiredCapabilities的Java代码

时间:2017-10-08 23:30:13

标签: java node.js selenium selenium-webdriver zalenium

我已经搜索了这里的selenium-webdriver(http://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/index_exports_Capabilities.html

的文档(这里有什么)

我无法找到从Java到NodeJS的等效代码。

以下是JAVA中的代码(我将在此处的“测试配置选项”部分复制https://github.com/zalando/zalenium/blob/master/docs/usage_examples.md#initial-setup

DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
  desiredCapabilities.setCapability(CapabilityType.BROWSER_NAME, BrowserType.FIREFOX);
  desiredCapabilities.setCapability(CapabilityType.PLATFORM, Platform.LINUX);
  desiredCapabilities.setCapability("name", "myTestName");

特别是我想设置能力" name"所以我可以用Zalenium命名我的测试。

我如何在NodeJS中执行此操作?

这是我目前使用的selnium webdriver代码(并且已经过编辑以删除敏感信息),但未设置测试名称。我已经尝试过.withCapabilities(webdriver.Capabilities.chrome(),{" name"," chrometest"})并且它编译但是我认为它没有做任何事情?

//Run using this project (https://github.com/qmu/dockerfiles/blob/master/src/selenium-webdriver-node/example/bin/run)

"use strict";

const webdriver = require('selenium-webdriver'),
    By = webdriver.By,
    until = webdriver.until,
    test = require('selenium-webdriver/testing');

//const expect = require('expect.js');
const assert = require('assert');

var driver = new webdriver.Builder()
   .withCapabilities(webdriver.Capabilities.chrome())
   .usingServer('http://localhost:4444/wd/hub')
   .build();

console.log('starting chrome...');

driver.manage().timeouts().implicitlyWait(10 * 1000);//10 seconds

driver.get('http://somewebsite.tech');
driver.findElement(webdriver.By.name('_username')).sendKeys('**');
driver.findElement(webdriver.By.name('_password')).sendKeys('**');
driver.findElement(webdriver.By.css("button")).click();
driver.quit();

//FIREFOX TESTS
console.log('starting firefox...');

var driver = new webdriver.Builder()
    .withCapabilities(webdriver.Capabilities.firefox())
    .usingServer('http://localhost:4444/wd/hub')
    .build();

driver.manage().timeouts().implicitlyWait(30 * 1000);//10 seconds

driver.get('http://somewebsite.tech/');
driver.findElement(webdriver.By.name('_username')).sendKeys('**');
driver.findElement(webdriver.By.name('_password')).sendKeys('**');
driver.findElement(webdriver.By.css("button")).click();
driver.quit();


console.log('all modules are ready!');

2 个答案:

答案 0 :(得分:6)

好的,我找到了答案。事实证明,您只需将其定义为对象中的额外参数。

var driver = new webdriver.Builder()
   .withCapabilities({'browserName': 'chrome','name':'Chrome Test','tz':'America/Los_Angeles','build':'Chrome Build','idleTimeout':'60'})
   .usingServer('http://localhost:4444/wd/hub')
   .build();

希望这会让某人感到沮丧。

答案 1 :(得分:1)

感谢您发布此信息。到处都是。就像FYI一样,您的某些真实字符串也需要为布尔值。至少使用geckodriver-v0.26.0,硒3.8.1,nodejs 10和Centos6。

const driver = new webdriver.Builder()
      .forBrowser('firefox')
      .withCapabilities({"browserName": "firefox","acceptSslCerts": true,"acceptInsecureCerts": true})
      .setFirefoxOptions(options)
      .build();