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 etc. And 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. In this article we will show you 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.

Integrate TestNG with Selenium WebDriver

  1. Add selenium library jars in build path. You can read 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 TestNG Eclipse Plugin to learn.

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 main().
  2. Test method threw exception should be catch in main().
  3. If one test method throws a runtime exception which can not be catch then all other methods will not execute.
  4. Do not generate execution result report.
  5. Java code is massive and unclear.
  6. If you want to run more test case, 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. ");
	  }
  }

Selenium Java Code Use TestNG

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

  1. Java code is simple and clear.
  2. TestNG framework manage all the issues such as exception, run order, execution result reports etc.
  3. If one test method fail, other methods will not be influenced.
  4. Generate useful graphic reports.
  5. Test method execute by it’s name alphabetically.
  6. @Test annotated method is 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 -"));
  }

Use Annotation Parameter To Specify Test Method Run Order

Below picture is the execution result reports about above test method.

READ :   Selenium WebDriver - Handle Keyboard And Mouse Events

testng report test method execute order

You can see the test method execute order is decided by it’s 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
      {
            ......
      }

    Execution result report of above code.
    testng test annotation priority parameter

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

Download “Run-Selenium-Test-Case-Use-TestNG-Example-Code.zip” Run-Selenium-Test-Case-Use-TestNG-Example-Code.zip – Downloaded 78 times – 60 KB

(Visited 185 times, 1 visits today)

Leave a Reply

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.