Checking if text is NOT displayed 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:

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 &gt; 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 &gt; 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?
Your IsElementVisible method can be condensed to:
public bool IsElementVisible(IWebDriver driver, By element)
{
    return driver.FindElements(element).Count &gt; 0
        &amp;&amp; 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)