Home > Enterprise >  I want to pass my test if the element in selenium returns empty text
I want to pass my test if the element in selenium returns empty text

Time:10-07

In below code i want to check when i have applied a "No Priority" filter it should check that whether that priority column have any text in it or not, if it has any text that means it is returning some priority number and the test case should fail. right now it is only printing the text as empty because i have applied filter but how do i validate that if it returns some text, and then mark it as failed. also a count of every element that returns empty string and another variable to count elements which returns some specific text would be a bonus.

        for (int i = 1; i <= noPriorityPageTotal; i  ) {
            List<WebElement> allListofNoPriority = driver.findElements(By.xpath("(//*[@class='sortable-row'])["   i   "]//td[3]"));
            for (WebElement element : allListofNoPriority) {
                logger.info("The text is "  element.getText());
            }
        }

CodePudding user response:

You can make use of .isEmpty() or .equals("") in conjunction like below

for (int i = 1; i <= noPriorityPageTotal; i  ) {
    List<WebElement> allListofNoPriority = driver.findElements(By.xpath("(//*[@class='sortable-row'])["   i   "]//td[3]"));
    for (WebElement element : allListofNoPriority) {
       // logger.info("The text is "  element.getText());
        if (element.getText().isEmpty() || element.getText().equals("")) {
            System.out.println("Test case should be pass");
        }
        else {
            System.out.println("Test case should be fail.");
        }
    }
}

in case you are testng as a framework, you can use assertion as below :

for (int i = 1; i <= noPriorityPageTotal; i  ) {
    List<WebElement> allListofNoPriority = driver.findElements(By.xpath("(//*[@class='sortable-row'])["   i   "]//td[3]"));
    for (WebElement element : allListofNoPriority) {
       // logger.info("The text is "  element.getText());
        if (element.getText().isEmpty() || element.getText().equals("")) {
            System.out.println("Test case should be pass");
            logger.info("The text is "  element.getText());
            Assert.assertTrue(true);
        }
        else {
            System.out.println("Test case should be fail.");
            logger.info("The text is "  element.getText());
            Assert.assertTrue(true);
        }
    }
}

CodePudding user response:

You can use Assertions to fail the case if string is not empty.

   for (int i = 1; i <= noPriorityPageTotal; i  ) {
            List<WebElement> allListofNoPriority = driver
                    .findElements(By.xpath("(//*[@class='sortable-row'])["   i   "]//td[3]"));
            for (WebElement element : allListofNoPriority) {
                try {
                    Assertions.assertThat(element.getText()).isEmpty();
                } catch (AssertionError e) {
                    logger.info("String is not empty. The text is "   element.getText());
                }
            }
        }
  • Related