vendredi 30 octobre 2015

If Loop in Java till condition is no longer valid then continue with script

I am trying to delete every (visible) row in a table untill there are none left, only then do I want to continue with the rest of my test case. For this reason I'm using an if/else statement. As part of this statement I need to select the first row, click the delete button and then confirm my action by clicking OK. Then I want to go back and check if there is still a first row, if so then repeat till there are no more rows. If not, then press other button. This is what I've got so far:

if(driver.findElement(By.xpath("//div[@id='ext-gen445']/div/table/tbody/tr/td[2]/div")) != null) //to determine if there is a first row present
        {
            driver.findElement(By.xpath("//div[@id='ext-gen445']/div/table/tbody/tr/td[2]/div")).click(); // select first row
            driver.findElement(By.xpath("//*[@type='button' and text()='Delete']")).click(); //click delete
            wait.until(ExpectedConditions.elementToBeClickable(By.id("ext-gen483")));

            /** as there is more then one OK button I need the following code to find and click the correct OK button to confirm deleting the row */
            List<WebElement> listOfOKbut = driver.findElements(By.xpath("//*[@class=' x-btn-text' and text()='OK']"));
            if(listOfOKbut.size() >= 2) {
                listOfOKbut.get(1).click(); //click OK button to delete
        } Now I need to back to see if there is a first row again and repeat this till there are no more rows
else{
Only when there are no more rows do I want to continue

Problem now is that when it deletes the first row it skips the else statement and continues with the test script after the else statement, even though there are more rows present.

Also, when I change the first line so that the element cannot be found the script stops completely with a NoSuchElementException. Seems this bit of code is also incorrect.

As some of you may have noticed, I ask a lot of questions. I have not had any Java training (yet) but love what I can do with test automation using this stuff.

Thanks!

Aucun commentaire:

Enregistrer un commentaire