2016-05-20 73 views
-1

我需要在C#中使用Selenium驗證下拉值。我有一組值我想用這個IWebElements的IList再次進行測試:Selenium C# - 如何驗證下拉列表

 IList<IWebElement> yearsDropdown = { "Select...", "Before 1996", "1996", "1997", "1998", "1999", "2000" } 

我需要編寫從下拉返回所有值的函數(即具有相同的確切信息),然後做一個斷言來確認比賽。下面是我寫打印從下拉值的代碼,但我需要與預期的結果斷言這些值:

 IList<IWebElement> yearsDropdown = { "Select...", "Before 1996", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014", "2015", "2016" }; 
     string[] dropdownListArray = new string[23]; 
     IList<IWebElement> dropdownList = wd.FindElements(By.CssSelector("#Components_0__Entity_SourceInfo_Value_Entity_ModelYear")); 
     foreach(IWebElement i in dropdownList) 
     { 
      dropdownListArray[] = i.Text; 
     } 

     for(int i = 0; i < dropdownList.Count; i++) 
     { 
      Assert.AreEqual(yearsDropdown[i], dropdownList[i].Text); 
     } 

的事情是,當我嘗試這樣做,它帶回了整個下拉IWebElement IList dropdownList。我需要將IWebElement列表分解。

回答

1

{ "Select...", "Before 1996", "1996", "1997", "1998", "1999", "2000" }是字符串列表,而不是IWebElement。下面 見代碼:

List<string> yearsDropdown = new List<string>(){ "Select...", "Before 1996", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014", "2015", "2016" }; 
IList<IWebElement> dropdownList = wd.FindElements(By.CssSelector("#Components_0__Entity_SourceInfo_Value_Entity_ModelYear")); 
foreach(IWebElement i in dropdownList) 
{ 
    Assert.AreEqual(i.Text, yearsDropdown.Text); 
} 
+0

我一直得到這個錯誤:「數組初始只能在一個變量或字段初始化被用於嘗試使用新的表達式來代替。」 所以我採取了一個迂迴的方式解決它,只是將年下移到一個字符串,它的工作。爲了確保它在將來增加更多年時的工作,我會做一個if語句來更新變量,使其始終具有正確的年份。 – Tscott

+0

@Tscott對不起,我的錯誤,第一行應該是'List年DOPdown = new List (){「Select ...」,「1996年以前」,「1996」,「1997」,「1998」,「1999 「」「2000」「2001」「2002」「2003」「2004」「2005」「2006」「2007」「2008」「2009」「2010」「2011」 「2012」,「2013」​​,「2014」,「2015」,「2016」};'。我已經更新了我的答案。 – Buaban