2017-02-16 26 views
0

我正在構建一個測試框架,我的目標是測試使用此框架的許多具有相互之間光照差異的類似頁面的網站。WebElements的Selenium Dynamic Selectors

我有一個問題,我希望WebElements選擇器是動態的,這意味着我想通過我想要的方式將查找元素作爲FindElement方法的參數。

我想建立這樣的事情:

public class WebComponent 
     { 
      public int ID { get; set; } 
      public string Name { get; set; } 
      public string Description { get; set; } 
      public IWebElement WebElement{get;set;} 
      public Accessor Accessor { get; set; } 
      public WebComponent() 
      { 
       Accessor = new Accessor(); 
      } 

    } 
    public class Accessor 
    { 
     OpenQA.Selenium.By By { get; set; } 
     public string Value { get; set; } 
    } 

,後來在我的代碼時,我想有這樣的類的實例:

WebComponent component = new WebComponent(); 
component.ID = 1; 
component.Name = "Logout Button"; 
component.Description = "The button to click when user wants to logout of website"; 
component.Accessor.By = By.Id; 
component.Accessor.Value = "logout"; 
component.WebElement = Browser.Driver.FindElement(//missing code); 

我的問題是我怎麼能找到WebElement使用component.Accessor,任何建議或建議編輯將非常感激。

回答

1

By.Id是一個方法組,您不能將它分配給類型OpenQA.Selenium.By。分配應該是

component.Accessor.By = By.id("logout"); // or any other By and value. 

然後你就可以找到使用

component.WebElement = Browser.Driver.FindElement(component.Accessor.By); 

編輯

要選擇的定位和價值動態,你可以這樣做

元素
private By chooseType(String locatorType, string value) { 
    switch(locatorType) { 
     case "id": 
      return By.id(value); 
     case "class": 
      return By.className(value); 
     //... 
    } 
} 
+0

但是這個wa y不會允許我使用可能在{Id,Name,ClassName等之間更改的動態訪問方法。} –

+0

@YahyaHHussein爲什麼不呢?你可以做'component.Accessor.By = By.Id(「logout」);'並在'component.Accessor.By = By.className(「someClass」);'下面進行操作。 '現在component.Accessor.By'是'By.className(「someClass」);' – Guy

+0

想想如果我想讓測試者選擇使用組合框的訪問方法。這種方式將需要每個訪問方法的條件。 –

相關問題