Pages

Search This Blog

Wednesday, May 19, 2010

How to handle SSL certificate issue in Selenium / firefox

Selenium creates a new and clean profile each time it runs a test, so it won't keep any cookies of the previous session and it wont use any browser plug-in however you can force selenium to use your own profile.

Steps
1. Close all instances of firefox browser.
2. Create a new firefox profile : Go to Run and type firefox -p and press OK . This will open a dialog bod where you create new firefox profile, have a name "nk" and save it in C: drive.
3. Install all plug-ins in this profile ; For SSL certificate issue install SSL security bypass

https://addons.mozilla.org/en-US/firefox/addon/10246/

4. Now you write you test cases like below

import org.openqa.selenium.server.RemoteControlConfiguration;
import org.openqa.selenium.server.SeleniumServer;
import org.testng.annotations.*;
import com.thoughtworks.selenium.*;
import java.io.File;

public class firefoxprofile extends SeleneseTestCase {

private SeleniumServer seleniumServer;
private Selenium selenium;
public static final String MAX_WAIT_TIME_IN_MS="60000";

@BeforeClass
public void setUp() throws Exception {

File template = new File("C:\\nk");
RemoteControlConfiguration rc = new RemoteControlConfiguration();
rc.setSingleWindow(true);
rc.setFirefoxProfileTemplate(template);
seleniumServer = new SeleniumServer(rc);
selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.google.com/");
seleniumServer.start();
selenium.start();

}


@Test
public void googling() {
selenium.open("/");
selenium.waitForPageToLoad(MAX_WAIT_TIME_IN_MS);
selenium.type("q", "http://automationtricks.blogspot.com");
selenium.click("btnG");
selenium.waitForPageToLoad(MAX_WAIT_TIME_IN_MS);
assertTrue(selenium.isTextPresent("http://automationtricks.blogspot.com"));
}


@AfterTest
public void tearDown() throws InterruptedException{
selenium.stop();
seleniumServer.stop();

}
}

How to run test cases in specified browser profile

Selenium creates a new and clean profile each time it runs a test, so it won't keep any cookies of the previous session and it wont use any browser plug-in however you can force selenium to use your own profile.

Steps
1. Close all instances of firefox browser.
2. Create a new firefox profile : Go to Run and type firefox -p and press OK . This will open a dilog bod where you create new firefox profile, have a name "nk" and save it in C: drive.
3. Start the browser: Go to run and type firefox -p that will open a dialog box. Select the profile "NK" which you created in the 2nd step.
4. Install all plug-ins in this profile ; like SSL security bypass

https://addons.mozilla.org/en-US/firefox/addon/10246/

5. Now you write you test cases like below

import org.openqa.selenium.server.RemoteControlConfiguration;
import org.openqa.selenium.server.SeleniumServer;
import org.testng.annotations.*;
import com.thoughtworks.selenium.*;
import java.io.File;

public class firefoxprofile extends SeleneseTestCase {

private SeleniumServer seleniumServer;
private Selenium selenium;
public static final String MAX_WAIT_TIME_IN_MS="60000";

@BeforeClass
public void setUp() throws Exception {

File template = new File("C:\\nk");
RemoteControlConfiguration rc = new RemoteControlConfiguration();
rc.setSingleWindow(true);
rc.setFirefoxProfileTemplate(template);
seleniumServer = new SeleniumServer(rc);
selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.google.com/");
seleniumServer.start();
selenium.start();

}


@Test
public void googling() {
selenium.open("/");
selenium.waitForPageToLoad(MAX_WAIT_TIME_IN_MS);
selenium.type("q", "http://automationtricks.blogspot.com");
selenium.click("btnG");
selenium.waitForPageToLoad(MAX_WAIT_TIME_IN_MS);
assertTrue(selenium.isTextPresent("http://automationtricks.blogspot.com"));
}


@AfterTest
public void tearDown() throws InterruptedException{
selenium.stop(); 
seleniumServer.stop();

}
}