- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to do Selenium Testing on different Browsers using TestNG?
TestNG is a powerful testing framework, an enhanced version of JUnit which was in use for a long time before TestNG came into existence. NG stands for 'Next Generation'.
A website should be tested across multiple browsers like IE, Chrome, Firefox, Safari to validate the compatibility of website and functionality. Since the HTML, CSS and java scripts are unique across all browsers, it is always recommended to do cross browser testing to ensure the compatibility of website. Selenium supports cross browser testing so does the TestNG.
In this article, we will analyse how to perform cross browser testing in Selenium with TestNG.
Approach/Algorithm to solve this problem
Step 1: Make sure Selenium, TestNG and initial set up of different browsers like Firefox, IE, Chrome driver is set up in system properly.
Step 2: Create a TestNG class and write selenium code as mentioned in program code.
Parameter(‘Browser’) will read the value from testng.xml and pass it to the code.
launchapp() will set up all initial steps for a browser to launch the website http://www.calculator.net
CalculatePercent will perform actual execution and validation.
Step 3: Create the testng.xml file with parameter name as browser as shown in testng.xml section.
Step 4: Run the testng.xml file.
Step 5: It will run the test case in all 3 browsers at a time.
Example
The following code to create a TestNG class with Selenium code:
import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.ie.InternetExplorerDriver; import java.util.concurrent.TimeUnit; import org.openqa.selenium.*; import org.testng.annotations.*; public class TestNGClass { private WebDriver driver; private String URL = "http://www.calculator.net"; @Parameters("browser") @BeforeTest public void launchapp(String browser) { if (browser.equalsIgnoreCase("firefox")) { System.out.println(" Executing on FireFox"); driver = new FirefoxDriver(); driver.get(URL); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.manage().window().maximize(); } else if (browser.equalsIgnoreCase("chrome")) { System.out.println(" Executing on CHROME"); System.out.println("Executing on IE"); System.setProperty("webdriver.chrome.driver", "D:\chromedriver.exe"); driver = new ChromeDriver(); driver.get(URL); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.manage().window().maximize(); } else if (browser.equalsIgnoreCase("ie")) { System.out.println("Executing on IE"); System.setProperty("webdriver.ie.driver", "D:\IEDriverServer.exe"); driver = new InternetExplorerDriver(); driver.get(URL); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.manage().window().maximize(); } else { throw new IllegalArgumentException("The Browser Type is Undefined"); } } @Test public void calculatepercent() { // Click on Math Calculators driver.findElement(By.xpath(".//*[@id = 'menu']/div[3]/a")).click(); // Click on Percent Calculators driver.findElement(By.xpath(".//*[@id = 'menu']/div[4]/div[3]/a")).click(); // Enter value 10 in the first number of the percent Calculator driver.findElement(By.id("cpar1")).sendKeys("10"); // Enter value 50 in the second number of the percent Calculator driver.findElement(By.id("cpar2")).sendKeys("50"); // Click Calculate Button driver.findElement(By.xpath(".//*[@id = 'content']/table/tbody/tr/td[2]/input")).click(); // Get the Result Text based on its xpath String result = driver.findElement(By.xpath(".//*[@id = 'content']/p[2]/span/font/b")).getText(); // Print a Log In message to the screen System.out.println(" The Result is " + result); if(result.equals("5")) { System.out.println(" The Result is Pass"); } else { System.out.println(" The Result is Fail"); } } @AfterTest public void closeBrowser() { driver.close(); } }
testng.xml
This is a configuration file that is used to organize and run the TestNG test cases.
It is very handy when limited tests are needed to execute rather than full suite.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "<a href="http://testng.org/testng-1.0.dtd">http://testng.org/testng-1.0.dtd</a>"> <suite name="TestSuite" thread-count="3" parallel="tests" > <test name="ChromeTest"> <parameter name="browser" value="chrome"/> <classes> <class name="TestNGClass "> </class> </classes> </test> <test name="FirefoxTest"> <parameter name="browser" value="firefox" /> <classes> <class name="TestNGClass"> </class> </classes> </test> <test name="ie"> <parameter name="browser" value="ie" /> <classes> <class name="TestNGClass"> </class> </classes> </test> </suite>
Output
[TestNG] Running: C://Users/************** Executing on CHROME Executing on IE Executing on FIREFOX The Result is 5 The Result is Pass The Result is 5 The Result is Pass The Result is 5 The Result is Pass =============================================== Suite1 Total tests run: 3, Passes: 3, Failures: 0, Skips: 0 ===============================================