|
1 |
| -import { ElementFinder, Locator } from 'protractor'; |
| 1 | +import { ElementFinder, Locator, by } from 'protractor'; |
2 | 2 | import { BaseElement } from './base.element';
|
| 3 | +import { Input } from './input.element'; |
3 | 4 |
|
4 | 5 | export class DatePicker extends BaseElement {
|
5 | 6 | constructor(locatorOrElement: Locator | ElementFinder) {
|
6 | 7 | super(locatorOrElement);
|
7 | 8 | }
|
| 9 | + monthNames = ['January', 'February', 'March', 'April', 'May', 'June', |
| 10 | + 'July', 'August', 'September', 'October', 'November', 'December' |
| 11 | + ]; |
| 12 | + |
| 13 | + linkInput = new Input(this.element.element(by.css('.ngx-datepicker-input'))); |
| 14 | + topBarTitle = this.element.element(by.css('.topbar-title')); |
| 15 | + prevMonth = this.element.element(by.id('prevMonth')); |
| 16 | + nextMonth = this.element.element(by.id('nextMonth')); |
| 17 | + yearButton = (year: number) => this.element |
| 18 | + .element(by.xpath( |
| 19 | + `.//*[contains(@class, 'main-calendar-years')]//*[contains(@class, 'year-unit') and normalize-space(text())='${year}']`)) |
| 20 | + dayButton = (day: number) => this.element |
| 21 | + .element(by.xpath( |
| 22 | + `.//*[contains(@class, 'main-calendar-days')]//*[contains(@class, 'day-unit') and normalize-space(text())='${day}']`)) |
| 23 | + |
| 24 | + openCalendar() { |
| 25 | + return this.linkInput.click(); |
| 26 | + } |
8 | 27 |
|
9 | 28 | async select(date: Date) {
|
10 |
| - throw new Error('Date Picker is not implemented.'); |
| 29 | + const selectDate = new Date(date); |
| 30 | + const month = this.monthNames[selectDate.getMonth()]; |
| 31 | + const year = selectDate.getFullYear(); |
| 32 | + const day = selectDate.getDate(); |
| 33 | + |
| 34 | + await this.openCalendar(); |
| 35 | + await this.selectYear(year); |
| 36 | + await this.selectMonth(month); |
| 37 | + return this.dayButton(day).click(); |
| 38 | + } |
| 39 | + |
| 40 | + isEditable(): Promise<boolean> { |
| 41 | + return this.linkInput.isPresent(); |
| 42 | + } |
| 43 | + |
| 44 | + private async selectMonth(month: string): Promise<void> { |
| 45 | + for (let i = 0; i < this.monthNames.length; i++) { |
| 46 | + const currentMonth = await this.getCurrentMonth(); |
| 47 | + if (currentMonth !== month) { |
| 48 | + if (this.monthNames.indexOf(currentMonth) < this.monthNames.indexOf(month)) { |
| 49 | + await this.nextMonth.click(); |
| 50 | + } |
| 51 | + if (this.monthNames.indexOf(currentMonth) > this.monthNames.indexOf(month)) { |
| 52 | + await this.prevMonth.click(); |
| 53 | + } |
| 54 | + } else { |
| 55 | + return; |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + private async selectYear(year: number): Promise<void> { |
| 61 | + const currentYear = await this.getCurrentYear(); |
| 62 | + if (currentYear !== year) { |
| 63 | + await this.topBarTitle.click(); |
| 64 | + return this.yearButton(year).click(); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private async getCurrentMonth(): Promise<string> { |
| 69 | + const currentMonthYear = await this.topBarTitle.getText(); |
| 70 | + return currentMonthYear.split(' ')[0]; |
| 71 | + } |
| 72 | + |
| 73 | + private async getCurrentYear(): Promise<number> { |
| 74 | + const currentMonthYear = await this.topBarTitle.getText(); |
| 75 | + return +currentMonthYear.split(' ')[1]; |
11 | 76 | }
|
12 | 77 | }
|
0 commit comments