Adrija Adrija - 1 year ago 145
C# Question

Assert statement in Selenium using C#

I am trying to make LoginFailTest, the idea is to give a successful result if the Login Fails and if it Logs in, it should give a Test Failed result.

The HTML code behind the page is:

<div id="waw1" class="grid" style="margin-top: 30px; padding-top: 20px;">
<div id="waw2" class="notice error">
<i class="icon-ok icon-large"/>
Please make sure the Organization ID, User ID, and Password you entered are correct.
<a href="#close" class="icon-remove"/>

The C# code is:

public class LoginFailTest : ibankTest
public void User_Login_Fail()
Assert.IsTrue(LoginFail.Title, "Logged In");

My LoginFail class code is:

public class LoginFail
public static bool Title
var title = Driver.Instance.FindElements(By.XPath("//*[@id='waw2']"));
if (title != null)
return true;
return false;

The problem that I am facing is even if it Logs in, it is giving a successful test result, it should give me a Test Fails result here.

Can someone please help?
Thanks in advance

Answer Source

Actually you are handling the condition on the basis of element existence which is wrong because the element with the id always existing on the page, you need to check existence of text instead as below :

var title = Driver.Instance.FindElements(By.XPath("//*[contains(text(), 'Please make sure the Organization ID, User ID, and Password you entered are  correct.')]"));
if (title.Count > 0)
   return true;
return false;

Note : - FindElements always returns list of IWebElements if found otherwise returns empty list, It does not return null so you need to check its count to verify list is empty or not instead.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download