selenium-adapter
A promise adapter for Selenium WebDriver for use with ES2017 async/await.
Installing
selenium-adapter
is available as an
npm package.
Dependencies
ChromeDriver depends on the system libraries libxi6
and libgconf-2-4
, which
need to be installed before installing selenium-adapter
.
Note that selenium-adapter
is only tested against recent versions of Chrome
and Firefox.
Usage
{ const chrome = await const searchBox = await await const searchButton = await await const body = await console};
Choosing a Browser
function makeChromeAdapter(): WebDriver
{ const chrome = };
Creates a Selenium WebDriver instance that controls a Chrome/Chromium browser via ChromeDriver.
function makeHeadlessChromeAdapter(): WebDriver
{ const chrome = };
Creates a Selenium WebDriver instance that controls a Chrome/Chromium browser in headless mode via ChromeDriver.
function makeFirefoxAdapter(): WebDriver
{ const firefox = };
Creates a Selenium WebDriver instance that controls a Firefox browser via GeckoDriver.
function makePhantomAdapter(): WebDriver
{ const phantom = };
Creates a Selenium WebDriver instance that controls a PhantomJS browser.
Interacting With The Browser
function close(adapter: WebDriver): Promise<void>
{ const chrome = await };
Closes the browser and Selenium WebDriver client.
function navigate(adapter: WebDriver, url: string): Promise<void>
{ const chrome = await };
Navigate to a given URL.
In Chrome, if navigation fails due to a connection error or an HTTP status code in the 400s or 500s, an error will be raised.
function evaluate<T> (adapter: WebDriver, fn: () => T): Promise<T>
{ const chrome = await console};
Evaluate a JavaScript function within the context of the currently open page. The function's return value is serialized and returned. Note that the function will only have access to local variables defined inside of the function.
function findElement (adapterOrElement: WebDriver | ?WebDriverElement, selector: string, text: ?string): Promise<?WebDriverElement>
{ const chrome = await const header = await const searchBox = await const searchSubmit = await };
Finds the first element matching a CSS selector and optionally containing a
string. If no matching element is found, null
is returned. If an adapter is
given as first argument, the search is scoped to the entire page. If an element
is given, the search is scoped to only descendants of that element.
For the use case of finding an element that will exist after a delay (e.g. upon
completion of an HTTP request), see
waitFor
Interacting With Elements
function getAttribute (element: ?WebDriverElement): Promise<?string>
{ const chrome = await const searchBox = await console console};
Reads an attribute, or if not present, a property by name for an element.
function getText (element: ?WebDriverElement): Promise<?string>
{ const chrome = await const title = await console};
Reads the text contained within an element.
function click (element: ?WebDriverElement): Promise<void>
{ const chrome = await const signUp = await await };
Clicks on a given element
function fillIn (element: ?WebDriverElement, value: string): Promise<void>
{ const chrome = await const searchBox = await await await };
Given an input element, clears its value and types in a new value.
Utilities
function waitFor<T> (time: number, getResult: () => Promise<T>): Promise<?T>
{ const chrome = await const delayedButton = };
Run the given function over and over until it returns a truthy value. If a
truthy value is not returned within the given number of milliseconds, null
is
returned.