我希望能够检查表单是否已提交,然后如果表单未提交,则会显示错误消息以将错误打印到记者日志或记录器。
点击登录按钮,我的代码如下:请检查
WebElement login = driver.findElement(By.id("dijit_form_Button_1_label"));
login button.
if(login.isDisplayed()){
login.click();
Reporter.log("Login Form Submitted | ");
logger1.info("Submit Button Clicked");
} else {
Reporter.log("Login Failed | ");
Assert.fail("Login Failed - Check Data | ");
}
我传入参数来填写登录表单,我在我的数据提供程序中设置了一个不正确的值,但是,当它到达这段代码时,表单总是被提交,所以如果数据是纠正代码运行正常并打印登录表单成功等。
如果登录表单没有提交但是被点击,那么您将返回同一个登录屏幕并显示错误,代码的else部分永远不会打印出来?
因此,如果单击该按钮但是无法登录,则需要能够单击该按钮,然后将else打印到记录器/记者日志。
如果没有登录,如何打印代码的else部分?
答案 0 :(得分:0)
您需要交叉检查登录是否已完成。有效凭据尝试交叉检查任何消息,如欢迎或可能会注销按钮等..如果他们没有显示,失败。
错误的凭据,交叉检查错误消息。如果显示错误消息,则通过,否则失败。
下面是一个示例,我在点击按钮后查找要显示的消息。它可以帮助你提出好主意
//System.setProperty("WebDriver.chrome.driver", "C:\\Selenium Webdrivers\\chromedriver_win_26.0.1383.0\\chromedriver.exe");
WebDriver driver=new FirefoxDriver();
driver.get("http://seleniumtrainer.com/components/buttons/");
driver.findElement(By.id("button1")).click();
//by using if condition to cross check
try{
if(driver.findElement(By.id("demo")).isDisplayed()==true){
System.out.println("clicked correctly");
}
}catch(Exception e){
System.out.println("U Clicked Button1 text is not displayed. so failing or throwing again to fail test");
throw new Exception(e.getMessage());
}
//simply by using testng assertion
Assert.assertEquals(driver.findElement(By.id("demo")).getText(), "U Clicked Button1");
由于
答案 1 :(得分:0)
你的执行顺序错了。
Psuedo代码应如下所示。
// Enter the form values
// Check if login button and click on it to submit the form
WebElement loginButton = driver.findElement(By.id("dijit_form_Button_1_label"));
if (loginButton.isDisplayed) {
loginButton.click();
Reporter.log("Submit Button Clicked");
}
// Now check again for the login button.
// If login button is displayed it means that the form submission
// is not successful and you have returned to the same page.
// We are using a try catch block here because if the element is not found
// ie. if form submission is successful NoSuchElementException would be thrown.
try {
// Element has to be found again else StaleElementReferenceException
// might be thrown because of page reload.
loginButton = driver.findElement(By.id("dijit_form_Button_1_label"));
// if login button is found again, this means form submission is not successful
Reporter.log("Form Not submitted");
} catch (Exception e) {
// Login button is not found. Form is submitted
Reporter.log("Form submitted successfully");
}
希望这会对你有所帮助。