Nightwatch API 建议编辑
Nightwatch 通过将所有必要的命令、设置和其他属性加载到单个 API 对象上运行,该对象在运行时作为单个参数提供给所有测试脚本。这样一来,一切准备就绪,您可以开始编写实际的测试脚本,而不必实例化其他对象。
"浏览器" 对象
我们将主 API 对象称为 browser
- 为了与其他 Selenium 相关的 JS 测试框架保持一致,并且从 v2 开始,它也作为 global
可用
module.exports = {
demoTest: function (browser) {
browser.init();
}
};
您也可能看到 Nightwatch 以前版本的示例,使用 client
。当然,这完全没问题,但对于刚接触 JavaScript 语法的用户来说可能令人困惑。
module.exports = {
demoTest: function (client) {
client.init();
}
};
用作全局
从 Nightwatch 2 开始,browser
可用作全局,因此这也有效
module.exports = {
demoTest: function () {
browser.init();
}
};
还有这个
describe('Nightwatch APIs', function() {
it('demoTest', function () {
browser.init();
})
};
API 内容
以下是 browser
对象上提供的所有公共属性和方法的列表。
WEBDRIVER_ELEMENT_ID
类型:
string
W3C 网页元素标识符,等于字符串常量:
"element-6066-11e4-a52e-4f735466cecf"
browserName
类型:
string
在功能对象中指定的 browserName。
isChrome
类型:
function isChrome(): boolean
如果使用的浏览器是 Google Chrome,则返回
true
|false
。isFirefox
类型:
function isFirefox(): boolean
如果使用的浏览器是 Mozilla Firefox,则返回
true
|false
。isSafari
类型:
function isSafari(): boolean
如果使用的浏览器是 Apple Safari,则返回
true
|false
。isEdge
类型:
function isEdge(): boolean
如果使用的浏览器是 Microsoft Edge,则返回
true
|false
。isInternetExplorer
类型:
function isInternetExplorer(): boolean
如果使用的浏览器是 Microsoft InternetExplorer,则返回
true
|false
。isOpera
类型:
function isOpera(): boolean
如果使用的浏览器是 Opera,则返回
true
|false
。baseUrl
类型:
string
如果使用的浏览器是 Opera,则返回
true
|false
。在 Nightwatch 配置中定义的
baseUrl
的值,它将用作命令.init()
的默认 URL。其他别名为:base_url
、launch_url
或launchUrl
。actions
类型:
function actions(options?: { async?: boolean; bridge?: boolean }): Actions
从 Selenium 返回
Actions
类的全新实例。有关详细信息,请参阅 用户操作 API 部分。capabilities
类型:
object
WebDriver 会话功能,浏览器驱动程序使用它来传达支持的功能。
WebDriver 功能用于传达会话支持的功能。客户端还可以使用功能来定义在创建新会话时需要驱动程序满足哪些功能。
创建 WebDriver 会话时,它会返回一组功能,描述会话的协商后的有效功能。这组功能中包含的一些功能是标准的,并在所有浏览器之间共享,但这组功能也可能包含浏览器特定的功能,并且这些功能始终带有前缀。更多关于 WebDriver 功能。
示例
{
acceptInsecureCerts: false,
browserName: 'chrome',
browserVersion: '96.0.4664.55',
'goog:chromeOptions': { debuggerAddress: 'localhost:50427' },
// ... continued
}
currentTest
类型:
NightwatchTestSuite
包含有关当前正在运行的测试用例的信息的对象。
可用属性
{
// name of the current running testcase
name: ' ... ',
// name of the current running testsuite, i.e. the test file
module: ' ... ',
// name of the current running test group, if any
group: '',
// the results object is shared among all testcases in the current testsuite
results: {
time: 0,
assertions: [Array],
passed: 0,
errors: 0,
failed: 0,
retries: [Number],
skipped: 0,
tests: 0,
steps: [],
stackTrace: '',
// an object accumulating the results of each testcase
testcases: [Object]
},
// the current timestamp, in the format: Wed, 01 Dec 2021 08:34:00 GMT
timestamp: ''
}
desiredCapabilities
类型:
NightwatchDesiredCapabilities
包含 Nightwatch 发送到 WebDriver 的功能的对象,如 Nightwatch 配置文件 (有关详细信息,请参阅 配置部分) 中所定义。
示例
{
browserName: 'chrome',
'goog:chromeOptions': {},
name: 'Example Test'
}
driver
类型:
WebDriver
Nightwatch 背后使用的 Selenium WebDriver 实例。在自定义命令中很有用,当需要扩展 Nightwatch 提供的主要功能时。如果在常规测试用例脚本中使用,则需要将其包装在
.perform()
命令中。示例
在下面的示例中,我们将检索 WebDriver 会话 实例。
describe('Nightwatch APIs', function() {
it('driver demoTest', async function () {
const session = await browser
.init()
.perform(function() {
return this.driver.getSession();
});
})
};
sessionId
类型:
string
WebDriver 会话 ID。WebDriver 为每个会话提供唯一的会话 ID,可用于区分一个会话与另一个会话。有关 WebDriver 会话的更多信息,请访问 W3C WebDriver 页面 和 Selenium WebDriver 文档。
示例
console.log(browser.sessionId); // e0b40362dcec8ec501ac2b42b62bdce2
globals
类型:
NightwatchGlobals
处理后的 Nightwatch 全局对象,其中包含所有当前的全局属性和方法。有关详细信息,请参阅 测试全局部分。
options
类型:
NightwatchOptions
处理后的 Nightwatch 配置对象,其中包含所有当前使用的属性和设置。有关详细信息,请参阅 配置部分。
Keys
类型:
NightwatchKeys
指向 Selenium 的 Key 枚举的链接,其中包含所有不可作为文本键入的按压键。这通常在使用发送文本到页面的命令 (如
.sendKeys()
) 时需要。page
类型:
NightwatchPage & NightwatchCustomPageObjects
包含当前页面对象的字典,为当前正在运行的测试用例创建。有关详细信息,请参阅 使用页面对象 部分。
assert
类型:
Assert
有关详细信息,请参阅 断言 API 部分。
verify
类型:
Assert
有关详细信息,请参阅 断言 API 部分。
ensure
类型:
Ensure
有关详细信息,请参阅 确保 API 部分。
expect
类型:
Expect
有关详细信息,请参阅 预期 API 部分。
chrome
类型:
object
Chromium 特定的命令。有关详细信息,请参阅 API 命令 部分。
firefox
类型:
object
Firefox 特定的命令。有关详细信息,请参阅 API 命令 部分。