0

I've disabled the control flow and started spreading async/await accross my e2e tests.

And here the problem arises that

Failed: Index out of bound. Trying to access element at index: 0, but there are only 0 elements that match locator By(css selector, .OneLine)

NoSuchElementError: Index out of bound. Trying to access element at index: 0, but there are only 0 elements that match locator By(css selector, .OneLine)

Here is my test

it('', async () => {
await page.dragElToCell('Text', {x: 1, y: 0});
// further some expectations that are not reached due to error thrown which points to page.dragElToCell
})

This is a page obj method

async dragElToCell(name: string, pos: {x: number, y: number}) {
  const el = this.getDraggableEl(elName);
  const cell = this.getCell(pos.x, pos.y);
  return browser.actions().dragAndDrop(el, cell).perform();
}

getDraggableEl(name: string) {
  return element(by.cssContainingText('.draggable', name);
}

getCell(x: number, y: number) {
  return $$('.OneLine').get(y).$$('.Column').get(x);
}

This code was working pretty well inside control flow. As soon as I've disabled it it stopped working.

The trick is that if I execute

  expect(await $$('.OneLine').get(0).$$('.Column').get(1).isPresent()).toBeTruthy();

inside my test suite it works fine and gives me true.

I've also debugged getCell and it receives proper numbers.

Also, I've set sleep for 10 sec to ensure that elements are visible (and for that also executed querySelectorAll twice to repeat same sequence and got the element I'd expected to retrieve).

Community
  • 1
  • 1
Sergey
  • 7,184
  • 13
  • 42
  • 85
  • Eventually came up with https://stackoverflow.com/questions/48349429/protractor-browser-actions-draganddrop-async-await-not-working as the only working solution in this case. – Sergey Oct 31 '19 at 12:08

1 Answers1

1

getDraggableEl and getCell return a Promise. So you need to add await in dragElToCell such as:

async dragElToCell(name: string, pos: {x: number, y: number}) {
  const el = await this.getDraggableEl(elName);
  const cell = await this.getCell(pos.x, pos.y);
  return browser.actions().dragAndDrop(el, cell).perform();
}
HTN
  • 3,388
  • 1
  • 8
  • 18
  • Since when `$` started returning Promises? It returns an `ElementFinder` what is proved by PhpStorm code assistance – Sergey Oct 31 '19 at 16:19