Node.js 在Nightwatch命令队列中使用REST客户端
我正在使用基于Node.js 在Nightwatch命令队列中使用REST客户端,node.js,rest,selenium,nightwatch.js,Node.js,Rest,Selenium,Nightwatch.js,我正在使用基于Nightwatch.js的Nightwatch cumber进行测试。Ans我还使用页面对象模式。这些测试不仅是基于Selenium的end2end测试,而且是restapi测试。对于其余的测试,我使用了一个。现在,我想将夜视功能(包括硒)与休息功能结合起来。因此,我想将这两种技术结合使用,并将RESTAPI调用集成到Nightwatch框架中 我尝试将REST API调用集成到Nightwatch的perform()函数中,以将REST调用添加到中,但没有完全成功。在执行下一个
Nightwatch.js
的Nightwatch cumber
进行测试。Ans我还使用页面对象模式
。这些测试不仅是基于Selenium
的end2end测试,而且是restapi
测试。对于其余的测试,我使用了一个。现在,我想将夜视功能(包括硒)与休息功能结合起来。因此,我想将这两种技术结合使用,并将RESTAPI调用集成到Nightwatch框架中
我尝试将REST API调用集成到Nightwatch的perform()
函数中,以将REST调用添加到中,但没有完全成功。在执行下一个Nightwatch命令之前,我必须确保REST调用完全完成。目前,REST调用之后的以下步骤将在REST调用完成之前执行。但我该如何解决这个问题呢
这是我的Cucumber功能文件:
Feature: JIRA projects tests
Scenario: my first test
When the user logs out
When the user deletes a still existing project with key "ABC-123" via REST API
When the user logs out
以下是我的步骤定义
:
const { client } = require("nightwatch-cucumber");
const { defineSupportCode } = require("cucumber");
const myPage = client.page.myPageView();
defineSupportCode(({ Given, When, Then }) => {
When(/^the user logs out$/, () => {
return myPage.logoutUser(client);
});
When(
/^the user deletes a still existing project with key "([^"]*)" via REST API$/,
projectKey => {
return myPage.deleteProjectViaRestApi(client, projectKey);
}
);
});
这些是我的页面对象
函数:
const restClientConnector = require("../../rest/restClientConnector");
const environmentVariables = require("../../helpers/getEnvironmentVariables");
module.exports = {
elements: {},
commands: [
{
logoutUser(client) {
console.log("1");
return client
.deleteCookies()
.url(
environmentVariables.launchUrl(client) +
"/crowd/console/logoff.action"
);
},
deleteProjectViaRestApi(client, projectKey) {
return client
.perform(function() {
//delete the given project
restClientConnector
.jiraConnector(
environmentVariables.jiraHostUrl,
environmentVariables.jiraAdminUsername,
environmentVariables.jiraAdminPassword
)
.project.deleteProject(
{
projectIdOrKey: projectKey
},
function(error, result) {
console.log("2");
}
);
})
.perform(function() {
restClientConnector
.jiraConnector(
environmentVariables.jiraHostUrl,
environmentVariables.jiraAdminUsername,
environmentVariables.jiraAdminPassword
)
.project.getProject(
{
projectIdOrKey: projectKey
},
function(error, result) {
console.log("3");
}
);
});
//.waitForTime(4000);
}
}
]
};
因此,我希望这3个Cucumber步骤同步运行,一个接一个。我添加了一些console.log()
输出来检查这一点。在测试运行期间,我希望控制台输出的顺序:
1
2
3
1
相反,我得到以下输出:
Starting selenium server... started - PID: 10436
.1
..1
..
1 scenario (1 passed)
3 steps (3 passed)
0m03.782s
3
2
因此,当用户注销时,Cucumber步骤的第二次调用在用户通过REST API删除键为“ABC-123”的现有项目时,Cucumber步骤完全完成之前开始执行
如果我在页面对象
中取消注释行.waitForTime(4000)
(这是一个自定义命令),那么我会得到正确的输出,但我不想以这种静态方式等待。非常脏:
Starting selenium server... started - PID: 10554
.1
.2
3
.1
..
1 scenario (1 passed)
3 steps (3 passed)
0m07.783s
如何解决问题以在下一个步骤之后执行一个步骤,或者如何将REST调用集成到Nightwatch命令队列中。我还尝试使我的功能异步
,并使用wait
执行所有命令,但也没有成功。如果需要同步运行异步任务,则必须在perform
函数中使用done
回调
browser.perform(function(done) {
//do some async stuff...
done();
});
异步任务完成后,您希望调用done
。在您的情况下,它应该类似于以下内容:
deleteProjectViaRestApi(client, projectKey) {
return client
.perform(function(done) {
//delete the given project
restClientConnector
.jiraConnector(
environmentVariables.jiraHostUrl,
environmentVariables.jiraAdminUsername,
environmentVariables.jiraAdminPassword
)
.project.deleteProject(
{
projectIdOrKey: projectKey
},
function(error, result) {
console.log("2");
done();
}
);
})
.perform(function(done) {
restClientConnector
.jiraConnector(
environmentVariables.jiraHostUrl,
environmentVariables.jiraAdminUsername,
environmentVariables.jiraAdminPassword
)
.project.getProject(
{
projectIdOrKey: projectKey
},
function(error, result) {
console.log("3");
done();
}
);
});
}
如果您遇到done
回调超时问题,您应该将外部全局文件中的asynchhooktimeout
的持续时间增加到适当的时间。如果我使用。执行并完成回调我的应用服务器在执行任务后停止