npm.io
10.0.2 • Published 3d ago

webdriver

Licence
MIT
Version
10.0.2
Deps
11
Size
432 kB
Vulns
0
Weekly
0
Stars
9.8K

WebDriver

A lightweight, non-opinionated implementation of the WebDriver and WebDriver BiDi specification including mobile commands supported by Appium

There are tons of Selenium and WebDriver binding implementations in the Node.js world. Every one of them has an opinionated API and recommended way to use it. This binding is the most non-opinionated you will find as it just represents the WebDriver specification and doesn't come with any extra or higher-level abstraction. It is lightweight and comes with support for the WebDriver specification and Appium.

The package supports the following protocols:

Commands are added to the client's protocol based on assumptions of provided capabilities. You can find more details about the commands by checking out the @wdio/protocols package. All commands come with TypeScript support.

Install

To install this package from NPM run:

npm i webdriver

WebDriver Example

The following example demonstrates a simple Google Search scenario:

import WebDriver from 'webdriver';

const client = await WebDriver.newSession({
    path: '/',
    capabilities: { browserName: 'firefox' }
})

await client.navigateTo('https://www.google.com/ncr')

const searchInput = await client.findElement('css selector', '#lst-ib')
await client.elementSendKeys(searchInput['element-6066-11e4-a52e-4f735466cecf'], 'WebDriver')

const searchBtn = await client.findElement('css selector', 'input[value="Google Search"]')
await client.elementClick(searchBtn['element-6066-11e4-a52e-4f735466cecf'])

console.log(await client.getTitle()) // outputs "WebDriver - Google Search"

await client.deleteSession()

WebDriver Bidi Example

To connect to the WebDriver Bidi protocol you have to send along a webSocketUrl flag to tell the browser driver to opt-in to the protocol:

import WebDriver from 'webdriver'

const browser = await WebDriver.newSession({
    capabilities: {
        webSocketUrl: true,
        browserName: 'firefox'
    }
})

await browser.sessionSubscribe({ events: ['log.entryAdded'] })

/**
 * returns: {"type":"console","method":"log","realm":null,"args":[{"type":"string","value":"Hello Bidi"}],"level":"info","text":"Hello Bidi","timestamp":1657282076037}
 */
browser.on('log.entryAdded', (entryAdded) => console.log('received %s', entryAdded))

await browser.executeScript('console.log("Hello Bidi")', [])
await browser.deleteSession()

Configuration

To create a WebDriver session call the newSession method on the WebDriver class and pass in your configurations:

import WebDriver from 'webdriver'
const client = await WebDriver.newSession(options)

The following options are available:

capabilities

Required: `true`

logLevel

protocol
hostname
port
path
queryParams
connectionRetryTimeout
connectionRetryCount
bidiResponseTimeout
agent
transformRequest
transformResponse

Keywords