如何获取接口返回数据
如何在 JavaScript 通过接口自动生成和返回接口数据呢?
在自动化测试中常常遇到接口测试,或是使用的数据需要从接口返回,那么如何来实现这种情况?
例如我想通过 generateLicense 方法生成一个 license,然后在之后的自动化测试用例中使用这个生成的 license 继续做下一步的操作,例如注册 license 等。
在 license.js 文件中创建一个 generateLicense 方法:
generateLicense: function(success, day, capacity, code) {
var request = require('request');
var options = { method: 'POST',
url: 'https://generate-license/api/licenses',
headers:
{ 'postman-token': 'd849e636-58c9-2705',
'cache-control': 'no-cache',
authorization: 'Basic YWRtaW46U',
'content-type': 'application/json' },
body: { company: 'Google',
email: '5012962@qq.com',
expiration: day,
capacity: capacity,
phone: '89262518',
address: 'Dalian',
code: code },
json: true
};
request(options, function (error, response) {
if (error) {
console.log(error);
return;
}
success(response);
});
},
对上面生成的 license 进行赋值,之后的测试用例中就可以使用 MVlicense 了。
使用中会涉及到异步操作,异步如何操作请看之前的文章。
const license = client.page.license();
let MVlicense;
license.generateLicense(function(response) {
MVlicense = response.body.data.license.license;
}, 365, 10, 'MV');
等待按钮文字变更
在使用 Nightwatch 做自动化测试的时候,会遇到这样一种情况:
需要创建一个 query,然后等待这个 query 的状态从 Wait 变成 Running 最后到 Available 时再执行后续操作。Nightwatch 并没有提供这样的方法,可以通过下面的方式解决。
'Wait for text': function waitForText(client) {
const query = client.page.query();
query.navigate();
for (let i = 0; i <= 10; i++) {
client.getText('status', function (result) {
if (result.value.indexOf('Available') == 0) {
this.break;
} else {
client.pause(1000);
i++;
}
});
}
// TODO something
}
测试用例失败继续执行
自动化测试中,有一个验证点,当测试通过时,后面的测试脚本继续执行;
当出现异常时,你希望标记出来这个错误,但不影响后面的测试脚本执行,在 Nightwatch 中如何做?
下面的一段代码验证 home 页面的 body 是否显示。这里如果显示则将验证点置为 false,如下:
home.waitForElementVisible('@body', 3000, true, function(result) {
if (result.value) {
// 测试报告中会显示失败,但是会继续执行后面的测试脚本
client.verify.equal(result.value, false);
} else {
// 验证点通过
console.log('Pass');
}
});
注意:这里如果用 assert,程序就会中断执行。
// 中断执行
client.assert.equal(result.value, false);
打开多个窗口
如果想打开两个窗口并控制那个窗口怎么办?
var url = process.env.BASE_URL, newWindow;
client.execute(function (url, newWindow) {
window.open(url, newWindow, 'height=768,width=1024');
}, [url, newWindow]);
client.window_handles(function(result) {
this.verify.equal(result.value.length, 2, 'There should be 2 windows open');
newWindow = result.value[1];
this.switchWindow(newWindow);
})
持续集成问题
在持续集成执行自动化测试用例时候会遇到那些问题呢?
- 运行时间过长
- 因为某些错误程序卡住
- 异常处理
针对以上三种情况,通过下面的三种方式进行解决。
运行时间过长, E2E 测试脚本中难免需要时间等待,例如
this.pause(1000);
// 尽可能将说有的 pause 换成 wait,例如:
this.element('@columns').to.be.visible.before(2000);
// 或
this.waitForElementVisible('@columns', 5000);
因为某些错误程序卡住, 在 TestCase 中进行验证时,例如
this.assert.equal(result.value.length, 1);
// 如果只想标注失败,继续执行后面的代码,则需将 assert 换成 verify
this.veriry.equal(result.value.length, 1);
// 在 waitForElementVisible 中加 abortOnFailure 参数,当设置为 false,在 wait 超时时,就会标志为 false 继续继续执行
this.waitForElementVisible('@columns', 5000, false);
//还可以通过在 nightwatch.conf.js 设置全局变量
abortOnAssertionFailure: false
异常处理
当程序执行运行一次时,程序运行正常,一旦遇到异常时,下次执行就回出错。
例如:比如邀请账号登录系统的操作。管理员添加一个新用户,然后用这个新用户登录,之后管理员删除这个账户。但如果删除这个账号失败时,下次执行这个程序再邀请这个账号时就会提示这个账号存在的,可能这个时候这个程序就执行不下去了。这个时候就需要考虑这些异常情况处理,保证程序能够良好的执行下去。
如何在 VS 里调试 Nightwatch
除了通过增加 log 之外
console.log('===========')
来调试 Nightwatch 代码,如何通过配置 VS code 来 Debug Nightwatch 代码?
Ctrl+Shift+D 打开 Debug 界面,配置如下:
{
// Use IntelliSense to learn about possible Node.js debug attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "npm test",
"program": "${workspaceRoot}/node_modules/nightwatch/bin/runner.js",
"args": [
"tests/DQA/DQA-221/login.js"
]
}
]
}
这样就可以打断点调试了。
本文来自投稿,不代表本人立场,如若转载,请注明出处:http://www.souzhinan.com/kj/244560.html