Skip to content

To Be Automation Testcase

andrewleo edited this page Nov 1, 2013 · 5 revisions

In previous post we actually created an automation script, not an automation testcase. Now use TestNG to convert it into a testcase.

Precondition

  • Basic knowledge about TestNG

Steps

Split the demo into several parts:

  • Prepare to test: start Chrome
  • Execute test: open Google, search keyword, check result
  • Clear up: quit Chrome
  • (If necessary) several groups of test data

Use TestNG's annotation to mark these parts such as:

package com.netease.demo;

import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import com.netease.dagger.BrowserEmulator;

public class TestNg {

	String googleUrl = "http://www.google.com";
	String searchBox = "//input[@name='q']";
	String searchBtn = "//input[@name='btnK']";
	BrowserEmulator be;

	@BeforeClass
	public void doBeforeClass() throws Exception {
		be = new BrowserEmulator();
	}

	@Test(dataProvider = "data")
	public void doTest(String keyword, String result) {
		be.open(googleUrl);
		be.type(searchBox, keyword);
		be.click(searchBtn);
		be.expectTextExistOrNot(true, result, 5000);
	}

	@AfterClass(alwaysRun = true)
	public void doAfterClass() {
		be.quit();
	}

	@DataProvider(name = "data")
	public Object[][] data() {
		return new Object[][] { 
				{ "java", "www.java.com" }, 
				{ "github", "https://github.com/" }, 
			};
	}
}

That's all.

If you have already installed TestNG plugin in Eclipse, just run the testcase as TestNG Test, then it will be executed in a sequence as follow:

  1. @BeforeClass method
  2. @Test method with 1st group data of @DataProvider
  3. @Test method with 2nd group data of @DataProvider
  4. @AfterClass method
Clone this wiki locally