Adrija Adrija - 4 months ago 13
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"/>
</div>


The C# code is:

[TestClass]
public class LoginFailTest : ibankTest
{
[TestMethod]
public void User_Login_Fail()
{
Assert.IsTrue(LoginFail.Title, "Logged In");
}
}


My LoginFail class code is:

public class LoginFail
{
public static bool Title
{
get
{
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

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.