简体   繁体   中英

Selenium tests failing in headless mode

I have created a Selenium+jUnit test suite that works fine when run through Eclipse but throws exceptions, mostly "Element is not currently visible and so may not be interacted with" exceptions when run in headless manner as a part of teamcity build.

Just to give you an idea of how i'm setup, i have following in the test setUp method:

driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().deleteAllCookies();

Here is the shell script that triggers ant script which in turn runs the test suite.

echo "Executing Xvfb"
sudo nohup Xvfb :40 -ac -screen 0 1600x900x24 &

echo "Setting the DISPLAY"
export DISPLAY=:40

echo "Executing the ANT script"
ant

Most errors are related to this piece of code:

....

UtilityMethods.waitUntilElementPresent(driver,By.id("add_section_button"));

Thread.sleep(3000);

driver.findElement(By.id("add_section_button")).click();

....

The waitUntilElementPresent() method is like so:

public static void waitUntilElementPresent(WebDriver driver, By by){

    WebDriverWait wait = new WebDriverWait(driver, 20);

    wait.until(ExpectedConditions.presenceOfElementLocated(by));

}

The above lines generate following error:

Element is not currently visible and so may not be interacted with
Command duration or timeout: 30.03 seconds

Please remember that it only happens in headless mode, this works fine in regular mode. Also, there is no ajax involved and no issue with opacity or visibility either. The 30 second timeout makes sure there is enough time. What could be wrong?

By the way, teamcity runs these tests on a ubuntu agent that has jdk 1.8 and FF 39. I have the same specs on my local machine.

Are you 100% sure that the element is visible in a 1600x900 display?
Maybe you just need to scroll down a few pixels (I assume your workstation having 1920x1080 resolution, so there's a little more room for displaying content) or there is some kind of Firefox popup due to a different profile setting.

Just see for yourself with a screenshot.

WebDriver driver = new FirefoxDriver();
try {
    doYourStuff();
} catch (Exception e) {
    byte[] screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES);
    Files.write(Paths.get("./screenshot.png"), screenshot, StandardOpenOption.CREATE, StandardOpenOption.WRITE);
} finally {
    requiredCleanup();
}

尝试使用PhantomJS而不是Firefox

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM