【问题标题】:Browser is not opening when running Jenkins as a service将 Jenkins 作为服务运行时浏览器未打开
【发布时间】:2018-11-14 10:33:16
【问题描述】:

在 Jenkins 的帮助下,我在 Windows VM 的 IE 中运行我的 selenium 测试。运行 java -jar jenkins.war 时,我的套件运行良好..

但是当 Jenkins 作为服务运行时,浏览器没有打开并且总是失败。

我已经打开了服务属性并给出了允许服务与桌面交互。

我运行 java -jar jenkins.war 作为启动的另一种方式.. 但是这是在我登录 VM 时启动 jenkins 但是如果由于 Windows 补丁更新而导致机器重新启动的某些情况下,这在我登录之前不起作用不是我想要的虚拟机。

Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73), userDataDir=C:\Windows\TEMP\scoped_dir6052_18573}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=70.0.3538.102, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=false, acceptInsecureCerts=false, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, setWindowRect=true, unexpectedAlertBehaviour=}]
Session ID: e6ef32b1e294ce9644a5078d9b8bf8c4
Failed ***********
Started InternetExplorerDriver server (32-bit)
2.51.0.0
Listening on port 45007
Only local connections are allowed
org.openqa.selenium.UnhandledAlertException: Modal dialog present: 
Build info: version: '2.41.0', revision: '3192d8a', time: '2014-03-27 17:18:15'
System info: host: 'xx-xx-xx', ip: 'xx.xx.xxx.xxx', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.8.0_191'
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
Capabilities [{browserAttachTimeout=0, enablePersistentHover=true, ie.forceCreateProcessApi=false, pageLoadStrategy=normal, ie.usePerProcessProxy=false, ignoreZoomSetting=false, handlesAlerts=true, version=11, platform=WINDOWS, nativeEvents=true, ie.ensureCleanSession=false, elementScrollBehavior=0, ie.browserCommandLineSwitches=, requireWindowFocus=false, browserName=internet explorer, initialBrowserUrl=http://localhost:45007/, takesScreenshot=true, javascriptEnabled=true, ignoreProtectedModeSettings=false, enableElementCacheCleanup=true, cssSelectorsEnabled=true, unexpectedAlertBehaviour=dismiss}]
Session ID: dbbe2316-a3b3-4e39-82e3-04dae698ec73
Nov 16, 2018 6:17:43 PM org.openqa.selenium.support.ui.ExpectedConditions findElement
WARNING: WebDriverException thrown by findElement(By.id: txtUserName)
org.openqa.selenium.UnhandledAlertException: Modal dialog present: 
Build info: version: '2.41.0', revision: '3192d8a', time: '2014-03-27 17:18:15'
System info: host: 'xxx-xx-xx', ip: 'xx.xx.xxx.xxx', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.8.0_191'
Driver info: org.openqa.selenium.ie.InternetExplorerDriver

【问题讨论】:

  • 您在 jenkins 控制台中看到了哪些错误?在您的问题中添加日志将有助于其他人更好地理解问题
  • 我在日志中没有看到任何错误
  • 这很奇怪......如果浏览器无法启动它应该在控制台中打印一些东西。只是为了确保我们在同一页面上,在每个 jenkins 作业中都有一个链接可以打开一个控制台,该控制台显示当前作业运行的进程日志
  • 我附上了异常日志。

标签: jenkins selenium-webdriver


【解决方案1】:

错误日志指出您的测试失败是因为:

org.openqa.selenium.UnhandledAlertException: Modal dialog present

当出现一些警报并且您的测试没有正确处理时会发生这种情况。要修复它,您需要在调用方法来查找元素findElement(By.id("txtUserName")) 之前添加一个逻辑来处理警报。代码可能如下所示:

try {
    Alert alert = driver.switchTo().alert();
    System.out.println("Alert appeared. Text= " + alert.getText());
    alert.accept();
} catch (NoAlertPresentException ignored) {
    //if alert is not present for some reason we don't want to fail and can continue a test 
}

或者您可以使用 try catch 换行到 findElement 调用,然后关闭警报,然后再次尝试您的逻辑:

WebElement el;
try {
    el = findElement(By.id("txtUserName"));
} catch (UnhandledAlertException f) {
    try {
        Alert alert = driver.switchTo().alert();
        System.out.println("Alert appeared. Text= " + alert.getText());
        alert.accept();
        el = findElement(By.id("txtUserName"));
    } catch (NoAlertPresentException ignored) {
        //if alert is not present for some reason we don't want to fail and can continue a test 
    }
}

【讨论】:

  • 但是在命令提示符下运行 Jenkins 时,相同的代码正在运行
  • 如果没有警报的详细信息,这有点难说,但事实是,在您的第二种情况下,有一些警报会影响您的测试。如果您添加带有警告文本的系统输出打印 ln,您很可能会看到问题所在。请分享提醒文本 - 它可能有助于解决您的问题。
猜你喜欢
  • 2020-12-09
  • 1970-01-01
  • 2016-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-03
  • 1970-01-01
  • 2014-02-04
相关资源
最近更新 更多