From e0c225b7343cf00b90f38a4784ab1f5e3980be62 Mon Sep 17 00:00:00 2001 From: Oleksandr_Halichenko Date: Sat, 23 Apr 2022 11:56:48 +0300 Subject: [PATCH] fixed unhandled promise rejection issue getting collection element by index or text --- package.json | 4 ++-- src/PO.js | 36 +++++++++++++++++++++++------------- tests/po.spec.js | 20 ++++++++++++++++++++ tests/samplePO.js | 8 ++++++++ 4 files changed, 53 insertions(+), 15 deletions(-) diff --git a/package.json b/package.json index 41c4af5..72ba92a 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,10 @@ { "name": "@cucumber-e2e/po2", - "version": "1.0.3", + "version": "1.0.4", "description": "library for plain-english access page object access", "main": "index.js", "scripts": { - "test": "jest" + "test": "jest --detectOpenHandles" }, "author": "Alexander Galichenko", "license": "MIT", diff --git a/src/PO.js b/src/PO.js index ba525c9..933d95e 100644 --- a/src/PO.js +++ b/src/PO.js @@ -98,7 +98,7 @@ class PO { if (token.prefix === '@') { condition = (text) => text === token.value; } - return new Promise(resolve => { + return new Promise((resolve, reject) => { let timer = 0; const waitInterval = setInterval(async () => { timer += TICK_INTERVAL; @@ -106,14 +106,19 @@ class PO { clearInterval(waitInterval); return resolve(this.getChildNotFound(element, token)); } - const collection = await this.getCollection(element, po.selector); - for (const el of collection) { - let text = await el.getText(); - if (text === undefined) text = await this.driver.execute(e => e.textContent, el); - if (condition(text)) { - clearInterval(waitInterval); - return resolve(el); + try { + const collection = await this.getCollection(element, po.selector); + for (const el of collection) { + let text = await el.getText(); + if (text === undefined) text = await this.driver.execute(e => e.textContent, el); + if (condition(text)) { + clearInterval(waitInterval); + return resolve(el); + } } + } catch (err) { + clearInterval(waitInterval); + return reject(err); } }, TICK_INTERVAL); }); @@ -143,7 +148,7 @@ class PO { */ async getElementByIndex(element, po, token) { const index = parseInt(token.value) - 1; - return new Promise((resolve) => { + return new Promise((resolve, reject) => { let timer = 0; const waitInterval = setInterval(async () => { timer += TICK_INTERVAL; @@ -151,10 +156,15 @@ class PO { clearInterval(waitInterval); return resolve(this.getChildNotFound(element, token)); } - const collection = await this.getCollection(element, po.selector); - if (collection.length > index) { + try { + const collection = await this.getCollection(element, po.selector); + if (collection.length > index) { + clearInterval(waitInterval); + return resolve(collection[index]); + } + } catch (err) { clearInterval(waitInterval); - return resolve(collection[index]); + return reject(err); } }, TICK_INTERVAL); }); @@ -184,7 +194,7 @@ class PO { } async getChildNotFound(parentElement, {value, suffix, elementName}) { - return parentElement.$(`ElementNotExist-${value}-${suffix}-${elementName}`.replace(/\s/g, '')) + return parentElement.$(`ElementNotExist-${value}-${suffix}-${elementName}`.replace(/[\W]/g, '')) } } diff --git a/tests/po.spec.js b/tests/po.spec.js index 7e00e1a..5a1479f 100644 --- a/tests/po.spec.js +++ b/tests/po.spec.js @@ -111,6 +111,26 @@ test('get collection element from collection', async () => { expect(elements.length).toBe(3); }); +test('get child from not existing element', async () => { + const shouldThrow = async () => await po.getElement('Not Existing Component > Item'); + await expect(shouldThrow).rejects.toThrow(); +}); + +test('get collection from not existing element', async () => { + const shouldThrow = async () => await po.getElement('Not Existing Component > Items'); + await expect(shouldThrow).rejects.toThrow(); +}); + +test('get collection from not existing element by index', async () => { + const shouldThrow = async () => await po.getElement('Not Existing Component > #1 of Items'); + await expect(shouldThrow).rejects.toThrow(); +}); + +test('get collection from not existing element by text', async () => { + const shouldThrow = async () => await po.getElement('Not Existing Component > #text in Items'); + await expect(shouldThrow).rejects.toThrow(); +}); + afterAll(async () => { await po.driver.deleteSession(); }) diff --git a/tests/samplePO.js b/tests/samplePO.js index b15ea2f..7d98f72 100644 --- a/tests/samplePO.js +++ b/tests/samplePO.js @@ -30,6 +30,13 @@ class Level1Elements { Level2Elements = $$(new Level2Elements()); } +class NotExistingComponent { + selector = 'not-exist'; + + Item = $('div'); + Items = $$('li > span'); +} + class App { SingleElement = $('.single-element'); List = $$('.list li'); @@ -37,6 +44,7 @@ class App { MultipleComponents = $$(new MultipleComponent()); AsyncComponent = $(new AsyncComponent()); Level1Elements = $(new Level1Elements()); + NotExistingComponent = $(new NotExistingComponent()); } module.exports = new App();