程式化 API
當作為常規函式庫套件包含時,Nightwatch 會匯出一個 API,透過該 API 可以程式化地使用它。您不需要指定設定檔,儘管從 v1.3 開始,預設會建立一個設定檔 (nightwatch.conf.js)。
語法
Nightwatch 2 引入了一個全新的程式化 API,它使得在外部使用 Nightwatch 非常容易,無論是透過建立自訂的執行器還是使用像 Jest、Mocha 或 Ava 這樣的外部測試執行器。
如何將 Nightwatch 與第三方測試執行器搭配使用的深入範例將會很快推出。
Nightwatch.createClient([options])
建立一個新的 Nightwatch 客戶端,可用於建立 WebDriver 工作階段。
語法
const Nightwatch = require('nightwatch');
const client = Nightwatch.createClient({
headless: true,
output: true,
silent: true, // set to false to enable verbose logging
browserName: 'firefox', // can be either: firefox, chrome, safari, or edge
// set the global timeout to be used with waitFor commands and when retrying assertions/expects
timeout: 10000,
// set the current test environment from the nightwatch config
env: null,
// any additional capabilities needed
desiredCapabilities: {
},
// can define/overwrite test globals here;
// when using a third-party test runner only the global hooks onBrowserNavigate/onBrowserQuit are supported
globals: {},
// when the test runner used supports running tests in parallel;
// set to true if you need the webdriver port to be randomly generated
parallel: false,
// All other Nightwatch config settings can be overwritten here, such as:
disable_colors: false
});
client.updateCapabilities([options])
給定一個使用上面列出的 createClient()
方法建立的現有 client
,這可用於更新最初指定的效能。
語法
client.updateCapabilities({
testCapability: 'one, two, three'
});
client.launchBrowser()
給定一個使用上面列出的 createClient()
方法建立的現有 client
,這可用於建立新的瀏覽器工作階段。
返回的物件將是 Nightwatch 瀏覽器 API 物件。
語法
const browser = await client.launchBrowser();