首页 文章

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

提问于
浏览
0

我在Jenkins的帮助下在Windows VM中运行我的selenium测试 . 当运行java -jar jenkins.war时,我的套件工作正常..

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

我打开了服务属性,并允许Allow服务与桌面交互 .

我正在运行java -jar jenkins.war的另一种方式作为启动..但是当我登录VM时这是启动jenkins但是如果由于Windows补丁更新的一些情况如果机器重新启动这不起作用直到我登录VM不是我想要的 .

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

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

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

相关问题