我的测试检查我是否可以发送消息到邮件。当我单击发送按钮时,应该会检查警报。
我的警报代码是:
Alert alert = driver.switchTo().alert();
System.out.println(alert.getText());
alert.accept();
Assert.assertTrue(alert.getText().contains("Thanks."));
我的错误是:
Starting ChromeDriver 2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9eed) on port 13707
Only local connections are allowed.
org.openqa.selenium.NoAlertPresentException: no alert open
(Session info: chrome=55.0.2883.87)
(Driver info: chromedriver=2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9eed),platform=Windows NT 6.3.9600 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 76 milliseconds
Build info: version: 'unknown', revision: '31c43c8', time: '2016-08-02 21:57:56 -0700'
System info: host: 'Gaga', ip: '192.168.1.6', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.8.0_101'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9eed), userDataDir=C:\Users\Dragana\AppData\Local\Temp\scoped_dir6652_7181}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=55.0.2883.87, platform=WIN8_1, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: b7131fce27b2ef40f1daff3c82188e7c
3条答案
按热度按时间py49o6xq1#
代码必须等待警报。
fhity93d2#
使用
try-catch
是一种非常糟糕的方法,尤其是在基于 selenium 的测试用例中,应该限制它们的使用:让我们了解此问题的真实的解决方案,以及为什么给出的解决方案不正确,因为:
1.如果我们尝试在
alert.accept();
之后调用alert.getText()
,很明显它将抛出错误,因为我们已经关闭了警报。1.它工作正常,因为通过使用
try-catch
,它将处理异常。1.问题是这里从来不检查Assert。
代码:以下代码每次在检查Assert之前就会终止。
**已更正代码:**应在获取文本后调用
alert.accept();
。mhd8tkvw3#
org.openqa.selenium.NoAlertPresentException: no such alert (Session info: chrome=107.0.5304.107) Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03' System info: host: 'DESKTOP-9RL5BOK', ip: '192.168.56.1', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '19.0.1' Driver info: org.openqa.selenium.chrome.ChromeDriver Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 107.0.5304.107, chrome: {chromedriverVersion: 107.0.5304.62 (1eec40d3a576..., userDataDir: C:\Users\DELL\AppData\Local...}, goog:chromeOptions: {debuggerAddress: localhost:60059}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:virtualAuthenticators: true} Session ID: d9592a97d4ff927e0b3f2d997ed63817 at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:67) at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500) at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:484) at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187) at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122) at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49) at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158) at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:609) at org.openqa.selenium.remote.RemoteWebDriver$RemoteTargetLocator.alert(RemoteWebDriver.java:932) at features.Omayo.User_should_be_able_to_login_based_on_expected_login_status(Omayo.java:57) at ✽.User should be able to login based on "success" login status(classpath:features/omayologin.feature:8)