Search in sources :

Example 31 with FirefoxProfile

use of org.openqa.selenium.firefox.FirefoxProfile in project selenium_java by sergueik.

the class ProtractorDriver method capabilitiesFirefox.

public DesiredCapabilities capabilitiesFirefox(DesiredCapabilities capabilities) {
    capabilities = DesiredCapabilities.firefox();
    FirefoxProfile profile = new FirefoxProfile();
    // java.lang.IllegalArgumentException: Preference network.http.phishy-userpass-length may not be overridden: frozen value=255, requested value=255
    // profile.setPreference("network.http.phishy-userpass-length", 255);
    profile.setEnableNativeEvents(true);
    profile.setAcceptUntrustedCertificates(true);
    capabilities.setCapability(FirefoxDriver.PROFILE, profile);
    capabilities.setBrowserName(DesiredCapabilities.firefox().getBrowserName());
    return capabilities;
}
Also used : FirefoxProfile(org.openqa.selenium.firefox.FirefoxProfile)

Example 32 with FirefoxProfile

use of org.openqa.selenium.firefox.FirefoxProfile in project selenium_java by sergueik.

the class App method main.

public static void main(String[] args) throws InterruptedException, MalformedURLException {
    // ProfilesIni p=new ProfilesIni();
    // WebDriver hangs on navigation with Firefox 40 / Selenium 2.44
    // driver=new FirefoxDriver(p.getProfile("default"));
    // only works with Firefox
    DesiredCapabilities capabilities = new DesiredCapabilities("firefox", "", Platform.ANY);
    FirefoxProfile profile = new ProfilesIni().getProfile("default");
    profile.setEnableNativeEvents(false);
    capabilities.setCapability("firefox_profile", profile);
    /*
      System.setProperty("webdriver.chrome.driver", "c:/java/selenium/chromedriver.exe");
      DesiredCapabilities capabilities = DesiredCapabilities.chrome();
      LoggingPreferences logging_preferences = new LoggingPreferences();
      logging_preferences.enable(LogType.BROWSER, Level.ALL);
      capabilities.setCapability(CapabilityType.LOGGING_PREFS, logging_preferences);
      //  prefs.js:user_pref("extensions.logging.enabled", true);
      //  user.js:user_pref("extensions.logging.enabled", true);
      driver = new ChromeDriver(capabilities);
      */
    // driver = new RemoteWebDriver(new URL("http://127.0.0.1:4444/wd/hub"), capabilities);
    System.setProperty("webdriver.ie.driver", "c:/java/selenium/IEDriverServer.exe");
    driver = new InternetExplorerDriver();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    new App();
    // non-modal windows are handled successfully.
    // driver.get("http://www.naukri.com/");
    driver.get("https://developer.mozilla.org/samples/domref/showModalDialog.html");
    // following two locator do not work with IE
    // driver.findElement(By.xpath("//input[@value='Open modal dialog']")).click();
    // driver.findElement(By.cssSelector("input[type='button']")).click();
    WebDriverWait wait = new WebDriverWait(driver, 5);
    wait.pollingEvery(500, TimeUnit.MILLISECONDS);
    Actions actions = new Actions(driver);
    wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("html/body"))));
    WebElement body = driver.findElement(By.xpath("html/body"));
    body.findElement(By.xpath("input")).click();
    System.out.println("main: sleeping 10 sec");
    Thread.sleep(20000);
    System.out.println("main: close");
    driver.close();
    driver.quit();
}
Also used : ProfilesIni(org.openqa.selenium.firefox.internal.ProfilesIni) InternetExplorerDriver(org.openqa.selenium.ie.InternetExplorerDriver) Actions(org.openqa.selenium.interactions.Actions) DesiredCapabilities(org.openqa.selenium.remote.DesiredCapabilities) WebDriverWait(org.openqa.selenium.support.ui.WebDriverWait) FirefoxProfile(org.openqa.selenium.firefox.FirefoxProfile) WebElement(org.openqa.selenium.WebElement)

Example 33 with FirefoxProfile

use of org.openqa.selenium.firefox.FirefoxProfile in project selenium_java by sergueik.

the class CommonFunctions method getSeleniumDriver.

@SuppressWarnings("deprecation")
public static WebDriver getSeleniumDriver() throws IOException {
    checkEnvironment();
    if (isDestopTesting) {
        // For desktop browser testing, run a Selenium node and Selenium hub on
        // port 4444
        // For Vagrant box browser testing have localhost port 4444 forwarded to
        // the hub 4444
        // Alternatively make the test class launch the browser
        osName = System.getProperty("os.name");
        if (browser.equals("chrome")) {
            System.setProperty("webdriver.chrome.driver", new File((chromeDriverPath == null) ? osName.toLowerCase().startsWith("windows") ? "C:\\java\\selenium\\chromedriver.exe" : "/tmp/chromedriver" : chromeDriverPath).getAbsolutePath());
            DesiredCapabilities capabilities = DesiredCapabilities.chrome();
            ChromeOptions options = new ChromeOptions();
            Map<String, Object> chromePrefs = new HashMap<>();
            chromePrefs.put("profile.default_content_settings.popups", 0);
            String downloadFilepath = System.getProperty("user.dir") + System.getProperty("file.separator") + "target" + System.getProperty("file.separator");
            chromePrefs.put("download.default_directory", downloadFilepath);
            chromePrefs.put("enableNetwork", "true");
            options.setExperimentalOption("prefs", chromePrefs);
            options.addArguments("allow-running-insecure-content");
            options.addArguments("allow-insecure-localhost");
            options.addArguments("enable-local-file-accesses");
            options.addArguments("disable-notifications");
            // options.addArguments("start-maximized");
            options.addArguments("browser.download.folderList=2");
            options.addArguments("--browser.helperApps.neverAsk.saveToDisk=image/jpg,text/csv,text/xml,application/xml,application/vnd.ms-excel,application/x-excel,application/x-msexcel,application/excel,application/pdf");
            options.addArguments("browser.download.dir=" + downloadFilepath);
            // options.addArguments("user-data-dir=/path/to/your/custom/profile");
            capabilities.setBrowserName(DesiredCapabilities.chrome().getBrowserName());
            capabilities.setCapability(ChromeOptions.CAPABILITY, options);
            capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
            seleniumDriver = new ChromeDriver(capabilities);
        } else if (browser.equals("firefox")) {
            // alternatively one can add Geckodriver to system path
            System.setProperty("webdriver.gecko.driver", "c:/java/selenium/geckodriver.exe");
            // https://github.com/SeleniumHQ/selenium/wiki/DesiredCapabilities
            DesiredCapabilities capabilities = DesiredCapabilities.firefox();
            // use legacy FirefoxDriver
            capabilities.setCapability("marionette", false);
            // http://www.programcreek.com/java-api-examples/index.php?api=org.openqa.selenium.firefox.FirefoxProfile
            capabilities.setCapability("locationContextEnabled", false);
            capabilities.setCapability("acceptSslCerts", true);
            capabilities.setCapability("elementScrollBehavior", 1);
            FirefoxProfile profile = new FirefoxProfile();
            profile.setAcceptUntrustedCertificates(true);
            profile.setAssumeUntrustedCertificateIssuer(true);
            // no longer supported as of Selenium 3.8.x
            // profile.setEnableNativeEvents(false);
            System.out.println(System.getProperty("user.dir"));
            capabilities.setCapability(FirefoxDriver.PROFILE, profile);
            try {
                // java.lang.ClassCastException:
                // org.openqa.selenium.remote.service.DriverCommandExecutor cannot be
                // cast to
                // org.openqa.selenium.firefox.FirefoxDriver$LazyCommandExecutor
                seleniumDriver = new FirefoxDriver(capabilities);
            } catch (WebDriverException e) {
                e.printStackTrace();
                throw new RuntimeException("Cannot initialize Firefox driver");
            }
        }
    } else {
        DesiredCapabilities capabilities = new DesiredCapabilities("phantomjs", "", Platform.ANY);
        capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, new String[] { "--web-security=false", "--ssl-protocol=any", "--ignore-ssl-errors=true", "--local-to-remote-url-access=true", // prevent local file test XMLHttpRequest Exception 101
        "--webdriver-loglevel=INFO" // set to DEBUG for a really verbose console output
        });
        seleniumDriver = new PhantomJSDriver(capabilities);
    }
    return seleniumDriver;
}
Also used : PhantomJSDriver(org.openqa.selenium.phantomjs.PhantomJSDriver) HashMap(java.util.HashMap) DesiredCapabilities(org.openqa.selenium.remote.DesiredCapabilities) FirefoxProfile(org.openqa.selenium.firefox.FirefoxProfile) FirefoxDriver(org.openqa.selenium.firefox.FirefoxDriver) ChromeOptions(org.openqa.selenium.chrome.ChromeOptions) ChromeDriver(org.openqa.selenium.chrome.ChromeDriver) File(java.io.File) WebDriverException(org.openqa.selenium.WebDriverException)

Example 34 with FirefoxProfile

use of org.openqa.selenium.firefox.FirefoxProfile in project selenium_java by sergueik.

the class SessionTest method beforeClass.

@BeforeClass
public void beforeClass() throws IOException {
    getOsName();
    if (browser.equals("chrome")) {
        capabilities = DesiredCapabilities.chrome();
    } else if (browser.equals("firefox")) {
        // capabilities = DesiredCapabilities.firefox();
        capabilities = new DesiredCapabilities("firefox", "", Platform.ANY);
        FirefoxProfile profile = new ProfilesIni().getProfile("default");
        capabilities.setCapability("firefox_profile", profile);
    }
    driver = null;
    try {
        driver = new RemoteWebDriver(new URL("http://127.0.0.1:4444/wd/hub"), capabilities);
    } catch (MalformedURLException ex) {
    }
    assertThat(driver, notNullValue());
    actions = new Actions(driver);
    driver.manage().timeouts().setScriptTimeout(scriptTimeout, TimeUnit.SECONDS);
    // Declare a wait time
    wait = new WebDriverWait(driver, flexibleWait);
    wait.pollingEvery(pollingInterval, TimeUnit.MILLISECONDS);
    screenshot = ((TakesScreenshot) driver);
    js = ((JavascriptExecutor) driver);
    driver.manage().window().setSize(new Dimension(600, 800));
    driver.manage().timeouts().pageLoadTimeout(pageLoadTimeout, TimeUnit.SECONDS);
    driver.manage().timeouts().implicitlyWait(implicitlyWaitTimeout, TimeUnit.SECONDS);
    // print the node information
    getIPOfNode(driver);
    // Go to URL
    driver.get(baseURL);
}
Also used : MalformedURLException(java.net.MalformedURLException) JavascriptExecutor(org.openqa.selenium.JavascriptExecutor) ProfilesIni(org.openqa.selenium.firefox.internal.ProfilesIni) Actions(org.openqa.selenium.interactions.Actions) RemoteWebDriver(org.openqa.selenium.remote.RemoteWebDriver) DesiredCapabilities(org.openqa.selenium.remote.DesiredCapabilities) WebDriverWait(org.openqa.selenium.support.ui.WebDriverWait) Dimension(org.openqa.selenium.Dimension) FirefoxProfile(org.openqa.selenium.firefox.FirefoxProfile) URL(java.net.URL) TakesScreenshot(org.openqa.selenium.TakesScreenshot) BeforeClass(org.testng.annotations.BeforeClass)

Example 35 with FirefoxProfile

use of org.openqa.selenium.firefox.FirefoxProfile in project selenium_java by sergueik.

the class BaseTest method beforeClass.

@SuppressWarnings("deprecation")
@BeforeClass
public void beforeClass() throws IOException {
    /*	 TODO: TripadvisorTest: observed user agent problem with firefox - mobile version of
				 page is rendered, and the toast message displayed with the warning:
				 "We noticed that you're using an unsupported browser. The TripAdvisor
		     website may not display properly.We support the following browsers:
				 Windows: Internet Explorer, Mozilla Firefox, Google Chrome. Mac:
				 Safari".
		*/
    System.err.println("Launching " + browser);
    if (browser.equals("chrome")) {
        System.setProperty("webdriver.chrome.driver", (new File("c:/java/selenium/chromedriver.exe")).getAbsolutePath());
        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        ChromeOptions chromeOptions = new ChromeOptions();
        Map<String, Object> chromePrefs = new HashMap<>();
        chromePrefs.put("profile.default_content_settings.popups", 0);
        String downloadFilepath = System.getProperty("user.dir") + System.getProperty("file.separator") + "target" + System.getProperty("file.separator");
        chromePrefs.put("download.prompt_for_download", "false");
        chromePrefs.put("download.directory_upgrade", "true");
        chromePrefs.put("plugins.always_open_pdf_externally", "true");
        chromePrefs.put("download.default_directory", downloadFilepath);
        chromePrefs.put("enableNetwork", "true");
        chromeOptions.setExperimentalOption("prefs", chromePrefs);
        for (String optionAgrument : (new String[] { "--user-agent=Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/20120101 Firefox/33.0", "--allow-running-insecure-content", "--allow-insecure-localhost", "--enable-local-file-accesses", "--disable-notifications", "--disable-save-password-bubble", /* "start-maximized" , */
        "--browser.download.folderList=2", "--disable-web-security", "--no-proxy-server", "--browser.helperApps.neverAsk.saveToDisk=image/jpg,text/csv,text/xml,application/xml,application/vnd.ms-excel,application/x-excel,application/x-msexcel,application/excel,application/pdf", String.format("--browser.download.dir=%s", downloadFilepath) /* "--user-data-dir=/path/to/your/custom/profile"  , */
        })) {
            chromeOptions.addArguments(optionAgrument);
        }
        // chromeOptions.addArguments("user-data-dir=/path/to/your/custom/profile");
        // options for headless
        /*
			for (String optionAgrument : (new String[] { "headless",
					"window-size=1200x600", })) {
				options.addArguments(optionAgrument);
			}
			*/
        capabilities.setBrowserName(DesiredCapabilities.chrome().getBrowserName());
        capabilities.setCapability(org.openqa.selenium.chrome.ChromeOptions.CAPABILITY, chromeOptions);
        capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
        loadChromeExtensionsBase64Encoded(chromeOptions);
        // see also:
        // https://github.com/pulkitsinghal/selenium/blob/master/java/client/src/org/openqa/selenium/chrome/ChromeOptions.java
        // For use with RemoteWebDriver
        /*
			RemoteWebDriver driver = new RemoteWebDriver(
			     new URL("http://localhost:4444/wd/hub"), capabilities);
			  */
        driver = new ChromeDriver(capabilities);
    } else if (browser.equals("firefox")) {
        // https://developer.mozilla.org/en-US/Firefox/Headless_mode
        // 3.5.3 and later
        System.setProperty("webdriver.gecko.driver", osName.toLowerCase().startsWith("windows") ? new File("c:/java/selenium/geckodriver.exe").getAbsolutePath() : "/tmp/geckodriver");
        System.setProperty("webdriver.firefox.bin", osName.toLowerCase().startsWith("windows") ? new File("c:/Program Files (x86)/Mozilla Firefox/firefox.exe").getAbsolutePath() : "/usr/bin/firefox");
        // https://github.com/SeleniumHQ/selenium/wiki/DesiredCapabilities
        DesiredCapabilities capabilities = DesiredCapabilities.firefox();
        // use legacy FirefoxDriver
        capabilities.setCapability("marionette", false);
        // http://www.programcreek.com/java-api-examples/index.php?api=org.openqa.selenium.firefox.FirefoxProfile
        capabilities.setCapability("locationContextEnabled", false);
        capabilities.setCapability("acceptSslCerts", true);
        capabilities.setCapability("elementScrollBehavior", 1);
        FirefoxProfile profile = new FirefoxProfile();
        profile.setAcceptUntrustedCertificates(true);
        profile.setAssumeUntrustedCertificateIssuer(true);
        profile.setEnableNativeEvents(false);
        // optional
        /* 
			 profile.setPreference("general.useragent.override",
			 		"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/20120101 Firefox/33.0");
			*/
        // System.out.println(System.getProperty("user.dir"));
        capabilities.setCapability(FirefoxDriver.PROFILE, profile);
        try {
            driver = new FirefoxDriver(capabilities);
        } catch (WebDriverException e) {
            e.printStackTrace();
            throw new RuntimeException("Cannot initialize Firefox driver");
        }
    }
    actions = new Actions(driver);
    driver.manage().timeouts().setScriptTimeout(scriptTimeout, TimeUnit.SECONDS);
    // Declare a wait time
    wait = new WebDriverWait(driver, flexibleWait);
    wait.pollingEvery(pollingInterval, TimeUnit.MILLISECONDS);
    screenshot = ((TakesScreenshot) driver);
    js = ((JavascriptExecutor) driver);
    // driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(implicitWait, TimeUnit.SECONDS);
    // Go to URL
    driver.get(baseURL);
}
Also used : JavascriptExecutor(org.openqa.selenium.JavascriptExecutor) HashMap(java.util.HashMap) Actions(org.openqa.selenium.interactions.Actions) DesiredCapabilities(org.openqa.selenium.remote.DesiredCapabilities) FirefoxProfile(org.openqa.selenium.firefox.FirefoxProfile) TakesScreenshot(org.openqa.selenium.TakesScreenshot) FirefoxDriver(org.openqa.selenium.firefox.FirefoxDriver) WebDriverWait(org.openqa.selenium.support.ui.WebDriverWait) ChromeOptions(org.openqa.selenium.chrome.ChromeOptions) ChromeDriver(org.openqa.selenium.chrome.ChromeDriver) File(java.io.File) WebDriverException(org.openqa.selenium.WebDriverException) BeforeClass(org.testng.annotations.BeforeClass)

Aggregations

FirefoxProfile (org.openqa.selenium.firefox.FirefoxProfile)46 FirefoxDriver (org.openqa.selenium.firefox.FirefoxDriver)25 DesiredCapabilities (org.openqa.selenium.remote.DesiredCapabilities)20 File (java.io.File)16 ChromeDriver (org.openqa.selenium.chrome.ChromeDriver)10 ChromeOptions (org.openqa.selenium.chrome.ChromeOptions)10 FirefoxOptions (org.openqa.selenium.firefox.FirefoxOptions)9 WebDriverWait (org.openqa.selenium.support.ui.WebDriverWait)9 IOException (java.io.IOException)7 WebDriverException (org.openqa.selenium.WebDriverException)7 Actions (org.openqa.selenium.interactions.Actions)7 HashMap (java.util.HashMap)6 RemoteWebDriver (org.openqa.selenium.remote.RemoteWebDriver)6 URL (java.net.URL)5 Dimension (org.openqa.selenium.Dimension)5 JavascriptExecutor (org.openqa.selenium.JavascriptExecutor)5 WebDriver (org.openqa.selenium.WebDriver)5 TakesScreenshot (org.openqa.selenium.TakesScreenshot)3 FirefoxBinary (org.openqa.selenium.firefox.FirefoxBinary)3 InternetExplorerDriver (org.openqa.selenium.ie.InternetExplorerDriver)3