简体   繁体   English

硒测试在无头模式下失败

[英]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. 我已经创建了一个Selenium + jUnit测试套件,它在运行Eclipse时工作正常但抛出异常,主要是“元素当前不可见,因此可能无法与作为teamcity构建的一部分以无头方式运行的异常进行交互”。

Just to give you an idea of how i'm setup, i have following in the test setUp method: 只是为了让您了解我是如何设置的,我在测试setUp方法中有以下内容:

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. 这是触发ant脚本的shell脚本,后者又运行测试套件。

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: waitUntilElementPresent()方法是这样的:

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. 此外,没有涉及ajax,也没有不透明度或可见性问题。 The 30 second timeout makes sure there is enough time. 30秒超时确保有足够的时间。 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. 顺便说一句,teamcity在具有jdk 1.8和FF 39的ubuntu代理上运行这些测试。我在本地计算机上具有相同的规范。

Are you 100% sure that the element is visible in a 1600x900 display? 您是否100%确定元素在1600x900显示屏中可见?
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. 也许您只需向下滚动几个像素(我假设您的工作站具有1920x1080分辨率,因此显示内容的空间更大)或者由于不同的配置文件设置而存在某种类型的Firefox弹出窗口。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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