![Cross browser testing in Selenium Webdriver](https://i0.wp.com/learn-automation.com/wp-content/uploads/2015/08/Cross-browser-testing-in-Selenium-Webdriver.png?resize=525%2C333&ssl=1)
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
Verify the output.
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
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
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://learn-automation.com/use-firefox-selenium-using-geckodriver-selenium-3/
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??
Hi Amartya,
Under test tag, you can another parameters as per your requirement.
Hi Mukesh,
How can we tackle the situation when an application behaves differently in different browser?
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.
your videos are excellent. – try to go little slow.
Hi Krishna,
Sure…:)
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
Hi Satveer,
Is it happening with other urls’ too?
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
Hi Mathi
Use webdriver wait for ElementToBeClickable inside fluent wait. If this doesn’t work the try with JavaScript click action.
Hi Mathi
Use webdriver wait for ElementToBeClickable inside fluent wait. If thsi doesn’t work the try with JavaScript click action
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.
Hi Ashish,
I will upload videos of WebdriverIO soon…:)
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
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.
Okay Great!. Thanks a lot. Through the help of your tutorials I have learnt TestNG and many more functions..,Thanks a lot
Hi Satveer,
You’re welcome…:)
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
===============================================
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.
Okay Thanks Mukesh. But Could you please suggest me to to solve this issue so I can run successfully my script.
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
if I can setup path gecko driver then it opens the browser but not get the url
Hi Satveer,
Use latest version of Selenium with latest gecko driver but use 1 or 2 previous version of firefox and try.
Hi Satveer,
Kindly check this link http://learn-automation.com/use-firefox-selenium-using-geckodriver-selenium-3/
Hi Satveer,
Provide browser parameter through testng.xml or through vm arguments…
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!!
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…:)
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();
}
}
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.
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…
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.
Thank you so much for clearing my doubt.
Hi Richa,
You are always welcome…:)
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.
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.
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?
Hi Upkar,
Please send your code to my email I will cross check mukeshotwani@learn-automation.com
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
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.
. Thanks for sharing this video
You are always welcome…:)
Thanks for explaining cross browser testing in easy words.
Welcome Mohinni, Keep learning.
Thanks Mukesh.
Hi Vina,
Your comments are driving force for me.
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
Dont call the quit browser in after method always call @AfterSuite
Thanks Never knew it was so easy to do cross browser testing… thanks for explaining in the simplest way
Gaurav….Thanks for your comments.
its Very goood , Really helpfull..
Tysm…..
I completely learn automation from your videos. No attend any classes or tutorials. It’s Very helpful. Tysm Mukesh
Thanks Shailesh I am glad you liked it. Keep visiting.
Getting the below error:
Parameter ‘browser’ is required by @Test on method Verify_Title but has not been marked @Optional or defined
Hi Nikhil,
You have not accepted parameter in @Test kindly check code and video again to fix this issue.
Mukesh I like your all the tutorials. Just dam explanation. Thanks the work doing for the people/candidates.
Thank you so much Navin 🙂
really nice video. it helps me alot.
thanks
Thanks Iswarya, Keep visiting.
Thank you sir 🙂
Most welcome Aayushi
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
Hey Aayushi,
Yes correct but recently Selenium has some changes. Kindly check below article to fix ff issue http://learn-automation.com/use-firefox-selenium-using-geckodriver-selenium-3/
Hello Sir..
Am not able to run the Firefox code in the Chrome driver by using the framework
Hi Mahan,
I got confused with your question 🙁 Kindly frame it again.
ThankYou So Much sir For your Video…It’s really Helpful…………………………..
Welcome Harini 🙂 Keep visiting and let me know if any help.
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.
HI Manisha,
You can check below article and make the changes in IE before execution.
its working……. thanks a lot.
Cheers Manisha
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
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);
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.
Thanks Shah 🙂 Edge browser not tried as such.
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
Hi johnson,
Seems like some issue with testng.xml file. Please cross check once or send me testng.xml to me for checking.
Thanks for the clear explanation about cross browser testing
Thanks Sriram
its goood , helpfull
Hi Vijay,
Thank you.
Please check other articles too and let me know if any help needed from my side 🙂
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 ?
Hi Alunkan,
Can you please share the application as well because I tried now and it is worked as expected.
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?
Hi Raja,
You can keep only browser initialization code inside if statement to reduce piece of code.
Thanks
Mukesh
Thanks friend for your knowledge sharing.
thanks
Again sorry, It is working perfectly. Thanks for your knowledge sharing.
Thanks Raja
Sorry, It is fixed. this is because of small typo mistake in .xml file.