简体   繁体   中英

Selenium-Webdriver NodeJS Equivalent to Java Code for DesiredCapabilities

I have scoured the documentation (what little there is) for selenium-webdriver located here ( http://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/index_exports_Capabilities.html )

I can't find out what the equivalent code from Java to NodeJS would be.

Here is the code in JAVA (which I am copying from here at the Test Configuration Options section, 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");

In particular I want to set the capability "name" so I can name my tests with Zalenium.

How do I do this in NodeJS?

This is my current selnium webdriver code which works (and was edited to remove sensitive info) but does not set the test name. I have tried this .withCapabilities(webdriver.Capabilities.chrome(),{"name","chrometest"}) and it compiles but I don't think it does anything?

//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!');

Ok I found out the answer. Turns out you just define it as extra parameters in an object.

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();

Hope this saves someone some frustration.

Thank you thank you for posting this. Was looking everywhere. Just as an FYI, some of your true strings need to be boolean. At least with geckodriver-v0.26.0, selenium 3.8.1, nodejs 10, and Centos6.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM