我试图检查页面上是否存在具有给定类的DIV
。测试团队使用的模式是查找元素,说明期望的结果,然后执行一个简单的if-else,如下所示(只是为了解释下面的布局)。WebDriver C# - IWebElement和字符串之间的平等检查
如果我从var topNavClassName assignment
的末尾删除.ToString()
,在if
声明平等检查报告Operator '=='
不能应用于IWebElement and string
类型的操作数。
但保持这和运行代码时,写入线回来为: 错误:
I expected to find the main navigation Div of 'container-fluid', but instead I got OpenQA.Selenium.Firefox.FirefoxWebElement
我怎么可以做什么期待,什么是找到了一个平等的检查?
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);
}
}
编辑:作为一个说明我试图改变,如果(topNavClassName == expectedTopNavClassName)
到如果(topNavClassName != null)
,我可以通过改变topNavClassName
类名称的字符串说("container-fluidssssss")
这件事上失败,它会失败。所以看起来有些东西正在被发现。
更新
进一步调查我自己的问题我修改了代码简单地检查所需的类名称的字符串存在在页面上。这很有效(显然?),但是我仍然认为最好是将字符串作为一种更直观的证据 - 它在那里并且符合预期。
下面是备用的代码:
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.感谢格式编辑:)