Reputation: 635
I'm trying to check that a DIV
with a given Class is present on the page. There's a pattern used in the test team here that is to find the element, state what's expected then do a simple if-else, as below (just to explain the layout below).
If I remove the .ToString()
from the end of the var topNavClassName assignment
, the equality check in the if
statement reports Operator '=='
cannot be applied to operands of type IWebElement and string
.
But when keeping that and running the code the Write-line comes back as: error:
I expected to find the main navigation Div of 'container-fluid', but instead I got OpenQA.Selenium.Firefox.FirefoxWebElement
How can I do an equality check on what's expected and what's found?
public static void validateTopBarNavigationIsPresent()
{
var expectedTopNavClassName = "container-fluid";
// Find the navigation element container to check it loaded
var topNavClassName = Driver.Instance.FindElement(By.ClassName("container-fluid")).ToString();
Console.WriteLine($"The variable topNavClassName contains the value: {topNavClassName}");
// OR perhaps it's better to find it this way?
//IJavaScriptExecutor js = Driver.Instance as IJavaScriptExecutor;
//string topNavClassHTML = (string)js.ExecuteScript("return arguments[0].innerHTML;", expectedTopNavClassName);
//Console.WriteLine($"The variable url contains the value {topNavClassHTML}");
if (topNavClassName == expectedTopNavClassName)
{
Console.WriteLine($"I found the main navigation Div by its Class Name: {topNavClassName}");
}
else
{
Console.WriteLine("The main navigation Div was NOT located on the page");
var topBarNavigationException = $"I expected to find the main navigation Div of 'container-fluid', but instead I got {topNavClassName}";
TakeScreenshot.SaveScreenshot();
throw new Exception(topBarNavigationException);
}
}
Edit: As a note I have tried changing if (topNavClassName == expectedTopNavClassName)
to if (topNavClassName != null)
and I can make this fail by changing the topNavClassName
class name string to say ("container-fluidssssss")
and it will fail. So it appears something is getting found.
Update
Further investigating my own problem I modified the code to simply check if the desired class name string was present on the page. This works (obviously?) but I'm still feeling it's better to actually grab the strings as a more visual proof-positive it's there and matches what's expected.
Here's the alternate code:
public static void validateTopBarNavigationIsPresent()
{
bool topNavClassName = Driver.Instance.PageSource.Contains("container-fluid");
if (topNavClassName == true)
{
Console.WriteLine($"The check for 'container-fluid' being present came back as: {topNavClassName.ToString().ToUpper()}");
}
else
{
var topBarNavigationException = $"The check for 'container-fluid' being present came back as: {topNavClassName.ToString().ToUpper()} but I expected TRUE";
TakeScreenshot.SaveScreenshot();
throw new Exception(topBarNavigationException);
}
}
p.s. Thanks for the formatting edits :)
Upvotes: 0
Views: 1956
Reputation: 1340
It seems like the condition you're testing for in this example will always be true, and is therefore not worth testing. Here's why: You're finding the topNav
element because of it's CSS class container-fluid
(Driver.Instance.FindElement(By.ClassName("container-fluid"))
), and later want to test whether that element has that particular CSS class. But iff that weren't the case, you wouldn't have found that element in the first place.
What I would do instead is trying to find that element based on some other property (ideally something like an ID, a name, or an XPath), and then validate that the found element also has the CSS class that you want to check for. Here's how you could do that:
var expectedTopNavClassName = "container-fluid";
var topNavElement = Driver.Instance.FindElement(By.Id("..."));
var topNavClassName = topNavElement.GetAttribute("class");
Console.WriteLine($"The variable topNavClassName contains the value: {topNavClassName}");
if (topNavClassName != expectedTopNavClassName)
{
throw new Exception("...");
}
Note that the thing I'm comparing is not the WebElement
itself, but the value of the class
attribute of that element.
Upvotes: 1