开发者

Using Personal SSL certificates with Webdriver (Selenium 2.0)

I am testing a website which requires personal SSL certificates in order to do certain things, such as sign-in.

I have a Webdriver (Selenium 2.0) 开发者_运维百科test that I have set up with a proxy:

    Proxy localhostProxy = new Proxy();
    localhostProxy.setProxyType(Proxy.ProxyType.MANUAL);
    localhostProxy.setHttpProxy("www-proxyname:port");
    
    FirefoxProfile profile = new FirefoxProfile();
    profile.setProxyPreferences(localhostProxy);
    driver = new FirefoxDriver(profile);

And this will access the homepage fine. The test then clicks the sign in button, enters in the correct credentials and clicks on submit. At this point the browser then goes into a loading state, and I'm assuming it's because the SSL certificate is missing from my side and therefore cannot connect to the sign in service.

I searched for different proxy solutions, and found this:

    profile.setAcceptUntrustedCertificates(true);
    profile.setAssumeUntrustedCertificateIssuer(true);

So I added it into my code, but it doesn't seem to do what I want. I think I'm looking for a way to tell WebDriver that my ssl certificate is in x directory, please use it when accessing this site. Does anyone know how to do this?

My Test code is:

@Test
public void userSignsInAndVerifiesDrawerViews(){
            driver.get("www.url.com");
            waitFor(5000);
    driver.findElement(By.xpath("//a[contains(text(), 'Sign in')]")).click();
    waitFor(3000);
    String username = "seleniumtest";
    String password = "seleniumtest1";
    driver.findElement(By.id("username")).sendKeys(username);
    driver.findElement(By.id("password")).sendKeys(password);
    driver.findElement(By.xpath("//signin")).click();
    waitFor(30000);
    String signInLinkText = driver.findElement(By.xpath("//xpath")).getText();
    assertEquals(signInLinkText, username);
}


Webdriver has no built in mechanism for adding a personal cert.

If you are using firefox the only way that I have found to do this is to create a firefox profile and add the certificate to it. You can then either reuse the profile when you run your tests OR, and this is my prefered option, take the cert8.db and key3.db files and add them to the profile that webdriver creates at runtime.

I am not sure how yo do this in java, but in ruby I override the layout_on_disk method of FirefoxProfile to add the extra files I required. Java has the same class so you should be able to do this same thing.


No need to overwrite the method layout_on_disk() as suggested.
You can simply load as profile a folder containing the files cert8.db and key3.db.

Selenium will complete the profile for you.

Then you can add the preferences you need to the firefox profile.
The resulting code looks like this:

    FirefoxProfile firefoxProfile = new FirefoxProfile(
            new File("/folder/location"));
    FirefoxOptions options = new FirefoxOptions();

    options.setProfile(firefoxProfile);

    WebDriver driver = new RemoteWebDriver(
            new URL("http://localhost:4444/wd/hub"),
            options.toCapabilities());

Tested with selenium 3.5.3.


Webdriver can do this, although Derek is right and it isn't built in.

All you need to do is make a custom Trust Manager that trusts all certs and then also override the "hostname verifier" to allow a non-real domain name.

There is somewhat of an example I found on Google here:

http://grepcode.com/file/repo1.maven.org/maven2/org.seleniumhq.selenium.server/selenium-server-coreless/1.0.3/org/openqa/selenium/server/TrustEverythingSSLTrustManager.java

This is the same method you would use with Apache HC components to override SSL settings without using WebDriver. I've used this method a lot with direct HTTP posts using Apache HT components and it "appears" that from the link above , this concept should also work with WebDriver.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜