量角器-在For Each中行为不正常

时间:2019-08-28 15:28:42

标签: javascript asynchronous foreach protractor click

遵循量角器代码

Writer w = new OutputStreamWriter(process.getOutputStream());
        w.write("custom_command");
        w.close();

仅在最后一个选项卡中单击radio1,因为saveAndContinue的最后一行被隐藏,因此其click()失败

虽然“睡眠”确实在FOR循环之外起作用,但是当我想让radio1单击之前没有时间

编辑1 :问题是每行都在执行,但是单选1单击的是最后一个Tab,而不是先前单击的Tab。使用Tab键单击可以使radio1更加快速。

4 个答案:

答案 0 :(得分:1)

有两点需要注意,“ id”属性必须唯一。您不应有多个具有相同ID的元素,否则可能会导致一些古怪的行为。 将此答案作为来源Does ID have to be unique in the whole page?

此外,element.all()将返回元素数组,因此您尝试单击元素数组。

请参阅量角器文档上element.all()的文档 https://www.protractortest.org/#/api?view=ElementArrayFinder

假设元素以数组形式返回,尽管使用重复的html id,您仍需要像这样单独单击它们

const dns = require('dns');
const dnsPromises = dns.promises;
const options = {
  family: 6,
  hints: dns.ADDRCONFIG | dns.V4MAPPED,
};

dnsPromises.lookup('example.com', options).then((result) => {
  console.log('address: %j family: IPv%s', result.address, result.family);
  // address: "2606:2800:220:1:248:1893:25c8:1946" family: IPv6
});

// When options.all is true, the result will be an Array.
options.all = true;
dnsPromises.lookup('example.com', options).then((result) => {
  console.log('addresses: %j', result);
  // addresses: [{"address":"2606:2800:220:1:248:1893:25c8:1946","family":6}]
});

当然也可以通过它们循环。

祝你好运

答案 1 :(得分:1)

由于您未附加HTML和应用程序的屏幕截图,因此这是我的暗处。尝试一下,让我知道是否可行

let tabs = element.all(by.id('tab'));
let radioButtons = element.all(by.id('radio1'));

let tabCount = await tabs.count();
for (let i = 0; i < tabCount; i++) {
  await tabs.get(i).click();
  let radioCount = await radioButtons.count();
  for (let j = 0; j < radioCount; j++) {
    await radioButtons.get(j).click();
  }
}
await element(by.id('saveAndContinue')).click();

答案 2 :(得分:1)

1)在await之前错过了tab.click()

2)element.all()。click()不起作用

const tabs = await element.all(by.id('tab'));
tabs.forEach(async tab => {
  await tab.click();
  await element.all(by.id('radio1')).first().click();
  // I think you should not find all radio1 of entire page, 
  // it will find radio1 of other tabs which is not visible in the active tab.
  // and protractor will fail to click on invisible radio1
  // thus you should find raido1 which is belongs to active tab

}); 
await element(by.id('saveAndContinue')).click();

答案 3 :(得分:0)

感谢以上Sergey Pleshakov的以下工作(微不足道的更改后):

async doGateway2bComplexHappyPath() {
let tabs = element.all(by.id('tab'));

let tabCount = await tabs.count();
for (let i = 0; i < tabCount; i++) {
  tabs.get(i).click();
  //browser.sleep(1000);
  element.all(by.id('radio1')).click();
  //browser.sleep(1000);
}

 //await browser.sleep(1000);
 await element(by.id('saveAndProceed')).click();
}