1

I am automating a form, the scenario is, when an invalid entry is given, the message 'Success' shouldn't appear

I tried to check this using

s_assert.assertEquals(driver.findElement(By.xpath("//div[contains(.,'Succes')]")).isDisplayed(), false);

But, while running, it shows 'Unable to locate element:'

The message appears only if my test fails. So expected behavior is that, the element won't be present.

How to tell webdriver to just check if it is present and to not to throw an error! Thanks in advance...

Saifur
  • 16,081
  • 6
  • 49
  • 73
Shari
  • 123
  • 1
  • 1
  • 9

2 Answers2

0

The problem you are facing is not related to Assertion rather find element mechanism. You are expecting an element that was not located by the Selenium and thus code has not reached till the Assertion

You have to do some kind of exception handling depending on what you want. before assert.

public bool Test()
{
    try
    {

        Driver.FindElement(By.Id("test"));
        return true;
    }
    catch (Exception ex)
    { // catch the exception you want
        return false;
    }
}

public void TestAssert()
{
    Assert.AreEqual(Test(),false);
}

Note: mine is C# and NUnit

Saifur
  • 16,081
  • 6
  • 49
  • 73
  • Thanks for the reply.. I just want to check if an element is present. Any way other than using findElement? – Shari Mar 25 '15 at 18:41
0

Thanks... I added the below code which fixed my problem.

     public static boolean isElementPresent(String xpath) {
          try {
           driver.findElement(By.xpath(xpath));
           return true;
          } catch (Exception e) {
           return false;
          }
         }

     public void formsNegetive() {                  

boolean b = isElementPresent(("//div[@class='alert-box success']")); if (b) { s_assert.assertEquals("Success: You will be contacted by our experts soon.", driver.findElement(By.cssSelector("div.alert-box.success")).getText()); }
}

Shari
  • 123
  • 1
  • 1
  • 9