【发布时间】:2018-07-27 03:51:58
【问题描述】:
我正在使用基于Nightwatch.js 的Nightwatch-Cucumber 进行我们的测试。 Ans 我也使用PageObject Pattern。这些测试不仅是基于Selenium 的端到端测试,也是REST API 测试。对于 REST 测试,我使用的是 Atlassian JIRA REST client for Node。现在我想将 Nightwatch(包括 Selenium)的强大功能与 REST 的强大功能结合起来。因此,我想同时使用这两种技术,并且想将 REST API 调用集成到 Nightwatch 框架中。
我尝试将 REST API 调用集成到 Nightwatch 的 perform() 函数中,以将 REST 调用添加到 Nightwatch command queue,但没有完全成功。在执行下一个 Nightwatch 命令之前,我必须确保 REST 调用完全完成。目前,REST 调用之后的以下步骤将在 REST 调用完全完成之前执行。但是我该如何解决这个问题呢?
这是我的黄瓜功能文件:
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
这些是我的Step Definitions:
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);
}
);
});
这些是我的Page Object 函数:
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 step When the user logs out 的第二次调用在 Cucumber step When the user deletes a still existing project with key "ABC-123" via REST API 完全完成之前开始执行。
如果我在 Page Object 中取消注释行 .waitForTime(4000)(它是一个自定义命令),那么我会得到正确的输出,但我不想以这种静态方式等待。很脏:
Starting selenium server... started - PID: 10554
.1
.2
3
.1
..
1 scenario (1 passed)
3 steps (3 passed)
0m07.783s
我怎样才能解决我的问题,以便在下一步之后准确地执行一个步骤,或者我怎样才能将 REST 调用集成到 Nightwatch 命令队列中。我也尝试让我的功能async 并使用await 执行所有命令,但也没有成功。
【问题讨论】:
标签: node.js rest selenium nightwatch.js