Using assertTrue in Selenium Webdriver

Share on Facebook0Share on LinkedIn0Share on Google+0Tweet about this on Twitter0Email this to someoneDigg this

Here is an example of using assertTrue to verify if the title is correct or verify if an element is present on a web page. In this example we are verifying if the text Selenium Wiki is present on the Google search results page.

try
{
assertTrue(selenium.isTextPresent("Selenium Wiki"));
System.out.println("Selenium Wiki is present on the google search results page");
}
catch (Throwable e)
{
System.out.println("Selenium Wiki is not present on the google search results page");
}

When we look at the try and catch we can see that we normally use catch (Exception e); but in the case of Webdriver we will have to use catch (Throwable e).
If you use catch (Exception e) and the assert returns false you will get the following error message:
Exception in thread “main” java.lang.AssertionError:
at org.junit.Assert.fail(Assert.java:91)
at org.junit.Assert.assertTrue(Assert.java:43)
at org.junit.Assert.assertTrue(Assert.java:54)

The complete test script is as under:

package Htmltests;
import com.thoughtworks.selenium.Selenium;
import org.openqa.selenium.*;
import org.openqa.selenium.htmlunit.*;
import org.openqa.selenium.firefox.*;
import org.openqa.selenium.chrome.*;
import org.openqa.selenium.ie.*;
import org.junit.*;
import static org.junit.Assert.*;
@SuppressWarnings("unused")
public class SW {
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
String baseUrl = "http://www.google.com.au";
Selenium selenium = new WebDriverBackedSelenium(driver, baseUrl);
selenium.open("");
selenium.type("q", "selenium wiki");
selenium.click("btnG");
Thread.sleep(5000);
try
{
assertTrue(selenium.isTextPresent("Selenium Wiki"));
System.out.println("Selenium Wiki is present on the google search results page");
}
catch (Throwable e)
{
System.out.println("Selenium Wiki is not present on the google search results page");
}
driver.close();
}
}


5 thoughts on “Using assertTrue in Selenium Webdriver

  1. Jayita says:

    try
    {
    assertTrue(selenium.isTextPresent(“Selenium Wiki”));
    System.out.println(“Selenium Wiki is present on the google search results page”);
    }
    catch (Throwable e)
    {
    System.out.println(“Selenium Wiki is present on the google search results page”);
    }

    In my scenario, it prints Selenium Wiki is present on the google search results page even if selenium wiki is not present.

    Please suggest

    • Pavandeep Puddupakkam says:

      Its printing Selenium Wiki is present on the google search results page as you will see that the search box has the text “selenium wiki”. I am sure you will see Selenium Wiki on page 1 of the search (Selenium Wiki is quite popular :)).

  2. Prathip says:

    in catch method write statement as Selenium Wiki is NOT present on the google search results page
    it will get issue resolved.

    U r printing the same sentence in both.

Leave a Reply

Your email address will not be published. Required fields are marked *