简体   繁体   English

如果另一个元素包含特定文本,请单击一个元素。 使用 Protractor 进行自动化测试

[英]Click an element if another element contains a specific text. Automated Test with Protractor

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.我需要检查 3 个元素中的哪一个包含文本“10%”,但我不确定我是否正确获取了元素的文本。 When I run the code I am not getting errors but the if condition is always false(also when it should be true).当我运行代码时,我没有收到错误,但 if 条件始终为假(当它应该为真时)。

My javascript code inside the spec:我在规范中的 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%'));
  }
}

I tryed all of these:我尝试了所有这些:

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 of these allowed to run the body of the if condition.这些都不允许运行 if 条件的主体。

Here my html:这是我的 html:

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

First of, your syntax is not typical for protractor (unless you're using some very old version).首先,您的语法对于 protractor 不是典型的(除非您使用的是一些非常旧的版本)。 if this is the tool you'r using checkout their documentation如果这是您使用的工具,请查看他们的文档

Second, in this line you're just declaring the element and not doing anything with it其次,在这一行中,您只是声明了元素,而不是对它做任何事情

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

you should have extracted element's text.你应该已经提取了元素的文本。 But using normal syntax it would look like so但是使用正常的语法它看起来像这样

var allOpts = await element(by.css(array[k])).getText();

Three, the reason this is not working三、这个不起作用的原因

if (allOpts.getText() == (' 10% '))

is because this is promise, which you need to resolve by .then() or by await ing it.这是因为这是 promise,您需要通过.then()await解决它。 Unless you figure out this process in depth (and I mean it... in ALL details), you won't go too far with protractor除非你深入了解这个过程(我的意思是......在所有细节中),你不会 go 与 protractor 太远

Four, when you do .getText() your string gets trimmed, meaning there is no spaces in the beginning and at the end ( '10%' not ' 10% ' )四,当您执行.getText()时,您的字符串会被修剪,这意味着开头和结尾没有空格( '10%'而不是 '10% ' 10% '

Five, when in doubt, just console.log() the value.五、当有疑问时,只需console.log()的值。 It's the easiest way to debug problems这是调试问题的最简单方法

And lastly, don't use == , use === instead.最后,不要使用== ,而是使用=== For 3 years working with protractor I never had a case when I HAD to use == , but I did have many confusions created by coercion when using it与 protractor 合作了 3 年,我从来没有遇到过必须使用==的情况,但是在使用它时我确实遇到了很多由强制造成的困惑

You missed to call getText() to read the content on the web element您错过了调用getText()来阅读 web 元素上的内容

var allOpts = (await driver.findElement(By.css(array[k])).getText()).trim()

first you need to read the text of the element ie by using getText() .首先,您需要使用getText()读取元素的文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM