How to use the Capabilities function from selenium-webdriver

Find comprehensive JavaScript selenium-webdriver.Capabilities code examples handpicked from public code repositorys.

selenium-webdriver.Capabilities is a JavaScript class that provides a way to customize the configuration and behavior of a Selenium WebDriver instance.

32
33
34
35
36
37
38
39
40
41
 * 127.0.0.1    firstparty.local
 * 127.0.0.1    thirdparty.local
 */

async function loadDriverWithExtension(extPath) {
  let chromeOptions = sw.Capabilities.chrome();
  chromeOptions.set("chromeOptions",  {"args": [
    `--load-extension=${extPath}`,
    '--no-sandbox',
  ]});
fork icon87
star icon718
watch icon54

+ 2 other calls in file

134
135
136
137
138
139
140
141
142
143
  path.resolve(os.homedir(), '.cargo', 'bin', 'tauri-driver'),
  [],
  { stdio: [null, process.stdout, process.stderr] }
)

const capabilities = new Capabilities()
capabilities.set('tauri:options', { application })
capabilities.setBrowserName('wry')

// start the webdriver client
fork icon334
star icon566
watch icon21

How does selenium-webdriver.Capabilities work?

selenium-webdriver.Capabilities works by providing a way to specify and customize the configuration and behavior of a Selenium WebDriver instance. When creating a new WebDriver instance, you can pass a Capabilities object to the constructor to specify any custom settings or behavior that you want the WebDriver to have. The Capabilities object provides a variety of methods and properties for specifying different settings, such as the browser name and version, the operating system, and the desired behavior of the WebDriver instance. For example, you can use the setBrowserName() method to specify the name of the browser to use, the setVersion() method to specify the browser version, and the setPlatform() method to specify the operating system. Once the Capabilities object is configured, you can pass it to the WebDriver constructor to create a new WebDriver instance with the desired settings and behavior. By allowing developers to customize the configuration and behavior of a WebDriver instance, Capabilities provides a flexible and powerful way to interact with web applications and automate web testing.

20
21
22
23
24
25
26
27
28
29
  return new Error('Unsupported browser.');
}

var browser;
if (browserName === 'firefox') {
  browser = webdriver.Capabilities.firefox();
  serverUrl = serverUrl || consts.DEFAULT_SERVER_URL_FIREFOX;
} else {
  // https://code.google.com/p/chromedriver/issues/detail?id=799
  // need to pass this arg, or else the chrome window displays an error
fork icon32
star icon365
watch icon17

+ 15 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
const { Builder, Capabilities } = require("selenium-webdriver");

// Create a new capabilities object with custom settings
const caps = new Capabilities();
caps.setBrowserName("firefox");
caps.setVersion("92.0");
caps.setPlatform("Windows 10");

// Create a new WebDriver instance with the custom capabilities
const driver = new Builder()
  .withCapabilities(caps)
  .forBrowser("firefox")
  .build();

// Use the WebDriver instance to interact with a web page
driver
  .get("https://www.example.com")
  .then(() => driver.getTitle())
  .then((title) => console.log(title))
  .finally(() => driver.quit());

In this example, we use selenium-webdriver.Capabilities to create a new WebDriver instance with custom settings. We first create a new Capabilities object and use its methods to specify the desired browser name, version, and platform. We then pass the Capabilities object to the withCapabilities() method of a Builder instance to create a new WebDriver instance with the desired settings. Finally, we use the WebDriver instance to interact with a web page, retrieving its title and logging it to the console. By using selenium-webdriver.Capabilities, we can easily customize the configuration and behavior of a WebDriver instance to meet our specific needs and requirements.

44
45
46
47
48
49
50
51
52
53
54
55


exports.closeBrowser = () => { };


function setBuilderCapabilities(builder, options) {
	if (options.browserIgnoreInsecureCerts !== undefined && options.browserIgnoreInsecureCerts) {
		const capabilities = new Capabilities();
		capabilities.setAcceptInsecureCerts(true);
		builder.withCapabilities(capabilities);
	}
}
fork icon18
star icon150
watch icon6

+ 6 other calls in file

13
14
15
16
17
18
19
20
21
22
/**
 * Set options
 */
set_options: function() {

    CTX['chrome_capabilities'] = webdriver.Capabilities.chrome();
    CTX['chrome_capabilities'].set('chromeOptions', {
        args: [
            '--headless', 
            '--disable-web-security', 
fork icon7
star icon35
watch icon2

+ 3 other calls in file

26
27
28
29
30
31
32
33
34
35
        verbose: false
};

var violationsCount = 0;
var options = customOptions ? Object.assign(defaultOptions, customOptions) : defaultOptions;
var chromeCapabilities = WebDriver.Capabilities.chrome();
var chromeOptions = options.headless ? { 'args': ['--headless'] } : {};
chromeCapabilities.set('chromeOptions', chromeOptions);
var driver = new WebDriver.Builder().withCapabilities(chromeCapabilities).build();
if (typeof options.scriptTimeout === 'number') {
fork icon7
star icon22
watch icon4

29
30
31
32
33
34
35
36
37
38
You can use phantom, firefox, etc, but we recommend headless chrome. Here is an
example of setup:

```js
const getBrowser = () => {
  const options = webdriver.Capabilities.chrome();
  options.set('chromeOptions', { 'args': [ '--headless' ] })
  const chrome = new webdriver.Builder().forBrowser('chrome').withCapabilities(options)
  return decorate(chrome.build()) // decorate the browser with the getDOM function
}
fork icon2
star icon9
watch icon10

+ 11 other calls in file

10
11
12
13
14
15
16
17
18
var PLUGIN_NAME = 'gulp-axe-core';
require('chromedriver');

//setup custom phantomJS capability
var phantomjs_exe = require('phantomjs-prebuilt').path;
var customPhantom = WebDriver.Capabilities.phantomjs().set('phantomjs.binary.path', phantomjs_exe);

var promise;
var results = [];
fork icon3
star icon7
watch icon2

207
208
209
210
211
212
213
214
215
216
      throw 'Could not find chromedriver at ' + config.chromeDriver;
    }
  }
  var service = new chrome.ServiceBuilder(config.chromeDriver).build();
  driver = chrome.createDriver(
      new webdriver.Capabilities(config.capabilities), service);
} else {
  driver = new webdriver.Builder().
      usingServer(config.seleniumAddress).
      withCapabilities(config.capabilities).build();
fork icon0
star icon1
watch icon16

13
14
15
16
17
18
19
20
21
22
let driver = await new webdriver.Builder().forBrowser(config.SELENIUM_BROWSER)
if (config.HUB_HOST) {
  driver = driver.usingServer(`http://${config.HUB_HOST}:${config.HUB_PORT}/wd/hub`)
}
const chromeCapabilities = webdriver.Capabilities.chrome()
const ffCapabilities = webdriver.Capabilities.firefox()
const chromeOptions = new chrome.Options()
const ffOptions = new firefox.Options()
// Accept self-signed certs for LoRa app servers
ffCapabilities.set('acceptInsecureCerts', true)
fork icon0
star icon1
watch icon0

+ 3 other calls in file

12
13
14
15
16
17
18
19
20
21
// Export selenium webdriver.
this.ActionSequence = webdriver.ActionSequence;
this.Browser = webdriver.Browser;
this.Builder = webdriver.Builder;
this.Button = webdriver.Button;
this.Capabilities = webdriver.Capabilities;
this.Capability = webdriver.Capability;
this.EventEmitter = webdriver.EventEmitter;
this.FileDetector = webdriver.FileDetector;
this.Key = webdriver.Key;
fork icon1
star icon0
watch icon2

18
19
20
21
22
23
24
25
26
27
28
 * @fileoverview Defines functions for configuring a webdriver proxy:
 *
 *     const proxy = require('selenium-webdriver/proxy');
 *     const {Capabilities} = require('selenium-webdriver');
 *
 *     let capabilities = new Capabilities();
 *     capabilities.setProxy(proxy.manual({http: 'host:1234'});
 */


'use strict'
fork icon0
star icon0
watch icon1

+ 59 other calls in file

37
38
39
40
41
42
43
44
45
46
// Export selenium webdriver.
ActionSequence = webdriver.ActionSequence;
Browser = webdriver.Browser;
Builder = webdriver.Builder;
Button = webdriver.Button;
Capabilities = webdriver.Capabilities;
Capability = webdriver.Capability;
EventEmitter = webdriver.EventEmitter;
FileDetector = webdriver.FileDetector;
Key = webdriver.Key;
fork icon0
star icon0
watch icon9

1
2
3
4
5
6
7
8
9
10
11


class Singleton {
    constructor(browser) {
        if (!Singleton._instance) {
            if (browser === 'chrome') {
                const chromeCapabilities = webdriver.Capabilities.chrome();
                const chromeOptions = {'args': ['--incognito']};
                chromeCapabilities.set("goog:chromeOptions", chromeOptions);
                Singleton._instance = new webdriver.Builder().forBrowser(browser).withCapabilities(chromeCapabilities).build();
                Singleton._instance.manage().window().maximize();
fork icon0
star icon0
watch icon1

131
132
133
134
135
136
137
138
139
140
// @see https://stackoverflow.com/questions/50642308/webdriverexception-unknown-error-devtoolsactiveport-file-doesnt-exist-while-t
if (browser === 'chrome') {
  var service = new chrome.ServiceBuilder(chromedriver.path).build();
  chrome.setDefaultService(service);

  capabilities = WebDriver.Capabilities.chrome();
  capabilities.set('chromeOptions', {
    args: [
      '--no-sandbox',
      '--headless',
fork icon0
star icon0
watch icon1

+ 4 other calls in file

79
80
81
82
83
84
85
86
87
88
driver = Selenium::WebDriver.for :chrome, :desired_capabilities => caps
driver.get('https://www.google.com')

E. Javascrypt
const {Builder, Capabilities} = require('selenium-webdriver');
const caps = new Capabilities();
caps.setPageLoadStrategy("normal");
(async function example() {
let driver = await new Builder().
withCapabilities(caps).
fork icon0
star icon0
watch icon1

+ 3 other calls in file

2
3
4
5
6
7
8
9
10
11
    until = require('selenium-webdriver').until;
var phantomjs = require('phantomjs');

describe('webdriver tests', function(){
  // var options = { desiredCapabilities: { browserName: 'phantomjs', 'phantomjs.cli.args': ['--web-security=no', '--ssl-protocol=any', '--ignore-ssl-errors=yes'] } };
  var caps = webdriver.Capabilities.phantomjs();
  caps.set(webdriver.Capability.ACCEPT_SSL_CERTS, true);
  caps.set(webdriver.Capability.SECURE_SSL, false);
  caps.set('phantomjs.cli.args', ['--web-security=no', '--ssl-protocol=any', '--ignore-ssl-errors=yes']);
  console.log(caps);
fork icon0
star icon0
watch icon2

20
21
22
23
24
25
26
27
28

fs.mkdirSync(__screenShotDir,  { recursive: true });

var common = '--require "src/step_definitions/**/*.js" --require hooks.js --require-module babel-core/register ';

let caps = new Capabilities();
caps.set('enableVNC', true);
//caps.set('enableVideo', true);
caps.set('pageLoadStrategy', 'normal');
fork icon0
star icon0
watch icon1

51
52
53
54
55
56
57
58
59
60
createInitialCapabilities() {
  if (this.desiredCapabilities instanceof Capabilities) {
    return this.desiredCapabilities;
  }

  return new Capabilities(this.desiredCapabilities);
}

alreadyDefinedAs(OptionsClass) {
  return this.desiredCapabilities instanceof OptionsClass;
fork icon0
star icon0
watch icon260

+ 5 other calls in file