将Jenkins作为服务运行时浏览器无法打开

时间:2018-11-14 10:33:17

标签: jenkins selenium-webdriver

借助Jenkins,我正在Windows VM中的IE中运行硒测试。当运行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

1 个答案:

答案 0 :(得分:0)

错误日志指出您的测试由于以下原因而失败:

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 
    }
}