Introduction to TestNG Framework


Introduction to TestNG Framework

I) Overview

II) Install TestNG in addition to write First TestNG Test Case.

III) Create multiple Test Cases in addition to Run

IV) Execute multiple programs/classes using XML
-----------------------------------------------
I) Overview

> In Selenium using Java at that spot are ii Testing frameworks available,

1) JUnit

2) TestNG
------------------------------
TestNG Testing Framework
> TestNG is a testing framework designed to simplify a wide attain of Testing needs, from Unit Testing to System Testing.

> Initially developed for Unit Testing, straight off used for all kinds of Testing.

> TestNG is an opened upward rootage framework, where NG stands for adjacent generation.

> TestNG inspired from Junit(Java platform) in addition to NUnit (.NET platform), simply introducing approximately novel functionalities that larn into to a greater extent than powerful in addition to easier to use.
--------------------------------------------
Advantages of TestNG

1) TestNG Annotations are slow to practice Test Cases

2) Test Cases tin hold upward grouped in addition to prioritized to a greater extent than easily.

3) Supports Parameterization.

4) Supports Data driven Testing using Dataproviders.

5) Generates HTML reports

6) Parallel exam execution is possible.

7) Readily supports integration alongside other tools in addition to plug ins similar Eclipse IDE, construct tools Ant, Maven etc...
---------------------------------
Note: Using TestNG nosotros tin practice Test Cases, grouping Test Cases, prioritize Test Cases, execute Test Cases in addition to generate Test Reports.
---------------------------------------------
II) Install TestNG in addition to write showtime Test Case
 

In Eclipse

Help bill of fare -> Install New Software -> Click Add
-> Enter Name every bit "TestNG"
-> Enter URL every bit "http://beust.com/eclipse/"
-> Select "TestNG"
-> Next -> Next -> Accept the Agreement -> Finish
------------------------------------------------------
Write TestNG Test Case

Manual Test Case

Test Case Name: Verify championship of the Page

Test Steps:

1) Launch Browser

2) Navigate to gmail.com
------------------------------
Verification point
Capture the Page championship in addition to compare alongside expected

Expected = Gmail

Actual =

Status =
--------------------------------------------
TestNG Test Case:

public course of educational activity Sample {
@Test
public void verifyTitle(){
WebDriver driver = novel FirefoxDriver();   
driver.get("https://www.gmail.com");
String pageTitle = driver.getTitle();
Assert.assertEquals(pageTitle, "Gmail");
}
}
-------------------------------------------
Note:
1) primary method is non used for TestNG programs.

2) TestNG programs contains solely methods that incorporate @Test Annotations

3) if nosotros don't write @Test Annotations thus the methods are non going to hold upward executed.
----------------------------------------------------------
III) Write Multiple Test Cases
 

public course of educational activity Sample {
@Test
public void testA(){
Assert.assertEquals("Gmail", "Gmail");
}
@Test
public void testC(){
Assert.assertEquals("Gmail", "Google");
}
@Test
public void testB(){
Assert.assertEquals("Yahoo", "Yahoo");
}
}

Note: TestNG Test cases are executed inward Alphabetical order,

If You desire to command the Test execution procedure thus utilisation priority attribute.
---------------------------------------------------------------
public course of educational activity Sample {
@Test (priority = 3)
public void abcd(){
Assert.assertEquals("Gmail", "Gmail");
}
@Test (priority = 2)
public void xyz(){
Assert.assertEquals("Gmail", "Google");
}
@Test (priority = 1)
public void pqr(){
Assert.assertEquals("Yahoo", "Yahoo");
}
}
-------------------
General Test Execution Flow:
abcd
pqr
xyz
-----------------
pqr
xyz
abcd
--------------------
public course of educational activity Sample {
@Test (priority = 3)
public void abcd(){
Assert.assertEquals("Gmail", "Gmail");
}
@Test (priority = 1, enabled = false)
public void xyz(){
Assert.assertEquals("Google", "Google");
}
@Test (priority = 2)
public void pqr(){
Assert.assertEquals("Yahoo", "Yahoo");
}
}
--------------------------------
public course of educational activity Sample {
@Test
public void login(){
System.out.println("Login Successful");
}
@Test (dependsOnMethods = {"login"})
public void search(){
System.out.println("Search Successful");
}
@Test (dependsOnMethods = {"search"})
public void advancedSearch(){
System.out.println("Advanced Search Successful");
}
@Test (dependsOnMethods = {"advancedSearch"})
public void logout(){
System.out.println("Logout Successful");
}
}
-------------------------------------
Hard dependency
@Test (dependsOnMethods ={"methodName"})

Soft Dependency
@Test (dependsOnMethods ={"methodName"}, alwaysRun=true)
--------------------------------------------------
public course of educational activity Sample {
public WebDriver driver;
@Test (priority=1)
public void launchBrowser(){
driver = novel FirefoxDriver();   
}

@Test (priority=2)
public void verifyPageTitle1(){
driver.get("https://www.gmail.com");
Assert.assertEquals("Gmail", driver.getTitle());
}

@Test(priority=3)
public void verifyPageTitle2(){
driver.get("https://in.yahoo.com/");
Assert.assertEquals("Yahoo", driver.getTitle());
}
@Test (priority=4)
public void closeBrowser(){
driver.close();
}
}
--------------------------------------
Test Execution Flow
1) closebrowser
2) launchBrowser
3) verifyPageTitle1
4) verifyPageTitle2

Test Execution Flow (As per priorities) :

1) launchBrowser
2) verifyPageTitle1
3) verifyPageTitle2
4) closeBrowser
--------------------------------------------------

BeforeMethod in addition to AfterMethod Annotations

@BeforeMethod - Pre-condition for every Test illustration inward a Class/Program
@AfterMethod Post-condition for every Test illustration inward a Class/Program

Example:


@BeforeMethod
public void launchBrowser(){
driver = novel FirefoxDriver();   
}

@Test
public void verifyPageTitle1(){
driver.get("https://www.gmail.com");
Assert.assertEquals("Gmail", driver.getTitle());
}

@Test
public void verifyPageTitle2(){
driver.get("https://in.yahoo.com/");
Assert.assertEquals("Yahoo", driver.getTitle());
}
@AfterMethod
public void closeBrowser(){
driver.close();
}
}

--------------------------------------
Test Execution Flow:
launchBrowser -pre-condition for every exam case.
closeBrowser -post-condition for every exam case

verifyPageTitle1
verifyPageTitle2
---------------------------
launchBrowser
verifyPageTitle1
closeBrowser

launchBrowser
verifyPageTitle2
closeBrowser

---------------------------------------------
BeforeClass in addition to AfterClass Annotations

@BeforeClass -Pre-condition for All Test cases inward a Class/Program
@AfterClasee -Post-condition for All Test cases inward a Class/Program


Example:
 
@BeforeClass
public void launchBrowser(){
driver = novel FirefoxDriver();   
}

@Test
public void verifyPageTitle1(){
driver.get("https://www.gmail.com");
Assert.assertEquals("Gmail", driver.getTitle());
}

@Test
public void verifyPageTitle2(){
driver.get("https://in.yahoo.com/");
Assert.assertEquals("Yahoo", driver.getTitle());
}
@AfterClass
public void closeBrowser(){
driver.close();
}
}
---------------------------------------------------

Sumber http://www.gcreddy.com/
Post a Comment (0)
Previous Post Next Post