我正在使用For循环遍历一个表。当某行满足某个条件时,我将从for循环中断。但是根据我的代码,FOR似乎异步运行,导致并行迭代,而我不希望我的程序这样做。有人可以解决我认为是由于承诺解决而导致的这个问题。
element.all(by.css('tbody tr')).then(function(rows){
for(var i = 1; i < (rows.length); i++) {
var count=0;
var pass=0;
//TEST VALUES BELOW
var appNameCreated="Test App 534";
//TEST VALUES ABOVE
console.log(i);
element(by.xpath('/html/body/gft-root/section/div[2]/app-onboard-list/div[4]/div[3]/table/tbody/tr['+ i +']')).element(by.css('td:nth-child(1)')).getText().then(function(appname){
console.log(i,appname);
if(appname==appNameCreated){
console.log(appname,appNameCreated,i);
element(by.xpath('/html/body/gft-root/section/div[2]/app-onboard-list/div[4]/div[3]/table/tbody/tr['+ i +']')).element(by.css('td:nth-child(6)')).getText().then(function(result){
console.log(result,i);
// if (result==data.resultSubmit){
if (result=="Activated"){
pass += 1;
element(by.xpath('/html/body/gft-root/section/div[2]/app-onboard-list/div[4]/div[3]/table/tbody/tr['+ i +']')).element(by.css('td:nth-child(1)')).element(by.css('a')).click().then(function(){
browser.sleep(4000);
});
element(by.id("btnTab3")).element(by.xpath('span')).click();
browser.wait(EC.visibilityOf(element(by.xpath('/html/body/gft-root/section/div[2]/app-onboard-list/div[4]/div[3]/table/tbody/tr[1]/td[1]'))),15000); browser.wait(EC.visibilityOf(element(by.id("button-basic"))),15000);
browser.wait(EC.visibilityOf(element(by.id("button-basic"))),15000);
element(by.id("button-basic")).click();
element.all(by.css('ul[class="dropdown-menu"]')).each(function(item1){
item1.element(by.css('li:nth-child(7)')).element(by.css('a')).click();
})
element(by.xpath('/html/body/gft-root/section/div[2]/app-onboard-list/div[4]/div[3]/table/tbody/tr['+i+']')).element(by.css('td:nth-child(6)')).getText().then(function(resultFin){
console.log(resultFin);
browser.actions().mouseMove(element(by.xpath('/html/body/gft-root/section/div[2]/app-onboard-list/div[4]/div[3]/table/tbody/tr['+ i +']')).element(by.css('td:nth-child(1)'))).perform();
expect(resultFin).toBe(data.resultFinal);
})
}
})
}
})
if(pass==1){
break;
}
}
})
答案 0 :(得分:0)
更新您的代码:
-使用async ... await
更改承诺解决
-将var
更改为let, const
-循环计数应从0
开始而不是1
-将==
更改为===
-.each()
无法与click()
一起用于异步操作
问题:
-从哪里获取data.resultSubmit?
const rows = await element.all(by.css('tbody tr'));
const appNameCreated = "Test App 534";
let count = 0;
let pass = 0;
for (let i = 0; i < rows.length; i++) {
console.log('Index is ', i);
const appname = await element(by.xpath('/html/body/gft-root/section/div[2]/app-onboard-list/div[4]/div[3]/table/tbody/tr['+ i +']')).element(by.css('td:nth-child(1)')).getText();
console.log('App name for current loop is ', appname);
if(appname === appNameCreated) {
console.log('App name is the same as created app name for loop with index ', i);
const result = await element(by.xpath('/html/body/gft-root/section/div[2]/app-onboard-list/div[4]/div[3]/table/tbody/tr['+ i +']')).element(by.css('td:nth-child(6)')).getText();
console.log('Result for current run is ' result);
if (result === data.resultSubmit && result === "Activated") {
pass += 1;
await element(by.xpath('/html/body/gft-root/section/div[2]/app-onboard-list/div[4]/div[3]/table/tbody/tr['+ i +']')).element(by.css('td:nth-child(1)')).element(by.css('a')).click();
await browser.sleep(4000);
});
await element(by.id("btnTab3")).element(by.xpath('span')).click();
await browser.wait(EC.visibilityOf(element(by.xpath('/html/body/gft-root/section/div[2]/app-onboard-list/div[4]/div[3]/table/tbody/tr[1]/td[1]'))),15000);
await browser.wait(EC.visibilityOf(element(by.id("button-basic"))),15000);
await browser.wait(EC.visibilityOf(element(by.id("button-basic"))),15000);
await element(by.id("button-basic")).click();
const menus = await element.all(by.css('ul[class="dropdown-menu"]'));
const amount = await menus.count();
for (let i = 0; i < amount; i++) {
const item = await menus.get(i);
await item.element(by.css('li:nth-child(7)')).element(by.css('a')).click();
}
const resultFin = await element(by.xpath('/html/body/gft-root/section/div[2]/app-onboard-list/div[4]/div[3]/table/tbody/tr['+i+']')).element(by.css('td:nth-child(6)')).getText();
console.log('resultFin is ', resultFin);
await browser.actions().mouseMove(element(by.xpath('/html/body/gft-root/section/div[2]/app-onboard-list/div[4]/div[3]/table/tbody/tr['+ i +']')).element(by.css('td:nth-child(1)'))).perform();
expect(resultFin).toBe(data.resultFinal);
})
}
})
}
})
if(pass ===1 ){
break;
}
}
})
我尽力消除了所有错误。您的代码很糟糕,因为您没有大笔的“那里到底是什么?”较小的功能。
答案 1 :(得分:0)
您可以使用类似数组的对象的<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Float test</title>
<link rel="stylesheet" href="./css/styles.css">
</head>
<body>
<div class="left-container">
<div class="left-container-heading">Lorem Ipsum</div>
<br>
<br>
<p>Float left doesn't work anymore since it is more than one line and I would like to know why and how to fix this.
</p>
</div>
</body>
</html>
来迭代行,并在满足条件的行中使用reduce()
来中断循环。
由于您拒绝了诺言,因此应使用Promise.reject()
来捕获诺言,然后单击匹配的行以进入下一页并进行操作,然后返回Promise.catch()
内的表格页
catch()