Skip to content

Commit

Permalink
Merge pull request #11 from AlexGalichenko/get-children-from-not-exis…
Browse files Browse the repository at this point in the history
…ting-elements

fixed unhandled promise rejection issue getting collection element by…
  • Loading branch information
AlexGalichenko authored Apr 23, 2022
2 parents b0a8200 + e0c225b commit dc2bf14
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 15 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
36 changes: 23 additions & 13 deletions src/PO.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,22 +98,27 @@ 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;
if (timer > this.config.timeout) {
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);
});
Expand Down Expand Up @@ -143,18 +148,23 @@ 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;
if (timer > this.config.timeout) {
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);
});
Expand Down Expand Up @@ -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, ''))
}

}
Expand Down
20 changes: 20 additions & 0 deletions tests/po.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
})
8 changes: 8 additions & 0 deletions tests/samplePO.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,21 @@ class Level1Elements {
Level2Elements = $$(new Level2Elements());
}

class NotExistingComponent {
selector = 'not-exist';

Item = $('div');
Items = $$('li > span');
}

class App {
SingleElement = $('.single-element');
List = $$('.list li');
SingleComponent = $(new SingleComponent());
MultipleComponents = $$(new MultipleComponent());
AsyncComponent = $(new AsyncComponent());
Level1Elements = $(new Level1Elements());
NotExistingComponent = $(new NotExistingComponent());
}

module.exports = new App();

0 comments on commit dc2bf14

Please sign in to comment.