2017-10-18 115 views
0

我想使用Assert來驗證在FB登錄頁面上顯示的月份。如何使用selenium webdriver中的斷言列表

但是我得到的輸出是不正確的。

下面是代碼:

public class DropDown3 { 

    @Test 

    public void selectByvalue() throws InterruptedException{ 

     WebDriver driver = new FirefoxDriver(); 

     driver.get("https://www.facebook.com/"); 

     driver.manage().window().maximize(); 

     WebElement month_dropdown= driver.findElement(By.id("month")); 

     Select month_dd = new Select(month_dropdown); 


     List<WebElement> month_list = month_dd.getOptions(); 

     int total_months = month_list.size(); 

     System.out.println("Total month Count is = " +total_months); 


     String[] exp = {"Month", "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"}; 

     for (WebElement ele: month_list){ 


      for(int i=0;i<exp.length;i++){ 

       if(ele.getText().equals(exp[i])){ 

        System.out.println("Matched "); 

      } 

回答

0

使用下面

public class DropDown3 { 

    @Test 

    public void selectByvalue() throws InterruptedException{ 

     WebDriver driver = new FirefoxDriver(); 

     driver.get("https://www.facebook.com/"); 

     driver.manage().window().maximize(); 




     List<WebElement> months = driver.findElements(By.xpath("//select[@id = 'month']/*")); 

     int total_months = months_list.size(); 

     System.out.println("Total month Count is = " +total_months); 


     String[] exp = {"Month", "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"}; 

     int count = 0; 
     for (WebElement ele: months){ 
       count++; 
       Assert.assertTrue(ele.getText().toLowerCase().equals(exp[count].toLowerCase){ 

        System.out.println("Matched "); 

      } 
0

@Sam下面的代碼:請找到附在下面的代碼片段:

 for(int count =0 ; count < month_list.size(); count++) 
    { 
     WebElement ele = month_dd.getOptions().get(count); 
     System.out.println(ele.getText()); 
     for(int i=0;i<exp.length;i++){ 
      if(ele.getText().toLowerCase().equals(exp[i].toLowerCase())){ 
       System.out.println("Matched "); 

      } 
     } 
    } 

供參考:獲取id:month選項的值如下 - 月,一月,二月,三月,四月,五月,六月,七月,八月,九月,十月,十一月,十二月 你可以,如果你想比較實際的字符串中刪除.toLowerCase()函數。

0

替換下面的代碼片段在代碼:

WebElement month_dropdown= driver.findElement(By.id("month")); 
Select month_dd = new Select(month_dropdown); 
List<WebElement> month_list = month_dd.getOptions(); 
int total_months = month_list.size(); 
System.out.println("Total month Count is = " +total_months); 
String[] exp = {"Month", "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"}; 

for (int i=0;i<total_months;i++) 
{ 

    // Use Assertion you yow want to abort execution if value is not expected 
      //assertEquals(month_list.get(i).getText().toUpperCase(), exp[i].toUpperCase()); 

    if(month_list.get(i).getText().equalsIgnoreCase(exp[i])) 
    { 
     System.out.println("Matching value : "+month_list.get(i).getText()+" : : "+exp[i]); 
    } 
    else 
    { 
     System.out.println("Non Matching value : "+month_list.get(i).getText()+" : : "+exp[i]); 
    } 

} 
0

你需要採取一些事情的護理如下:

  1. Firefox Browser以最大化模式打開,這樣你就可以eleminate driver.manage().window().maximize();
  2. 您正在將month_dd.getOptions();存儲在List<WebElement>中,因此稍後要比較這些值,我們更好地使用List<String>而不是String[]數組。
  3. 初始化一個迭代器遍歷List
  4. 在比較,而不是equals使用equalsIgnoreCase將採取案件敏感的照顧文本。
  5. 這裏是你自己的代碼有一些簡單的調整:

    public class Assert_List 
    { 
         @Test 
         public void selectByvalue() throws InterruptedException 
         { 
          System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe"); 
          WebDriver driver = new FirefoxDriver(); 
          driver.get("https://www.facebook.com/"); 
          WebElement month_dropdown= driver.findElement(By.id("month")); 
          Select month_dd = new Select(month_dropdown); 
          List<WebElement> month_list = month_dd.getOptions(); 
          System.out.println("Total month_list Count is = " +month_list.size()); 
          List<String> month = Arrays.asList("Month", "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"); 
          for(int i=0; i<month_list.size(); i++) 
          { 
           if(month_list.get(i).getText().equalsIgnoreCase(month.get(i))) 
            System.out.println("Matched"); 
           else 
            System.out.println("Didn't Match"); 
          } 
         } 
    } 
    

控制檯輸出如下:

Total month_list Count is = 13 
    Matched 
    Matched 
    Matched 
    Matched 
    Matched 
    Matched 
    Matched 
    Matched 
    Matched 
    Didn't Match 
    Matched 
    Matched 
    Matched 
    PASSED: selectByvalue 

    =============================================== 
     Default test 
     Tests run: 1, Failures: 0, Skips: 0 
    =============================================== 


    =============================================== 
    Default suite 
    Total tests run: 1, Failures: 0, Skips: 0 
    =============================================== 

Note:在https://www.facebook.com/月份是Sept其中as o烏爾List包含SEP。因此未匹配

0

如果您不想使用太多循環,則可以使用集合。

首先字符串數組轉換爲ArrayList。並創建一個包含預期ArrayList

List<String> months = new ArrayList<String>(Arrays.asList("Month", "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC")); 
List<String> actualMonths = new ArrayList<String>(); 


List<WebElement> month_list = month_dd.getOptions(); 
for (WebElement ele: month_list){ 
    actualMonths.add(ele.getText()); 

} 

如果順序並不重要,一個新的列表,同時應用列表排序上。如果訂單很重要,你甚至不需要進行排序。

Collections.sort(months); 
Collections.sort(actualMonths); 
Assert.assertTrue(months.equals(actualMonths)); 
相關問題