使用 Jenkins 的 Selenium 测试 运行 无法与元素交互

Selenium test run with Jenkins cannot interact with element

我需要在IE11上测试一个网站,所以我用IEDriver做了一个selenium测试项目。我也使用 Maven。在本地一切正常,我现在必须使用 Jenkins 来启动我的测试。

我创建了一个简单的作业来尝试我的一个测试。 Jenkins 找到我的 pom.xml,用 IEDriver 打开浏览器,导航到我的 url,然后问题就开始了。

我的测试在本地 运行 时做了什么:

  • Clicks on the "Welcome screen" to access the next page;
  • Checks two elements on the new page with 3 assertions :

    1) element1.getText().trim().equalsIgnoreCase(myText1);

    2) element2.getText().trim().equalsIgnoreCase(myText2);

    3) element2.isEnabled();

  • Clicks on element2 to get to the next page;

  • Checks other elements.

我的测试在通过 Jenkins 启动时做了什么:

  • Clicks on the "Welcome screen" to access the next page; => this is OK, but easy, because this page only contains 1 element that takes all the screen
  • Fails at checking the 2 elements :

    1) is false, and element1.getText() is blank

    2) is false, and element2.getText() is blank

    3) is true

  • If I remove the assertions and try to directly click on element2, I have a ElementNotInteractableException

我捕获任何异常以在出现错误时获取屏幕截图。这里的截图只是黑屏。

据我了解,当使用 Jenkins 启动时,Selenium "knows" 页面上有一些元素(=> element2.isEnabled() 是正确的),但 "see" 它们(=> element2.getText() 是空的)并且无法与它们交互,就好像它们是 "behind" 黑屏并且 Selenium 无法聚焦 "real" 屏幕。

我真的不明白这是怎么回事。我尝试 运行 我的测试 Chrome 驱动程序,我有同样的问题。我很确定我遗漏了一些明显的东西。

如果您有任何想法,我将不胜感激。这是我第一次在这里留言,如果我写的不对或者有什么地方不对,你也可以说。

这是 ElementNotInteractableException 的堆栈跟踪:

org.openqa.selenium.ElementNotInteractableException: Cannot click on element

Build info: version: '3.6.0', revision: '6fbf3ec767', time: '2017-09-27T15:28:36.4Z' System info: host: 'BLABLA', ip: 'BLABLA', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_102'

Driver info: org.openqa.selenium.ie.InternetExplorerDriver

Capabilities [{proxy=Proxy(), acceptInsecureCerts=false, browserVersion=11, se:ieOptions={nativeEvents=true, browserAttachTimeout=0, ie.ensureCleanSession=false, elementScrollBehavior=0, enablePersistentHover=true, ie.browserCommandLineSwitches=, ie.forceCreateProcessApi=false, requireWindowFocus=false, initialBrowserUrl=http://localhost:43020/, ignoreZoomSetting=false, ie.fileUploadDialogTimeout=3000, ignoreProtectedModeSettings=false}, timeouts={implicit=0, pageLoad=300000, script=30000}, browserName=internet explorer, pageLoadStrategy=normal, javascriptEnabled=true, platformName=WINDOWS, setWindowRect=true, platform=WINDOWS}]

Session ID: 50e4def1-11aa-43c6-8f9e-a52cd324a1b5

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)

at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)

at java.lang.reflect.Constructor.newInstance(Constructor.java:423)

at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:185)

at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:120)

at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)

at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164)

at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)

at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:586)

at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:279)

at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:83)

at framework.elements.CustomElement.click(CustomElement.java:141)

at parionsDirect.pageObject.PageVeilleAccueil.clicPremiereVisite(PageVeilleAccueil.java:56)

at testAutomatises.TestHorsConnexion.pageVeilleVisiteur(TestHorsConnexion.java:97)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:50)

at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)

at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)

at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)

at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)

at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)

at org.junit.rules.TestWatcher.evaluate(TestWatcher.java:55)

at org.junit.rules.RunRules.evaluate(RunRules.java:20)

at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)

at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)

at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)

at org.junit.runners.ParentRunner.run(ParentRunner.java:290)

at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)

at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)

at org.junit.runners.ParentRunner.access[=13=]0(ParentRunner.java:58)

at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)

at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)

at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)

at org.junit.runners.ParentRunner.run(ParentRunner.java:363)

at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)

at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)

at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)

at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)

at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)

at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)

at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)

仅对于 IE 11,您需要在目标计算机上设置注册表项,以便驱动程序可以保持与其创建的 Internet Explorer 实例的连接。对于 32 位 Windows 安装,您必须在注册表编辑器中检查的密钥是 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BFCACHE.

对于 64 位 Windows 安装,密钥是 HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BFCACHE。请注意 FEATURE_BFCACHE 子项可能存在也可能不存在,如果不存在则应创建。在此键中,创建一个名为 iexplore.exe 的 DWORD 值,值为 0.

IE 还会缓存 runs/usage 之间的内容,您必须清除缓存和个人设置。我发现以下内容有助于确保实例在启动时是干净的。这些是在实例化 WebDriver 时将传递到 IE 实例的选项。

本地:

var options = new InternetExplorerOptions();
options.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
//Clean the session before launching the browser
options.EnsureCleanSession = true;

远程:

capabilities = DesiredCapabilities.InternetExplorer();
capabilities.SetCapability("ie.ensureCleanSession", true);

你说,你运行测试Jenkins的时候,安装在你PC上,连浏览器都看不到,你试过chrome和IE
我个人在安装 Jenkins 刚启动时遇到过这种情况jenkins.exe。尝试使用 "java -jar jenkins.war" 命令将其 运行 作为服务器。 就我而言,这个问题帮助了我 Jenkins : Selenium GUI tests are not visible on Windows