简体   繁体   English

声明元素列表遵循JavaScript诺言规则

[英]Asserting list of elements follow a rule with JavaScript promises

I have a set of titles that all look like: 我有一组看起来都像这样的标题:

<div class='items'>
  * Some | Text *
</div>

or 要么

<div class='items'>
  * Some more | Text *
</div>

Given that there are numerous blocks that follow those two patterns on the page, I would like to loop through them all and ensure that they all contain the pattern '* Some [more] | 考虑到页面上遵循这两种模式的块很多,我想遍历它们,并确保它们都包含模式'* Some [more] | Text *' where [more] is optional and * is anything. 文本*',其中[更多]是可选的,*是任何东西。

I currently have: 我目前有:

yield browser

...Some code...

.elements('.items')
 .then((elementList)=>{
  for(var i = 0; i < elementList.value.length; i++){

   this.elementIdText(elementList.value[i].ELEMENT)
    .then((currElement)=>{
      var str = currElement.value;
      if(str.indexOf("Some | Text") == -1 || str.indexOf("Some more | Text") == -1){
       assert.ok(false, 'invalid string');
      }
    })

  }
 })

However it seems that the asserts get ignored since they're inside a for loop (which i guess messes everything up since it's not asynch??) 但是似乎断言被忽略了,因为它们位于for循环内(我猜这使所有东西都弄乱了,因为它不是异步的??)

Although I want the program to throw a fail as soon as one of the strings do not match the required pattern so I assumed a loop was necessary. 尽管我希望程序在字符串之一与所需的模式不匹配时立即引发失败,所以我认为有必要进行循环。

Is there a good way to do this? 有什么好方法吗?

The current behavior if I placed a console.log inside the for loop: 如果将console.log放在for循环中,则当前行为:

Command: find element
Command: find element
... n times
log: element 1 text
log: element 2 text
... n times

Where it should be: 应该在哪里:

Command
log
Command
log
... n times 

Found a solution without using q promise library! 找到了不使用q promise库的解决方案!

(Alternative would have been to use the .all([]).then() function that q provides) (替代方法是使用q提供的.all([])。then()函数)

yield browser

...Some code...

.elements('.items')
 .then((elementList)=>{

  var chain = this; // start a promise chain

  for(var i = 0; i < elementList.value.length; i++){

   // add this whole promise to the chain every iteration
   chain = chain.elementIdText(elementList.value[i].ELEMENT)
            .then((currElement)=>{
              var str = currElement.value;
              if(str.indexOf("Some | Text") == -1 || str.indexOf("Some more | Text") == -1){
               assert.ok(false, 'invalid string');
              }
            })

  }

  // Simply return the chain of promises created
  return chain;
 })

The idea was that the .then is just a promise that what's inside it will eventually be evaluated. 这个想法是.then只是一个承诺,即其中的内容最终将被评估。 This meant that the stuff inside may not be evaluated until after the test has completed. 这意味着直到测试完成后才能评估内部的东西。

Although one solution would have been to add pauses (maybe?), by creating a chain of promises, the next promise doesn't get evaluated until the previous one, and as a result, the test ends after all code is run. 尽管一个解决方案是添加暂停(也许?),但是通过创建一个Promise链,直到下一个Promise都不会评估下一个Promise,因此,测试在所有代码运行之后结束。

below way may helps you.. 下面的方法可能会帮助您。

    //assume that i am loding list of elements by driver
    List<String> data=new ArrayList<>();

    data.add("murali");

    data.add("murali [selenium]");

    //for loop to iterate list
    for(int i=0; i<data.size(); i++){

        //using asserting to check data brought by list with actual
        //you can change here as per requirement
        //as know any one in list fails then it throws fail with out checking remaining elements in list
        assert data.get(i)=="murali" || data.get(i)=="murali [selenium]";
    }

i think you may need to adjust assert command above as i just used equals there but not same your case. 我认为您可能需要在上面调整assert命令,因为我刚才使用的等于,但您的情况不相同。

Thank You, Murali 谢谢,穆拉利

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM