Tuesday, 4 August 2015

How to run Chrome, IE, Safari, Opera and Mozilla Firefox on selenium webdriver?

import java.util.concurrent.TimeUnit; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 
 
public class MozillaFirefoxbrowser { 
 
    public static void main(String args[]) { 
        // Initialize driver     
        WebDriver dr = new FirefoxDriver(); 
        //Maximize browser window      
        dr.manage().window().maximize(); 
        //Go to URL     
        dr.get("http://www.google.com"); 
        //Set  timeout     
        dr.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
 
        //close firefox browser 
        dr.close(); 
    } 
 
}  

Internet Explorer
Download Internet Explorer Driver Server

import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.ie.InternetExplorerDriver; 
 
public class InternetExplorerBrowser { 
 
    public static void main(String args[]) { 
 
        //set the system property for Internet Explorer 
 //System.setProperty("webdriver.ie.driver", "Location of IE Driver"); 
  System.setProperty("webdriver.ie.driver", "IEDriverServer.exe"); 
 
        // Initialize IE driver   
        WebDriver driver = new InternetExplorerDriver(); 
        //Maximize browser window      
        driver.manage().window().maximize(); 
        //Go to URL    
        driver.get("http://www.google.com"); 
 
    } 
}  

Google Chrome
Download Chrome Driver Server

import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.chrome.ChromeDriver; 
import org.openqa.selenium.ie.InternetExplorerDriver; 


public class Chromebrowser { 
      public static void main(String args[]) { 
        //set the system property for Chrome 
//System.setProperty("webdriver.chrome.driver", "Location of Chrome Driver"); 
    System.setProperty("webdriver.chrome.driver", "chromedriver.exe"); 
 
        // Initialize IE driver   
        WebDriver driver = new ChromeDriver(); 
        //Maximize browser window      
        driver.manage().window().maximize(); 
        //Go to URL    
        driver.get("http://www.google.com"); 
    } 
 


Safari
Download Selenium WebDriver for Safari. Add jar file in your project

import java.util.concurrent.TimeUnit; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 
 
public class Safaribrowser { 
 
    public static void main(String args[]) { 
        // Initialize driver     
        WebDriver dr = new SafariDriver(); 
        //Maximize browser window      
        dr.manage().window().maximize(); 
        //Go to URL     
        dr.get("http://www.google.com"); 
        //Set  timeout     
        dr.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
 
        //close Safari browser 
        dr.quit(); 
    } 
 


Opera
import com.opera.core.systems.OperaDriver; 
import java.util.concurrent.TimeUnit; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.remote.DesiredCapabilities; 
 
public class Operabrowser { 
 
    public static void main(String args[]) { 
         
        DesiredCapabilities capabilities = DesiredCapabilities.opera(); 
//        capabilities.setCapability("opera.binary", " Absolute Path of Opera browser "); 
        capabilities.setCapability("opera.binary", "C:\\Program Files (x86)\\Opera\\28.0.1750.40\\opera.exe");  
        WebDriver dr = new OperaDriver(capabilities); 
 
        //Maximize browser window      
        dr.manage().window().maximize(); 
        //Go to URL     
        dr.get("http://google.com"); 
        //Set  timeout     
        dr.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
 
        //close Oper browser 
        dr.quit(); 
    } 
 
}  

HtmlUnit Driver
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.htmlunit.HtmlUnitDriver; 
 
public class Htmlunit { 
 
    public static void main(String args[]) { 
        // Initialize driver       
        WebDriver dr = new HtmlUnitDriver(); 
        //Go to page       
        dr.get("http://www.google.com"); 
        //get page title      
        System.out.println("Title =: " + dr.getTitle()); 
        //get page url 
        System.out.println("URL =: " + dr.getCurrentUrl()); 
 
        //close Htmlunit  
        dr.close(); 
    } 
 

No comments:

Post a Comment