2017-03-04 171 views
0

我的测试检查用户是否可以登录到该站点。 我写了一个代码来提醒该作品,但在catch中显示部分。 我试图在没有trycatch的情况下写入警报,但显示错误。不显示警报

如何在没有trycatch的情况下编写报警,并显示警报(测试通过)。

org.openqa.selenium.TimeoutException: Expected condition failed: waiting for alert to be present (tried for 5 second(s) with 500 MILLISECONDS interval) 

这是我的代码:

public void getMessage() { 
     //Verify that is message preview 
     try { 
     WebDriverWait wait = new WebDriverWait(driver, 5); 
     wait.until(ExpectedConditions.alertIsPresent()); 
     Alert alert = driver.switchTo().alert(); 
     System.out.println(alert.getText()); 
     alert.accept(); 
     Assert.assertTrue(alert.getText().contains("ERROR: Invalid login credentials.")); 
     } catch (Exception e) { 
     System.out.println("Alert is not displayed"); 
     } 
+0

那么问题是什么? – Andersson

+1

我的猜测是警报是HTML对话框而不是JS警报。当警报弹出时,右键单击它。如果您获得右键单击上下文菜单并可以检查,那么它是基于HTML的,而不是警报。 – JeffC

+0

是的,这是html。

错误 :登录凭证无效。 –

回答

0

既然您已确认的对话框是不是真正的JS警告对话框中,你将无法使用Alert。好消息是,它就像HTML页面的其他部分一样,所以你只需要刮掉包含错误文本的元素即可。