如何使用量角器测试日期选择器?

时间:2018-12-27 12:51:27

标签: angular typescript protractor e2e-testing

我正在测试使用ngbDatepicker的日期选择器,它仅允许用户从日历中选择日期。有没有可能使用量角器从中选择日期?

<form class="form-inline">
  <div class="form-group">
    <div class="input-group">
      <input class="form-control" placeholder="yyyy-mm-dd"
             name="dp" [(ngModel)]="model" ngbDatepicker #d="ngbDatepicker">
      <div class="input-group-append">
        <button class="btn btn-outline-secondary calendar" (click)="d.toggle()" type="button"></button>
      </div>
    </div>
  </div>
</form>

这是日期选择器的屏幕截图: enter image description here

我需要在日期选择器中选择隔天的一天。

提前谢谢!

2 个答案:

答案 0 :(得分:1)

是的,可以使用量角器选择日期。您可以模拟用户可以执行的所有行为。

自此,您尚未提供e2e测试源代码。以下几点将指导您编写日期选择程序的e2e测试。

const EC = protractor.ExpectedConditions;
browser.wait(EC.presenceOf(element(by.css('btn-outline-secondary'))), 5000).then(() => {
    element(by.css('btn-outline-secondary')).click(); // This will click calendar icon
    const d = new Date().getDate()+1; // This will get you next day value

    // Write your code to find next day element and click it using click() function
    // Hint: Each day is a "div" with class "btn-light" and day as content of that div element 
});

EC.presenceOf()将检查日历图标按钮是否存在(并在5秒钟后超时)。
如果存在,则将单击该图标。因此,日历将打开。

现在,您的任务是识别并选择第二天。
如上所示,可以使用getDate()函数来识别第二天。
通过上面给出的提示,选择它也很容易。

答案 1 :(得分:0)

您可以直接将密钥发送到该日期的输入。 关于格式,它很棘手。从屏幕快照中,我可以看到它的日/月/年

尝试以下代码: 您将需要momentjs软件包:https://www.npmjs.com/package/moment 这是有关时刻日期格式化程序的信息:https://devhints.io/moment

    const datenow = new Date();
    datenow.setDate(datenow.getDate() + 1); // today + 1 day.

    const moment = require('moment');

    // format the date to string.
    const formatedDate = moment(datenow).format('D MMM YYYY'); 

    // send the formated date to the input.
    element(by.xpath('//input[@class=\'form-control\']')).sendKeys(formatedDate);

注意:适应您的需求,这只是如何处理日期选择器的示例。