com.sdl.selenium.bootstrap.form.DatePicker Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of Testy Show documentation
Show all versions of Testy Show documentation
Automated Acceptance Testing. Selenium and Selenium WebDriver test framework for web applications.
(optimized for dynamic html, ExtJS, Bootstrap, complex UI, simple web applications/sites)
The newest version!
package com.sdl.selenium.bootstrap.form;
import com.sdl.selenium.web.SearchType;
import com.sdl.selenium.web.WebLocator;
import com.sdl.selenium.web.utils.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
/**
* Used for finding element process (to generate xpath address)
* If you want more information about this datepicker visit this site: http://vitalets.github.io/bootstrap-datepicker/
* Example:
* {@code
*
*
*
*
* }
* In Java write this:
* {@code
* DatePicker datePicker = new DatePicker().setId("dp3");
* datePicker.select("19/02/2016");
* }
*/
public class DatePicker extends WebLocator {
private static final Logger LOGGER = LoggerFactory.getLogger(DatePicker.class);
private WebLocator input = new WebLocator(this).setClasses("icon-calendar").setInfoMessage("Open Calendar");
private WebLocator dataPicker = new WebLocator().setClasses("datepicker-dropdown dropdown-menu").setStyle("display: block;");
private WebLocator dataPickerDays = new WebLocator(dataPicker).setClasses("datepicker-days").setStyle("display: block;");
private WebLocator dataPickerMonths = new WebLocator(dataPicker).setClasses("datepicker-months").setStyle("display: block;");
private WebLocator dataPickerYear = new WebLocator(dataPicker).setClasses("datepicker-years").setStyle("display: block;");
private WebLocator switchDay = new WebLocator(dataPickerDays).setClasses("switch").setInfoMessage("switchMonth");
private WebLocator switchMonth = new WebLocator(dataPickerMonths).setClasses("switch").setInfoMessage("switchYear");
private WebLocator monthSelect = new WebLocator(dataPickerMonths).setClasses("month");
private WebLocator yearSelect = new WebLocator(dataPickerYear).setClasses("year");
private WebLocator daySelect = new WebLocator(dataPickerDays).setClasses("day").setExcludeClasses("old", "new");
public WebLocator getInput() {
return input;
}
public WebLocator getSwitchDay() {
return switchDay;
}
public WebLocator getSwitchMonth() {
return switchMonth;
}
public WebLocator getMonthSelect() {
return monthSelect;
}
public WebLocator getYearSelect() {
return yearSelect;
}
public WebLocator getDaySelect() {
return daySelect;
}
public DatePicker() {
setClassName("DatePicker");
setClasses("date");
}
public DatePicker(WebLocator container) {
this();
setContainer(container);
}
public DatePicker(WebLocator container, String id) {
this(container);
setId(id);
}
/**
* example new DatePicker().select("19/05/2013")
*
* @param date accept only this format: 'dd/MM/yyyy'
* @return true if is selected date, false when DatePicker doesn't exist
*/
public boolean select(String date) {
return select(date, "dd/MM/yyyy", Locale.ENGLISH);
}
/**
* example new DatePicker().select("19/05/2013", "dd/MM/yyyy", Locale.ENGLISH)
*
* @param date "19/05/2013"
* @param format "dd/MMM/yyyy"
* @param locale Locale.ENGLISH
* @return true if is selected date, false when DatePicker doesn't exist
*/
public boolean select(String date, String format, Locale locale) {
SimpleDateFormat inDateFormat = new SimpleDateFormat(format, locale);
SimpleDateFormat outDateForm = new SimpleDateFormat("dd/MMM/yyyy", locale);
try {
Date fromDate = inDateFormat.parse(date);
date = outDateForm.format(fromDate);
} catch (ParseException e) {
LOGGER.error("ParseException: {}", e);
}
String[] dates = date.split("/");
return setDate(Integer.parseInt(dates[0]) + "", dates[1], dates[2]);
}
public boolean setDate(String day, String month, String year) {
if (getInput().click()) {
boolean ok = true;
getMonthSelect().setText(month);
String fullDate = getSwitchDay().getText();
if (!fullDate.contains(year)) {
getSwitchDay().click();
getSwitchMonth().click();
goToYear(year, fullDate);
getYearSelect().setText(year, SearchType.EQUALS);
ok = getYearSelect().click() &&
getMonthSelect().click();
} else if (!fullDate.contains(month)) {
getSwitchDay().click();
ok = getMonthSelect().click();
}
getDaySelect().setText(day, SearchType.EQUALS);
return ok && getDaySelect().click();
}
return false;
}
private void goToYear(String year, String fullDate) {
int currentYear = Integer.parseInt(fullDate.split(" ")[1]);
int yearInt = Integer.parseInt(year);
int count = (int) Math.ceil((yearInt - currentYear) / 10.0);
doGoToYear(count);
WebLocator yearEl = new WebLocator(dataPickerYear).setClasses("year").setText(year);
if (!yearEl.isPresent()) {
WebLocator firstYearEl = new WebLocator(dataPickerYear).setClasses("year");
int actualYear = Integer.parseInt(firstYearEl.getText());
Utils.sleep(1);
count = (int) Math.ceil((yearInt - actualYear) / 3.0);
doGoToYear(count);
}
}
private void doGoToYear(int count) {
while (count > 0) {
WebLocator next = new WebLocator(dataPickerYear).setClasses("next");
next.click();
count--;
}
while (0 > count) {
WebLocator prev = new WebLocator(dataPickerYear).setClasses("prev");
prev.click();
count++;
}
}
public String getDate() {
WebLocator webLocator = new WebLocator(this).setTag("input");
return webLocator.getAttribute("value");
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy