API
client
This variable represents the Nightwatch WebDriver client. This is the main part of this package. All Nightwatch API is available on this variable. Important to note that every method call is wrapped in a promise. So you can await its execution usingawait
keyword.
Also chaining is supported as well.
Before using it you need to create a WebDriver session.Type
Api
Examples
const { client } = require('nightwatch-api');
(async function() {
await client
.url('https://duckduckgo.com/')
.setValue('input[name="q"]', 'WebDriver')
.click('input[type="submit"]')
.assert.containsText('#links', 'WebDriver - w3.org');
)();
const { client } = require('nightwatch-api');
(async function() {
// This is much easier to debug, place console.logs, use if conditions and for loops
await client.url('https://duckduckgo.com/');
await client.setValue('input[name="q"]', 'WebDriver');
await client.click('input[type="submit"]');
await client.assert.containsText('#links', 'WebDriver - w3.org');
)();
const { client } = require('nightwatch-api');
const googleSearch = client.page.google();
(async function() {
await googleSearch.init();
await googleSearch.setValue('@searchField', 'WebDriver');
await googleSearch.click('@searchButton');
await googleSearch.assert.containsText('@searchResult', 'WebDriver - w3.org');
)();
startWebDriver(options)
Starts WebDriver server according to selected environment configuration. You can use it to start chromedriver, geckodriver, selenium server and other WebDrivers.Parameters
options: object
configFile?: string
Nightwatch configuration file location. By default it'snightwatch.json
ornightwatch.conf.js
in current process working directory.env?: string
Selected Nightwatch environment. By default it'sdefault
.silent?: false | true
Disable Nightwatch success logs like "√ Element <body> was visible after 96 milliseconds." By default it's false.
Returns
Promise<void>
Examples
const { startWebDriver, stopWebDriver } = require('nightwatch-api');
beforeAll(async () => {
await startWebDriver({ env: 'firefox' });
});
afterAll(async () => {
await stopWebDriver();
});
const { startWebDriver, stopWebDriver } = require('nightwatch-api');
(async function() {
try {
await startWebDriver({ env: 'chrome' });
// create WebDriver client
// use WebDriver session
} catch (err) {
console.log(err.stack);
} finally {
// close WebDriver session
await stopWebDriver();
}
)();
stopWebDriver()
Stops the currently running WebDriver.Returns
Promise<void>
createSession(options)
Creates a new WebDriver session. You need to create a session to be able to communicate with the browser.Parameters
options: object
configFile?: string
Nightwatch configuration file location. By default it'snightwatch.json
ornightwatch.conf.js
in current process working directory.env?: string
Selected Nightwatch environment. By default it's the same used for previous startWebDriver call otherwisedefault
.silent?: false | true
Disable Nightwatch success logs like "√ Element <body> was visible after 96 milliseconds." By default it's false.
Returns
Promise<void>
Examples
const { createSession, closeSession } = require('nightwatch-api');
beforeAll(async () => {
await createSession({ env: 'firefox' });
});
afterAll(async () => {
await closeSession();
});
const { createSession, closeSession } = require('nightwatch-api');
(async function() {
try {
// create WebDriver session
await createSession({ env: 'chrome' });
// use WebDriver client
} catch (err) {
console.log(err.stack);
} finally {
await closeSession();
// close WebDriver session
}
)();
closeSession()
Closes the active WebDriver session.Returns
Promise<void>
Section
This class enables creation of Nightwatch page object sections dynamically.Constructors
new Section(definition,options)
Parameters
definition: object
options: object
Returns
Section
Properties
api
Type
Api
Examples
const { client, Section } = require('nightwatch-api');
function createSearchSection(provider, parent) {
if (provider === 'google') {
return new Section(
{
selector: 'body',
elements: {
searchField: 'input[name="q"]',
searchButton: 'input[type="submit"]',
},
},
{
name: 'Search Section',
parent: client
}
);
}
}
(async function() {
const section = createSearchSection('google');
await client.init();
await section.setValue('@searchField', 'WebDriver');
await section.click('@searchButton');
)();
getNewScreenshots()
Return the screenshot filenames which were created after latest call of this method.Returns