在Nightwatch命令队列中使用REST客户端

时间:2018-02-16 09:10:13

标签: node.js rest selenium nightwatch.js

我使用基于Nightwatch-Cucumber的{​​{1}}进行测试。我也使用Nightwatch.js。这些测试不仅基于PageObject Pattern而且还基于Selenium测试的end2end测试。对于REST测试,我使用Atlassian JIRA REST client for Node。现在我想将Nightwatch的功能(包括Selenium)与REST的强大功能结合起来。所以,我希望将两种技术结合使用,并希望在Nightwatch框架中集成REST API调用。

我尝试在Nightwatch的REST API功能中集成REST API调用,将REST调用添加到Nightwatch command queue,但没有取得圆满成功。我必须确保在执行下一个Nightwatch命令之前完成REST调用。目前,在REST调用完成之前,将执行REST调用之后的以下步骤。但是我该如何解决这个问题?

这是我的Cucumber Feature文件:

perform()

这些是我的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

所以,我希望3个Cucumber步骤同步运行,一个接一个。我添加了一些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); } } ] }; 输出来检查这一点。在我的测试运行期间,我期待控制台输出的顺序:

console.log()

相反,我得到以下输出:

1
2
3
1

因此,Cucumber步骤Starting selenium server... started - PID: 10436 .1 ..1 .. 1 scenario (1 passed) 3 steps (3 passed) 0m03.782s 3 2 的第二次调用在黄瓜步骤When the user logs out完全结束之前开始执行。

如果我在When the user deletes a still existing project with key "ABC-123" via REST API中取消注释行.waitForTime(4000)(这是一个自定义命令),那么我会得到正确的输出,但我不想以这种静态方式等待。它很脏:

Page Object

如何解决我的问题,在下一步之后执行一步,或者如何在Nightwatch命令队列中集成REST调用。我也尝试使用Starting selenium server... started - PID: 10554 .1 .2 3 .1 .. 1 scenario (1 passed) 3 steps (3 passed) 0m07.783s 制作功能并使用async执行所有命令,但也没有成功。

1 个答案:

答案 0 :(得分:2)

如果您需要同步运行异步任务,则必须在>>> elems = np.arange(2*3*4).reshape(2,3,4) >>> ind = np.arange(0,8,2).reshape(2, 2) % 3 >>> >>> elems array([[[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]], [[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]]]) >>> elems[np.arange(2)[:, None], ind] array([[[ 0, 1, 2, 3], [ 8, 9, 10, 11]], [[16, 17, 18, 19], [12, 13, 14, 15]]]) 函数中使用done回调。

perform

您希望在异步任务完成后调用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的持续时间增加到适当的值。