Issue Description
I have a strange problem where my WebDriver tests work perfectly fine with regular Firefox browser but completely fail when I try to run them in virtual display mode using xvfb.
Working Configuration
- Selenium standalone: version 3.0.1
- Firefox browser: 38.0.1
- Server IP: 192.168.1.40
- Result: All tests pass
Failing Configuration
- Selenium standalone: version 3.0.1
- Firefox browser: 38.0
- Server IP: 172.18.0.60
- Virtual display command:
xvfb-run java -Dwebdriver.firefox.marionette="/opt/gecko" -Dwebdriver.firefox.bin="/opt/ff/firefox" -jar selenium-server-standalone-3.0.1.jar -role webdriver -hub http://192.168.1.106:4444/grid/register -port 5566 -host 172.18.0.60 - Result: Tests fail immediately
Sample Test Code
package automation;
import org.junit.Assert;
import org.openqa.selenium.*;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import java.net.MalformedURLException;
public class LoginTest {
public WebDriver browser;
public String targetURL, gridNode;
private JavascriptExecutor jsExecutor;
private String siteURL;
protected ThreadLocal<RemoteWebDriver> driverThread = null;
@Parameters({"browserType", "nodeURL"})
@BeforeTest
public void setupBrowser(String browserType, String nodeURL) throws MalformedURLException {
siteURL = "http://testsite";
if (browserType.equalsIgnoreCase("firefox")) {
System.out.println("Starting Firefox browser");
String gridNode = nodeURL;
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setBrowserName("firefox");
browser = new RemoteWebDriver(new URL(gridNode), capabilities);
browser.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
browser.navigate().to(siteURL);
browser.manage().window().maximize();
} else {
throw new IllegalArgumentException("Unsupported browser type");
}
jsExecutor = (JavascriptExecutor) browser;
}
@Test
public void userLoginTest() throws InterruptedException {
browser.findElement(By.linkText("Login")).click();
browser.findElement(By.id("username")).clear();
browser.findElement(By.id("username")).sendKeys("[email protected]");
browser.findElement(By.id("password")).clear();
browser.findElement(By.id("password")).sendKeys("testpass");
browser.findElement(By.id("submitBtn")).click();
jsExecutor.executeScript("window.scrollBy(0, 300);");
browser.findElement(By.cssSelector(".userMenu a")).click();
Thread.sleep(1500);
browser.findElement(By.linkText("Dashboard")).click();
Thread.sleep(2500);
jsExecutor.executeScript("window.scrollBy(0, 300);");
Thread.sleep(1500);
jsExecutor.executeScript("window.scrollBy(0, -300);");
Thread.sleep(2500);
browser.findElement(By.cssSelector(".userMenu a")).click();
Thread.sleep(1500);
browser.findElement(By.linkText("Logout")).click();
Thread.sleep(2500);
}
@AfterTest
public void tearDown() {
browser.quit();
}
}
Error Details
The virtual display setup fails right at the first element lookup with this error:
07:16:53.571 INFO - Executing: [new session: Capabilities [{marionette=true, browserName=firefox, version=, platform=ANY}]]
07:16:53.580 INFO - Creating a new session for Capabilities [{marionette=true, browserName=firefox, version=, platform=ANY}]
07:17:00.268 INFO - Attempting bi-dialect session, assuming Postel's Law holds true on the remote end
07:17:02.648 INFO - Detected dialect: OSS
07:17:02.665 INFO - Done: [new session: Capabilities [{marionette=true, firefoxOptions=org.openqa.selenium.firefox.FirefoxOptions@3e74110c, browserName=firefox, moz:firefoxOptions=org.openqa.selenium.firefox.FirefoxOptions@3e74110c, version=, platform=ANY}]]
07:17:02.701 INFO - Executing: [implicit wait: 50000]]
07:17:02.717 INFO - Done: [implicit wait: 50000]
07:17:02.724 INFO - Executing: [get: http://testsite]]
07:18:03.564 INFO - Done: [get: http://testsite]
07:18:03.570 INFO - Executing: [maximise window]]
07:18:03.580 INFO - Done: [maximise window]
07:18:03.593 INFO - Executing: [find element: By.linkText: Login]]
07:18:53.917 WARN - Exception thrown
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"link text","selector":"Login"}
The error clearly shows it cannot find the element that works fine in regular browser mode. Why would the same element be missing only in virtual display mode? Are there specific limitations or configuration issues when using headless browser automation?