Testing your web application across different browsers to make sure:
- Application performs similar manner for different browsers.
- Webelements should be working as expected.(Active or inactive)
- Application should have same font and color for objects.
- Length/width of fields should be same across browsers.
Test case: verify saucedemo site login
For this we will use selenium java and testNG framework.
- Use testng.xml to send parameters as firefox / edge / chrome to run tests on that specific browser.
- Make sure to have some value to parallel attribute in testng.xml.
- Place respective browser drivers under your project folder for ease of access.
BaseTest.java
package mypckg; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.ie.InternetExplorerDriver; import org.testng.annotations.*; public class BaseTest { public WebDriver driver; //declared outside, so each method in class can access @Parameters("browser") //parameter is a tag with attribute name = "browser" in testng.xml @BeforeTest //BeforeTest will execute before every Test(method) executes public void setup(String browser) throws Exception { if(browser.equalsIgnoreCase("firefox")) { System.setProperty("webdriver.gecko.driver", "./geckodriver"); driver = new FirefoxDriver();} else if(browser.equalsIgnoreCase("safari")) { //File file = new file("./IEDriverServer.exe"); //System.setProperty("webdriver.ie.driver", file.getAbsolutePath()); driver = new SafariDriver();} else if(browser.equalsIgnoreCase("chrome")) { System.setProperty("webdriver.chrome.driver","./chromedriver"); driver=new ChromeDriver();} else throw new Exception("browser name is not valid!"); } }
SampleTest.java
package mypckg; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.ie.InternetExplorerDriver; import org.testng.annotations.*; import mypckg.BaseTest; public class SampleTest extends BaseTest{ @Test public void Login() { driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("https://www.saucedemo.com/"); WebElement uname = driver.findElement(By.id("user-name")); uname.sendKeys("standard_user"); WebElement password = driver.findElement(By.id("password")); password.sendKeys("secret_sauce"); driver.findElement(By.id("login-button")).click(); String url = driver.getCurrentUrl(); if (url.contains("inventory")) { System.out.println("test case: PASS"); } } }
testNG.xml should be something like:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="TestSuite" thread-count="2" parallel="tests" > <test name="ChromeTest"> <parameter name="browser" value="chrome" /> <classes> <class name="mypckg.SampleTest"/> </classes> </test> <test name="FirefoxTest"> <parameter name="browser" value="firefox" /> <classes> <class name="mypckg.SampleTest"/> </classes> </test> <test name="IETest"> <parameter name="browser" value="safari" /> <classes> <class name="mypckg.SampleTest"/> </classes> </test> </suite>
Now right click on the testng.xml and run the test.
You will see all the browsers launched parallel and run the login test.
Thanks Sunil…Very brief explanation & Notes.