在循环中使用条件

时间:2017-08-17 19:37:30

标签: javascript loops if-statement asynchronous nightwatch.js

我是javascript和nightwatch js的新手。

我正在尝试自动化在线测试,该测试最多可以随机生成4到5个问题。每个问题都会出现一个新的页面加载,所以基本上你回答一个问题,提交它然后呈现下一个问题。

答案可以是不同的类型,其中一些是YES / NO按钮,其中一些是您点击的多项选择,其中一些是在文本字段中键入数据,然后您必须按Enter键提交键盘上的键。幸运的是,用于这些类型的答案中的每一个的定位器总是相同的(不是动态生成的)。例如。是/否问题,是的回答将始终是.answerYes或如果它是文本字段将永远是#answerBox。

我需要经历4或5个问题,如果我正确回答它们并不重要。

我正在考虑使用for循环,我检查是否得到了我已经识别的其中一个元素,如果是,我会采取行动并继续下一页,直到调查问卷完成。

我想到的是类似下面的例子。我认为这不会起作用,因为javascript是异步的 - 我还不完全理解。

for (i = 0; i < 10; i++) { 

    if (client.waitForElementVisible('.element1',1000)) {

        client.click('.element1');  //This would select first answer 
        client.click('nextButton'); //This would continue to the next question

    }
    else if (client.waitForElementVisible('.element2',1000)) {

        client.click('.element2');  //This would select first answer 
        client.click('nextButton'); //This would continue to the next question

    }
    else if (client.waitForElementVisible('.element3',1000)) {

        client.click('.element3');  //This would select first answer 
        client.click('nextButton'); //This would continue to the next question

    }
    else if (client.waitForElementVisible('.element4',1000)) {

        client.click('.element4');  //This would select first answer 
        client.click('nextButton'); //This would continue to the next question
    }
    else if (client.waitForElementVisible('.element5',1000)) { //element 5 would be the confirmation that test is over

        client.click('.element5'); //this would click the OK button when notified test is done
        leaveTheLoop(); //Not sure what would be the command to leave the loop  
    }
}

我也在查看关于javascript的Switch语句,但我不认为我可以在表达式中写一些可以验证我期望的任何元素是否会出现在一个表达式下的内容。

2 个答案:

答案 0 :(得分:3)

你可以继续链接 .waitForElementVisible(selector,timeout,abortOnFailure,callback)检查元素是否可见。

通过将abortOnFailure设置为false,整个测试不会失败,您可以继续使用element2,3,4,5,即使element1不可见。

以下是代码:

test(client){
     client.waitForElementVisible('.element1',1000,false,function(){
           client.click('.element1');  //This would select the first answer 
                 .click('nextButton'); 
            })
           .waitForElementVisible('.element2',1000,false,function(){
           client.click('.element2');  //This would select the second answer 
                 .click('nextButton');
           })                                     
           .waitForElementVisible('.element3',1000,false,function(){
            client.click('.element3');  //This would select the third answer 
                  .click('nextButton');
           })                                
           .waitForElementVisible('.element4',1000,false,function(){
           client.click('.element4');  //This would select the forth answer 
                 .click('nextButton');
           })                                       
          .waitForElementVisible('.element5',1000,false,function(){
           client.click('.element5');  //This would select the fifth answer 
           }) 
}

详细说明: http://nightwatchjs.org/api/#waitForElementVisible

<强>用法:

do{
    test(client);
    client.click('submit_or_whatevertothenextpage');
}while (client.except.to.not.be.visible(".result_element_non_existing"));

答案 1 :(得分:0)

我用下面的代码解决了我的问题。

for (var i = 0; i < 15; i++) {

  //If answer type is yes/no, do this
  preTestPage.api.element('css selector', preTestPage.elements.yesAnswer.selector, function (result) {

    if (result.value && result.value.ELEMENT) {

        preTestPage.expect.element('@yesAnswer').to.be.visible.after(5000);
        preTestPage.click('@yesAnswer');
        preTestPage.api.pause(2000);
    } 
  });

  //If answer type is text input, do this
  preTestPage.api.element('css selector', preTestPage.elements.textInputAnswer.selector, function (result) {

    if (result.value && result.value.ELEMENT) {

        preTestPage.setValue('@textInputAnswer',10);
        preTestPage.api.keys(client.Keys.ENTER);
        preTestPage.api.pause(2000);

    } 
  });

  //If answer type is multiple choise with submit button, do this
  preTestPage.api.element('css selector', preTestPage.elements.submitAnswerButton.selector, function (result) {

    if (result.value && result.value.ELEMENT) {

        preTestPage.expect.element('@firstSubmitAnswer').to.be.visible.after(5000);
        preTestPage.click('@firstSubmitAnswer');
        client.pause(1500);
        preTestPage.expect.element('@submitAnswerButton').to.be.enabled.after(5000);

        preTestPage.click('@submitAnswerButton');
        preTestPage.api.pause(2000);

    } 
  });

  //If answer type is multiple answer, do this
  preTestPage.api.element('css selector', preTestPage.elements.firstMultipleAnswer.selector, function (result) {

    if (result.value && result.value.ELEMENT) {

        preTestPage.click('@firstMultipleAnswer');
        preTestPage.api.pause(2000);

    } 
  });

  //If none of the above then is the end of the test so do this
  preTestPage.api.element('css selector', preTestPage.elements.wellDoneButton.selector, function (result) {

    if (result.value && result.value.ELEMENT) {
        // Element is visible, do the appropriate tests

        preTestPage.expect.element('@wellDoneButton').to.be.visible.after(5000);
        preTestPage.click('@wellDoneButton');
        preTestPage.api.pause(1000);
        i = false;
        console.log("i = " + i + "contador = " + contador);
        return;

    } 
  });

  }


}   },

现在的问题是,for循环并不是很理想,因为我不确定总共有多少问题,看起来nightwatch js(或node.js我不确定)继续在我的循环中运行所有迭代如果我在最后一个屏幕出现时休息一下。所以我的测试挂起并保持在那里,直到for循环在后台完成检查所有答案,直到我设置的迭代次数。

有人建议使用while(true),但是现在问题是由于某种原因,node.js挂起并且它似乎在这里做了一个无限循环所以它完全崩溃了。我怀疑我的休息;命令在这里没有做任何事情。我在这做错了什么?

var i = true;
var count = 0;
while (i) {

  //If answer type is yes/no, do this
  preTestPage.api.element('css selector', preTestPage.elements.yesAnswer.selector, function (result) {

    if (result.value && result.value.ELEMENT) {

        preTestPage.expect.element('@yesAnswer').to.be.visible.after(5000);
        preTestPage.click('@yesAnswer');
        preTestPage.api.pause(2000);
    } 
  });

  //If answer type is text input, do this
  preTestPage.api.element('css selector', preTestPage.elements.textInputAnswer.selector, function (result) {

    if (result.value && result.value.ELEMENT) {

        preTestPage.setValue('@textInputAnswer',10);
        preTestPage.api.keys(client.Keys.ENTER);
        preTestPage.api.pause(2000);

    } 
  });

  //If answer type is multiple choise with submit button, do this
  preTestPage.api.element('css selector', preTestPage.elements.submitAnswerButton.selector, function (result) {

    if (result.value && result.value.ELEMENT) {

        preTestPage.expect.element('@firstSubmitAnswer').to.be.visible.after(5000);
        preTestPage.click('@firstSubmitAnswer');
        client.pause(1500);
        preTestPage.expect.element('@submitAnswerButton').to.be.enabled.after(5000);

        preTestPage.click('@submitAnswerButton');
        preTestPage.api.pause(2000);

    } 
  });

  //If answer type is multiple answer, do this
  preTestPage.api.element('css selector', preTestPage.elements.firstMultipleAnswer.selector, function (result) {

    if (result.value && result.value.ELEMENT) {

        preTestPage.click('@firstMultipleAnswer');
        preTestPage.api.pause(2000);

    } 
  });

  //If none of the above then is the end of the test so do this
  preTestPage.api.element('css selector', preTestPage.elements.wellDoneButton.selector, function (result) {

    if (result.value && result.value.ELEMENT) {
        // Element is visible, do the appropriate tests

        preTestPage.expect.element('@wellDoneButton').to.be.visible.after(5000);
        preTestPage.click('@wellDoneButton');
        preTestPage.api.pause(1000);
        i = false;
        console.log("i = " + i + "count = " + count);
        return;

    } 
  });
  count++;

  }