Run Selenium Test Case Use TestNG

TestNG is an automation test framework. It has a lot of useful features such as graphic execution result reports, annotation support. Selenium webdriver is popular in web-based application automation, but it can not generate reports. So most selenium testers like to use TestNG and Selenium webdriver together to build their automation framework.

1. What Can We Learn From This Article?

In this article, we will show you the below tasks.

  1. Integrate TestNG with Selenium WebDriver.
  2. Example selenium Java code without TestNG
  3. Example selenium Java code use TestNG
  4. Use annotation parameter to specify test method run order.

2. Integrate TestNG with Selenium WebDriver.

  1. Add selenium library jars in the eclipse java project build path. You can read the article How to add selenium server standalone jar file into your java project to learn.
  2. Add TestNG library jars in Eclipse build path. You can read the article TestNG Eclipse Plugin to learn.

3. Selenium Java Code Without TestNG.

Below is selenium webdriver java code without TestNG. There are below disadvantages.

  1. All test methods should be run in the method main().
  2. Test method threw exception should be caught in main().
  3. If one test method throws a runtime exception that can not be caught then all other methods will not execute.
  4. Do not generate execution result reports.
  5. Java code is massive and unclear.
  6. If you want to run more test cases, you have to change the java code in main().
  7. Test method execute order should be managed in main() by java code.
  public static void main(String args[])
  {
	  TestNGSelenium tnsInstance = new TestNGSelenium();
	  
	  FirefoxDriver ffWebDriver = new FirefoxDriver();
	  
	  try
	  {
		  tnsInstance.b_testBingPageTitle(ffWebDriver);
	  }catch(Exception ex)
	  {
		  ex.printStackTrace();
	  }
	  
	  try
	  {
		  tnsInstance.a_testBingSearchResultPageTitle(ffWebDriver);
	  }catch(Exception ex)
	  {
		  ex.printStackTrace();
	  }
	  
	  ffWebDriver.quit();
  }
  
  /* We want this run first. It verify the Bing home page title. */
  public void b_testBingPageTitle(WebDriver ffDriver) throws Exception
  {  
	  ffDriver.get("http://www.bing.com");
	  
	  String pageTitle = ffDriver.getTitle();
	  
	  if(!"Bing".equalsIgnoreCase(pageTitle))
	  {
		  System.out.println("b_testBingPageTitle() fail. ");
		  throw new Exception("Bing homepage title not equal. ");
	  }else
	  {
		  System.out.println("b_testBingPageTitle() pass. ");
	  }
  }
  
  /* We want this run second. It check the Bing search result page title. */
  public void a_testBingSearchResultPageTitle(WebDriver ffDriver) throws Exception
  {
	  ffDriver.get("http://www.bing.com");
	  
	  Thread.sleep(1000);
	  
	  /* Get the search keyword input box. */
	  By searchKeywordBoxBy = By.xpath("//*[@id=\"sb_form_q\"]");
	  WebElement searchKeywordElement = ffDriver.findElement(searchKeywordBoxBy);
	  
	  /* Input java in the search box. */
	  searchKeywordElement.sendKeys("java");
	 
	  /* Get the submit button. */
	  By submitBtnBy = By.xpath("//*[@id=\"sb_form_go\"]");
	  WebElement submitButton = ffDriver.findElement(submitBtnBy);
	  
	  /* Submit the search form. */
	  submitButton.submit();
	  
	  Thread.sleep(5000);
	  
	  /* Get search result page title. */
	  String pageTitle = ffDriver.getTitle();
	  
	  if(pageTitle.startsWith("java -"))
	  {
		  System.out.println("a_testBingSearchResultPageTitle() pass. ");
	  }else
	  {
		  System.out.println("a_testBingSearchResultPageTitle() fail. ");
		  throw new Exception("Bing search result page title not equal. ");
	  }
  }

4. Selenium Java Code Use TestNG.

Below we use TestNG to implement the above test methods again, we can get a lot of benefits as below list.

  1. Java code is simple and clear.
  2. TestNG framework manages all the issues such as exception, run order, execution result reports.
  3. If one test method fails, other methods will not be influenced.
  4. Generate useful graphic reports.
  5. Test method executes by its name alphabetically.
  6. @Test annotated method is a test method.
  7. @BeforeClass annotated method will be executed before the class’s first test method run.
  8. @AfterClass annotated method will be executed after all the class’s test method run.
  private WebDriver ffDriver = null;
  
  @BeforeClass
  public void beforeClass() {
	  if(this.ffDriver==null)
	  {
		  this.ffDriver = new FirefoxDriver();
	  }
  }

  @AfterClass
  public void afterClass() {
	  if(this.ffDriver!=null)
	  {
		  this.ffDriver.quit();
		  this.ffDriver = null;
	  }
  }
  
  @Test
  /* Check the Bing home page title. */
  public void b_testBingPageTitleTestNG() throws Exception
  {  
	  ffDriver.get("http://www.bing.com");
	  
	  String pageTitle = ffDriver.getTitle();
	  
          Assert.assertEquals(pageTitle, "Bing");
  }
  
  @Test
  /* Check the Bing search result page title. */
  public void a_testBingSearchResultPageTitleTestNG() throws Exception
  {
	  ffDriver.get("http://www.bing.com");
	  
	  Thread.sleep(1000);
	  
	  /* Get the search keyword input box. */
	  By searchKeywordBoxBy = By.xpath("//*[@id=\"sb_form_q\"]");
	  WebElement searchKeywordElement = ffDriver.findElement(searchKeywordBoxBy);
	  
	  /* Input java in the search box. */
	  searchKeywordElement.sendKeys("java");
	 
	  /* Get the submit button. */
	  By submitBtnBy = By.xpath("//*[@id=\"sb_form_go\"]");
	  WebElement submitButton = ffDriver.findElement(submitBtnBy);
	  
	  /* Submit the search form. */
	  submitButton.submit();
	  
	  Thread.sleep(5000);
	  
	  /* Get search result page title. */
	  String pageTitle = ffDriver.getTitle();
	  
	  Assert.assertTrue(pageTitle.startsWith("java -"));
  }

5. Use Annotation Parameter To Specify Test Method Run Order.

 

When you run the example above, you can see the test method execute order is decided by the method name alphabetically. How to change the order if we want? How to not run some test cases?

  1. Use @Test (priority = positive number) such as @Test(priority = 1) to change method execute order. The method with small priority value will run before others. From below java code, b_testBingPageTitleTestNG() will run before a_testBingSearchResultPageTitleTestNG().
      @Test(priority=1)
      /* Verify the Bing home page title. */
      public void b_testBingPageTitleTestNG() throws Exception
      {  
            ......
      }
    
      @Test(priority=2)
      /* Check the Bing search result page title. */
      public void a_testBingSearchResultPageTitleTestNG() throws Exception
      {
            ......
      }

     

  2. Use @Test(enabled = bool value) such as @Test( enabled = false ) to disable or enable a test method execution. The default value for the enabled parameter is true. If set its value to false, then the method will not be executed.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Index