如何使用Selenium Webriver处理保存在同一个html标记中的月份和年份的日期选择器?

问题描述:

我想创建一个接受字符串变量的方法'2018年4月1日'。该字符串将被拆分,并且日期'成分'将用于在选取器See the picker image中选择日期。我已经涵盖了选择日期和点击箭头来改变'月份 - 年份'对。我也知道如何达到用户插入的年份,但我不确定如何选择当年达到的正确月份。下面是我到目前为止的代码。如何使用Selenium Webriver处理保存在同一个html标记中的月份和年份的日期选择器?

public void pickTheDate() { 

    String date = "10 April 2018"; 
    String[] dateElements = date.split(" "); 
    WebElement nextMonth = driver.findElement(By.xpath("somexpath")); 
    WebElement previousMonth = driver.findElement(By.xpath("somexpath1")); 

    String dayFromUserInput = dateElements[0]; 
    String monthFromUserInput = dateElements[1]; 
    String yearFromUserInput = dateElements[2]; 

    WebDriverWait wait = new WebDriverWait(driver, 10); 

    WebElement datePickerHeader= wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("somexpath"))); 
    String textFromDatePickerHeader = datePickerHeader.getText(); 

让我们假设在日期选取器标题中的日期是“2017年十月”

String[] dateElementsFromDatePickerHeader = textFromDatePickerHeader.split(" "); 
    int yearDiff = Integer.parseInt(yearFromUserInput) - Integer.parseInt(dateElementsFromDatePickerHeader[1]); 

    if (yearDiff != 0) { 
     //moving to next year 
     if (yearDiff > 0) { 
      for (int i = 0; i < yearDiff; i++) { 
       nextMonth.click(); 
      } 

在这一点上,我知道我到达月份是“2018一月”

 } 
     //moving to previous year 
     else if (yearDiff < 0) { 
      for (int i = 0; i < (yearDiff * (-1)); i++) { 
       previousMonth.click(); 
      } 
     } 
    } 
} 

如果我想在2016年内搜索一个日期,我知道第一个遇到的月份是“2016年12月”。 请告诉我如何将合适的导航逻辑合并到用户插入的月份。提前致谢。

它取决于您的日期选择器行为。仅供参考我下面的日期选择器的结构:

enter image description here

如果点击“2017年十月”页眉然后打开个月的选择,如下图:

enter image description here

并再次点击“ 2017“标题然后打开选择年份

enter image description here

所以这里的自动化测试用例将

  1. 获取日期为2017年10月25日
  2. 拆分在3块
  3. 点击标题数据和导航,直到它显示年列表选择
  4. 导航日历收到一年即2017年
  5. 然后浏览和选择的月份,即收到十月
  6. 最后选择的日期即

在这里找到示例代码:

public void enterFromDate(String fromDate) throws InterruptedException 
{ 
    // Split the data and store it in string array 
    String[] data = fromDate.split(" "); 

    // click datepicker icon 
    fromDateCalenderIcon.click(); 
    // Click date picker header, It moves to month list 
    calenderHeader.click(); 
    // Again click date picker header, It now moves to Year listing 
    calenderHeader.click(); 


    mainloop: while(true) 
    { 
     //Iterate all year 
     for(int i=0;i<datePickerData.size();i++) 
     { 
      // check weather year available in list if yes then select it 
      if(datePickerData.get(i).getText().trim().contains(data[2])) 
      { 

       datePickerData.get(i).click();  
       break mainloop;      
      } 
     } 
      // else click back arrow 
     datePickerLeftArrow.click(); 
    } 

    // Iterate month list 
    for(WebElement e: datePickerData) 
    { 
     // If months available then click on it 
     if(e.getText().equals(data[1])) 
     { 
      e.click(); 
      break; 
     } 
    } 

    // Iterate Date and select as required 
    for(WebElement e: datePickerData) 
    { 
     if((e.getText().equals(data[0]) && (e.findElement(By.tagName("span"))).getAttribute("class").equals("ng-binding"))) 
     { 
      e.click(); 
      break; 
     } 
    } 
}