mercredi 21 octobre 2015

Can I write that code more efficient/smooth?

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.

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?

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire