如果另一个元素包含特定文本,请单击一个元素.量角器自动测试 [英] Click an element if another element contains a specific text. Automated Test with Protractor
本文介绍了如果另一个元素包含特定文本,请单击一个元素.量角器自动测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要检查3个元素中的哪一个包含文本"10%".但是我不确定我是否正确获取了元素的文本.当我运行代码时,我没有收到错误,但是if条件始终为false(也应该为true).
I need to check which one of 3 elements contains the text "10%" but I am not sure I am getting correctly the text of the element. When I run the code I am not getting errors but the if condition is always false(also when it should be true).
我在规范内的javascript代码:
My javascript code inside the spec:
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%'))
这些都不允许运行if条件的正文.
None of these allowed to run the body of the if condition.
这是我的html:
<td _ngcontent-lqu-c20="">
<p _ngcontent-lqu-c20="" class="font-weight-bold-light">
<!---->
<span _ngcontent-lqu-c20=""> 10% </span>
</p>
</td>
推荐答案
您错过了致电getText()
来阅读Web元素上的内容
You missed to call getText()
to read the content on the web element
var allOpts = (await driver.findElement(By.css(array[k])).getText()).trim()
这篇关于如果另一个元素包含特定文本,请单击一个元素.量角器自动测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文