4

From within Java unit tests, I want to use Selenium to test my web page with Firefox. My tests require that I set an environment variable for Firefox. (Specifically, I want to set the DISPLAY variable.)

The FirefoxBinary class has a method setEnvironmentProperty, which sounds like it should set environment variables for the environment the Firefox process runs in, but in practice it does not have that effect. (I have confirmed that with cat /proc/<firefox_pid>/environ.)

Back with selenium-java 3.0.1, I could build a GeckoDriverService with custom environment variables, and the FirefoxDriver constructor accepted the driver service as an argument, like so:

Map<String, String> customEnvironmentMap = new HashMap<>();
customEnvironmentMap.put("DISPLAY", ":1");
GeckoDriverService driverService = new GeckoDriverService.Builder(binary)
        .withEnvironment(customEnvironmentMap)
        .usingPort(0)
        .build()
FirefoxDriver driver = new FirefoxDriver(driverService, capabilities, null);

The custom variables would be present in the environment of the geckodriver process and the environment of the Firefox process.

That constructor is not present in version 3.4.0, and FirefoxDriver uses a private method to create the driver service, so I can't customize it. So, how do I configure the environment of the Firefox process that Selenium launches?

My current workaround is to substitute a script like this one for the geckodriver executable path:

#!/bin/bash
exec /usr/bin/env DISPLAY=:1 /path/to/geckodriver $@

I don't really like this technique, for various reasons (it's hacky, I have to create a temp file for the script in the filesystem, etc.).

user4851
  • 765
  • 1
  • 8
  • 19

2 Answers2

2

As of Selenium 3.7.1, the constructor that takes a GeckoDriverService has returned, so you can once again do the following:

Map<String, String> environment = new HashMap<>();
environment.put("DISPLAY", ":1");
GeckoDriverService service = new GeckoDriverService.Builder()
        .usingAnyFreePort()
        .withEnvironment(environment)
        .build();
FirefoxDriver driver = new FirefoxDriver(service);
user4851
  • 765
  • 1
  • 8
  • 19
0

Would this website help? https://testautomationarchives.blogspot.com/2013/08/how-to-configure-selenium-webdriver.html.

Starting at Step 5 from the above site, since 1-4 are installing things:

Step 5 : Set Environment Variables (Windows 7 )

  1. Right Click On Computer
  2. Click on Properties
  3. Click On ‘Advance Settings’
  4. On ‘System Properties ‘ Click On tab ‘Advance’
  5. Click on ‘Environment Variables ‘
  6. Copy following path where JDK is installed. C:\Program Files\Java\jdk1.6.0\bin
  7. On Environment Variable Window Click ‘New’ Under System Variable and Set Path: C:\Program Files\Java\jdk1.6.0\bin. If Path Variable already Exist Then Edit it .
  8. Set CLASSPATH : Copy following Path Where JDK is installed :
  9. On Environment Variable Window Click ‘New’ under User Variables and Set CLASSPATH :
  10. Enviornment is set up now start JAVA IDE
Jason
  • 514
  • 5
  • 21
  • 1
    Thanks, but I think not. Those instructions are for setting the system or user environment variables, but I want to customize environment variables only for the Firefox process. – user4851 Sep 08 '17 at 19:29