我需要使用java读取弹出窗口中显示的警报和确认消息,并在控制台上打印.在从IDE导出selenium记录作为Junit4(WebDriver)java文件时,我的代码是:
private WebDriver driver;
private String baseUrl;
private StringBuffer verificationErrors = new StringBuffer();
@Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
}
Run Code Online (Sandbox Code Playgroud)
现在当我尝试使用getAlert或getConfirmation功能如下所示:
@Test
public void testSample() throws Exception {
Alert alert = driver.switchTo().alert();
message = alert.getText();
System.out.println("message is "+message);
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
java.lang.NullPointerException
at com.example.tests.Sample.testSample(Sample.java:40)
at com.example.tests.Sample.main(Sample.java:149)
Exception: null
Run Code Online (Sandbox Code Playgroud)
我该如何处理?还有其他方法来阅读弹出消息吗?