Sunday 17 July 2016

Selenium2: Hello World application

In this post, I am going to explain, How to open a url in browser using WebDriver instance.
Following application opens the gmail url in Firefox browser.

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class App {
 public static void main(String[] args) {
  WebDriver driver = new FirefoxDriver();
  driver.get("http://gmail.com");
 }
}

WebDriver driver = new FirefoxDriver();
Above statement instantiate FirefoxDriver class, which is Firefox specific implementation of WebDriver interface. Following table summarizes various browser implementations of WebDriver interface.

Browser
Implementation class
InternetExplorerDriver
Driver specific to Internet Explorer
ChromeDriver
Driver specific to Chrome browser
SafariDriver
Driver specific to Safari browser
AndroidDriver
A driver for running tests on an Android device or emulator.
IPhoneDriver
Driver for running tests on Mobile Safari on the iPhone, iPad and iPod Touch.
EdgeDriver
Driver specific to Edge browser.

Following are the implementation classes, you need to import while working with specific browser.

import org.openqa.selenium.android.AndroidDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.iphone.IPhoneDriver;
import org.openqa.selenium.safari.SafariDriver;

driver.get("http://gmail.com");
Above statement loads given url in the current browser window. If the browser is not already opened, it launch the new fire fox browser (If you are using ChromeDriver instance, it launch Chrome browser etc.,).

Note
a.   IPhoneDriver is deprecated, you can use ios-driver or appium instead
b.   AndroidDriver is deprecated, you can use either http://selendroid.io or http://appium.io instead.









Previous                                                 Next                                                 Home

No comments:

Post a Comment