0

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).

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%'));
                            }
                        }

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.

Here my html:

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

1 Answers1

0

in this line let allOpts = element(by.css(array[i])); you are declaring ONE single element (elementArrayFinder would have been element.all...)

even if multiple elements are present, it will find the first one!

so your viewName is a text string, it's not an array

if your viewName === '10%', then viewName.length === 3 since it's just a length of the string

this is why your condition should be if (viewName === '10%') {

additionally don't forget about await before .click()

Sergey Pleshakov
  • 7,964
  • 2
  • 17
  • 40