量角器测试中的条件:如果元素包含特定文本则执行操作

Condition in Protractor test: doing action if element contains a certain text

我需要检查 3 个元素中的哪一个包含文本“10%”,但我不确定我是否正确获取了该元素的文本。当我 运行 代码时,我没有收到错误,但 if 条件始终为假(当它应该为真时)。

我的 javascript 规范中的代码:

    await driver.wait(until.elementIsVisible(await driver.findElement(By.css("tr:nth-child(1) .font-weight-bold-light > span"))), 30000)
    await driver.wait(until.elementIsVisible(await driver.findElement(By.css("tr:nth-child(2) .font-weight-bold-light > span"))), 30000)
    await driver.wait(until.elementIsVisible(await driver.findElement(By.css("tr:nth-child(3) .font-weight-bold-light > span"))), 30000)

    var array = ["tr:nth-child(1) .font-weight-bold-light > span", "tr:nth-child(2) .font-weight-bold-light > span", "tr:nth-child(2) .font-weight-bold-light > span"];
    var arrayDelete = ["tr:nth-child(1) img:nth-child(2)", "tr:nth-child(2) img:nth-child(2)", "tr:nth-child(3) img:nth-child(2)"]
    for (var k = 0; k<array.length; k++){

            var allOpts = (await driver.findElement(By.css(array[k])));
    await driver.sleep(2000)


                            if (allOpts == ('10%')) {
                                await driver.sleep(2000)
                                await driver.wait(until.elementIsVisible(await driver.findElement(By.css(arrayDelete[k]))), 30000)
                                // 11 | click | css=.actions > img:nth-child(2) | 
                                await driver.findElement(By.css((arrayDelete[k]))).click()
                                // 12 | pause | 1000 | 
                                await driver.sleep(5000)
                                // 13 | waitForElementVisible | css=.ml-3 | 30000
                                await driver.wait(until.elementIsVisible(await driver.findElement(By.css(".ml-3"))), 30000)
                                // 14 | click | css=.ml-3 | 
                                await driver.findElement(By.css(".ml-3")).click()
                                // 15 | pause | 3000 | 
                                await driver.sleep(10000)
                                // 16 | verifyElementNotPresent | css=td:nth-child(1) | 
                                {
                                  const elements = await driver.findElements(By.css((allOpts)))
                                  assert(!elements.length)
                                }
                                console.log(('ho cancellato l obiettivo ')+ k);
                            }
                            else {
                                console.log(('obiettivo ') + k + (' non cancellato, in quanto il suo peso non è 10%'));
                            }
                        }

我尝试了所有这些:

                if (allOpts == (' 10% ')) 
                if (allOpts == ('10%')) 
                if (allOpts === (' 10% ')) 
                if (allOpts === ('10%')) 
                if (allOpts.getText() == (' 10% ')) 
                if (allOpts.getText()  == ('10%')) 
                if (allOpts.getText()  === (' 10% ')) 
                if (allOpts.getText()  === ('10%')) 
                if (allOpts.getAttribute("value") == (' 10% ')) 
                if (allOpts.getAttribute("value")  == ('10%')) 
                if (allOpts.getAttribute("value")  === (' 10% ')) 
                if (allOpts.getAttribute("value")  === ('10%'))

None 其中允许 运行 if 条件的主体。

这是我的 html:

<td _ngcontent-lqu-c20="">
<p _ngcontent-lqu-c20="" class="font-weight-bold-light">
<!---->
<span _ngcontent-lqu-c20=""> 10% </span>
</p>
</td>

在这一行 let allOpts = element(by.css(array[i])); 中,您声明了一个单一元素(elementArrayFinder 应该是 element.all...)

即使存在多个元素,它也会找到第一个!

所以你的 viewName 是一个文本字符串,它不是一个数组

如果你的viewName === '10%',那么viewName.length === 3因为它只是字符串的长度

这就是为什么你的条件应该是 if (viewName === '10%') {

此外不要忘记 await.click()

之前