2017-03-02 93 views
0

我试图检查页面上是否存在具有给定类的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.感谢格式编辑:)

回答

1

看起来你在这个例子中测试的条件将总是是真实的,因此不值得测试。原因如下:您找到topNav元素,因为它的是CSS类container-fluidDriver.Instance.FindElement(By.ClassName("container-fluid"))),并且稍后要测试该元素是否具有该特定CSS类。但如果情况并非如此,那么你首先就不会找到这个元素。

我会做什么,而不是试图寻找基于一些其他财产(最好是这样的ID,名称,或XPath)该元素,然后验证找到的元素也有你想要的CSS类检查。 这里是你如何能做到这一点:

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("..."); 
} 

请注意,我比较的不是WebElement本身,而是该元素的class属性的值。