如何在Karate UI中使用条件逻辑重复自定义函数

huangapple go评论70阅读模式
英文:

How to repeat customized function with conditional logic in Karate UI

问题

我试图编写一个测试用例来验证页面上的上下文。

页面上有几个<div>在迭代,单个页面的内容可以通过这个解决方案合并:链接

还有一个问题 - 内容被切分成了多个页面。除非按Enter键,否则没有按钮可用于转到下一页。

此外,唯一确定上下文是否结束的方法是根据以下部分:

&lt;div id=&quot;msgList&quot; class=&quot;test&quot;&gt;
   &lt;table&gt;
      &lt;tbody&gt;
         &lt;tr class=&quot;infoText&quot;&gt;
            &lt;td class=&quot;Type&quot;&gt;reminder&lt;/td&gt;
            &lt;td class=&quot;Text&quot;&gt;99  more data to view&lt;/td&gt;
            &lt;td class=&quot;messageNumber&quot;&gt;#12345&lt;/td&gt;
         &lt;/tr&gt;
      &lt;/tbody&gt;
   &lt;/table&gt;
   &lt;div id=&quot;message&quot; style=&quot;display:none;&quot;&gt;reminder  more data to view&lt;/div&gt;
&lt;/div&gt;

直到来到最后一页,它将变成以下内容:

&lt;div id=&quot;msgList&quot; class=&quot;test&quot;&gt;
   &lt;table&gt;
      &lt;tbody&gt;
         &lt;tr&gt;
            &lt;td class=&quot;Type&quot;&gt;23456&lt;/td&gt;
            &lt;td class=&quot;Text&quot;&gt;enter your input&lt;/td&gt;
         &lt;/tr&gt;
      &lt;/tbody&gt;
   &lt;/table&gt;
   &lt;div id=&quot;message&quot; style=&quot;display:none;&quot;&gt;23456 enter your input&lt;/div&gt;
&lt;/div&gt;

编辑:或('.messageNumber'仍然显示 - 但值不同)

&lt;div id=&quot;msgList&quot; class=&quot;test&quot;&gt;
   &lt;table&gt;
      &lt;tbody&gt;
         &lt;tr&gt;
            &lt;td class=&quot;Type&quot;&gt;reminder&lt;/td&gt;
            &lt;td class=&quot;Text&quot;&gt;66  no more data to view&lt;/td&gt;
            &lt;td class=&quot;messageNumber&quot;&gt;#98765&lt;/td&gt;
         &lt;/tr&gt;
      &lt;/tbody&gt;
   &lt;/table&gt;
   &lt;div id=&quot;message&quot; style=&quot;display:none;&quot;&gt;random text&lt;/div&gt;
&lt;/div&gt;

只是想知道是否有办法使用条件逻辑循环浏览所有页面并将内容合并在一起以进行断言?

每个页面的格式完全相同。

编辑:最终通过js函数使其工作

* def allData = []
* def loopContent =
    &quot;&quot;&quot;
    function() {
    while (allData) {
        let list = locateAll(&#39;form div&#39;, x =&gt; { let id = x.attribute(&#39;id&#39;); return id ? id.startsWith(&#39;line1_R&#39;) : false });
        let data = list.map(x =&gt; x.text.trim());
        let msgText = script(&#39;.Text&#39;,&#39;_.textContent&#39;).trim();

        if (msgText != &quot;reminder  more data to view&quot;){
            allData.push(data);
            return allData;
        }

        else if(msgText == &quot;reminder  more data to view){
            allData.push(data);
            input(&#39;body&#39;, Key.ENTER);
        }

        else{
            karate.fail(&quot;Content not being looped properly&quot;);
        }
    }
    }

    &quot;&quot;&quot;

* def letterContent = loopContent()
* print letterContent
英文:

I was trying to write a test case to verify context on the page.

There are several <div> iterating, the content for a single page could be merged by the solution from: https://stackoverflow.com/questions/76300824/how-to-use-scriptall-to-find-all-the-div-with-id-or-style-attributes-in-karate

There is one more issue with this scenario - the content has been cut off into multiple pages. There is no button available to go to next page unless pressing Enter key.

Also the only way to identify if it has come to the end of the context is from following section:

&lt;div id=&quot;msgList&quot; class=&quot;test&quot;&gt;
   &lt;table&gt;
      &lt;tbody&gt;
         &lt;tr class=&quot;infoText&quot;&gt;
            &lt;td class=&quot;Type&quot;&gt;reminder&lt;/td&gt;
            &lt;td class=&quot;Text&quot;&gt;99  more data to view&lt;/td&gt;
            &lt;td class=&quot;messageNumber&quot;&gt;#12345&lt;/td&gt;
         &lt;/tr&gt;
      &lt;/tbody&gt;
   &lt;/table&gt;
   &lt;div id=&quot;message&quot; style=&quot;display:none;&quot;&gt;reminder  more data to view&lt;/div&gt;
&lt;/div&gt;

It will change as below until coming to the last page:

&lt;div id=&quot;msgList&quot; class=&quot;test&quot;&gt;
   &lt;table&gt;
      &lt;tbody&gt;
         &lt;tr&gt;
            &lt;td class=&quot;Type&quot;&gt;23456&lt;/td&gt;
            &lt;td class=&quot;Text&quot;&gt;enter your input&lt;/td&gt;
         &lt;/tr&gt;
      &lt;/tbody&gt;
   &lt;/table&gt;
   &lt;div id=&quot;message&quot; style=&quot;display:none;&quot;&gt;23456 enter your input&lt;/div&gt;
&lt;/div&gt;

EDITED: or ('.messageNumber' still displaying - but with different value)

&lt;div id=&quot;msgList&quot; class=&quot;test&quot;&gt;
   &lt;table&gt;
      &lt;tbody&gt;
         &lt;tr&gt;
            &lt;td class=&quot;Type&quot;&gt;reminder&lt;/td&gt;
            &lt;td class=&quot;Text&quot;&gt;66  no more data to view&lt;/td&gt;
            &lt;td class=&quot;messageNumber&quot;&gt;#98765&lt;/td&gt;
         &lt;/tr&gt;
      &lt;/tbody&gt;
   &lt;/table&gt;
   &lt;div id=&quot;message&quot; style=&quot;display:none;&quot;&gt;random text&lt;/div&gt;
&lt;/div&gt;

Just wondering is there a way to use conditional logic to loop through all pages and merge the content(with solution extracting context from each single page) together for assertion?

The format for each page is exactly the same.

EDITED: Eventually made it work with a js function

    * def allData = []
    * def loopContent =
    &quot;&quot;&quot;
    function() {
    while (allData) {
        let list = locateAll(&#39;form div&#39;, x =&gt; { let id = x.attribute(&#39;id&#39;); return id ? id.startsWith(&#39;line1_R&#39;) : false });
        let data = list.map(x =&gt; x.text.trim());
        let msgText = script(&#39;.Text&#39;,&#39;_.textContent&#39;).trim();

    if (msgText != &quot;reminder  more data to view&quot;){
        allData.push(data);
        return allData;
        }

        else if(msgText == &quot;reminder  more data to view){
        allData.push(data);
        input(&#39;body&#39;, Key.ENTER);
        }

        else{
        karate.fail(&quot;Content not being looped properly&quot;);
        }
    }
    }

    &quot;&quot;&quot;

    * def letterContent = loopContent()
    * print letterContent

Many thanks

答案1

得分: 1

以下是翻译好的部分:

  • 查看此处的waitUntil()与函数一起使用的描述
  • 或许这个其他回答会给你一些提示:链接在这里
  • 根据更新后的问题,以下是如何从循环中“收集”所有数据的方式:
    * def allData = []
    * def loopContent =
    &quot;&quot;&quot;
    function() {
        if (!exists(&#39;.messageNumber&#39;)) {
            let list = locateAll(&#39;form div&#39;, x =&gt; { let id = x.attribute(&#39;id&#39;); return id ? id.startsWith(&#39;line1_R&#39;) : false });
            let data = list.map(x =&gt; x.text.trim());
            allData.push(data);
            return true;
        }
        input(&#39;body&#39;, Key.ENTER);
    }
    &quot;&quot;&quot;
    * waitUntil(loopContent)
    * print allData
    
  • 这可能会重复所有前几页的数据,但我认为你可以改进例程,只有在加载新行时才使用push()。请注意,有一个帮助函数可以对数组进行去重:
    * def cleaned = karate.distinct(allData)
    
英文:

Yes, it may need some experimenting, but please read the description of waitUntil() with function.

Maybe this other answer will give you some hints: https://stackoverflow.com/a/76268570/143475

EDIT: based on the updated question, here is how you could "collect" all the data from the loops:

* def allData = []
* def loopContent =
&quot;&quot;&quot;
function() {
    if (!exists(&#39;.messageNumber&#39;)) {
        let list = locateAll(&#39;form div&#39;, x =&gt; { let id = x.attribute(&#39;id&#39;); return id ? id.startsWith(&#39;line1_R&#39;) : false });
        let data = list.map(x =&gt; x.text.trim());
        allData.push(data);
        return true;
    }
    input(&#39;body&#39;, Key.ENTER);
}
&quot;&quot;&quot;
* waitUntil(loopContent)
* print allData

This is possibly going to duplicate all the first few pages, but I think you can improve the routine to only push() the newly loaded rows. That may be tricky.

Do note that there is a helper function to de-duplicate an array:

* def cleaned = karate.distinct(allData)

huangapple
  • 本文由 发表于 2023年5月22日 05:18:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76301962.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定