我试图在selenium中为gmail登录声明一个负面的测试用例,但最终会得到一个异常。任何人都可以提出问题是什么?
@Test(dataProvider="LoginData")
public void testMethod(String email,String password) throws InterruptedException{
browser.findElement(By.id("Email")).clear();
browser.findElement(By.id("Email")).sendKeys(email);
browser.findElement(By.id("Passwd")).sendKeys(password);
browser.findElement(By.id("signIn")).click();
browser.wait(30000);
assertTrue(browser.getPageSource().contains("The username or password you entered is incorrect."));
}
生成的异常
java.lang.IllegalMonitorStateException
at java.lang.Object.wait(Native Method)
at seleniumExcel.DataProviderAutomation.testMethod(DataProviderAutomation.java:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
答案 0 :(得分:2)
抓取整个页面源只测试一个小错误文本是不可行的。最好的方法是找到元素(当然,如果元素存在,我希望我首先确定)并对其执行.contains()
。因为,您没有提供任何html
我不知道您如何找到目标元素。但绝对要使用FindElement()
。类似下面的内容是您可能需要的代码块
@Test(dataProvider="LoginData")
public void testMethod(String email,String password) throws InterruptedException{
WebDriver browser = new ChromeDriver();
browser.findElement(By.id("Email")).clear();
browser.findElement(By.id("Email")).sendKeys(email);
browser.findElement(By.id("Passwd")).sendKeys(password);
browser.findElement(By.id("signIn")).click();
browser.wait(30000);
try{
String error = browser.findElement(By.id("Your id")).getText();
assertTrue(error.contains("The username or password you entered is incorrect."));
}catch (NoSuchElementException e){
//something else
}
}