Set value of ion-datetime for e2e test

This solution is a bit simpler than @chager’s solution:

describe('something', () => {
  it('should ...', async () => {
    expect(await $('ion-datetime').getAttribute('pickerformat')).toBe('h mm A');
    await pick($('ion-datetime'), 10, 0, 'PM');
    expect($('some-element').isPresent()).toBeTruthy();
  });
});

const pause = t => new Promise(resolve => setTimeout(resolve, t));

const pick = async (datetimeEl, h, m, a) => {
  if (typeof a === 'string') { a = (a === 'PM') ? 2 : 1; }
  m += 1; // 0-59 => 1-60

  // open time picker
  await datetimeEl.click();
  await pause(500);

  // set value for each column
  await [h, m, a].map(async (val, col) => {
    const el = $$('.picker-col').get(col).$(`button:nth-of-type(${val})`);
    await browser.executeScript('arguments[0].click()', el.getWebElement());
  });
  await pause(500);

  // close time picker
  await $('.picker-toolbar-button:not(.picker-toolbar-cancel) button').click();
  await pause(500);
};

The pause is necessary because of the animations. But having some kind of direct access to the ngModel would still be a lot easier.

1 Like