37 – TestNG Testing Framework for Selenium Part-2
Selenium Class 37 – TestNG Testing Framework for Selenium Part-2
2) Write first TestNG program
Write TestNG program/Test Case
a) Manual Test case
Test Case name: Verify page Title (Google)
Test Steps:
i) Launch the Browser
ii) Navigate to Google Home page (https://www.google.com/)
Expected Result:
“Google”
Verification Point:
Capture the Page Title and compare with expected
Test Data/Input:
NA
TestNG Test Case:
i) main method is not used in TestNG programs
ii) TestNG programs contain methods only that contain Annotations
iii) If we don’t write @Test Annotation then the method won’t be executed
public class VerifyTitle {
WebDriver driver;
@Test
public void verifyTitle(){
System.setProperty(“webdriver.chrome.driver”, “E:/chromedriver.exe”);
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get(“https://www.google.com/”);
String pageTitle = driver.getTitle();
/if (pageTitle.equals(“Google2”)){
System.out.println(“Google Application was Launched – Passed”);
}
else {
System.out.println(“Google Application was Not Launched – Failed”);
}/
Assert.assertEquals(“Google”, pageTitle);
}
@AfterMethod
public void closeBrowser(){
driver.close();
}
}
3) Create multiple Test cases and Run
Example:
Test Cases:
i) launchBrowser()
ii) verifyGoogleTitle()
iii) closeBrowser()
Genearal Test Execution Flow:
TestNG executes Test cases in Alphabetical order
i) closeBrowser()
ii) launchBrowser()
iii) verifyGoogleTitle()
TestNG testing program as per Alphabetical order
public class Sample1 {
WebDriver driver;
@Test
public void launchBrowser(){
System.setProperty(“webdriver.chrome.driver”, “E:/chromedriver.exe”);
driver = new ChromeDriver();
driver.manage().window().maximize();
}
@Test
public void verifyGoogleTitle(){
driver.get(“https://www.google.com/”);
Assert.assertEquals(“Google”, driver.getTitle());
}
@Test
public void closeBrowser(){
driver.close();
}
}
Usnig “priority” attribute we can prioritize Test cases then we can control the test execution process
public class Sample1 {
WebDriver driver;
@Test (priority=1)
public void launchBrowser(){
System.setProperty(“webdriver.chrome.driver”, “E:/chromedriver.exe”);
driver = new ChromeDriver();
driver.manage().window().maximize();
}
@Test (priority=2)
public void verifyGoogleTitle(){
driver.get(“https://www.google.com/”);
Assert.assertEquals(“Google”, driver.getTitle());
}
@Test (priority=3)
public void closeBrowser(){
driver.close();
}
}
Example 2:
Test Cases:
i) launchBrowser()
ii) verifyGoogleTitle()
iii) verifyYahooTitle()
iv) verifyGcritTitle()
v) closeBrowser()
General Test execution Flow:
i)closeBrowser()
ii) launchBrowser()
iii) verifyGcritTitle()
iv) verifyGoogleTitle()
v) verifyYahooTitle()
Create a TestNG program with priority:
public class Sample2 {
WebDriver driver;
@Test (priority=1)
public void launchBrowser(){
System.setProperty(“webdriver.chrome.driver”, “E:/chromedriver.exe”);
driver = new ChromeDriver();
driver.manage().window().maximize();
}
@Test (priority=2)
public void verifyGoogleTitle(){
driver.get(“https://www.google.com/”);
Assert.assertEquals(“Google”, driver.getTitle());
}
@Test (priority=3)
public void verifyYahooTitle(){
driver.get(“https://in.yahoo.com/”);
Assert.assertEquals(“Yahoo”, driver.getTitle());
}
@Test (priority=4)
public void verifyGcritTitle(){
driver.get(“http://www.gcrit.com/build3/admin/”);
Assert.assertEquals(“GCR Shop”, driver.getTitle());
}
@Test (priority=5)
public void closeBrowser(){
driver.close();
}
}
TestNG program with some failed test cases
public class Sample2 {
WebDriver driver;
@Test (priority=1)
public void launchBrowser(){
System.setProperty(“webdriver.chrome.driver”, “E:/chromedriver.exe”);
driver = new ChromeDriver();
driver.manage().window().maximize();
}
@Test (priority=2)
public void verifyGoogleTitle(){
driver.get(“https://www.google.com/”);
Assert.assertEquals(“Google”, driver.getTitle());
}
@Test (priority=3)
public void verifyYahooTitle(){
driver.get(“https://in.yahoo.com/”);
Assert.assertEquals(“xyz”, driver.getTitle());
}
@Test (priority=4)
public void verifyGcritTitle(){
driver.get(“http://www.gcrit.com/build3/admin/”);
Assert.assertEquals(“GCR Shop”, driver.getTitle());
}
@Test (priority=5)
public void closeBrowser(){
driver.close();
}
}
WebDriver driver;
@Test (priority=1)
public void launchBrowser(){
System.setProperty(“webdriver.chrome.driver”, “E:/chromedriver.exe”);
driver = new ChromeDriver();
driver.manage().window().maximize();
}
@Test (priority=2)
public void verifyGoogleTitle(){
driver.get(“https://www.google.com/”);
Assert.assertEquals(“Google”, driver.getTitle());
}
@Test (priority=3)
public void verifyYahooTitle(){
driver.get(“https://in.yahoo.com/”);
Assert.assertEquals(“xyz”, driver.getTitle());
}
@Test (dependsOnMethods =”verifyYahooTitle”)
public void verifyGcritTitle(){
driver.get(“http://www.gcrit.com/build3/admin/”);
Assert.assertEquals(“GCR Shop”, driver.getTitle());
}
@Test (priority=5)
public void closeBrowser(){
driver.close();
}
}
Conclusion:
Use “priority” attribute if there is no functionality dependency between test cases,
Use “dependsOnMethods” attribute if there is functionality dependency between test cases,
4) TestNG Annotations
@Test – The annotated method is a Test Case
@BeforMethod – The annotated method will be run before each Test case/method in the current class is invoked
@AfterMethod -The annotated method will be run after each Test case in the current class have been run
@BeforeClass – The annotated method will be run the first Test Method in the current class is invoked
@AfterClass – The annotated method will be run after all the Test methods in the current class have been run
Example:
Test Cases:
launchBrowser()
i) verifyGoogleTitle()
ii) verifyYahooTitle()
iii) verifyGcritTitle()
closeBrowser()
Test Execution Flow 1:
launchBrowser()
i) verifyGoogleTitle()
closeBrowser()
launchBrowser()
ii) verifyYahooTitle()
closeBrowser()
launchBrowser()
iii) verifyGcritTitle()
closeBrowser()
For this Test execution flow use @BeforeMethod and @AfterMethod Annotations
Test Execution Flow 1:
launchBrowser()
i) verifyGoogleTitle()
ii) verifyYahooTitle()
iii) verifyGcritTitle()
closeBrowser()
For this Test execution flow use @BeforeClass and @AfterClass Annotations
TestNG program using @BeforeMethod and @AfterMethod Annotations
public class Sample2 {
WebDriver driver;
@BeforeMethod
public void launchBrowser(){
System.setProperty(“webdriver.chrome.driver”, “E:/chromedriver.exe”);
driver = new ChromeDriver();
driver.manage().window().maximize();
}
@Test (priority=1)
public void verifyGoogleTitle(){
driver.get(“https://www.google.com/”);
Assert.assertEquals(“Google”, driver.getTitle());
}
@Test (priority=2)
public void verifyYahooTitle(){
driver.get(“https://in.yahoo.com/”);
Assert.assertEquals(“Yahoo”, driver.getTitle());
}
@Test (priority=3)
public void verifyGcritTitle(){
driver.get(“http://www.gcrit.com/build3/admin/”);
Assert.assertEquals(“GCR Shop”, driver.getTitle());
}
@AfterMethod
public void closeBrowser(){
driver.close();
}
}
TestNG program using @BeforeClass and @AfterClass Annotations
public class Sample2 {
WebDriver driver;
@BeforeClass
public void launchBrowser(){
System.setProperty(“webdriver.chrome.driver”, “E:/chromedriver.exe”);
driver = new ChromeDriver();
driver.manage().window().maximize();
}
@Test (priority=1)
public void verifyGoogleTitle(){
driver.get(“https://www.google.com/”);
Assert.assertEquals(“Google”, driver.getTitle());
}
@Test (priority=2)
public void verifyYahooTitle(){
driver.get(“https://in.yahoo.com/”);
Assert.assertEquals(“Yahoo”, driver.getTitle());
}
@Test (priority=3)
public void verifyGcritTitle(){
driver.get(“http://www.gcrit.com/build3/admin/”);
Assert.assertEquals(“GCR Shop”, driver.getTitle());
}
@AfterClass
public void closeBrowser(){
driver.close();
}
}
……………………………………