Welcome to Selenium Webdriver Free Training. Last time, we worked with iframes and checked how to switch between 2 different iframes.
In this tutorial, we are going to write a script to launch 2 different browsers like Chrome or Firefox. In this script, we are not going to launch browsers in parallel mode. So, the script will launch browsers in sequence. To achieve parallelism, you can create a script with the TestNG framework that we will discuss later.
What is Cross Browser Testing?
Cross-browser testing is the process of testing a website on different browsers.
Why do we need Cross Browser Testing?
Web-based applications are not the same as Windows applications. A web application can be opened in any browser by the end user. For example, some people prefer to open https://www.qaonlinetraining.com in the Firefox browser, while others can be using Chrome browser or Edge.
Selenium Webdriver Script Actions
- Chrome browser with compatible chrome driver.
- Firefox browser with compatible firefox driver.
So, we will fill up the form and leave one required field. After submitting the form, we should get an error message.
Here are the tasks that we need to perform by selenium script.
- Open http://training.qaonlinetraining.com/testPage.php page.
- Enter name.
- Enter email.
- Enter website
- Enter comment.
But if you see the http://training.qaonlinetraining.com/testPage.php page where we have 4 required fields
- Name
- Gender
- Country
Top 2 out of these required fields, we will provide the inputs and the last Country will take the USA as the default value. But we are not providing anything in Gender, it means we should get “Gender is required” message and this is what we are going to check and at last, close the browser.
Live script demo
Webdriver Script to performs Cross Browser testing
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; public class MultiBrowser { public static void performOperation(WebDriver driver) throws InterruptedException { driver.get("http://training.qaonlinetraining.com/testPage.php"); driver.findElement(By.name("name")).sendKeys("DemoName"); System.out.println("name\")).sendKeys(\"DemoName"); // E mail section driver.findElement(By.name("email")).sendKeys("DemoEmail@demo.com"); System.out.println("email\")).sendKeys(\"DemoEmail@demo.com"); // Website section driver.findElement(By.name("website")).sendKeys("www.demosite.com"); System.out.println("website\")).sendKeys(\"www.demosite.com"); // Comment section driver.findElement(By.name("comment")).sendKeys("demo comment"); System.out.println("comment\")).sendKeys(\"demo comment"); driver.findElement(By.name("submit")).click(); Thread.sleep(1000); WebElement gendererr = driver.findElement(By.xpath("/html/body/form/span[4]")); String errtext = gendererr.getText(); if(errtext.contains("Gender is required")) { System.out.println("Got gender required error."); }else { System.out.println("Form functionality is not working correctly."); } driver.close(); } public static void main(String[] args) throws InterruptedException { // TODO Auto-generated method stub // Chrome browser System.setProperty("webdriver.chrome.driver", "c:\\xampp\\chromedriver_win32\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); performOperation(driver); // Firefox browser System.setProperty("webdriver.gecko.driver", "C:\\xampp\\geckodriver\\geckodriver.exe"); driver = new FirefoxDriver(); performOperation(driver); } }
Script Explanation
If you see the script, we defined one performOperation(WebDriver driver) method, this method has common functionality that we need to perform through both browser. So, this method is performing following
- Enter the name.
- Enter the Email.
- Enter website
- Enter comment.
- And click on submit button.
Instructor-led Training
Important note: we like to tell you that free tutorials are useful to get started but if you are interested in the best online LIVE Master of Automation Testing training program from the experts, please refer to the following link:
For Instructor-led training
Master of Functional Automation Testing
For Self-Driven training
Conclusion
So in this way, you can perform multi browser testing for starting. To achieve parallelism, you can work with TestNG. In the next tutorial, we will talk about how to test with radio and checkbox.
Until then, Happy learning 🙂