2

I'm running Selenium tests from within Eclipse, but I can't load a custom Firefox profile.

Most sources suggest I need to launch the Selenium Server like this:

java -jar selenium-server.jar -firefoxProfileTemplate </path/to/template/>

But when launching my test from within Eclipse it doesn't use that - the tests will run if the Selenium Server isn't running.

This thread suggests that I can set the profile in the DefaultSelenium constructor:

Selenium RC - disabling browser cookie

But the code generated for me by Selenium IDE (Firefox plugin) doesn't use that constructor:

package com.example.tests;

import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;

public class Example extends SeleneseTestCase {
    public void setUp() throws Exception {
        setUp("http://www.example.com/", "*firefox");
    }
    public void testExample() throws Exception {
        selenium.open("/");
        selenium.click("//body");
    }
}

Where should I set the DefaultSelenium configuration options? Or is there some other method I can use to load my custom Firefox template?

Thanks! Stu

Community
  • 1
  • 1
stubotnik
  • 1,952
  • 2
  • 17
  • 31

2 Answers2

2

I made a SeleniumTestCase that starts/stops the server before/after each test class and starts/stops the Selenium instance before/after each test:

public class SeleniumTestCase {
    protected static Selenium selenium;
    protected static AppNavUtils appNavUtils;

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        SeleniumServerControl.getInstance().startSeleniumServer();
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        SeleniumServerControl.getInstance().stopSeleniumServer();
    }

    @Before
    public void setUp() throws Exception {
        // Replace "*chrome" with "*firefox" for Selenium > 1.0
        selenium = new DefaultSelenium("localhost", 5444, "*chrome", "http://localhost:8080/"); 
        selenium.start();
        appNavUtils = new AppNavUtils(selenium);
    }

    @After
    public void tearDown() throws Exception {
        selenium.stop();
    }  
}

The SeleniumServerControl starts and stops the server:

public class SeleniumServerControl {
    private static final SeleniumServerControl instance = new SeleniumServerControl();

    public static SeleniumServerControl getInstance()
    {
        return instance;
    }

    private SeleniumServer server = null;

    protected SeleniumServerControl(){}

    public void startSeleniumServer() {
        if (server == null) {
            RemoteControlConfiguration rcc = new RemoteControlConfiguration();
            rcc.setPort(5444);
            //rcc.setFirefoxProfileTemplate(newFirefoxProfileTemplate)
            server = new SeleniumServer(rcc);
        }

        server.start();
    }

    public void stopSeleniumServer()
    {
        if (server != null) {
            server.stop();
            server = null;
        }
    }
}
Carl Pritchett
  • 674
  • 6
  • 13
1

the version of code you have above assumes that you are running your tests against localhost on port 4444 thats why it is has 2 parameters in the setup.

To set up eclipse to run it you will need to update the run configuration. That is under

Run > Run Configurations 

Have a look for the item that has selenium in it and add the config above so that when it runs it will pick it up and run.

I personally just fire up the server when I start working by running a batch file and kill it at the end of the day.

AutomatedTester
  • 22,188
  • 7
  • 49
  • 62
  • 1
    You can also instantiate and start an instance of org.openqa.selenium.server.SeleniumServer if you want to do it all within the code. See .setFirefoxProfileTemplate(). – dhackner Aug 03 '10 at 06:56