2015-10-06 90 views
-1

[![在這裏輸入的形象描述] [1] [1]硒webdriver的標籤自動化驗證

在圖像的編號不是序列順序,我需要驗證,如果沒有在序列1, 2,3,4然後測試用例通過否則失敗。

社交網絡URL#1: 文本框中

社交網絡URL#2: 文本框中 社交網絡URL#1: 文本 社交網絡URL#1: 文本框中

要驗證編號應該去1,2,3,4

請幫忙,他們不允許我附上圖片以獲得更多通關

回答

-1

您可以通過driver.find_elements_by_css_selector(css_selector)與給定css_selector然後得到的所有元素,你可以得到它的.text文本最終檢查數量sequencially增加:

的Python:

def check_if_sequencial(css_selector): 
    result = True 
    elms = driver.find_elements_by_css_selector(css_selector) 

    for i in xrange(len(elms) - 1): 
     text1 = elms[i].text 
     text2 = elms[i+1].text 

     # slicing after dash to end of string, convert to int 
     number1 = int(text1[text1.index("#")+1 : len(text1)]) 
     number2 = int(text1[text2.index("#")+1 : len(text2)]) 

     if number2 != number1 + 1: 
      print "number1: %d and number2: %d are not sequencial" %(number1, number2) 
      result = False 
      break 

    return result 

的Java:

public boolean check_if_sequencial(String css_selector){ 
    boolean result = true; 
    List elms = driver.findElements(By.cssSelector(css_selector)); 

    for (int i = 0; i < elms.length - 1; i++) { 
     String text1 = elms[i].text; 
     String text2 = elms[i+1].text; 

     # spliting by dash take the second, convert to int 
     String[] parts1 = text1.split("#"); 
     int number1 = Integer.parseInt(parts1[1]); 

     String[] parts2 = text2.split("#"); 
     int number2 = Integer.parseInt(parts2[1]); 

     if (number2 != number1 + 1){ 
      System.out.println("number1: " + number1 + " and number2: " + number2 + " are not sequencial"); 

      result = false; 
      break; 
     } 
    } 
    return result; 
} 
+0

先生我正在使用java, – abhiman

+1

社交網絡URL#1:文本框 社交網絡URL#2:文本框 社交網絡URL#1:文本框 社交網絡URL#1:文本框 – abhiman

+0

@abhiman添加了java代碼。 –