简体   繁体   English

如何从夜表 javascript 自动化中的日期选择器日历中获取 select 日期?

[英]How to select a date from date picker calender in nightwatch javascript automation?

I want to select a date from datepicker, that date is not static ie selecting same day, it should be dynamic in nature.我想 select 来自 datepicker 的日期,该日期不是 static 即选择同一天,它应该是动态的。 ie should be able to select any past date including today.即应该能够 select 任何过去的日期,包括今天。

Problem: After click on a field i get this calender, from calender i have to select a date.问题:点击一个字段后,我得到这个日历,从日历我必须到 select 一个日期。 how to do it?怎么做? preferable in Nightwatch js.在 Nightwatch js 中更可取。

在此处输入图像描述

This is a very non-trivial question.这是一个非常重要的问题。 I know of no "off the shelf" solution that you can just plug in to Nightwatch.我知道没有“现成”的解决方案可以插入 Nightwatch。

You're going to want to construct a javascript file that allows you to get the day of the month.您将要构建一个 javascript 文件,该文件允许您获取月份中的日期。 but because it could be the last day of the month, or the last day of the year, you also may need to determine those, too.但因为它可能是一个月的最后一天,或者一年的最后一天,你也可能需要确定这些。

Something like this to start:像这样开始:

const monthNames = ["January", "February", "March", "April", "May", "June",
  "July", "August", "September", "October", "November", "December"
];
var monthShortNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
];
const dayNames = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", 
"Saturday", "Sunday"
];
var dayShortNames = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
];

const d = new Date();
var month = monthNames[d.getMonth()];
var mmm = monthShortNames[d.getMonth()];
var today = new Date();
var tomorrow = new Date();
tomorrow.setTime(tomorrow.getTime() + (24 * 60 * 60 * 1000));
var mm = today.getMonth()+1; // because January is 0.
var singleDigitDate = today.getDate();
var dd = today.getDate();
var ddnlz = today.getDate(); // single digit dates, no leading zero
var dayShortName = dayShortNames[d.getDay()]; // Mon, Tue, etc.
var mmt = tomorrow.getMonth()+1;
var dt = tomorrow.getDate();
var ddt = tomorrow.getDate();
var yyyy = today.getFullYear();
var yyyyt = tomorrow.getFullYear();
if ( dd<10 ){ dd='0'+dd } // if we need leading zeros for days 
if ( mm<10 ){ mm='0'+mm } // if we need leading zeros for months
if ( ddt<10 ){ ddt='0'+ddt } // if we need leading zeros for days 
if ( mmt<10 ){ mmt='0'+mmt } // if we need leading zeros for months

put that in /conf or otherwise make it available to your base-test-class.js将其放入 /conf 或以其他方式使其可用于您的 base-test-class.js

This is a whole lot easier to do if you always want to pick today.如果您今天总是想选择,这要容易得多。 You'll want to use xpath selectors to pick the day that you want to click.您需要使用 xpath 选择器来选择要单击的日期。

You will use XPATH to click on a date like, say you click //*[.='16'], where 16 is the date today.您将使用 XPATH 单击日期,例如单击 //*[.='16'],其中 16 是今天的日期。

If, say you want to pick tomorrow on the calendar, then things get more challenging.如果说你想在日历上选择明天,那么事情就会变得更具挑战性。

You need to have a way to make sure tomorrow is not a new month/year.您需要有一种方法来确保明天不是新的月份/年份。 I was working on a project once where that came in handy, so I built that, but the code I wrote all stayed with the client.我曾经在一个项目上工作,它派上用场,所以我构建了它,但我编写的代码都留在了客户端。 I seem to recall that there was an array with the count of days in for each month, and that I didn't bother solving for the leap year.我似乎记得每个月都有一个包含天数的数组,而且我没有费心解决闰年。 In cases where you're clicking any day that is not TODAY, you'll need some if statements that check, "is it the last day of the year?"如果您单击的任何一天不是今天,您将需要一些if语句来检查“这是一年中的最后一天吗?” and is it the last day of the month?" and make the appropriate button presses to get to a location where select those dates (those up arrows and down arrows). Sorry that I don't have sample code for that.它是本月的最后一天吗?”并按下相应的按钮以到达 select 那些日期(那些向上箭头和向下箭头)的位置。抱歉,我没有示例代码。

This is, perhaps not the ready-made solution you want.这可能不是您想要的现成解决方案。 I was unable to find a date-picker module when I was working with calendars and Nightwatch.我在使用日历和 Nightwatch 时找不到日期选择器模块。 Maybe you'll get lucky and someone has built and shared one...也许你会很幸运,有人已经建立并分享了一个......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM