未处理警报异常:消除用户提示对话框测试在控制台上运行,但不在Jenkins中运行

l0oc07j2  于 2023-02-15  发布在  Jenkins
关注(0)|答案(2)|浏览(115)

我用selenium webDriver运行我的测试自动化。它在intellij、我的计算机cmd和服务器cmd(Jenkins通过bat脚本运行)上运行得非常好。但是,当用Jenkins运行我的bat文件时,它抛出了这个异常。
这是我的错误日志(出于安全原因,我更改了一些名称):

Feb 13, 2019 1:25:45 PM org.openqa.selenium.support.ui.ExpectedConditions findElement
WARNING: WebDriverException thrown by findElement(By.cssSelector: a[class="icon-info"])
org.openqa.selenium.UnhandledAlertException: Dismissed user prompt dialog: https://myAuthentification.com is requesting your username and password.: 
Build info: version: '3.14.0', revision: 'aacccce0', time: '2018-08-02T20:13:22.693Z'
System info: host: 'HOSTSRV', ip: '129.103.116.133', os.name: 'Windows Server 2012 R2', os.arch: 'amd64', os.version: '6.3', java.version: '1.8.0_141'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities {acceptInsecureCerts: true, browserName: firefox, browserVersion: 65.0, javascriptEnabled: true, moz:accessibilityChecks: false, moz:geckodriverVersion: 0.23.0, moz:headless: false, moz:processID: 21192, moz:profile: C:\Windows\Temp\rust_mozpro..., moz:shutdownTimeout: 60000, moz:useNonSpecCompliantPointerOrigin: false, moz:webdriverClick: true, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, platformVersion: 6.3, rotatable: false, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: f3bbe8e0-771b-484e-82c4-ab165de1cb23

我已经更新了Jenkins和所有插件,更新了Firefox,并尝试了switchTo().alert.accept()是否可以在不同的位置工作。
这是我的方法现在的样子:

protected void checkForEntitlementAutoIt(WebDriver driver) {

    // waits for alert to be present, if not throws exception
   WebDriverWait wait = new WebDriverWait(driver, Settings.waitAlert);
    Alert alert;
    try {
        try {
            wait.until(ExpectedConditions.alertIsPresent());
            driver.switchTo().alert();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.message(driver, "Alert is present.");
    } catch(Exception e) {
        Log.message(driver, "Alert not present.");
        throw e;

    }

    /* searches for login data
     *    -> if script present, use script
     *    -> if not use data in object "login"
     */
    try {
        if(script != null && new File(script).exists()) {
            Log.message(driver, "Executing script.exe ...");
            Process runExe = Runtime.getRuntime().exec(script);
            runExe.waitFor();
        } else {
            AutoItScript script = new AutoItScript(login.getEmail(), login.getPW());
            script.create();
            script.run();
            script.delete();
        }
        //wait for alert again to check if success
        try {
            wait.until(ExpectedConditions.alertIsPresent());
            driver.switchTo().alert().accept();
        } catch (Exception e) {
            e.printStackTrace();
        }
    } catch(org.openqa.selenium.NoAlertPresentException E) {
        Log.message(driver, "No alert: Authentication succeeded.");
    } catch(org.openqa.selenium.TimeoutException E) {
        Log.message(driver, "Timeout: Authentication succeeded.");
    } catch(InterruptedException | IOException e) {
        e.printStackTrace();
        Log.message(driver, "Err: Something went wrong. Exiting...");
        System.exit(0);
    }
}

它应该切换到报警窗口,然后执行登录脚本并接受报警

c7rzv4ha

c7rzv4ha1#

所以我终于找到了一个几乎太简单的解决方案,我不知道为什么我没有早点这样做(可能是因为我太专注于为什么警报不在那里)。
我试图找到另一种方法来将我的凭据输入到警报中(在我习惯于执行包含用户名和密码的额外脚本之前)。
现在我只是从脚本中获取我的用户名和密码,并使用“sendKeys”参数将它们放入警报中。
之前

try {
            Process execScript = Runtime.getRuntime().exec("autoit/autoit.exe " + this.path + this.file);
            execScript.waitFor();
            Log.message("Executed script " + this.file + ".");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();

之后

try {
            driver.switchTo().alert().sendKeys(this.user+Keys.TAB+this.pw);
            Log.message("send keys");
        } catch (Exception e) {
            e.printStackTrace();
        }
yh2wf1be

yh2wf1be2#

如果有人像我一样在这里结束,为了贝哈特/mink:

/**
 * Click a button by it's id or name and confirm the dialog
 *
 * @When I click the object :idOrName and confirm
 */
public function iClickAndConfirm($idOrName)
{
    $this->getSession()->getDriver()->executeScript('window.confirm = function () { return true; };');
    $this->getSession()->getPage()->find('named', ['id_or_name', $idOrName])->click();
}

相关问题