2

如何在 Katalon Studio 中处理日期选择器控制?

我对 Katalon 工作室很陌生。

4

3 回答 3

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')

注意:我们在关键字文件夹中创建了“框架”文件夹,然后我们创建了“组件”关键字

在此处输入图像描述

这样您就可以根据您的组件创建自定义关键字。

我希望这可以帮助您和其他开发人员。

于 2019-01-27T04:25:36.703 回答
1

也许这会有所帮助,这似乎是一种类似的情况: https ://forum.katalon.com/discussion/1999/how-to-handle-date-pickers-in-katalon 。

于 2018-05-22T11:19:56.070 回答
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()
    }
}
于 2019-06-14T07:18:21.690 回答