Set ion-datetime with protractor e2e-test


#1

Does anyone know if it is currently possible to set an ion-datetime using a protractor e2e-test?


Set value of ion-datetime for e2e test
#2

Ok I did find a solution. For anyone trying to do the same my solution was to build a helper function which simulates the user swiping to the right numbers in the datetime.

public static setIonDatetimeDate(day: number, month: number, year: number ) {
    
    return Observable.create((observer: Observer<any>) => {

        let e = element.all(by.css('div.picker-col')).get(0).element(by.css('button.picker-opt-selected'));
        let e2 = element.all(by.css('div.picker-col')).get(1).element(by.css('button.picker-opt-selected'));
        let e3 = element.all(by.css('div.picker-col')).get(2).element(by.css('button.picker-opt-selected'));
        Observable.forkJoin(
            [e.getText(),
            e2.getText(),
            e3.getText()]
        ).subscribe(res => {

            let curDay = Number(res[0]);
            let curMonth = Number(res[1]);
            let curYear = Number(res[2]);
            let offsetDay = day - curDay;
            let offsetDaySign = offsetDay > 0 ? -1 : 1;
            offsetDay = offsetDay <0 ? offsetDay * -1 : offsetDay;
            let offsetMonth = month - curMonth;
            let offsetMonthSign = offsetMonth > 0 ? -1 : 1;
            offsetMonth = offsetMonth <0 ? offsetMonth * -1 : offsetMonth;
            let offsetYear = year - curYear;
            let offsetYearSign = offsetYear < 0 ? -1 : 1;
            offsetYear = offsetYear <0 ? offsetYear * -1 : offsetYear;

            browser.actions().mouseDown(e3).perform().then(() => {
                ProtractorHelper.scroll(offsetYear, offsetYearSign).then(() => {
                    browser.actions().mouseUp().perform().then(() => {
                        browser.actions().mouseDown(e2).perform().then(() => {
                            ProtractorHelper.scroll(offsetMonth, offsetMonthSign).then(() => {
                                browser.actions().mouseUp().perform().then(() => {
                                    browser.actions().mouseDown(e).perform().then(() => {
                                        ProtractorHelper.scroll(offsetDay, offsetDaySign).then(() => {
                                            browser.actions().mouseUp().perform().then(() => {
                                                element(by.buttonText("Ok")).click().then(() => {
                                                    observer.complete();
                                                });
                                            });
                                        });
                                    });
                                });
                            });
                        });
                    });
                });
            });
        });

    }).toPromise();
}

private static scroll(stepsLeft, sign) {
    return Observable.create((observer: Observer<any>) => {
        browser.actions().mouseMove({ x: 0, y: 43 * sign }).perform().then(() => {
            browser.driver.sleep(100);
            stepsLeft--;
            if (stepsLeft > 0) {
                ProtractorHelper.scroll(stepsLeft, sign).then(() => {
                    observer.complete();
                });
            } else {
                observer.complete();
            }
        });
    }).toPromise();
}

#3

Also see this: Set value of ion-datetime for e2e test