Search code examples
javaseleniumwebdriverassert

Using assertTrue() to validate the presence of text in selenium


I want to validate the presence of a text using assertTrue(), but I am getting a StackOverflow error. I am not sure that the code I have written is correct or not. Need your suggestions.

// Checking the posted text
WebElement postedtext= driver.findElement(By.cssSelector("css123"));
assertTrue("The text is verfied",postedtext.getText().equals(enteredText));

private static void assertTrue(String string, boolean equals) {
assertTrue(string,equals);}

Solution

  • It is a name conflicting that you happened to name you assert method the same name as the library method. Rename your assertTrue can solve the problem.

    // Checking the posted text
    WebElement postedtext= driver.findElement(By.cssSelector("css123"));
    myAssertTrue("The text is verfied",postedtext.getText().equals(enteredText));
    
    private static void myAssertTrue(String string, boolean equals) {
        try {
            assertTrue(string,equals);
        } catch (AssertionError e) {
            System.out.println(e.getMessage());
            throw e;
        }
    }
    

    Or you just delete your assertTrue and use the library method instead.