英文:
How to repeat customized function with conditional logic in Karate UI
问题
我试图编写一个测试用例来验证页面上的上下文。
页面上有几个<div>在迭代,单个页面的内容可以通过这个解决方案合并:链接
还有一个问题 - 内容被切分成了多个页面。除非按Enter键,否则没有按钮可用于转到下一页。
此外,唯一确定上下文是否结束的方法是根据以下部分:
<div id="msgList" class="test">
<table>
<tbody>
<tr class="infoText">
<td class="Type">reminder</td>
<td class="Text">99 more data to view</td>
<td class="messageNumber">#12345</td>
</tr>
</tbody>
</table>
<div id="message" style="display:none;">reminder more data to view</div>
</div>
直到来到最后一页,它将变成以下内容:
<div id="msgList" class="test">
<table>
<tbody>
<tr>
<td class="Type">23456</td>
<td class="Text">enter your input</td>
</tr>
</tbody>
</table>
<div id="message" style="display:none;">23456 enter your input</div>
</div>
编辑:或('.messageNumber'仍然显示 - 但值不同)
<div id="msgList" class="test">
<table>
<tbody>
<tr>
<td class="Type">reminder</td>
<td class="Text">66 no more data to view</td>
<td class="messageNumber">#98765</td>
</tr>
</tbody>
</table>
<div id="message" style="display:none;">random text</div>
</div>
只是想知道是否有办法使用条件逻辑循环浏览所有页面并将内容合并在一起以进行断言?
每个页面的格式完全相同。
编辑:最终通过js函数使其工作
* def allData = []
* def loopContent =
"""
function() {
while (allData) {
let list = locateAll('form div', x => { let id = x.attribute('id'); return id ? id.startsWith('line1_R') : false });
let data = list.map(x => x.text.trim());
let msgText = script('.Text','_.textContent').trim();
if (msgText != "reminder more data to view"){
allData.push(data);
return allData;
}
else if(msgText == "reminder more data to view){
allData.push(data);
input('body', Key.ENTER);
}
else{
karate.fail("Content not being looped properly");
}
}
}
"""
* 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:
<div id="msgList" class="test">
<table>
<tbody>
<tr class="infoText">
<td class="Type">reminder</td>
<td class="Text">99 more data to view</td>
<td class="messageNumber">#12345</td>
</tr>
</tbody>
</table>
<div id="message" style="display:none;">reminder more data to view</div>
</div>
It will change as below until coming to the last page:
<div id="msgList" class="test">
<table>
<tbody>
<tr>
<td class="Type">23456</td>
<td class="Text">enter your input</td>
</tr>
</tbody>
</table>
<div id="message" style="display:none;">23456 enter your input</div>
</div>
EDITED: or ('.messageNumber' still displaying - but with different value)
<div id="msgList" class="test">
<table>
<tbody>
<tr>
<td class="Type">reminder</td>
<td class="Text">66 no more data to view</td>
<td class="messageNumber">#98765</td>
</tr>
</tbody>
</table>
<div id="message" style="display:none;">random text</div>
</div>
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 =
"""
function() {
while (allData) {
let list = locateAll('form div', x => { let id = x.attribute('id'); return id ? id.startsWith('line1_R') : false });
let data = list.map(x => x.text.trim());
let msgText = script('.Text','_.textContent').trim();
if (msgText != "reminder more data to view"){
allData.push(data);
return allData;
}
else if(msgText == "reminder more data to view){
allData.push(data);
input('body', Key.ENTER);
}
else{
karate.fail("Content not being looped properly");
}
}
}
"""
* def letterContent = loopContent()
* print letterContent
Many thanks
答案1
得分: 1
以下是翻译好的部分:
- 查看此处的
waitUntil()
与函数一起使用的描述。 - 或许这个其他回答会给你一些提示:链接在这里。
- 根据更新后的问题,以下是如何从循环中“收集”所有数据的方式:
* def allData = [] * def loopContent = """ function() { if (!exists('.messageNumber')) { let list = locateAll('form div', x => { let id = x.attribute('id'); return id ? id.startsWith('line1_R') : false }); let data = list.map(x => x.text.trim()); allData.push(data); return true; } input('body', Key.ENTER); } """ * 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 =
"""
function() {
if (!exists('.messageNumber')) {
let list = locateAll('form div', x => { let id = x.attribute('id'); return id ? id.startsWith('line1_R') : false });
let data = list.map(x => x.text.trim());
allData.push(data);
return true;
}
input('body', Key.ENTER);
}
"""
* 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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论