2015-11-17 19 views
0

此代码是由火狐硒创建:如何在不声明对象的情况下知道Selenium对象的方法? C#

[Test] 
    public void TheNB1UnitTest() 
    { 
     driver.Navigate().GoToUrl(baseURL + "/Login"); 
     driver.FindElement(By.Id("Group")).Clear(); 
    } 

    private bool IsElementPresent(By by) 
    { 
     try 
     { 
      driver.FindElement(by); 
      return true; 
     } 
     catch (NoSuchElementException) 
     { 
      return false; 
     } 
    }  

鉴于By是从类org.openqa.selenium package一个对象,如何IsElementPresent(By by)甚至利用它与问候driver.FindElement(By.Id("Group")).Clear();

+0

你能解释一下你是什么意思吗?即使使用它关于driver.FindElement(By.Id(“Group”))。Clear(); “你的意思是因为你没有把它声明为一个新对象? –

回答

0

您可以在IsElementPresent中设置断点以查看它何时被调用。然而在这种情况下,我猜IsElementPresent是生成的,但是直到你做了一些使用IsElementPresent的断言才会被使用。

的IsElementPresent产生,为您在您的单元测试断言:

public void TheNB1UnitTest() 
{ 
    driver.Navigate().GoToUrl(baseURL + "/Login"); 
    driver.FindElement(By.Id("Group")).Clear(); 

    Assert.IsTrue(IsElementPresent(By.<my search>)); 
} 

after-generating-webdriver-code-how-and-where-to-modify-code

1

已经在source code of C# binding of Selenium

一看,你会发现该方法定义为By.Id(),它是static方法:

public static By Id(string idToFind) 

也许这已经回答你的问题了?由于该方法是static,因此不需要“声明”任何内容。

,在这种方法中,你会发现这条线:

By by = new By(); 

所以的确创造并交给您IsElementPresent()方法的“新”的对象。

相关问题