如何在量角器中选择日期选择器的日期 [英] How to select date of datepicker in protractor

查看:53
本文介绍了如何在量角器中选择日期选择器的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过量角器自动化应用程序,并且作为一个需要选择日期的测试用例的一部分,日期选择器定义如下.我尝试使用无效的 sendkeys 选择日期.

I am automating the application through protractor and as part of one testcase need to select the date, the date-picker definition is as below. I tried to select the date using sendkeys which is not working.

element(by.lid("new-transaction-doat-field)).sendKeys("24 Aug 2019");

请帮我选择日期.

注意:lid是自定义属性,要通过lid选择值,在量角器中创建自定义属性方法

Note: lid is the custom attribute and to select the value by lid, create a custom attribute method in protractor

推荐答案

我找到了通过 datepicker 选择日期的解决方案,其中 'sendKeys' 将无法工作,因为输入被禁用.解决方案就像我们将如何手动遍历日历并选择日期.

I found the solution to select the date through datepicker where 'sendKeys' won't work as input is disabled. The solution is like how manually we will traverse through the calendar and select the date.

代码如下:

this.selectDate = function (dateElement, dateValue, yearButton, arrowButton) {
        var splitDate = dateValue.split('/');
        var year = splitDate[2];
        var month = splitDate[1];
        var day = splitDate[0];
        var monthName = getMonthName(month);

        dateElement.click()
            .then(() => yearButton.click())
            .then(() => yearButton.click())
            .then(() => selectYear(year, arrowButton))
            .then(() => element(by.xpath("//span[text()='" + year + "']")).click())
            .then(() => element(by.xpath("//span[text()='" + monthName + "']")).click())
            .then(() => element(by.xpath("//span[text()='" + day + "']")).click());

    };

    async function selectYear(year, arrowButton) {
        for(var i=0; i < 10; i++) {
            var present  = await element(by.xpath("//span[text()='" + year + "']")).isPresent()
            if(present) {
                break;
            } else {
                await arrowButton.click();
            }
        }
    }

函数将输入作为 dateElement、dateValue、yearButton - 这是遍历到年份的中心按钮,箭头按钮 - 日历中的上一个或下一个按钮

function takes inputs as dateElement, dateValue, yearButton - which is center button to traverse to years, arrowButton - previous or next button in the calendar

第一个脚本点击上图中中间的年份按钮,它是 'September 2019' ,在 '2019' 年日历到来后,它再次点击它.那么2001年到2019年的日历如下

First script clicks on the year button in the center in the above image it is 'September 2019' , after that '2019' year calendar comes, it clicks on that again. Then 2001 to 2019 calendar comes as below

如果显示年份,则点击它,否则它会根据要选择的日期点击上一个"或下一个"按钮,直到它显示为止(循环 10 次以搜索年份,如果不是在 10 次点击中抛出)错误).

if the year displays it clicks on that otherwise it clicks on 'previous' or 'next' button based on the date to select until it got displayed (looping for 10 times to search for the year, if not in 10 clicks throws error).

点击年份后,点击月份和日期.

After year clicked, it clicks on month and day.

这篇关于如何在量角器中选择日期选择器的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆