forked from victorugob/servicenow-dev-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
63 lines (47 loc) · 1.96 KB
/
index.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
const userInfo = require('dotenv').config()
const puppeteer = require('puppeteer');
const myInstance = process.env.DEVINST;
const instance = "https:" + myInstance + ".service-now.com/login";
function delay(time) {
return new Promise(function(resolve) {
setTimeout(resolve, time)
});
}
(async () => {
const browser = await puppeteer.launch();
/* Use o código abaixo dentro de .launch() para que abra a aba para ver o processo
Sem esse código o bot rodará as instruções no background
{headless: false, executablePath: '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'}
ou
{ headless: false, defaultViewport: null, args: [
'--window-size=1360,768',
] }
*/
const page = await browser.newPage();
await page.goto(instance); // atentar ao <numero da sua instancia> e substituir a que irá usar
// Validate if instance is sleeping, if so, login to wake
if(await (await page.$$('.instance-hibernating-page')).length > 0) {
console.log('Instance is sleeping, Bot will redirect and wake your stance')
await page.goto('https://developer.service-now.com/login')
await page.type('#username', process.env.USEREMAIL)
await page.click('#usernameSubmitButton')
await delay(2000)
await page.type('#password', process.env.EMAILPW);
await Promise.all([
page.waitForNavigation(),
page.click('#submitButton')
]);
console.log('Bot awakaned your instance, please wait a few minutes');
} else {
console.log('Your stance is not sleeping')
await page.type('#user_name', process.env.USER_NAME )
await page.type('#user_password', process.env.PASSWORD)
await Promise.all([
page.waitForNavigation(), // promise resolve quando navigation finaliza
page.click('#sysverb_login') // clicar no link causa uma navigation indireta
]);
//await page.screenshot({path: 'confirmation/anything.png'}); //path e nome da screenshot
console.log('Bot has finalized');
await browser.close();
}
})();