patterncsharpModerate
Checking if text is NOT displayed
Viewed 0 times
textcheckingdisplayednot
Problem
I'm currently writing unit tests in Selenium (C#). I need to do two things in this particular test:
Check if 2 errors (with html container) are displayed and check if another (given) error is not displayed.
The test looks like this:
I use two methods here:
So, all of that is working, but I am not sure about quality of that code.
On the other hand, I don't need anything over-fancy. It just should be good, clean and understandable.
Should I keep it intact or update some parts?
Check if 2 errors (with html container) are displayed and check if another (given) error is not displayed.
The test looks like this:
bool errorWindowVisible = tmethods.IsElementVisible(driver, By.XPath("//div[@class='message error']"));
if (errorWindowVisible == false)
{
Assert.Fail("No error notification displayed.");
}
string errorTextTemp = "Error 1 text";
bool productIssue = tmethods.IsElementVisible(driver, By.XPath("//*[contains(.,'" + errorTextTemp + "')]"));
errorTextTemp = "Error 2 text";
bool productIssueB = tmethods.IsElementVisible(driver, By.XPath("//*[contains(.,'" + errorTextTemp + "')]"));
if (productIssue == false || productIssueB == false)
{
Assert.Fail("No information about error cause.");
}
// Checking if another one is not visible
errorTextTemp = "Extra error";
bool uploadedFileIssue = tmethods.IsElementNotVisible(driver, By.XPath("//*[contains(.,'" + errorTextTemp + "')]"));
if (uploadedFileIssue == false) // Should be set to true
{
Assert.Fail("Extra error shown.");
}I use two methods here:
public bool IsElementVisible(IWebDriver driver, By element)
{
if (driver.FindElements(element).Count > 0)
{
if (driver.FindElement(element).Displayed)
return true;
else
return false;
}
else
{
return false;
}
}
public bool IsElementNotVisible(IWebDriver driver, By element)
{
if (driver.FindElements(element).Count > 0)
{
if (driver.FindElement(element).Displayed)
return false;
else
return false;
}
else
{
return true;
}
}So, all of that is working, but I am not sure about quality of that code.
On the other hand, I don't need anything over-fancy. It just should be good, clean and understandable.
Should I keep it intact or update some parts?
Solution
Your
I guess the method
Boolean comparisons
Expressions
Expressions
For example, you don't need to write:
you could write instead:
IsElementVisible method can be condensed to:public bool IsElementVisible(IWebDriver driver, By element)
{
return driver.FindElements(element).Count > 0
&& driver.FindElement(element).Displayed;
}I guess the method
IsElementNotVisible should return boolean negation of result of the IsElementVisible method:public bool IsElementNotVisible(IWebDriver driver, By element)
{
return !IsElementVisible(driver, element);
}Boolean comparisons
Expressions
x == true and just x are eqivalent.Expressions
x == false and just !x are also eqivalent.For example, you don't need to write:
if (productIssue == false || productIssueB == false)you could write instead:
if (!productIssue || !productIssueB)Code Snippets
public bool IsElementVisible(IWebDriver driver, By element)
{
return driver.FindElements(element).Count > 0
&& driver.FindElement(element).Displayed;
}public bool IsElementNotVisible(IWebDriver driver, By element)
{
return !IsElementVisible(driver, element);
}if (productIssue == false || productIssueB == false)if (!productIssue || !productIssueB)Context
StackExchange Code Review Q#108321, answer score: 11
Revisions (0)
No revisions yet.