-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
41 changed files
with
7,117 additions
and
12,088 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,207 @@ | ||
import { createSession as createNightwatchSession, runQueue } from './client'; | ||
import * as Client from './client'; | ||
import { promisifyApi, promisifySection, promisifyExpect, promisifyPageObjects } from './promisify'; | ||
import proxy from './proxy'; | ||
import { Api } from 'nightwatch'; | ||
import section from 'nightwatch/lib/page-object/section'; | ||
|
||
let nightwatchClient: Api; | ||
|
||
export { startWebDriver, stopWebDriver, closeSession } from './client'; | ||
/** | ||
* This variable represents the Nightwatch WebDriver client. | ||
* This is the main part of this package. | ||
* All Nightwatch [API](http://nightwatchjs.org/api) is available on this variable. | ||
* Important to note that every method call is wrapped in a promise. | ||
* So you can await it's execution using `await` keyword. | ||
* Also chaining is supported as well. | ||
* Before using it you need to create a WebDriver session. | ||
* @example | ||
* 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'); | ||
* )(); | ||
* @example | ||
* 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'); | ||
* )(); | ||
* @example | ||
* | ||
* 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'); | ||
* )(); | ||
*/ | ||
export const client = proxy(() => nightwatchClient); | ||
|
||
export interface IApiConfig { | ||
env?: string; | ||
configFile?: string; | ||
/** | ||
* Starts WebDriver server according to selected environment configuration. | ||
* You can use it to start chromedriver, geckodriver, selenium server and other WebDrivers. | ||
* @example | ||
* const { startWebDriver, stopWebDriver } = require('nightwatch-api'); | ||
* | ||
* beforeAll(async () => { | ||
* await startWebDriver({ env: 'firefox' }); | ||
* }); | ||
* | ||
* afterAll(async () => { | ||
* await stopWebDriver(); | ||
* }); | ||
* @example | ||
* 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(); | ||
* } | ||
* )(); | ||
*/ | ||
export async function startWebDriver( | ||
options: { | ||
/** | ||
* Selected Nightwatch [environment](http://nightwatchjs.org/gettingstarted#test-settings). | ||
* By default it's `default`. | ||
*/ | ||
env?: string; | ||
/** | ||
* Nightwatch configuration file location. | ||
* By default it's `nightwatch.json` or `nightwatch.conf.js` in current process working directory. | ||
*/ | ||
configFile?: string; | ||
} = {} | ||
) { | ||
const { | ||
env = Client.getDefautEnvironment(), | ||
configFile = Client.getDefaultConfigFile() | ||
} = options; | ||
return Client.startWebDriver({ env, configFile }); | ||
} | ||
|
||
export async function createSession({ env, configFile }: IApiConfig = {}) { | ||
nightwatchClient = await createNightwatchSession({ env, configFile }); | ||
promisifyApi(nightwatchClient, runQueue); | ||
promisifyExpect(nightwatchClient, runQueue); | ||
promisifyPageObjects(nightwatchClient, runQueue); | ||
/** | ||
* Stops the currently running WebDriver. | ||
*/ | ||
export async function stopWebDriver() { | ||
await Client.stopWebDriver(); | ||
} | ||
|
||
export const client = proxy(() => nightwatchClient); | ||
/** | ||
* Creates a new WebDriver session. | ||
* You need to create a session to be able to communicate with the browser. | ||
* @example | ||
* const { createSession, closeSession } = require('nightwatch-api'); | ||
* | ||
* beforeAll(async () => { | ||
* await createSession({ env: 'firefox' }); | ||
* }); | ||
* | ||
* afterAll(async () => { | ||
* await closeSession(); | ||
* }); | ||
* @example | ||
* 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 | ||
* } | ||
* )(); | ||
*/ | ||
export async function createSession( | ||
/** | ||
* Options are ignored if you already started a the WebDriver using `startWebDriver`. | ||
*/ | ||
options: { | ||
/** | ||
* Selected Nightwatch [environment](http://nightwatchjs.org/gettingstarted#test-settings). | ||
* By default it's `default`. | ||
*/ | ||
env?: string; | ||
/** | ||
* Nightwatch configuration file location. | ||
* By default it's `nightwatch.json` or `nightwatch.conf.js` in current process working directory. | ||
*/ | ||
configFile?: string; | ||
} = {} | ||
) { | ||
const { | ||
env = Client.getDefautEnvironment(), | ||
configFile = Client.getDefaultConfigFile() | ||
} = options; | ||
nightwatchClient = await Client.createSession({ env, configFile }); | ||
promisifyApi(nightwatchClient, Client.runQueue); | ||
promisifyExpect(nightwatchClient, Client.runQueue); | ||
promisifyPageObjects(nightwatchClient, Client.runQueue); | ||
} | ||
|
||
/** | ||
* Closes the active WebDriver session. | ||
*/ | ||
export async function closeSession() { | ||
await Client.closeSession(); | ||
} | ||
|
||
/** | ||
* This class enables creations of Nightwatch page object sections dynamically. | ||
* @example | ||
* 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'); | ||
* )(); | ||
*/ | ||
export class Section extends section { | ||
constructor(definition: object, options: object) { | ||
super(definition, options); | ||
promisifySection(this.api, runQueue); | ||
promisifySection(this.api, Client.runQueue); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
node_modules | ||
*.log | ||
dist | ||
typedoc.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.