Advance Selenium

Cross Browser Testing using Selenium Webdriver

Cross browser testing in Selenium Webdriver

Hello Welcome to Selenium tutorial, today we will discuss Cross Browser Testing using Selenium Webdriver.

What is Cross browser testing?

Cross browser, testing refers to testing the application in multiple browsers like IE, Chrome, Firefox so that we can test our application effectively.IE, Chrome, Firefox so that we can test our application effectively.

Cross browser, testing is a very important concept in Automation because here the actual automation comes into the picture.

Example- Suppose if you have 20 test cases that you have to execute manually, so it is not a big deal right we can execute in 1 day or 2 days. However, if the same test cases you have to execute in five browsers it means 100 test cases then probably you will take one week or more than one week to do the same and it will be quite boring as well.

If you automate these 20 test cases and run them then it will not take more than one or two hour depends on your test case complexity.

What is the need of Cross Browser Testing using Selenium Webdriver

For the better experience, we need to do cross browser testing so that customer will get the same UI of application even if he use different or any browser.

Let me list down a few reasons why we should perform cross browser testing

1- Browser compatibility with different OS.

2- Image orientation

3- Each browser has the different orientation of Javascript which can cause issue sometimes.

4- Font size mismatch or not rendered properly.

5- Compatibility with the new web framework.

 

Cross Browser Testing using Selenium Webdriver

To achieve this we will use TestNG parameter feature, we will pass the parameter from TestNG.xml file, and based on our parameter Selenium will initiate our browsers.

In this scenario, we will run the same test case with two different browser parallel.

Step 1- Write testcase

package SampleTestcases;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class TestCase1 {

@Test

// Here this parameters we will take from testng.xml
@Parameters("Browser")
public  void test1(String browser) {

if(browser.equalsIgnoreCase("FF")){

WebDriver driver=new FirefoxDriver();

driver.manage().window().maximize();

driver.get("http://www.facebook.com");

driver.quit();

}
else if(browser.equalsIgnoreCase("IE")){

System.setProperty("webdriver.ie.driver", "./server/IEDriverServer.exe");

WebDriver driver=new InternetExplorerDriver();

driver.manage().window().maximize();

driver.get("http://www.facebook.com");

driver.quit();
}
}

}

 

Step 2- Create testng.xml and specify

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
Here parallel is an attribute which specify the mode of execution and thread-count specify how many browser should open
<suite name="Suite" parallel="tests" thread-count="2">

<test name="Test">

<parameter name="Browser" value="FF" />

<classes>

<class name="SampleTestcases.TestCase1"/>

</classes>

</test>

<test name="Test1">

<parameter name="Browser" value="IE" />

<classes>

<class name="SampleTestcases.TestCase1"/>

</classes>

</test>

</suite>

 

Step 3- Run this xml file refer the below screenshot.

Note- To create testng.xml- Right, click on your testcase then go to TestNG then convert to TestNG> It will generate testng.xml then make changes as per above xml file and finish. You will get testng.xml file inside the project

 

Cross Browser Testing using Selenium Webdriver

Verify the output.

 

 

Cross Browser Testing using Selenium Webdriver

 

Note- For Cross Browser Testing using Selenium Webdriver you have to execute through testng.xml only.

Thanks for visiting my blog. Keep in touch.

Have a nice day 🙂

For More updates Learn Automation page

For any query join Selenium group- Selenium Group

author-avatar

About Mukesh Otwani

I am Mukesh Otwani working professional in a beautiful city Bangalore India. I completed by BE from RGPV university Bhopal. I have passion towards automation testing since couple of years I started with Selenium then I got chance to work with other tools like Maven, Ant, Git, GitHub, Jenkins, Sikuli, Selenium Builder etc.

91 thoughts on “Cross Browser Testing using Selenium Webdriver

  1. Pratiksha nigade says:

    Hello mukesh I have one doubt why you don’t give the sysytem.set.property for Firefox. And you did not give the system.set.property still how it is executing

    1. Hi Pratiksha,

      This is pretty much old video. At that time, Firefox doesn’t require any driver but now it requires geckodriver. Please refer this link https://vistasadprojects.com/mukeshotwani-blogs-v2/use-firefox-selenium-using-geckodriver-selenium-3/

  2. Amartya says:

    Hi Mukesh
    In this video you have discussed how to pass to pass single parameter.Can I get the link the video where you have discussed multiple parameters??

    1. Hi Amartya,

      Under test tag, you can another parameters as per your requirement.

  3. saurabh misra says:

    Hi Mukesh,

    How can we tackle the situation when an application behaves differently in different browser?

    1. Hi Saurabh,

      Usually locator selection matters most where CSS Selector is preferable to work with all browser. For other Ui changes, try to find locator best DOM property which shopuld most likely to work. Apart from this, we have to handle specifically for each browser.

  4. KRISHNA MOJUMDER says:

    your videos are excellent. – try to go little slow.

    1. Hi Krishna,

      Sure…:)

  5. SATVEER BAJWA says:

    Hi Mukesh,

    I am using testing for cross browser testing but for Mozilla I am getting this error :
    org.openqa.selenium.WebDriverException: Timed out waiting 45 seconds for Firefox to start. I put this driver.manage().timeouts().pageLoadTimeout(50,TimeUnit.SECONDS); before get page. Please help me to solve this issue

    1. Hi Satveer,

      Is it happening with other urls’ too?

  6. Mathi says:

    Hi Mukesh,
    I am trying to automate my project application. i need to click on the right arrow button the application. I got the below error “element is not clickable at point because another element obscures it”
    Exception Name: org.openqa.selenium.ElementClickInterceptedException

    When i use the particular code in different class, then the element is clicking.
    When i run the code the from the login page, the element is not clicking instead of the error is displayed.

    Kindly help me in the scenario.

    Thanks,
    Mathi

    1. Hi Mathi

      Use webdriver wait for ElementToBeClickable inside fluent wait. If this doesn’t work the try with JavaScript click action.

    2. Hi Mathi

      Use webdriver wait for ElementToBeClickable inside fluent wait. If thsi doesn’t work the try with JavaScript click action

  7. Ashish says:

    Hi Mukesh,

    Can you help me out regarding webdriverIO + cucumber set up (configuration on windows) OR please try to add a tutorial regarding this on your site.

    1. Hi Ashish,

      I will upload videos of WebdriverIO soon…:)

  8. SATVEER BAJWA says:

    Hi Mukesh,

    Hope you are doing great. I would like to ask you one question regarding selenium with java. I wanted to run multiple test cases in one class but I don’t want to write all testcases again and again like for same function signup. Is there any way to overcome the rewrite code.

    Thanks
    Satveer

    1. Hi Satveer,

      Yes, you don’t need to write same lines of code again and again. Better you create reusable method for same and call it.

      1. SATVEER BAJWA says:

        Okay Great!. Thanks a lot. Through the help of your tutorials I have learnt TestNG and many more functions..,Thanks a lot

        1. Hi Satveer,

          You’re welcome…:)

  9. SATVEER BAJWA says:

    Hi Mukesh,
    I am following your video above mentioned but not able to run successfully, getting this error when running my script

    [Utils] [ERROR] [Error] org.testng.TestNGException:
    Parameter 'Browser' is required by @Test on method verifypagetitle but has not been marked @Optional or defined
    in C:\Users\satveer\AppData\Local\Temp\testng-eclipse-2070363565\testng-customsuite.xml
    FAILED: verifypagetitle

    ===============================================
    Default test
    Tests run: 1, Failures: 1, Skips: 0
    ===============================================

    1. Hi Satveer,

      As per this statement Parameter ‘Browser’ is required by @Test on method verifypagetitle but has not been marked @Optional or defined*, it requires Browser parameter to be passed as VM argumnets or pass it from testng.xml.

      1. SATVEER BAJWA says:

        Okay Thanks Mukesh. But Could you please suggest me to to solve this issue so I can run successfully my script.

        1. SATVEER BAJWA says:

          Hi Mukesh,
          Thanks for your help. Yes it works for me now. but it runs only internet explorer not Firefox there is error in console
          java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see https://github.com/mozilla/geckodriver. The latest version can be downloaded from https://github.com/mozilla/geckodriver/releases

          1. SATVEER BAJWA says:

            if I can setup path gecko driver then it opens the browser but not get the url

          2. Hi Satveer,

            Use latest version of Selenium with latest gecko driver but use 1 or 2 previous version of firefox and try.

        2. Hi Satveer,

          Provide browser parameter through testng.xml or through vm arguments…

  10. Alejandro says:

    Excellent web site you have got here.. It’s difficult to find good quality writing
    like yours nowadays. I truly appreciate people like you!
    Take care!!

    1. Hi Alejandro,

      Very very thanks alot for your valuable comments and appreciation. I’ll try my level best to keep my blog readers update with new topics.
      Please be in touch…:)

  11. Richa Binani says:

    Hy Mukesh, I am having the issue in very simple code:-
    driver.quit(); is not closing all the windows I have opened, its working like driver.close();
    Please help

    Code:-

    package practice;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;

    public class Demo {
    WebDriver driver;
    public void open() throws InterruptedException
    {
    System.setProperty(“webdriver.chrome.driver”,”C:\\Users\\gten-
    008\\Downloads\\jars\\chromedriver_win32\\chromedriver.exe”);
    driver=new ChromeDriver();
    driver.get(“https://www.facebook.com/”);
    }
    public void go()
    {
    driver=new ChromeDriver();
    driver.get(“https://www.google.com/”);
    }
    public void hide()
    {
    driver.quit();
    }
    public static void main(String[] args) throws InterruptedException {
    demo d=new demo();
    d.open();
    d.go();
    d.hide();
    }
    }

    1. Hi Richa,

      In your code. you have initialized same webdriver object twice in different methods that is the reason, one window is always getting closed. driver.quit() always close the instance of window which it is holding at that time.

      1. Richa Binani says:

        Then what should I do if I want to open 2 different browsers or 2 different windows of the same browser and want to close them altogether?
        Driver.quit(); will be useful in this case?
        Please tell…

        1. Hi Richa,

          In your case, you need to have two driver objects of same browser like driver_1 & driver_2 and call driver_1 & driver_2 quit() methods separately.

          1. Richa Binani says:

            Thank you so much for clearing my doubt.

          2. Hi Richa,

            You are always welcome…:)

  12. upkar singh says:

    Can we run multiple browser parallely, because i run the same and in my case it launch both browser but script run only in one browser.

    1. Hi Upkar,

      If you have mentioned test class twice, each with different browser along with parallel and thread-count values then it should work properly. Kindly cross verify testng.xml file contents.

      1. Upkar Singh says:

        Hi Mukesh,

        Thank you for your response, actually i’m new to automation from my end i think it’s right, can you please look up to my code?

        1. Hi Upkar,

          Please send your code to my email I will cross check mukeshotwani@learn-automation.com

  13. Tharanga says:

    Hi Mukesh,

    Need help from you. I watched your video and getting this error

    [RemoteTestNG] detected TestNG version 6.14.2
    org.testng.TestNGException:
    Cannot find class in classpath: com.CrossBrowser.CrossBrowser
    at org.testng.xml.XmlClass.loadClass(XmlClass.java:77)
    at org.testng.xml.XmlClass.init(XmlClass.java:69)
    at org.testng.xml.XmlClass.(XmlClass.java:55)
    at org.testng.xml.TestNGContentHandler.startElement(TestNGContentHandler.java:575)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.AbstractXMLDocumentParser.emptyElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.emptyElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)

    Please advice

    1. Hi Tharanga,

      Kindly check this line Cannot find class in classpath: com.CrossBrowser.CrossBrowser whether you have given correct name of file in testng.xml file. It should be package name with file name.

  14. NIlesh says:

    . Thanks for sharing this video

    1. You are always welcome…:)

  15. Mohinni says:

    Thanks for explaining cross browser testing in easy words.

    1. Welcome Mohinni, Keep learning.

  16. Vina says:

    Thanks Mukesh.

    1. Hi Vina,

      Your comments are driving force for me.

  17. srinivas says:

    Hi Mukesh,

    I have tried to execute the program which you explained in the video.For firefox it is working fine but for Chrome & IE it was not working , browsers are not even launched..The below is the error i got
    org.openqa.selenium.remote.SessionNotFoundException: The FirefoxDriver cannot be used after quit() was called.
    Build info: version: ‘2.53.1’, revision: ‘a36b8b1’, time: ‘2016-06-30 17:37:03’
    System info: host: ‘LENOVO-PC’, ip: ‘192.168.0.20’, os.name: ‘Windows 7’, os.arch: ‘amd64’, os.version: ‘6.1’, java.version: ‘1.8.0_111’
    Driver info: driver.version: RemoteWebDriver

    1. Dont call the quit browser in after method always call @AfterSuite

  18. Thanks Never knew it was so easy to do cross browser testing… thanks for explaining in the simplest way

    1. Gaurav….Thanks for your comments.

  19. Shailesh says:

    its Very goood , Really helpfull..
    Tysm…..

  20. Shailesh says:

    I completely learn automation from your videos. No attend any classes or tutorials. It’s Very helpful. Tysm Mukesh

    1. Thanks Shailesh I am glad you liked it. Keep visiting.

  21. Nikhil says:

    Getting the below error:
    Parameter ‘browser’ is required by @Test on method Verify_Title but has not been marked @Optional or defined

    1. Hi Nikhil,

      You have not accepted parameter in @Test kindly check code and video again to fix this issue.

  22. Navinkumar Choudhary says:

    Mukesh I like your all the tutorials. Just dam explanation. Thanks the work doing for the people/candidates.

    1. Thank you so much Navin 🙂

  23. iswarya says:

    really nice video. it helps me alot.
    thanks

    1. Thanks Iswarya, Keep visiting.

  24. Aayushi says:

    Thank you sir 🙂

    1. Most welcome Aayushi

  25. Aayushi says:

    hello sir,

    my code is not running in firefox browser, when i gave the command to open facebook page,its simply open the browser but not the particular page. I used the same code as you mentioned

    1. Hey Aayushi,

      Yes correct but recently Selenium has some changes. Kindly check below article to fix ff issue https://vistasadprojects.com/mukeshotwani-blogs-v2/use-firefox-selenium-using-geckodriver-selenium-3/

  26. Mahan says:

    Hello Sir..

    Am not able to run the Firefox code in the Chrome driver by using the framework

    1. Hi Mahan,

      I got confused with your question 🙁 Kindly frame it again.

  27. Harini says:

    ThankYou So Much sir For your Video…It’s really Helpful…………………………..

    1. Welcome Harini 🙂 Keep visiting and let me know if any help.

  28. manisha says:

    Hi sir,
    when i m running my script just watever you said in this crossbrowser testing video, mozilla and chorme are working fine but In internetExplorer, it is returning title as Webdriver and unable to close it .still my TestNG reports is showning like 3 test cases run successfully. i m unable to find the solution of it. plz help me out.

    1. HI Manisha,

      You can check below article and make the changes in IE before execution.

      1. manisha says:

        its working……. thanks a lot.

    2. May be you are using drv.close().

      With IE it does not work . Try using drv.quit();

      i tried with close() IE did not close but with quit it got closed

      1. Hi Gaurav

        driver.close() -> Close the browser window on which currently focus is lying
        driver.quit() -> Close all browser windows and ends driver session

        But sometimes on windows environment even though you quit driver but still you can observe IEDriverServer.exe & chromedriver.exe instance running in processes list. In order to kill these process, you can call java methods like WindowsUtils.killByName(processName) or WindowsUtils.killPID(processID);

  29. Shah says:

    Hi,Mukesh!
    I tried with Microsoft Edge,driver launch the browser but didn’t went to desired page,in output it shows configuration error=1,one test skips,any idea.
    Thank you for your great effort to serving the community.

    1. Thanks Shah 🙂 Edge browser not tried as such.

  30. U johnson says:

    HI mukesh i am getting the error while executing this code, can help me with this

    org.testng.TestNGException: org.xml.sax.SAXParseException; lineNumber: 3; columnNumber: 1; Content is not allowed in prolog.
    at org.testng.TestNG.initializeSuitesAndJarFile(TestNG.java:320)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:89)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:205)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:176)
    Caused by: org.xml.sax.SAXParseException; lineNumber: 3; columnNumber: 1; Content is not allowed in prolog.
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(Unknown Source)
    at javax.xml.parsers.SAXParser.parse(Unknown Source)
    at org.testng.xml.XMLParser.parse(XMLParser.java:38)
    at org.testng.xml.SuiteXmlParser.parse(SuiteXmlParser.java:16)
    at org.testng.xml.SuiteXmlParser.parse(SuiteXmlParser.java:9)
    at org.testng.xml.Parser.parse(Parser.java:172)
    at org.testng.TestNG.initializeSuitesAndJarFile(TestNG.java:300)
    … 3 more

    1. Hi johnson,

      Seems like some issue with testng.xml file. Please cross check once or send me testng.xml to me for checking.

  31. sriram says:

    Thanks for the clear explanation about cross browser testing

  32. vijay says:

    its goood , helpfull

    1. Hi Vijay,

      Thank you.
      Please check other articles too and let me know if any help needed from my side 🙂

  33. Alunkan says:

    Hi Mokesh, Thanks for your effort. it seems text box value inserting is not happening as expected while running browser Pararell.i have tried same (above code) and extend to insert values to text boxes..But result was unlucky for me. could you please help me on this ?

    1. Hi Alunkan,

      Can you please share the application as well because I tried now and it is worked as expected.

  34. Raja says:

    Hi mukesh, Here in the above example the same code is repeating in Firefox and IE, ri8. Is there any optimize way to reduce this?

    1. Hi Raja,

      You can keep only browser initialization code inside if statement to reduce piece of code.

      Thanks
      Mukesh

  35. Raja says:

    Thanks friend for your knowledge sharing.

  36. Raja says:

    Again sorry, It is working perfectly. Thanks for your knowledge sharing.

  37. Raja says:

    Sorry, It is fixed. this is because of small typo mistake in .xml file.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.