[英]How to check if button clicked, IF form is actually submitted Selenium / TestNG
I am new to java/selenium/testNG, I have an issue were I run 4 sets of data using the dataprovider, however, One set of data is incorrect just for the purpose of testing that the test works. 我是java / selenium / testNG的新手,我有一个问题,我使用dataprovider运行4组数据,但是,一组数据不正确,仅出于测试测试有效的目的。 I first check the button is displayed then clicked.
我首先检查显示的按钮,然后单击。 I want to be able to check that the button was clicked and then check if the form was submitted IF the form was not submitted then print error.
我希望能够检查是否单击了该按钮,然后如果未提交表单则检查是否提交了表单,然后打印错误。 I am unsure on how to do this my code is below:
我不确定如何执行我的代码如下:
WebElement login = driver.findElement(By.id("dijit_form_Button_1_label"));
//If statement - will check if element is Displayed before clicking on login button.
if(login.isDisplayed()){
login.click();
//Main Event is logged If Passed
Reporter.log("Login Form Submitted | ");
logger1.info("Submit Button Clicked");
}else{
Reporter.log("Login Failed | ");
Assert.fail("Login Failed - Check Data | ");
//Main Event Log Fail
}
WebElement logout = driver.findElement(By.id("dijit_form_Button_0_label"));
if(logout.isDisplayed()){
logout.click();
Reporter.log("Logout Successful | ");
}else {
Reporter.log("Logout Failed");
Assert.fail("Logout Failed - Check Data | ");
}
The test displays a error message as "logout Failed" but it should display the first message "Login Failed" How do I get it to check if login and form was submitted successfully? 测试显示一条错误消息为“注销失败”,但应显示第一条消息“登录失败”。如何获取它以检查登录名和表单是否已成功提交?
The reason it prints the second message is the code runs passed that point and selenium cant see the logout button displayed. 它输出第二条消息的原因是代码运行通过该点,并且硒无法看到显示的注销按钮。
Any thoughts on were I am going wrong? 我有什么想法要错吗?
Try this: 尝试这个:
private static By outButton = By.id("dijit_form_Button_0_label");
if(driver.findElements(outButton).size() > 0)
{
logout.click();
Reporter.log("Logout Successful | ");
}
after submitting the form, you should make the webdriver wait for some condition to be true. 提交表单后,您应该让网络驱动程序等待某些条件为真。
something like that 这样的东西
int timeout = 30
WebDriverWait wait = new WebDriverWait(driver, timeout);
wait.until(new Function <WebDriver, Boolean>(){
//@Override
public Boolean apply(WebDriver driver) {
//if the login has been successful return true
//if not, return false
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.