Tuesday, August 27, 2013

Selenium Webdriver Declaration



FIREFOX:-
 
protected static WebDriver driver = null;

FirefoxBinary binary = new FirefoxBinary(new File("C:/Program Files (x86)/Mozilla Firefox/firefox.exe"));

ProfilesIni profile = new ProfilesIni();
FirefoxProfile ffprofile = profile.getProfile("default");
// FirefoxProfile profile = new FirefoxProfile();
ffprofile.setPreference("capability.policy.default.Window.QueryInterface", "allAccess");
ffprofile.setPreference("capability.policy.default.Window.frameElement.get", "allAccess");
ffprofile.setPreference("capability.policy.default.HTMLDocument.compatMode.get", "allAccess");
ffprofile.setPreference("capability.policy.default.Window.pageYOffset.get", "allAccess");
ffprofile.setPreference("capability.policy.default.Window.mozInnerScreenY.get", "allAccess");
ffprofile.setPreference("network.proxy.type", ProxyType.AUTODETECT.ordinal());

// Handle untrusted certificate. This will set the true value
ffprofile.setAcceptUntrustedCertificates(true);

driver = new FirefoxDriver(binary, ffprofile);



For Files Download:

public static String downloadPath = System.getProperty("user.dir") + \\DownloadFiles;

public static FirefoxProfile firefoxDriverProfile() throws Exception {
//Create FireFox Profile object
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.manager.showWhenStarting", false);
//Set Location to store files after downloading (0-desktop, 1-download folder, 2-user defined location)
profile.setPreference("browser.download.dir", downloadPath);
profile.setPreference("browser.download.folderList", 2);
//Set Preference to not show file download confirmation dialogue using MIME types Of different file extension types
profile.setPreference("browser.helperApps.neverAsk.openFile",
"text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml");
profile.setPreference("browser.helperApps.neverAsk.saveToDisk",
"text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml");
profile.setPreference("browser.helperApps.alwaysAsk.force", false);
profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
profile.setPreference("browser.download.manager.focusWhenStarting", false);
profile.setPreference("browser.download.manager.useWindow", false);
profile.setPreference("browser.download.manager.showAlertOnComplete", false);
profile.setPreference("browser.download.manager.closeWhenDone", false);
return profile;
}
driver = new FirefoxDriver(firefoxDriverProfile());
 
 
INTERNET_EXPLORER:-
protected static WebDriver driver = null;

protected static InternetExplorerDriverService service;

System.setProperty("webdriver.ie.driver",                             System.getProperty("user.dir")+ \\lib\\IEDriverServer.exe");
service = new InternetExplorerDriverService.Builder().usingAnyFreePort().withLogFile(new File("./TestOutput/Logs/IEDriver.log")).withLogLevel(InternetExplorerDriverLogLevel.TRACE).build();

    try {
         service.start();
         } catch (IOException e) {
         throw Throwables.propagate(e);
         }
Runtime.getRuntime().addShutdownHook(new Thread() {
               @Override
               public void run() {
                    service.stop();
               }
               });

DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,

true);

capabilities.setPlatform(Platform.WINDOWS);

capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);

capabilities.setCapability(CapabilityType.HAS_NATIVE_EVENTS, true);
capabilities.setJavascriptEnabled(true);
capabilities.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING, true);
capabilities.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);
capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,
true);
capabilities.setCapability(InternetExplorerDriver.REQUIRE_WINDOW_FOCUS, true);

driver = new InternetExplorerDriver(service, capabilities);
;
Note: download the IEDriverServer.exe and setup it in environment     variables and copy it into some(IEDriverServer.exe) location
àShould stop the server in teardown method (service.stop())

CHROME:-

 protected static WebDriver driver = null;


Private static ChromeDriverService service;

System.setProperty("webdriver.chrome.driver",       System.getProperty("user.dir")+ "\\lib\\chromedriver.exe");
service = new ChromeDriverService.Builder().usingAnyFreePort()        .withLogFile(new File("./TestOutput/Logs/ChromeDriver.log"))       .build();
        try {
          service.start();
            } catch (IOException e) {
              throw Throwables.propagate(e);
            }
              Runtime.getRuntime().addShutdownHook(new Thread() {
                           @Override
                           public void run() {
                           service.stop();
                           }
                        });
          driver = new ChromeDriver(service);

Note: download the chromedriver.exe and setup it in environment variables and copy it into some(C:\ chromedriver.exe)location.
àShould stop the server in teardown method (service.stop())

For Files Download:

public static String downloadPath = System.getProperty("user.dir") + \\DownloadFiles;


public static ChromeProfile chromeDriverProfile() throws Exception {
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", downloadPath);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
return cap;
}


WebDriver driver = new ChromeDriver(chromeDriverProfile());

SAFARI:-
DesiredCapabilities capabilities = new DesiredCapabilities();
     capabilities.setBrowserName( "safari" );
     CommandExecutor executor = new SeleneseCommandExecutor(new URL( "http://localhost:4444/" WebDriver driver = new RemoteWebDriver(executor, capabilities);

PhantomJS:-
DesiredCapabilities caps = new DesiredCapabilities();
caps.setJavascriptEnabled(true); //< not really needed: JS enabled by default
caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "C:/phantomjs.exe");
caps.setCapability("takesScreenshot", true);
 driver = new PhantomJSDriver(caps);
Note: download the phantomjs.exe and setup it in environment variables and copy it into some (C:\ phantomjs.exe)location.
 


No comments:

Post a Comment