如何使用WebDriver处理在页面导航中发生的警报?

编程入门 行业动态 更新时间:2024-10-27 00:34:37
本文介绍了如何使用WebDriver处理在页面导航中发生的警报?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这里的情况是,我正在测试的某些页面将有一个卸载事件-即,如果有未保存的更改,则提示保存更改是一个常见的例子,我希望能够检测到并处理它.

The scenario here is that certain pages I'm testing will have an unload event - i.e. prompt to save changes if there are unsaved changes is a common example and I want to be able to detect that and handle it.

这是具体问题:

我正在测试一个非常复杂的Web应用程序,它将允许用户在浏览器中编辑丰富的内容,并且该应用程序将自动保存用户的更改.因此,此测试执行以下操作:

I'm testing a pretty complicated web app which will allow users to edit rich content in a browser and the app will auto-save changes from the user. So this test does something like the following:

  • 导航到应用程序
  • 进行一些编辑
  • 导航离开
  • 但是,由于该应用在导航时会自动保存更改,并且有未保存的更改-此提示将显示: i.stack.imgur/c9iP2.png

    However, since the app auto-saves changes on navigating away and there are unsaved changes - this prompt will show up: i.stack.imgur/c9iP2.png

    每当Selenium中出现警报时,下一步操作都会因类似以下的调用堆栈而惨败:

    Whenever there is an alert in Selenium, the next action will fail miserably with a callstack like:

    org.openqa.selenium.UnhandledAlertException: unexpected alert open (Session info: chrome=39.0.2171.65) (Driver info: chromedriver=2.11.298604 (75ea2fdb5c87f133a8e1b8da16f6091fb7d5321e),platform=Windows NT 6.1 SP1 x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 87 milliseconds: null Build info: version: '2.43.1', revision: '5163bceef1bc36d43f3dc0b83c88998168a363a0', time: '2014-09-10 09:43:55' System info: host: 'ip-10-231-174-40', ip: '10.231.174.40', os.name: 'Linux', os.arch: 'amd64', os.version: '3.11.0-19-generic', java.version: '1.7.0_51' Session ID: 889cbda1d1a946a38e90e4ec9f32e827 Driver info: org.openqa.selenium.remote.RemoteWebDriver Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true, hasMetadata=true, browserName=chrome, chrome={userDataDir=C:\Users\ADMINI~1\AppData\Local\Temp\scoped_dir1584_15883}, rotatable=false, mobileEmulationEnabled=false, locationContextEnabled=true, webdriver.remote.sessionid=889cbda1d1a946a38e90e4ec9f32e827, version=39.0.2171.65, takesHeapSnapshot=true, databaseEnabled=false, cssSelectorsEnabled=true, handlesAlerts=true, browserConnectionEnabled=false, nativeEvents=true, webStorageEnabled=true, applicationCacheEnabled=false, takesScreenshot=true}] at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:526) at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204) at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:162) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:614) at org.openqa.selenium.remote.RemoteWebDriver.getCurrentUrl(RemoteWebDriver.java:319)

    所以一种解决方案可能是,每当我的测试离开此页面时,我都会尝试/捕获,但是我想知道是否存在更优雅,更系统的解决方案(例如,可以检测警报或页面导航以使它们得到处理).

    So one solution might be that I put try/catches whenever my test navigates away from this page but I'm wondering if there is a more elegant and systematic solution (i.e. something that would either detect alerts or page navigation so they get handled).

    有人对此问题有可行的解决方案吗?

    Does anyone have a working solution for this problem?

    推荐答案

    您需要切换到警报并接受. Java示例:

    You need to switch to alert and accept it. Example in Java:

    Alert alert = driver.switchTo().alert(); alert.accept();

    在切换之前,您可能还需要等待它出现:

    You may also need to wait for it to appear before switching:

    WebDriverWait wait = new WebDriverWait(driver, 10); wait.until(ExpectedConditions.alertIsPresent()); Alert alert = driver.switchTo().alert(); alert.accept();

    或者,您可以停止将弹出窗口显示在第一位.这个想法是用javascript删除所有beforeunload事件监听器:

    Alternatively, you can stop the popup to be shown in the first place. The idea is to remove all beforeunload event listeners with javascript:

    JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript( "window.addEventListener(\"load\", foo, false);" + "function foo() { " + "var u = \"beforeunload\";" + "var v = unsafeWindow;" + "if (v._eventTypes && v._eventTypes[u]) {" + "var r=v._eventTypes[u];" + "for(var s=0;s<r.length;s++) { " + "v.removeEventListener(u,r[s],false);" + "}" + "v._eventTypes[u]=[];" + "}");

    更多推荐

    如何使用WebDriver处理在页面导航中发生的警报?

    本文发布于:2023-11-27 23:00:04,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1639963.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:警报   如何使用   发生   页面   WebDriver

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!