Selenium supports only web-based applications and to open them we need a browser. Selenium can support various browser for test automation.
In this tutorial, we will explain how to set up drivers for the different browsers available in the market.
Recommended IPTV Service Providers
- IPTVGREAT – Rating 4.8/5 ( 600+ Reviews )
- IPTVRESALE – Rating 5/5 ( 200+ Reviews )
- IPTVGANG – Rating 4.7/5 ( 1200+ Reviews )
- IPTVUNLOCK – Rating 5/5 ( 65 Reviews )
- IPTVFOLLOW -Rating 5/5 ( 48 Reviews )
- IPTVTOPS – Rating 5/5 ( 43 Reviews )
In the current industry, there are only three popular browsers which are widely used i.e. Google Chrome, Mozilla Firefox and Internet Explorer. However, Selenium supports other browsers as well. To execute our script on different browsers we need the driver of that browser.
=> Read all Selenium Tutorials Here
What You Will Learn:
How to Set Up Drivers for Different Browsers
Recommended Tools:
#1) CrossBrowserTesting
Easily take your existing Selenium scripts and run them on over 2050 real desktop and mobile browsers. Or check out their Record & Replay feature, allowing you to record a live test and run that recorded test in parallel.
#2) LambdaTest
Perform Selenium automation testing on a scalable, secure, and reliable cloud-based Selenium Grid online. Accelerate your Selenium test automation at the same time increase your test coverage by testing on an online infrastructure of 2000+ desktop and mobile browser environments.
When we first start with Selenium automation our very first line of code comes as:
WebDriver driver = new FireFoxDriver ();
It means that WebDriver is an interface and we are defining a reference variable (driver) whose type is an interface.
Now, any object we assign to it must be an instance of a class (FireFoxDriver) or any other drivers that implement that interface. In our case, FireFoxDriver is a class and interface is WebDriver.
When all our driver setup is done we execute any Selenium command such as:
driver.getTitle ();
Refer screenshot:
What happens now is that internally an HTTP request is created and sent to the specific browser driver that we defined, the browser driver uses that HTTP server for getting the HTTP requests and it determines the steps needed for implementing the Selenium command.
Our created logic is executed on the browser, then the execution result is sent back to the HTTP server and it again sends back the status to the automation script.
Thus, after setting the driver we can access all the in-built methods of driver’s class like:
- findElement();
- close();
- getClass(); and many more
Refer Screenshot:
To access these methods, type “driver.” in the editor and it will show all methods or else you can press “ctrl+space” and it will show you the methods.
Refer screenshot:
Sometimes built-in methods are not accessible when you press “ctrl+space”. Then you need to check your JAVA_HOME path settings made in Environment Variable and make sure they are correct.
Steps to set up Environment Variable:
- Go to Control Panel -> Click System
- Go to Advance System Settings
- Click Environment Variables button
- Set JAVA_HOME path on clicking the new button.
Selenium comes with default Mozilla Firefox driver which is bundled in Selenium WebDriver jar file. That’s why for calling Firefox driver, no setup is required. If we want to use other browsers, we need to set up its system property.
Recommended read => Cross Browser Testing using Selenium Grid
Cross-browser Testing Using Selenium WebDriver
Now, we will see the setup and execution of drivers in the below-mentioned browsers:
#1) Mozilla Firefox
#2) Google Chrome
#3) Internet Explorer
#4) Opera
#5) Ghost Driver or PhantomJS
#6) HTML Unit
The drivers for the mentioned browsers (except PhantomJS and HTML Unit – check below for these) can be downloaded from here: SeleniumHQ
Assuming you all are aware of the different browsers mentioned above, I will now explain what is Ghost driver and HTML Unit driver functionality and how to set them up for your script.
#1) HTML Unit Driver
Using this driver we can do Headless Browser Testing which means there is no GUI for it that you can see as it runs internally. And, you cannot perform all operations as you do in normal browsers.
Generally, for testing, HTML Unit Driver is not recommended. However, we can use it as it is faster, most lightweight implementation of WebDriver is used for generating test data, to pass the content of one webpage to other program or script.
For using HTML Unit Driver there is no need to install any additional APIs or jar files. You can use it once you have Selenium server standalone jar file.
Refer below code:
//Create a Java Project, under it create a package, and under package create a class packageheadless_browser_testing; import org.openqa.Selenium.WebDriver; importorg.openqa.Selenium.htmlunit.HtmlUnitDriver; import org.testng.Assert; import org.testng.annotations.Test; publicclassvefifyTestTitle { //You can run your script with TestNG or JUnit or using Java Application // I am using TestNG and using TestNG annotations @Test publicvoidverifyFacebookTitle() { //Call HtmlUnit Driver WebDriver driver = newHtmlUnitDriver(true); //It will get the Facebook URL and run the script in background, means you //will not see the Facebook page driver.get("http://www.facebook.com"); //It will fetch the FB title and store in String String facebook_Title= driver.getTitle(); //Assert condition will check the expected and actual title, if it matches //our test passes Assert.assertTrue(facebook_Title.contains("Facebook")); System.out.println(facebook_Title); } }
Output: Facebook – Log In or Sign Up
PASSED: verifyFacebookTitle
HTML Unit driver is not recommended for complex applications and which uses jquery or javascript or HTML 5. By default, it does not support javascript. Hence, you have to give condition true to support it.
#2) PhantomJS Driver
PhantomJS browser is also used for performing Headless Browser Testing. It uses a JavaScript API. You can use it for Headless Website Testing and access webpages. One advantage over HTML Unit Driver is that it can capture screenshots. It means your test will run in the background and will capture the screenshots.
In order to use PhantomJS browser with Selenium WebDriver, we have to use and download GhostDriver. It is an implementation of WebDriver wire protocol in simple JS for PhantomJS browser. Now in the latest release of PhantomJS they had integrated GhostDriver with PhantomJS. Thus, we don’t have to install it separately now.
Download PhantomJs.exe file from here: PhantomJs
To execute the PhantomJS, we require PhantomJS driver. Download link: PhantomJS driver
And we need to set PhantomJs.binary.path property file, when we execute the script.
Refer below code:
//Create a Java Project, then under it create a package, under package create a class packageheadless_browser_testing; import java.io.File; import org.openqa.Selenium.WebDriver; import org.openqa.Selenium.phantomjs.PhantomJSDriver; import org.testng.annotations.Test; publicclass phantom_Js_Driver { //You can run your script with TestNG or JUnit or using Java Application // I am using TestNG and using TestNG annotations @Test publicvoidverifyFacebookTitle() { //Set the path to access the phantomjs.exe file File src = newFile("E:\exe\phantomjs-2.1.1-windows\bin\phantomjs.exe"); //You need to specify the property here and give path of driver System.setProperty("phantomjs.binary.path", src.getAbsolutePath()); //Call your PhantomJs Driver WebDriver driver = newPhantomJSDriver(); //It will get the Facebook URL and run the script in background, means you //will not see the Facebook page driver.get("http://www.facebook.com"); //Print the currentURL of the page System.out.println(driver.getCurrentUrl()); } }
Output: https://www.facebook.com/
PASSED: verifyFacebookTitle
#3) Mozilla Firefox Driver
How to run WebDriver in Firefox browser:
For calling Firefox Driver there is no need to install or configure additional jar files. It is the default driver that Selenium WebDriver supports.
Refer below code for execution:
package Different_Drivers; import org.openqa.Selenium.WebDriver; import org.openqa.Selenium.firefox.FirefoxDriver; import org.testng.annotations.Test; import org.testng.Assert; public class FF_Driver { @Test public void Test_Gmail_Login() { WebDriver driver = new FirefoxDriver(); driver.get(“http://www.gmail.com”); driver.findElement(By.id("Email")).sendKeys("Enter user name"); driver.findElement(By.id("next")).click(); Thread.sleep(2000); driver.findElement(By.id("Passwd")).sendKeys("Enter Password"); driver.findElement(By.id("signIn")).click(); Thread.sleep(2000); String title_Of_Page = driver.getTitle(); Assert.assertEquals(driver.getTitle(), title_Of_Page); System.out.println("Page title matched"); } }
Output: Page title matched
PASSED: Test_Gmail_Login
#4) Google Chrome Driver
How to Run WebDriver in Chrome browser:
For calling Google Chrome Driver, first download the driver then set system property using below code:
package Different_Drivers; import org.openqa.Selenium.WebDriver; import org.openqa.Selenium.chrome.ChromeDriver; import org.testng.annotations.Test; importjava.util.Iterator; import java.util.Set; public class googleChrome_Driver { @Test public void Test_Rediff_Alert()throws InterruptedException{ //set system property, so that we can access chrome driver System.setProperty("webdriver.chrome.driver", "E:\chromedriver.exe"); // It will open the Chrome browser and execute your logic WebDriverdriver = new ChromeDriver(); //Open rediff page in chrome browser driver.get(“http://www.rediffmail.com”); //wait for page to load Thread.sleep(5000); // It will get and store the main window page handle or id String mainpage = driver.getWindowHandle(); String subwinhandleString = null; //set a loop which will store all window pop up handles Set<String> handle = driver.getWindowHandles(); Iterator<String> iterator = handle.iterator(); while(iterator.hasNext ()) { subwinhandleString = iterator.next( ); } driver.switchTo().window(subwinhandleString); System.out.println(driver.getTitle()); Thread.sleep(2000); driver.close(); //Again switch back to main window driver.switchTo().window(mainpage); System.out.println(driver.getTitle()); } }
Output: Welcome to rediff.com
Rediff.com: Online Shopping, Rediffmail, Latest India News, Business, Bollywood, Sports, Stock, Live Cricket Score, Money, Movie Reviews
PASSED: Test_Rediff_Alert
Also read => Selenium tutorial – Locate Elements in Chrome and IE Browsers
#5) Internet Explorer Driver
How to Run WebDriver in IE browser:
For calling Internet Explorer Driver, download the driver and set a system property.
Refer below code:
package Different_Drivers; import org.openqa.selenium.WebDriver; import org.openqa.selenium.ie.InternetExplorerDriver; import org.openqa.selenium.remote.DesiredCapabilities; import org.testng.annotations.Test; public class&nbsp; internetExplorer_Driver { @Test public void ieDriver() throws InterruptedException { //set system property, so that we can access IE driver System.setProperty("webdriver.ie.driver","E\IEDriverServer.exe"); //set desiredcapabilites for calling ie driver DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer(); capabilities.setCapability (InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true); WebDriver driver = new InternetExplorerDriver(capabilities); driver.get("https://www.google.com"); Thread.sleep(5000); String title_Of_Page = driver.getTitle(); System.out.println(title_Of_Page); } }
Output: Google
PASSED: ieDriver
#6) Opera Driver
For calling Opera Driver, download the driver and set system property.
Refer below code:
package Different_Drivers; import org.openqa.Selenium.WebDriver; importorg.openqa.Selenium.opera.OperaDriver; import org.testng.annotations.Test; import org.testng.Assert; public class operaDriver { @Test public void createAccount(){ //set system property, so that we can access opera driver System.setProperty("webdriver.opera.driver", "E:\operadriver.exe"); // it will open the opera browser WebDriver driver = newOperaDriver(); driver.get("https://www.google.com/intl/en/mail/help/about.html"); // Here driver will try to find out create an account link on the application WebElement createAccount = driver.findElement(By.xpath(".//*[@id='gmail-create-account']")); Assert.assertTrue(createAccount.isDisplayed()); //Create Account will be clicked only if the above condition is true createAccount.click(); System.out.println(createAccount.getText()); } }
Output: Create an account
PASSED: operaDriver
Also read => TestNG Annotations in Selenium
Conclusion
In this tutorial, I covered how to setup and use drivers for various browsers.
We saw how HTML Unit Driver and PhantomJS Driver are different from other commonly used browsers. The reason behind this is that they are not used for common browsing such as Google Chrome and others; instead, they work internally and execute our commands faster as it does not support GUI. We can also perform server-side scripting with PhantomJS.
Now that we have understood how to setup drivers and get them working on various browsers, we need to be clear why we are doing this. As you are all aware Selenium supports only web-based applications and to open them we need a browser.
There are various drivers (discussed above) available to open these browsers. A WebDriver is an interface which contains all the abstract methods defined in it. Hence, we call these methods that are discussed in this tutorial to perform our tasks.
Let us know if you have any queries/comments about this tutorial.