38 – TestNG Framework for Selenium Part-3
Selenium Class 38 – TestNG Testing Framework for Selenium Part-3
5) Execute multiple Programs / Classes using XML
- HTML and XML are tag based languages and no control flow in these languages
- HTML – Web page design (Web UI design)
- XML – Data Transporting
XML file for executing multiple programs / classes
………………………………….
Tags in this XML file
suite tag,
test tag,
classes tag,
class tag
………………………………….
Writing tags,
-Open tag -Close tag
Or
-Open and Close
………………………………….
Navigation for creating XML file in Eclipse IDE,
In Eclipse IDE,
Select Java Package and right click
- New
- Other
- Enter “TestNG”
- Enter source and package names
- Enter XML file Name
- Finish
………………………………….
1
1) Execute multiple programs / classes using xml with @BeforeMethod and @AfterMethod
XML File:
………………………………….
Class1 File:
public class Class1 {
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();
}
}
Class2 File:
public class Class2 {
WebDriver driver;
@BeforeMethod
public void launchBrowser(){
System.setProperty(“webdriver.chrome.driver”, “E:/chromedriver.exe”);
driver = new ChromeDriver();
driver.manage().window().maximize();
}
@Test (priority=4)
public void verifyBankOfAmericaTitle(){
driver.get(“https://www.bankofamerica.com/”);
Assert.assertEquals(“Bank of America – Banking, Credit Cards, Home Loans and Auto Loans”, driver.getTitle());
}
@Test (priority=5)
public void verifyICICIBankTitle(){
driver.get(“https://www.icicibank.com/”);
Assert.assertEquals(“Personal Banking, Online Banking Services – ICICI Bank”, driver.getTitle());
}
@Test (priority=6)
public void verifyGcreddyTitle(){
driver.get(“http://www.gcreddy.com/”);
Assert.assertEquals(“Software Testing – A blog about Manual Testing, Selenium, UFT/QTP, SQL, Java and Python Step by Step Tutorials by G C Reddy.”, driver.getTitle());
}
@AfterMethod
public void closeBrowser(){
driver.close();
}
}
2) Execute multiple programs / classes using xml with @BeforeClass and @AfterClass
Class1 File:
public class Class1 {
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();
}
}
Class2 File:
public class Class2 {
WebDriver driver;
@BeforeClass
public void launchBrowser(){
System.setProperty(“webdriver.chrome.driver”, “E:/chromedriver.exe”);
driver = new ChromeDriver();
driver.manage().window().maximize();
}
@Test (priority=4)
public void verifyBankOfAmericaTitle(){
driver.get(“https://www.bankofamerica.com/”);
Assert.assertEquals(“Bank of America – Banking, Credit Cards, Home Loans and Auto Loans”, driver.getTitle());
}
@Test (priority=5)
public void verifyICICIBankTitle(){
driver.get(“https://www.icicibank.com/”);
Assert.assertEquals(“Personal Banking, Online Banking Services – ICICI Bank”, driver.getTitle());
}
@Test (priority=6)
public void verifyGcreddyTitle(){
driver.get(“http://www.gcreddy.com/”);
Assert.assertEquals(“Software Testing – A blog about Manual Testing, Selenium, UFT/QTP, SQL, Java and Python Step by Step Tutorials by G C Reddy.”, driver.getTitle());
}
@AfterClass
public void closeBrowser(){
driver.close();
}
}
………………………………….
Using @BeforeTest and @AfterTest Annotations
(Common precondition and common post condition for all test cases in multiple programs/classes)
Note: use one parent class to create Browser driver and close, by inheriting classes we can execute
@BeforeTest and @AfterTest Annotations
XML File:
………………………………….
Parent File:
public class ClassP {
static WebDriver driver;
@BeforeTest
public void launchBrowser(){
System.setProperty(“webdriver.chrome.driver”, “E:/chromedriver.exe”);
driver = new ChromeDriver();
driver.manage().window().maximize();
}
@AfterTest
public void closeBrowser(){
driver.close();
}
}
Class1 File:
public class Class1 extends ClassP {
@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());
}
}
Class2 File:
public class Class2 extends ClassP{
@Test (priority=4)
public void verifyBankOfAmericaTitle(){
driver.get(“https://www.bankofamerica.com/”);
Assert.assertEquals(“Bank of America – Banking, Credit Cards, Home Loans and Auto Loans”, driver.getTitle());
}
@Test (priority=5)
public void verifyICICIBankTitle(){
driver.get(“https://www.icicibank.com/”);
Assert.assertEquals(“Personal Banking, Online Banking Services – ICICI Bank”, driver.getTitle());
}
@Test (priority=6)
public void verifyGcreddyTitle(){
driver.get(“http://www.gcreddy.com/”);
Assert.assertEquals(“Software Testing – A blog about Manual Testing, Selenium, UFT/QTP, SQL, Java and Python Step by Step Tutorials by G C Reddy.”, driver.getTitle());
}
}
6) Grouping Test Cases
a) tags in XML file for executing multiple programs
suite,
test,
classes,
class,
b) Tags in XML file for grouping Test Cases
suite,
test,
groups,
run,
include,
classes,
class,
………………………………….
Test Groups
1) Sanity Tests
i) launchBrowser()
ii) verifyGoogleTitle()
iii) verifyGcritTitle()
iv) closeBrowser()
2) Regression Tests
i) launchBrowser()
ii) verifyYahooTitle()
iii) verifyGcritTitle()
iv) verifyGcreddyTitle()
v) closeBrowser()
………………………………….
XML File:
Class File:
public class Class7 {
WebDriver driver;
@Test(groups = {“sanity”, “regression”}, priority=1)
public void launchBrowser(){
System.setProperty(“webdriver.chrome.driver”, “E:/chromedriver.exe”);
driver = new ChromeDriver();
driver.manage().window().maximize();
}
@Test(groups = {“sanity”, “regression”}, priority=6)
public void closeBrowser(){
driver.close();
}
@Test (groups = {“sanity”}, priority=2)
public void verifyGoogleTitle(){
driver.get(“https://www.google.com/”);
Assert.assertEquals(“Google”, driver.getTitle());
}
@Test (groups = {“regression”}, priority=3)
public void verifyYahooTitle(){
driver.get(“https://in.yahoo.com/”);
Assert.assertEquals(“Yahoo”, driver.getTitle());
}
@Test (groups = {“sanity”, “regression”}, priority=4)
public void verifyGcritTitle(){
driver.get(“http://www.gcrit.com/build3/admin/”);
Assert.assertEquals(“GCR Shop”, driver.getTitle());
}
@Test (groups = {“regression”}, priority=5)
public void verifyGcreddyTitle(){
driver.get(“http://www.gcreddy.com/”);
Assert.assertEquals(“Software Testing – A blog about Manual Testing, Selenium, UFT/QTP, SQL, Java and Python Step by Step Tutorials by G C Reddy.”, driver.getTitle());
}
}
………………………………….