Constructor
new Driver(config, opt)
Constructor returning a Driver object, which will be used to interface with a Webdriver.
It's used as a base class for specific drivers (ChromeDriver, FirefoxDriver, EdgeDriver, SafariDriver).
Specific drivers know how to run a local webdriver command (if present) and are able to adjust calls and return values so that they follow the w3c webdriver Protocol
Parameters:
Name | Type | Description | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
config |
Config |
The session config that will be sent to the webdriver |
||||||||||||||||||||||||||||||||
opt |
Object |
Options to configure the API itself Properties
|
- Mixes In:
- Source:
Example
// Create a driver using the Chrome browser
var driver1 = new Driver(new Config())
await driver.newSession()
// Create a driver, but does NOT spawn a new webcontroller process
var driver2 = new Driver(new Config(), {
spawn: false,
hostname: '127.0.0.1',
port: 4444
})
await driver.newSession()
Extends
Members
(static) Using
A value used by Driver#findElement and Driver#findElements to decide what kind of filter will be applied
- Source:
(static) Key
Constant returning special KEY characters (enter, etc.) Constant are from the global variable KEY
- Source:
Example
var body = await driver.findElementTagName('body')
body.sendKeys(driver.Key.ENTER + 'n')
Methods
setExecutable(executable)
Set executable for the specific webdriver. Deriving classes like ChromeDriver, SafariDriver etc. will set the specific executable to run.
Parameters:
Name | Type | Description |
---|---|---|
executable |
string |
The name of the executable to run |
- Source:
(async) run(opt)
Run the actual webdriver's executable, depending on the browser This method might be overridden by deriving classes like ChromeDriver or SafariDriver
Parameters:
Name | Type | Description | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
opt |
Object |
Options to configure the webdriver executable Properties
|
- Source:
setPollInterval(interval)
Set the default poll interval
Parameters:
Name | Type | Description |
---|---|---|
interval |
number |
How many milliseconds between each polling attempt |
- Source:
Example
// Create a driver using the Chrome browser
var driver = new drivers.ChromeDriver(new Config())
driver.setPollInterval(200)
setPollTimeout(timeout)
Set the default waitFor timeout
Parameters:
Name | Type | Description |
---|---|---|
timeout |
number |
How many milliseconds between failing the call |
- Source:
(async) waitFor(timeout)
This method will wrap ANY method in Driver or Element with a polling mechanism, which will retry
the call every pollInterval
milliseconds (it defaults to 300, but it can
be set when running the Driver's constructor)
It's also possible to pass an extra parameter to the original method in Driver,
which represents a checker function that will need to return truly for success
Parameters:
Name | Type | Default | Description |
---|---|---|---|
timeout |
number | 0 |
How long to poll for |
- Source:
Example
// Create a driver using the Chrome browser
var driver = new drivers.ChromeDriver(new Config())
await driver.navigateTo('http://www.google.com')
// The findElementCss has 5 seconds to work
var el = driver.waitFor(5000).findElementCss('[name=q]')
// The findElementsCss (note the plural: it returns an array)
// has 10 seconds to work, AND the result needs to be not-empty
// (see the tester function added)
await el.waitFor(10000).findElementsCss('.listItem', (r) => r.length))
(async) startWebDriver()
Start the right webdriver, depending on what browser is attached to it. Since webdrivers can take a little while to answer, this method also checks that the webdriver process is actively and properly dealing with connections This method is called by newSession
- Source:
(async) stopWebDriver(signal)
Stops the webdriver process, if running. It does so by sending it a SIGTERM message. You can specify the message to send the process
Parameters:
Name | Type | Default | Description |
---|---|---|---|
signal |
string | number | SIGTERM |
The signal to send. To know which signals you can send, |
- Source:
Example
// Create a driver using the Chrome browser
var driver = new drivers.ChromeDriver(new Config())
// ...
// ...
await driver.stopWebDriver()
(async) sleep(ms)
Sleep for ms milliseconds NOTE: you shouldn't use this as a quick means to wait for AJAX calls to finish. If you need to poll-wait, use waitFor
Parameters:
Name | Type | Description |
---|---|---|
ms |
number |
The number of milliseconds to wait for * |
- Source:
(async) newSession() → {Promise.<object>}
Create a new session. If the driver was created with spawn
set to true
, it will
run the webdriver for the associated browser before asking for the session
- Source:
(async) deleteSession() → {Promise.<Driver>}
Delete the current session. This will not kill the webdriver process (if one was spawned). You can create a new session with newSession
- Source:
(async) status() → {Promise.<object>}
Get status
- Source:
(async) performActions(actions) → {Promise.<Driver>}
Perform actions as specified in the passed actions object To see how to create an actions object, check the Actions class
Parameters:
Name | Type | Description |
---|---|---|
actions |
Actions |
An object |
- Source:
Example
var actions = new Actions()
actions.tick.keyboardDown('R').keyboardUp('R')
await driver.performActions(actions)
(async) releaseActions()
Release the current actions
- Source:
(async) getTimeouts() → {Promise.<Object>}
Get timeout settings in the page
- Source:
(async) setTimeouts(param)
Set timeouts
Parameters:
Name | Type | Description | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
param |
Object |
The object with the timeouts Properties
|
- Source:
(async) navigateTo() → {Promise.<Driver>}
Navigate to page
- Source:
(async) getCurrentUrl() → {Promise.<string>}
Bake a cake without coffee Also, get the current URL
- Source:
(async) back() → {Promise.<Driver>}
Go back one step
- Source:
(async) forward() → {Promise.<Driver>}
Go forward one step
- Source:
(async) refresh() → {Promise.<Driver>}
Refresh the page
- Source:
(async) getTitle() → {Promise.<string>}
Get page's title
- Source:
(async) getWindowHandle() → {Promise.<string>}
Get the current window's handle
- Source:
(async) closeWindow() → {Promise.<Driver>}
Close the current window
- Source:
(async) getWindowHandles() → {Promise.<Array.<string>>}
Get window handles as an array
- Source:
(async) switchToWindow(string) → {Promise.<Driver>}
Switch to window
Parameters:
Name | Type | Description |
---|---|---|
string |
handle |
The window handle to switched to |
- Source:
(async) switchToFrame(frame) → {Promise.<Driver>}
Switch to frame
Parameters:
Name | Type | Description |
---|---|---|
frame |
string | number | Element |
Element object, or element ID, of the frame |
- Source:
(async) switchToParentFrame() → {Promise.<Driver>}
Switch to the parent frame
- Source:
Example
var frame = await driver.findElementCss('iframe')
await driver.switchToFrame(frame)
await driver.switchToParentFrame()
(async) getWindowRect() → {Promise.<Object>}
Get window rect
- Source:
(async) setWindowRect(rect) → {Promise.<Object>}
Set window rect
Parameters:
Name | Type | Description | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
rect |
Promise.<Object> |
An object with properties Properties
|
- Source:
(async) maximizeWindow() → {Promise.<Driver>}
Maximize window
- Source:
(async) minimizeWindow() → {Promise.<Driver>}
Minimize window
- Source:
(async) fullScreenWindow() → {Promise.<Driver>}
Make window full screen
- Source:
(async) getPageSource() → {Promise.<string>}
Get page source
- Source:
(async) executeScript(script, args)
Execute sync script
Parameters:
Name | Type | Description |
---|---|---|
script |
string |
The string with the script to be executed |
args |
array |
The arguments to be passed to the script |
- Source:
(async) executeAsyncScript(script, args)
Execute async script
NOTE: An extra argument is passed to the script, in addition to the arguments
in args
: it's the callback the script will need to call
once the script has executed.
To return a value from the async script, pass that value to the callback
Parameters:
Name | Type | Description |
---|---|---|
script |
string |
The string with the script to be executed |
args |
Array |
The arguments to be passed to the script |
- Source:
Example
var script = 'var name = arguments[0];var cb = arguments[1];cb(\'Hello \' + name);'
await driver.executeAsyncScript(script, ['tony'])
(async) getAllCookies() → {Array.<Object>}
Get all cookies
- Source:
(async) getNamedCookie() → {Object}
Get cookies matching a specific name
- Source:
(async) addCookie(cookie) → {Promise.<Driver>}
Get cookies matching a specific name
Parameters:
Name | Type | Description |
---|---|---|
cookie |
object |
The cookie object |
- Source:
Example
await driver.addCookie({
name: 'test',
value: 'a test',
path: '/',
domain: 'google.com.au',
expiry: 1732569047,
secure: true,
httpOnly: true})
(async) deleteCookie() → {Promise.<Driver>}
Delete cookie matching a specific name
- Source:
(async) deleteAllCookies() → {Promise.<Driver>}
Delete all cookies
- Source:
(async) dismissAlert() → {Promise.<Driver>}
Dismiss an alert
- Source:
(async) acceptAlert() → {Promise.<Driver>}
Accepts an alert
- Source:
(async) getAlertText() → {string}
Get an alert's text
- Source:
(async) sendAlertText(text) → {Promise.<Driver>}
Send text to an alert
Parameters:
Name | Type | Description |
---|---|---|
text |
string |
The text that should be sent |
- Source:
(async) takeScreenshot() → {buffer}
Take screenshot *
- Source:
(async) getActiveElement() → {Element}
Get active element
- Source:
(async) findElement(using, value) → {Element}
Find an element.
Note that you are encouraged to use one of the helper functions:
findElementCss()
, findElementLinkText()
, findElementPartialLinkText()
,
findElementTagName()
, findElementXpath()
Parameters:
Name | Type | Description |
---|---|---|
using |
string |
It can be |
value |
string |
The parameter to the |
- Source:
(async) findElements(using, value) → {Element}
Find several elements
Note that you are encouraged to use one of the helper functions:
findElementsCss()
, findElemenstLinkText()
, findElementsPartialLinkText()
,
findElementsTagName()
, findElementsXpath()
Parameters:
Name | Type | Description |
---|---|---|
using |
string |
It can be |
value |
string |
The parameter to the |
- Source:
(async) findElementCss(value)
Find first element matching CSS
Parameters:
Name | Type | Description |
---|---|---|
value |
string |
The CSS expression |
- Overrides:
- Source:
(async) findElementLinkText(value)
Find first element matching text
Parameters:
Name | Type | Description |
---|---|---|
value |
string |
The text to match |
- Overrides:
- Source:
Example
var a = driver.findElementLinkText('cick here')
// or...
var el = driver.findElementCss('#something') // or
var l = item.findElementLinkText('click here')
(async) findElementPartialLinkText(value)
Find first element matching link text
Parameters:
Name | Type | Description |
---|---|---|
value |
string |
The link text to match |
- Overrides:
- Source:
Example
var a = driver.findElementPartialLinkText('cick here')
// or...
var el = driver.findElementCss('#something') // or
var l = item.findElementPartialLinkText('click here')
(async) findElementTagName(value)
Find first element matching tag name
Parameters:
Name | Type | Description |
---|---|---|
value |
string |
The tag name to match |
- Overrides:
- Source:
Example
var item = driver.findElementTagName('item') // or
var subItem = item.findElementTagName('subitem')
(async) findElementXpath(value)
Find first element matching xpath
Parameters:
Name | Type | Description |
---|---|---|
value |
string |
The xpath to match |
- Overrides:
- Source:
Example
var item = driver.findElementXpath('/html/body/form[1]') // or
var subItem = item.findElementXpath('/div/div/subitem')
(async) findElementsCss(value)
Find all elements matching CSS
Parameters:
Name | Type | Description |
---|---|---|
value |
string |
The CSS expression |
- Overrides:
- Source:
Example
var allItems = driver.findElementsCss('.list')
var item = allItems[0]
var allTitles = item.findElementsCss('.title')
(async) findElementsLinkText(value)
Find all elements matching text
Parameters:
Name | Type | Description |
---|---|---|
value |
string |
The text to match |
- Overrides:
- Source:
Example
var links = driver.findElementsLinkText('cick here')
// or...
var el = driver.findElementCss('#something') // or
var links = item.findElementsLinkText('click here')
(async) findElementsPartialLinkText(value)
Find all elements matching link text
Parameters:
Name | Type | Description |
---|---|---|
value |
string |
The link text to match |
- Overrides:
- Source:
Example
var links = driver.findElementsPartialLinkText('cick here')
// or...
var el = driver.findElementCss('#something') // or
var links = item.findElementsPartialLinkText('click here')
(async) findElementsTagName(value)
Find all elements matching tag name
Parameters:
Name | Type | Description |
---|---|---|
value |
string |
The tag name to match |
- Overrides:
- Source:
Example
var items = driver.findElementsTagName('item') // or
var el = items[0]
var subItem = el.findElementsTagName('subitem')
(async) findElementsXpath(value)
Find all elements matching xpath
Parameters:
Name | Type | Description |
---|---|---|
value |
string |
The xpath to match |
- Overrides:
- Source: