如何在Katalon工作室处理datepicker控件?
我对Katalon工作室很新。
答案 0 :(得分:2)
这是用于处理引导日期选择器的自定义关键字。
package framework
class component{
@Keyword
public static void handleDatepicker(TestObject calender, String exp_Date, String exp_Month,
String exp_Year) throws Exception {
String expDate = null, calYear = null,datepickerText=null,minYear=null,maxYear=null;
int expMonth = 0, expYear = 0;
WebElement datePicker;
List<WebElement> noOfDays=null,noOfMonths=null,noOfYears=null;
boolean dateNotFound = true;
List<String> monthList = Arrays.asList("None","Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
"Aug", "Sep", "Oct", "Nov", "Dec");
def driver = DriverFactory.getWebDriver()
WebElement SelectCalender = WebUiCommonHelper.findWebElement(calender, 20);
SelectCalender.click()
expDate = (exp_Date);
expMonth = Integer.parseInt(exp_Month);
expYear = Integer.parseInt(exp_Year);
WebElement datePicker_Heading1 =(driver).findElement(By.xpath("//div[@class='datepicker-days']/table/thead/tr[1]/th[2]"));
WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditions.elementToBeClickable(datePicker_Heading1));
datePicker_Heading1.click();
WebElement datePicker_Heading2 =(driver).findElement(By.xpath("//div[@class='datepicker-months']/table/thead/tr[1]/th[2]"));
wait.until(ExpectedConditions.elementToBeClickable(datePicker_Heading2));
datePicker_Heading2.click();
while (dateNotFound) {
WebElement datePicker_Heading3 =(driver).findElement(By.xpath("//div[@class='datepicker-years']/table/thead/tr[1]/th[2]"));
wait.until(ExpectedConditions.visibilityOf(datePicker_Heading3));
datepickerText =datePicker_Heading3.getText();
String[] datepickerYear = datepickerText.split("-");
minYear =datepickerYear[0];
maxYear = datepickerYear[1];
if((expYear >= Integer.parseInt(minYear)) && (expYear<=Integer.parseInt(maxYear)))
{
datePicker = (driver).findElement(By.xpath("//div[@class='datepicker-years']/table"));
noOfYears = datePicker.findElements(By.xpath("//span[contains(@class,'year')]"));
firstloop:
for (WebElement year : noOfYears) {
if (year.getText().equalsIgnoreCase((String)exp_Year)) {
year.click();
Thread.sleep(1500);
datePicker = (driver).findElement(By.xpath("//div[@class='datepicker-months']/table"));
//noOfMonths = datePicker.findElements(By.xpath("//span[@class='month']"));
noOfMonths = datePicker.findElements(By.cssSelector("span.month"));
Thread.sleep(1000);
for (WebElement month : noOfMonths) {
System.out.println(" the expected month in int is : "+expMonth);
System.out.println(" the expected month is : "+monthList.get(expMonth));
System.out.println(" the Actual month is : "+month.getText());
if ((monthList.get(expMonth)).equalsIgnoreCase(month.getText())) {
System.out.println("days ");
month.click();
datePicker = (driver).findElement(By.xpath("//div[@class='datepicker-days']/table"));
noOfDays = datePicker.findElements(By.xpath("//td[@class='day']"));
Thread.sleep(1500);
for (WebElement cell : noOfDays) {
if (cell.getText().equalsIgnoreCase(expDate)) {
System.out.println("days ");
cell.click();
break firstloop;
}
}
}
}
}
}
dateNotFound = false;
}else if (expYear > Integer.parseInt(maxYear)) {
WebElement Next =(driver).findElement(By.xpath("//div[@class='datepicker-years']/table/thead/tr[1]/th[@class='next']"));
if(Next.getAttribute("style").equalsIgnoreCase("visibility: visible;"))
{// Click on next button of date picker.
Next.click();
}else {
throw new Exception("This is exception")
}
}
// If current selected month and year are greater than expected
// month and year then go Inside this condition.
else if (expYear < Integer.parseInt(minYear)) {
WebElement Previous =(driver).findElement(By.xpath("//div[@class='datepicker-years']/table/thead/tr[1]/th[@class='prev']"));
if(Previous.getAttribute("style").equalsIgnoreCase("visibility: visible;"))
{
// Click on previous button of date picker.
Previous.click();
}else{
throw new Exception("This is exception")
}
}
}
}
}
您可以按以下方式使用上述关键字
CustomKeywords.'framework.component.handleDatepicker'(findTestObject('testobject'),'10', '12', '1990')
注意:“框架”文件夹是我们在“关键字”文件夹中创建的,然后我们创建了“组件”关键字
通过这种方式,您可以根据自己的组件创建自定义关键字。
我希望这会对您和其他开发人员有所帮助。
答案 1 :(得分:1)
答案 2 :(得分:0)
Katalon Studio 6.2.0中有一些通用项目模板。其中之一是“使用Katalon Studio的技巧和窍门”,其中包含一个“ Datepicker.groovy”文件,其中包含以下通用日期选择器代码,您可以根据需要进行调整:
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.annotation.Keyword
import com.kms.katalon.core.model.FailureHandling
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import static java.util.Calendar.*
import org.apache.xerces.jaxp.datatype.XMLGregorianCalendarImpl.DaysInMonth
class Datepicker {
Date date;
TestObject obj;
Datepicker() {}
Datepicker(TestObject object, String input_date) {
this.obj = object;
date = new Date().parse("MM/dd/yyyy", input_date)
}
def open_calendar_form() {
WebUI.waitForElementClickable(this.obj, 0)
WebUI.click(this.obj)
}
def displaying_month() {
WebUI.waitForElementVisible(findTestObject('Object Repository/jqueryui/widgets/datepicker/date_month'), 0)
return WebUI.getText(
findTestObject('Object Repository/jqueryui/widgets/datepicker/date_month'),
FailureHandling.STOP_ON_FAILURE)
}
def displaying_year() {
WebUI.waitForElementVisible(findTestObject('Object Repository/jqueryui/widgets/datepicker/date_year'), 0)
return WebUI.getText(
findTestObject('Object Repository/jqueryui/widgets/datepicker/date_year'),
FailureHandling.STOP_ON_FAILURE)
}
def displaying_date() {
return new Date().parse("MMM/yyyy", displaying_month() + "/" + displaying_year())
}
def pick_year() {
if (displaying_date()[YEAR] == date[YEAR]) return
while (displaying_date()[YEAR] < date[YEAR]) {
WebUI.click(findTestObject('Object Repository/jqueryui/widgets/datepicker/Next'))
}
while (displaying_date()[YEAR] > date[YEAR]) {
WebUI.click(findTestObject('Object Repository/jqueryui/widgets/datepicker/Prev'))
}
}
def pick_month() {
if (displaying_date()[MONTH] == date[MONTH]) return
while (displaying_date()[MONTH] < date[MONTH]) {
WebUI.click(findTestObject('Object Repository/jqueryui/widgets/datepicker/Next'))
}
while (displaying_date()[MONTH] > date[MONTH]) {
WebUI.click(findTestObject('Object Repository/jqueryui/widgets/datepicker/Prev'))
}
}
def pick_day() {
println date[DAY_OF_MONTH]
WebUI.click(findTestObject('Object Repository/jqueryui/widgets/datepicker/date_day', [('day') : date[DAY_OF_MONTH]]))
}
def pick_date() {
pick_year()
pick_month()
pick_day()
}
@Keyword
def pickDate(TestObject ob, String date) {
def picker = new Datepicker(ob, date)
picker.open_calendar_form()
picker.pick_date()
}
}