问题
I have to develop a unit test which fails if element is present and passes the test if element is not present.
To be detailed, I have a simple form like name, email, address etc. When I click on the save button, error message is displayed if required fields are empty. If error message is displayed then I have to fail the test and if not displayed then pass the test. Is there any solution?
try
{
//Click on save button
IWebElement save_profile = driver.FindElement(By.XPath("//div[@class='form-group buttons']/div/input"));
save_profile.Click();
//Locate Error Message below the text box
IWebElement FirstNameError = driver.FindElement(By.XPath("//form[@class='default form-horizontal']/fieldset/div[4]/div[2]/span/div"));
//I want to fail the test here if above element is found
}
catch
{
//pass the test if element is not found in try statement
}
I think I am going in the wrong direction but couldn't find the solution. Please advice. Thansk in advance.
回答1:
Following is the behavior of "findElement" If the element you are looking exists, it returns the WebElement. If the element does not exist, it throws Exception and if not handled properly leads troubles.
So use "findElements"(here observe 's' at the end) Following is the behavior of "findElements" It returns a list, If the element exists, the size obviously more than 0, If the element does not exist the size will be 0. So you can just put a if condition with size
Here is pseudo code in Java
if (driver.findElements(By.XPath("//div[@class='form-group buttons']/div/input")).size()>0)
//print element exists
else
//print element does not exists.
I hope the above helps.
回答2:
You have two options to do this.
Firstly, by using Assert.assertTrue(boolean value);
try
{
//Click on save button
IWebElement save_profile = driver.FindElement(By.XPath("//div[@class='form-group buttons']/div/input"));
save_profile.Click();
//Locate Error Message below the text box
IWebElement FirstNameError = driver.FindElement(By.XPath("//form[@class='default form-horizontal']/fieldset/div[4]/div[2]/span/div"));
//I want to fail the test here if above element is found
Assert.assertTrue(false);
}
catch
{
//pass the test if element is not found in try statement
Assert.assertTrue(true);
}
Secondly, by using boolean return type if used inside a method.:
boolean isValidated = false;
try
{
//Click on save button
IWebElement save_profile = driver.FindElement(By.XPath("//div[@class='form-group buttons']/div/input"));
save_profile.Click();
//Locate Error Message below the text box
IWebElement FirstNameError = driver.FindElement(By.XPath("//form[@class='default form-horizontal']/fieldset/div[4]/div[2]/span/div"));
//I want to fail the test here if above element is found
isValidated =false;
return isValidated;
}
catch(Exception e)
{
//pass the test if element is not found in try statement
isValidated = true;
return isValidated;
}
If the method returns true, pass the test and vice versa
来源:https://stackoverflow.com/questions/25276857/how-to-fail-the-test-if-element-is-found-and-pass-the-test-if-element-is-not-fou