Selenium 2 WebDriver Advanced
  • Preface
  • 1. API Testing Using Selenium WebDriver
    • 1.1 Code Snippet for API testing
    • 1.2 SoapUI
      • 1.2.1 Add selenium jars to soapUI
      • 1.2.2 Run selenium tests
      • 1.2.3 Data Driven Testing using SoapUI
  • 2. Advanced Profiles for Browsers
    • 2.1 Profile for Firefox Browser
    • 2.2 Profile for Chrome Browser
    • 2.3 Profile for IE Browser
    • 2.4 Profile for Safari Browser
    • 2.5 Profile for Opera Browser
  • 3. TestNG
    • 3.1 Sample TestNG class
    • 3.2 Parallel execution
    • 3.3 Execution of tests from batch file
  • 4. ExtentReports
    • 4.1 Basics
    • 4.2 Sample Extent Reports
  • 5. Advanced tricks
    • 5.1 Keystroke handling
    • 5.2 Screenshot capture
    • 5.3 Get HTML Source of WebElement
Powered by GitBook
On this page

Was this helpful?

  1. 2. Advanced Profiles for Browsers

2.1 Profile for Firefox Browser

I have seen many people struggling for setting the firefox profile for a number of reasons due to some of the restrictions in the corporate companies.

Now there are two ways of setting proxy, either we set the proxy using ip and port number. or we set by the URL. The code snippet shows how to achieve the same using both the methods. The code snippet for setting proxy via URL is commented. Uncomment this code if you wish to use it and comment the other part.

Finally the code to accept security certificates is also mentioned in the snippet.

package code;

import java.io.File;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class ProxiesCertificates {
    WebDriver driver;
    String baseUrl;

    @BeforeTest
    public void setup() {

        // The below proxy is a string that contains proxy ip and port
        // The port and ip is separated by ":" below you can see port is 80
        Proxy proxy = new Proxy();
        String proxyIp = "10.122.76.32:80";

        // Below lines sets the proxy defiend above
        proxy.setHttpProxy(proxyIp);
        proxy.setSslProxy(proxyIp);
        proxy.setFtpProxy(proxyIp);

        // In case you wish to set proxy with URL uncomment below code. and
        // comment the above 3 lines

        /*
         * proxy.setProxyType(Proxy.ProxyType.PAC);
         * proxy.setProxyAutoconfigUrl("http://someServer/server.pac");
         */

        // Below code create capability with mentioned proxy
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(CapabilityType.PROXY, proxy);

        // Path to the copy of firefox installation. Here you can provide direct
        // path to your firefox installation also.
        System.setProperty("webdriver.firefox.bin",System.getProperty("user.dir")+ "toolsfirefoxfirefox.exe");

        // This is the path to the firefox profile you copied in your project.
        FirefoxProfile fp = new FirefoxProfile(new File(System.getProperty("user.dir")+ "toolsfirefoxDefaultProfile"));

        // Below code accepts security certificates..
        fp.setPreference("setAcceptUntrustedCertificates", "true");
        fp.setAcceptUntrustedCertificates(true);
        fp.setAssumeUntrustedCertificateIssuer(true);

        fp.clean(null);
        //0- Desktop, 1-Browser's Default Path , 2- Custom Download Path
        fp.setPreference("browser.download.folderList", 2);    
        fp.setPreference("plugin.scan.plid.all", false);
        fp.setPreference("pdfjs.disabled", true);
        fp.setPreference("plugin.scan.Acrobat", "99");
        fp.setPreference("marionette.logging", false);
        fp.setPreference("browser.download.dir", CONSTANTS.STRDOWNLOADPATH);
        fp.setPreference("browser.download.manager.alertOnEXEOpen", false);
        fp.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream;application/pdf");
        fp.setPreference("browser.download.manager.showWhenStarting", false);
        fp.setPreference("browser.download.manager.focusWhenStarting", false);
        //"application/msword,application/csv,text/csv,image/png ,image/jpeg, text/html,text/plain");
        fp.setPreference("browser.download.useDownloadDir", true);
        fp.setPreference("browser.helperApps.alwaysAsk.force", false);
        fp.setPreference("browser.download.manager.alertOnEXEOpen", false);
        fp.setPreference("browser.download.manager.closeWhenDone", false);
        fp.setPreference("browser.download.manager.showAlertOnComplete", false);
        fp.setPreference("browser.download.manager.useWindow", false);
        fp.setPreference("browser.download.manager.showWhenStarting", false);
        fp.setPreference("network.proxy.type", proxyType);
        fp.setPreference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", false);
        fp.setPreference("plugin.disable_full_page_plugin_for_types", "application/pdf");

        // Create browser instance with above mentioned requsites
        driver = new FirefoxDriver(new FirefoxBinary(), fp, capabilities);
        driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
        driver.manage().deleteAllCookies();
        driver.manage().window().maximize();
    }

    @Test
    public void testMethod() {
        baseUrl = "http://docs.seleniumhq.org/";
        driver.get(baseUrl);
    }

    @AfterTest
    public void teardown() {
        driver.quit();
    }

}
Previous2. Advanced Profiles for BrowsersNext2.2 Profile for Chrome Browser

Last updated 5 years ago

Was this helpful?