通过 Linux 以无头模式运行 Selenium 会导致错误

Tal*_*gel 4 java selenium headless-browser jenkins google-chrome-headless

我在一个网站上运行了大量测试。当我在 Windows 上本地运行测试时,它们都 100% 通过。该测试是在 Google Chrome 上设计和运行的。

现在,我们开始通过 Jenkins 作业在无头模式下在 Linux 上运行测试。现在有些测试在 0% 的情况下失败,或者仅在 20% 甚至 10% 的情况下通过。在我的代码中,我通过 ID、xpath 或 css 查找元素,然后简单地单击它们。我使用 WebDriverWait 对象进行等待 - 既等待元素出现又可单击。

我的代码示例:

    WebDriverWait wait = new WebDriverWait(browser, secondsToWait);
    wait.until(ExpectedConditions.presenceOfElementLocated(By.id(elementID)));           
    lastFoundElement = wait.until(ExpectedConditions.elementToBeClickable(By.id(elementID)));
    clickLastFoundElement();
Run Code Online (Sandbox Code Playgroud)

在我的报告中,我主要看到未找到元素,并且我通过了等待对象中设置的超时。

如何让无头测试更加稳定?

为什么无头状态会造成如此多的问题?

cru*_*dey 6

事实上,Selenium 社区已知的问题是 Headless 并不稳定,请阅读此处了解更多信息

当激活无头模式时,Chrome 运行非常不稳定。有多种不同的问题和错误,具体取决于:Chrome 版本、ChromeDriver版本和executed tests.

问题和修复(到目前为止发生的):

Chrome 无法在无头模式下启动

Exception:
No informative Error message. Only a timeout exception when navigate() is called on the driver:
org.openqa.selenium.TimeoutException: timeout

Fix:
options.addArguments("--proxy-server='direct://'");
options.addArguments("--proxy-bypass-list=*");
Run Code Online (Sandbox Code Playgroud)

Chrome 在无头模式下非常慢

Fix:
options.addArguments("--proxy-server='direct://'");
options.addArguments("--proxy-bypass-list=*");
Run Code Online (Sandbox Code Playgroud)

当测试运行很长时间时,Chrome 在一定时间后无法正常运行。在一个测试会话中执行许多操作

Exception:
... Timed out receiving message from renderer: ...
Fix (not tested yet!):
options.addArguments("--disable-browser-side-navigation");
Run Code Online (Sandbox Code Playgroud)