如何使用WebDriver处理在页面导航中发生的警报? [英] How can I handle alerts that occur on page navigation with WebDriver?

查看:75
本文介绍了如何使用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:

  1. 导航到应用程序
  2. 进行一些编辑
  3. 导航离开

但是,由于该应用在导航时会自动保存更改,并且有未保存的更改-此提示将显示: http://i.stack.imgur.com/c9iP2.png

However, since the app auto-saves changes on navigating away and there are unsaved changes - this prompt will show up: http://i.stack.imgur.com/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处理在页面导航中发生的警报?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆