简体   繁体   English

赛普拉斯获取特定路径内的元素

[英]Cypress get elements within specific path

Automating some tests with Cypress, I've found this issue.使用赛普拉斯自动化一些测试,我发现了这个问题。

We are asking many questions to the customers and these question will appear one-by-one depending on the previous answers.我们向客户提出了许多问题,这些问题将根据之前的答案一一出现。 The next question (and for instance the kind of answer) will be unknown until it is shown.下一个问题(例如答案的类型)在显示之前将是未知的。 I leave this here as an example in which every 'div' (not 'rootDiv') is a question and they are only shown when the previous one has been responded.我把这个留在这里作为一个例子,其中每个“div”(不是“rootDiv”)都是一个问题,并且它们仅在前一个得到响应时显示。

    <div class="rootDiv">
        <div class="type1">
           <div class="type1b">
              <button class="button1">
              <button class="button1">
           </div>
        </div>
        <div class="type2">
           <div class="type2b">
              <button class="button2">
              <button class="button2">
              <button class="button2">
              <button class="button2">
           </div>
        </div>
        <div class="type1">
           <div class="type1b">
              <button class="button1">
              <button class="button1">
           </div>
        </div>
        .
        .
        .
       <div class="typeX">
           <div class="typeXb">
              <button class="buttonX">
              <button class="buttonX">
              <button class="buttonX">
              <button class="buttonX">
           </div>
       </div>
    </div>

So the issues I am facing are:所以我面临的问题是:

  1. Is there a way to access to the last 'div' within 'rootDiv', check its class and based on it click any of the buttons?有没有办法访问“rootDiv”中的最后一个“div”,检查它的 class 并基于它点击任何按钮?
  2. Is there a way to get the 'text' written on the buttons of the last 'div'?有没有办法让最后一个“div”的按钮上写上“文本”?
  3. Is there a way to have a list of elements (buttons in this case) and iterate through them?有没有办法拥有一个元素列表(在这种情况下是按钮)并遍历它们?

There are many options to solve it, the main problem relies on how to get to those elements有很多选项可以解决它,主要问题取决于如何获得这些元素

  1. Is there a way to access to the last 'div' within 'rootDiv', check its class and based on it click any of the buttons?有没有办法访问“rootDiv”中的最后一个“div”,检查它的 class 并基于它点击任何按钮?

It is possible to access the last 'div' within 'rootDiv': cy.get('.rootDiv div').last() .可以访问 'rootDiv' 中的最后一个 'div': cy.get('.rootDiv div').last() However, checking the class and then performing actions based on the value of the class is a bad idea, as it is conditional testing and Cypress was not really made for it.但是,检查 class 然后根据 class 的值执行操作是一个坏主意,因为它是条件测试,而赛普拉斯并不是真的为此而生。 Nevertheless, if you really need this attitude (no other, better ways), I think it is doable.不过,如果你真的需要这种态度(没有其他更好的方法),我认为这是可行的。

  1. Is there a way to get the 'text' written on the buttons of the last 'div'?有没有办法让最后一个“div”的按钮上写上“文本”?

Yes, it is possible to get 'text' of last div.是的,可以获得最后一个 div 的“文本”。 cy.get('.rootDiv div').last().invoke('text').then((textOnTheButton) => {console.log(textOnTheButton})

3.Is there a way to have a list of elements (buttons in this case) and iterate through them? 3.有没有办法拥有一个元素列表(在这种情况下是按钮)并遍历它们?

To get all of the buttons all you need is cy.get(button) .要获得所有按钮,您只需要cy.get(button) If you want to iterate through all of them using index, you may use for loop and eq() , however it may not be the best/most efficient solution for all cases.如果您想使用索引遍历所有这些,您可以使用 for 循环和eq() ,但它可能不是所有情况下最好/最有效的解决方案。

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

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