Abhishek Swain
Abhishek Swain

Reputation: 1054

Check if website has any ssl certificate warnings using Selenium webDriver

I need to automate a scenario where I have to verify that website has no warnings regarding ssl certificates. How to archive it using Selenium WebDriver 2?

Upvotes: 3

Views: 2114

Answers (1)

barak manos
barak manos

Reputation: 30136

SSL certificate warnings appear different on each browser (as they are generated by the browser itself and not by the web-page that you are trying to access).

If you are using Selenium with Firefox, then you can use the following Java class:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxBinary;

class Whatever
{
    private WebDriver webDriver;

    public void open() throws Exception
    {
        webDriver = new FirefoxDriver();
    }

    public void openHeadless() throws Exception
    {
        FirefoxBinary binary = new FirefoxBinary(new File("/usr/local/bin/firefox"));
        binary.setEnvironmentProperty("DISPLAY",System.getProperty("lmportal.xvfb.id",":99"));
        webDriver = new FirefoxDriver(binary,null);
    }

    public void close() throws Exception
    {
        webDriver.quit();
    }

    public boolean sslWarningIssued(final String url) throws Exception
    {
        webDriver.get(url);
        String pageSource = webDriver.getPageSource();
        return webDriver.getTitle().equals("Untrusted Connection"); // should be faster than the one below...
        return pageSource.contains("This Connection is Untrusted") && pageSource.contains("What Should I Do?");
    }
}

Then you can call:

Whatever instance = new Whatever();
instance.open(); // or openHeadless for faster execution
boolean sslWarningIssued1 = instance.sslWarningIssued(website_1_url);
boolean sslWarningIssued2 = instance.sslWarningIssued(website_2_url);
...
boolean sslWarningIssuedN = instance.sslWarningIssued(website_N_url);
instance.close();

Upvotes: 2

Related Questions