-
Notifications
You must be signed in to change notification settings - Fork 160
/
helper.js
68 lines (55 loc) · 2.27 KB
/
helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const cardsDetails = require("./data/cardsDetails.json");
const card = require("./cards")
// const teamIdsArray = [167, 192, 160, 161, 163, 196, '', 'fire'];
//cardColor = (id) => cardsDetails.find(o => o.id === id) ? cardsDetails.find(o => o.id === id).color : '';
const validDecks = ['Red', 'Blue', 'White', 'Black', 'Green']
const colorToDeck = { 'Red': 'Fire', 'Blue': 'Water', 'White': 'Life', 'Black': 'Death', 'Green': 'Earth' }
// const tes = teamIdsArray.forEach(id => {
// console.log('DEBUG', id, cardColor(id))
// if (validDecks.includes(cardColor(id))) {
// return colorToDeck[cardColor(id)];
// }
// })
const deckValidColor = (accumulator, currentValue) => validDecks.includes(card.color(currentValue)) ? colorToDeck[card.color(currentValue)] : accumulator;
const reload = async (page) => { console.log('reloading page...') ; await page.reload(); }
const sleep = (ms) => {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
const teamActualSplinterToPlay = (teamIdsArray) => teamIdsArray.reduce(deckValidColor, '')
const clickOnElement = async (page, selector, timeout=20000, delayBeforeClicking = 0) => {
try {
const elem = await page.waitForSelector(selector, {timeout: timeout });
if(elem) {
await sleep(delayBeforeClicking);
console.log('Clicking element', selector);
await elem.click();
return true;
}
} catch (e) {
}
console.log('No element', selector, 'to be closed');
return false;
}
const getElementText = async (page, selector, timeout=15000) => {
const element = await page.waitForSelector(selector, {timeout: timeout });
const text = await element.evaluate(el => el.textContent);
return text;
}
const getElementTextByXpath = async (page, selector, timeout=20000) => {
try {
const element = await page.waitForXPath(selector, { timeout: timeout });
const text = await element.evaluate(el => el.textContent);
return text;
} catch (e) {
console.log('Get text by xpath error.', e);
return false
}
}
module.exports.teamActualSplinterToPlay = teamActualSplinterToPlay;
module.exports.clickOnElement = clickOnElement;
module.exports.getElementText = getElementText;
module.exports.getElementTextByXpath = getElementTextByXpath;
module.exports.sleep = sleep;
module.exports.reload = reload;