Selenium is compatible with web-based applications and requires a browser to open them. Selenium can work with various browsers for test automation.
In this tutorial, we will explain how to set up drivers for different browsers used 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 three popular browsers widely used, namely Google Chrome, Mozilla Firefox, and Internet Explorer. However, Selenium also supports other browsers. To run our script on different browsers, we need the respective browser’s driver.
=> Read all Selenium Tutorials Here
What You Will Learn:
How to Set Up Drivers for Different Browsers
Recommended Tools:
#1) CrossBrowserTesting
You can easily take your existing Selenium scripts and run them on over 2050 real desktop and mobile browsers with CrossBrowserTesting. They also have a 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 with LambdaTest. Increase your test coverage by testing on an online infrastructure of 2000+ desktop and mobile browser environments.
When we start with Selenium automation, our first line of code looks like this:
WebDriver driver = new FireFoxDriver ();
This means that WebDriver
is an interface, and we are defining a reference variable (driver
) of that interface’s type.
Now, any object assigned to it must be an instance of a class (FireFoxDriver
) or any other driver that implements that interface. In this case, FireFoxDriver
is a class and WebDriver
is the interface.
Once the driver setup is complete, we can execute any Selenium command, for example:
driver.getTitle ();
Refer to the screenshot:
Internally, an HTTP request is created and sent to the specific browser driver that we defined. The browser driver uses that HTTP server to handle the request and determine the steps needed for implementing the Selenium command.
Our created logic is executed on the browser, and the execution result is sent back to the HTTP server. The server then sends back the status to the automation script.
After setting up the driver, we can access all the built-in methods of the driver’s class, such as:
findElement();
close();
getClass();
and many more
Refer to the screenshot:
To access these methods, type “driver.
” in the editor, and it will show all the available methods. You can also press “Ctrl+Space” to display the methods.
Refer to the screenshot:
Sometimes, built-in methods may not be accessible when you press “Ctrl+Space”. In that case, you need to check the JAVA_HOME path settings in the Environment Variable and make sure they are correct.
Steps to set up Environment Variables:
- Go to Control Panel -> Click System
- Go to Advanced System Settings
- Click the Environment Variables button
- Set the JAVA_HOME path by clicking the New button
Selenium comes with a default Mozilla Firefox driver, which is bundled in the Selenium WebDriver JAR file. Hence, no additional setup is required for calling the Firefox driver. However, for other browsers, we need to set up the respective system property.
Recommended read: Cross Browser Testing using Selenium Grid
Cross-browser Testing Using Selenium WebDriver
Now, let us see how to set up and execute drivers in the following 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) can be downloaded from SeleniumHQ.
Assuming you are already familiar with the mentioned browsers, let us now explain the functionality of the Ghost driver and HTML Unit driver and how to set them up.
#1) HTML Unit Driver
This driver allows us to perform Headless Browser Testing, which means there is no GUI for it that we can see as it runs internally. Also, not all operations that can be performed in normal browsers are available in this driver.
Generally, HTML Unit Driver is not recommended for testing. However, we can use it for its speed and lightweight implementation of WebDriver. It can be used to generate test data or transfer the content of one webpage to another program or script.
No additional APIs or JAR files need to be installed for using HTML Unit Driver. It can be used once the Selenium server standalone JAR file is available.
Refer to the code below:
//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 the background, without showing the Facebook page driver.get("http://www.facebook.com"); //It will fetch the Facebook title and store it in a String String facebook_Title= driver.getTitle(); //Assert condition will check the expected and actual title, if it matches the 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 that use jQuery, JavaScript, or HTML 5 as it does not support them by default. However, you can enable JavaScript support by setting the condition to “true”.
#2) PhantomJS Driver
PhantomJS browser is also used for performing Headless Browser Testing. It uses a JavaScript API and can be used for Headless Website Testing and capturing screenshots. It can run tests in the background while capturing screenshots.
To use PhantomJS browser with Selenium WebDriver, we need to download the driver and set up the system property. In the latest release of PhantomJS, the GhostDriver has been integrated with PhantomJS, so there is no need to install it separately.
You can download the PhantomJs.exe file from here: PhantomJs
The PhantomJS driver can be downloaded from the PhantomJS driver link.
We also need to set the PhantomJs.binary.path property when executing the script.
Refer to the code below:
//Create a Java Project, then under it create a package, under the 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; publicclassphantom_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:exephantomjs-2.1.1-windowsbinphantomjs.exe"); //You need to specify the property here and give the path of the 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 the background, without showing the Facebook page driver.get("http://www.facebook.com"); //Print the current URL 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 the Firefox Driver, there is no need to install or configure additional JAR files. It is the default driver supported by Selenium WebDriver.
Refer to the code below:
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:
To call the Google Chrome Driver, download the driver and set the system property using the code below:
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 the 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 Sethandle = driver.getWindowHandles(); Iterator 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
#5) Internet Explorer Driver
How to Run WebDriver in IE browser:
To call the Internet Explorer Driver, download the driver and set the system property.
Refer to the code below:
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  internetExplorer_Driver { @Test public void ieDriver() throws InterruptedException { //set system property, so that we can access the IE driver System.setProperty("webdriver.ie.driver","EIEDriverServer.exe"); //set desired capabilities 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
To call the Opera Driver, download the driver and set the system property.
Refer to the code below:
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 the 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, we have covered how to set up and use drivers for different browsers.
We have seen how the HTML Unit Driver and PhantomJS Driver differ from commonly used browsers. These drivers are not recommended for general browsing purposes like Google Chrome and others. They work internally and execute our commands faster as they do not support a GUI. PhantomJS can also be used for server-side scripting.
Now that we understand how to set up drivers and use them in different browsers, we need to remember that Selenium only supports web-based applications, and we need a browser to open them.
There are various drivers available to open these browsers. WebDriver is an interface that contains all the abstract methods defined in it. We can call these methods, as discussed in this tutorial, to perform our tasks.
If you have any queries or comments about this tutorial, please let us know.