How to use the Builder function from selenium-webdriver

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

selenium-webdriver.Builder is a JavaScript class that allows you to configure and build an instance of the Selenium WebDriver, which is used for automating browser-based tests.

213
214
215
216
217
218
219
220
221
  {{< tab header="JavaScript" >}}
const webdriver = require('selenium-webdriver');
const BROWSER_NAME = webdriver.Browser.CHROME;

async function getDriver() {
  return new webdriver.Builder()
  .usingServer('<grid-url>')
  .forBrowser(BROWSER_NAME)
  .build();
fork icon916
star icon568
watch icon29

85
86
87
88
89
90
91
92
93
94
if (capabilities.browserName === 'chrome') {
  cap = getChromeCapabilities(capabilities);
} else {
  cap = getFirefoxCapabilities(capabilities);
}
const driver = await new Builder()
  .usingServer(getBrowserStackUrl())
  .withCapabilities(cap)
  .build();
return new BrowserStackSession(driver, appName);
fork icon453
star icon641
watch icon59

+ 5 other calls in file

How does selenium-webdriver.Builder work?

selenium-webdriver.Builder is a class in the Selenium WebDriver library that allows you to configure and build an instance of the WebDriver. When you create a new instance of selenium-webdriver.Builder, you can specify various configuration options, such as the browser to use, the desired capabilities, and the proxy settings. These options allow you to customize the behavior of the WebDriver to suit your specific testing needs. Once you have configured the selenium-webdriver.Builder instance, you can use it to create a new instance of the WebDriver by calling the build() method. This method will return a new instance of the WebDriver that you can use to interact with the browser. The WebDriver instance returned by selenium-webdriver.Builder can be used to navigate to a specific URL, interact with elements on the page, and execute JavaScript code in the browser. You can also use it to take screenshots, log events, and perform other actions that are useful for testing and debugging web applications. By using selenium-webdriver.Builder to configure and build instances of the WebDriver, you can automate browser-based tests and ensure that your web application is functioning correctly across a variety of browsers and platforms.

139
140
141
142
143
144
145
146
147
148
  const capabilities = new Capabilities()
  capabilities.set('tauri:options', { application })
  capabilities.setBrowserName('wry')

  // start the webdriver client
  driver = await new Builder()
    .withCapabilities(capabilities)
    .usingServer('http://localhost:4444/')
    .build()
})
fork icon334
star icon566
watch icon21

33
34
35
36
37
38
39
40
41
42
W pliku `bingtest.js`, dodaj następujący kod, który sprawdza, czy szukasz `webdriver` w Bing, zwracając wyniki.

```js
test('Bing Search', async t => {
        const keyword = 'webdriver';
        const driver = new Builder().forBrowser('chrome').build();
        await driver.get('https://www.bing.com');
        await driver.findElement(By.name('q')).sendKeys(keyword + Key.ENTER);
        await driver.wait(until.titleIs(keyword + ' - Bing'));
        t.true((await driver.findElements(By.css('#b_content #b_results li'))).length > 0);
fork icon101
star icon498
watch icon21

+ 5 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 } = require("selenium-webdriver");
const chrome = require("selenium-webdriver/chrome");

// Create a new instance of the WebDriver builder
const builder = new Builder();

// Configure the builder to use the Chrome browser
const options = new chrome.Options();
options.addArguments("--headless");
builder.setChromeOptions(options);

// Build the WebDriver instance
const driver = builder.build();

// Navigate to a website and output the page title
driver
  .get("https://www.example.com")
  .then(() => driver.getTitle())
  .then((title) => console.log(title))
  .then(() => driver.quit());

In this example, we start by importing selenium-webdriver and selenium-webdriver/chrome modules, and creating a new instance of the selenium-webdriver.Builder class. We then configure the builder to use the Chrome browser with headless mode, which will run the browser in the background without a visible user interface. This is done by creating an instance of chrome.Options and passing it to the builder using setChromeOptions. We then build the WebDriver instance using builder.build(). The driver object returned by this method can be used to interact with the browser. We use the driver object to navigate to a website, retrieve the page title, output the title to the console, and then quit the driver. This example demonstrates how you can use selenium-webdriver.Builder to configure and build a WebDriver instance, and then use that instance to automate browser-based tests.

29
30
31
32
33
34
35
36
37
  return !(typeof url !== 'string' || !url.startsWith(urlPrefix));
};

const visitUrl = (url) => {
  return new Promise(async (resolve) => {
    const driver = new webdriver.Builder('chrome')
      .usingServer('http://selenium:4444/wd/hub/')
      .withCapabilities(capabilities)
      .build();
fork icon68
star icon362
watch icon14

199
200
201
202
203
204
205
206
207
208

```javascript
const { Builder, By } = require('selenium-webdriver');

(async () => {
    const driver = await new Builder().forBrowser('MicrosoftEdge').build();
    try {
        await driver.get('https://bing.com');

        const element = await driver.findElement(By.id('sb_form_q'));
fork icon502
star icon321
watch icon0

253
254
255
256
257
258
259
260
261
262
### [JavaScript](#tab/javascript)

Start IEDriver by calling `Builder.forBrowser('ie')` and `setIeoptions(ieOptions)`.  IEDriver launches Microsoft Edge in IE mode.  All page navigation and subsequent interactions occur in IE mode.

```javascript
let driver = await new Builder().
                       forBrowser('ie').
                       setIeOptions(ieOptions).
                       build();
```
fork icon501
star icon321
watch icon105

+ 3 other calls in file

64
65
66
67
68
69
70
71
72
    };

log.info(i18n('Starting Sauce Connect process'));
sauceConnectLauncher(this.sauceConnectLauncherOptions, function (err, sauceConnectProcess) {
  log.info(i18n('Sauce Connect Complete'));
  this.driver = new webdriver.Builder()
                             .withCapabilities(caps)
                             .usingServer(this.server + '/wd/hub/')
                             .build();
fork icon45
star icon299
watch icon36

149
150
151
152
153
154
155
156
157
    chrome_options.addArguments("--disable-features=NetworkService")
    chrome_options.addArguments('--disable-dev-shm-usage')
    chrome_options.addArguments('--autoplay-policy=no-user-gesture-required')
    chrome_options.addArguments('user-agent=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.50 Safari/537.36')
    console.log("Webdriver started")
    this.driver = new webdriver.Builder().forBrowser('chrome').setChromeOptions(chrome_options).build()
    this.driver.get(this.client_url)
    this.driver.executeScript(`localStorage.setItem("token", '"${token}"')`)
}
fork icon91
star icon289
watch icon5

+ 7 other calls in file

127
128
129
130
131
132
133
134
135
136
require("chromedriver");

// driver setup
const capabilities = Capabilities.chrome();
capabilities.set('chromeOptions', { "w3c": false });
const driver = new Builder().withCapabilities(capabilities).build();

Given('I am on the Google search page', async function () {
    await driver.get('http://www.google.com');
});
fork icon457
star icon121
watch icon86

+ 3 other calls in file

37
38
39
40
41
42
43
44
45
46
  let chromeOptions = sw.Capabilities.chrome();
  chromeOptions.set("chromeOptions",  {"args": [
    `--load-extension=${extPath}`,
    '--no-sandbox',
  ]});
  return new sw.Builder()
      .forBrowser('chrome')
      .withCapabilities(chromeOptions)
      .build();
}
fork icon87
star icon718
watch icon54

+ 2 other calls in file

-4
fork icon51
star icon130
watch icon41

+ 3 other calls in file

33
34
35
36
37
38
39
40
41
42

```js
const AxeBuilder = require('@axe-core/webdriverjs');
const WebDriver = require('selenium-webdriver');

const driver = new WebDriver.Builder().forBrowser('firefox').build();

driver.get('https://dequeuniversity.com/demo/mars/').then(() => {
  new AxeBuilder(driver).analyze((err, results) => {
    if (err) {
fork icon47
star icon476
watch icon15

35
36
37
38
39
40
41
42
43
44

```js
const AxeBuilder = require('@axe-core/webdriverjs');
const WebDriver = require('selenium-webdriver');
(async () => {
  const driver = new WebDriver.Builder().forBrowser('chrome').build();
  await driver.get('https://dequeuniversity.com/demo/mars/');
  const results = await new AxeBuilder(driver, null, {
    noSandbox: true
  })
fork icon47
star icon476
watch icon15

+ 5 other calls in file

308
309
310
311
312
313
314
315
316
317
```javascript
var webdriver = require('selenium-webdriver'),
    By = webdriver.By,
    until = webdriver.until;

var driver_chr = new webdriver.Builder()
    .forBrowser('chrome')
    .build();

searchTest(driver_chr);
fork icon114
star icon101
watch icon22

+ 2 other calls in file

2
3
4
5
6
7
8
9
10
11
const {Builder} = require('selenium-webdriver')
const {Eyes, Target, ConsoleLogHandler, StitchMode} = require('../index') // should be replaced to '@applitools/eyes-selenium'

;(async () => {
  // Open a Chrome browser.
  const driver = new Builder()
    .usingServer('https://hub-cloud.browserstack.com/wd/hub')
    .withCapabilities({
      browserName: 'Safari',
      browserVersion: '12',
fork icon42
star icon65
watch icon9

61
62
63
64
65
66
67
68
69
70
                        <p>selenium-server-standalone 与 测试脚本在同一台机器上</p>
                        <pre class="screen">
                        
var webdriver = require('selenium-webdriver');

var driver = new webdriver.Builder().
    withCapabilities(webdriver.Capabilities.chrome()).
    build();

driver.get('http://www.google.com');
fork icon407
star icon0
watch icon101

+ 13 other calls in file

143
144
145
146
147
148
149
150
151
## Running with Selenium WebDriver

```javascript
require('chromedriver');
var webdriver = require('selenium-webdriver');
var driver = new webdriver.Builder()
  .forBrowser('chrome')
  .build();
```
fork icon173
star icon444
watch icon15

+ 7 other calls in file

6
7
8
9
10
11
12
13
14
15
 * Creates a Selenium WebDriver using Chrome as the browser
 * @returns {ThenableWebDriver} selenium web driver
 */
module.exports = function() {

    var driver = new selenium.Builder().withCapabilities({
        browserName: 'chrome',
        javascriptEnabled: true,
        acceptSslCerts: true,
        chromeOptions: {
fork icon92
star icon0
watch icon7

+ 3 other calls in file

94
95
96
97
98
99
100
101
102
103

var driver_chr = new webdriver.Builder()
    .forBrowser('chrome')
    .build();

var driver_saf = new webdriver.Builder()
    .forBrowser('safari')
    .build();

searchTest(driver_fx);
fork icon114
star icon102
watch icon22

+ 5 other calls in file