Writing test case using testNG

Creating a new TestNG Project

Step 1: Click File > New > Java Project

Step 2: Type "FirstTestNGProject" as the Project Name then click Next.

Step 3:
  • We will now start to import the TestNG Libraries onto our project. Click on the "Libraries" tab, and then "Add Library…"
  • On the Add Library dialog, choose "TestNG" and click Next.

Writing test case using testNG
Step 4:
  1. Click Finish.
  2. You should notice that TestNG is included on the Libraries 
  3. Then, add Selenium JAR files to the project

Step 5: Click Finish and verify that our FirstTestNGProject is visible on Eclipse's Package Explorer window.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class TestNGDemo {
 public static WebDriver driver;

 @Test
 public void f() {
  driver.get("http://www.google.com");
  System.out.println(driver.getCurrentUrl());
 }

 @BeforeMethod
 public void beforeMethod() {
  System.setProperty("webdriver.gecko.driver", "Driver/geckodriver.exe");
  driver = new FirefoxDriver();
 }

 @AfterMethod
 public void afterMethod() {
  driver.quit();
 }
}

Creating a New TestNG Test File:
Project setup is done, now let us create a new TestNG file.


Step 1: Right-click on the "src" package folder then choose New > Other…


Writing test case using testNG

Step 2: Click on the TestNG folder and select the "TestNG class" option. Click Next.
Writing test case using testNG

Step 3: Type the correct values in appropriate input boxes and click Finish. 


Writing test case using testNG

Step 4: After click on finish, you can see a TestNG class is created as follows.

Writing test case using testNG


Step 5: Modify the code as below and execute the code. To run the test, simply run the file in Eclipse as you normally do. 



Step 6: Checking results created by testNG
  1. Right click on project and click on refresh
  2. You will see the test-output folder is created. Expand it
  3. Double-click on that index.html file to open it within Eclipse's built-in web browser. You can refresh this page any time after you rerun your test

testNG results


Multiple Test Cases :
We can use multiple @Test annotations in a single TestNG file. By default, methods annotated by @Test are executed alphabetically. 

We can run multiple test cases by using @Test annotations in a single TestNG file. 
1
2
3
4
5
6
7
@Test
public void addition() {
}

@Test
public void subtraction() {
}

Parameters :
You can control the order of the execution of your methods with the help of the parameter "priority". Parameter is a keyword that modifies the annotation's function.

TestNG will execute the @Test annotation with the lowest priority value up to the largest. 
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class TestNG_Demo {

 public static WebDriver driver;

 @Test(priority = 2)
 public void addition() {
 }

 @Test(priority = 1)
 public void subtraction1() {
 }

 @Test(priority = 0)
 public void multiply() {
 }

 @BeforeMethod
 public void beforeMethod() {
 }

 @AfterMethod
 public void afterMethod() {
 }
}

Multiple parameters :
Aside from "priority", @Test has another parameter called "alwaysRun" which can only be set to either "true" or "false". To use two or more parameters in a single annotation, separate them with a comma such as the one shown below.
1
2
3
4
5
@Test(priority=0, enabled=true)
public void multiply() {
 }

@Test(priority = 3, enabled = false) //to skip the test 

Dependent Test :
Sometime you need to run test cases in perticular order like some test cases are depend upon another test case. In such a scenario you can use attributes dependsOnMethods in @Test annotations

In following example, test method CheckMail is depend upon Login method. If Login fails you need not to execute CheckMail method. In such scenario, you can use dependsOnMethods with @Test annotation of CheckMail method 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class TestNG_Demo {

 public static WebDriver driver;

 @Test
 public void Login() {
 }

 @Test(dependsOnMethods = { "Login" })
 public void CheckMail() {
 }

 @Test
 public void LogOut() {
 }

 @BeforeMethod
 public void beforeMethod() {
 }

 @AfterMethod
 public void afterMethod() {
 }
}


<--Previous|| Next -->

1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete