|

Cross Browser Testing – Selenium Free Training

Welcome to Selenium Webdriver Free Training. Last time, we worked with iframes and checked how to switch between 2 different iframes.

Read How to handle iframes using Selenium webdriver?

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.

keepersecurity.com
keepersecurity.com

Selenium Webdriver Script Actions

So by this script, we are going to open the same page on Chrome and Firefox browsers and perform the same operation on both.
Therefore, what are the main requirements of this script?
  1. Chrome browser with compatible chrome driver.
  2. 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.

  1. Open http://training.qaonlinetraining.com/testPage.php page.
  2. Enter name.
  3. Enter email.
  4. Enter website
  5. Enter comment.
And click on submit button.

But if you see the http://training.qaonlinetraining.com/testPage.php page where we have 4 required fields

  1. Name
  2. Email
  3. Gender
  4. 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.

GotPrint
GotPrint

Read How to handle Checkbox and Radio button?

Live script demo

So, here is the demo of what we are going to do:

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);
}
}

Read How to handle Alerts using Selenium Webdriver?

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

  1. Enter the name.
  2. Enter the Email.
  3. Enter website
  4. Enter comment.
  5. 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

Edureka - Month End Offer-Flat 30% OFF ON Live Course
Edureka - Month End Offer-Flat 30% OFF ON Live Course

Automation with Selenium WebDriver (Java)

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 🙂

Similar Posts