-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHackerrank_Automate.js
120 lines (91 loc) · 3.97 KB
/
Hackerrank_Automate.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//node Hackerrank_Automate.js --url=https://www.hackerrank.com --config=config.json
//config file contains moderators name and UserId , Password for Login
let minimist = require("minimist");
let puppeteer = require("puppeteer");
let fs = require("fs");
let args = minimist(process.argv);
let configJSON = fs.readFileSync(args.config, "utf-8");
let configJSO = JSON.parse(configJSON);
(async function() {
// start the browser
let browser = await puppeteer.launch({
headless: false,
args: [
'--start-maximized'
],
defaultViewport: null
});
// get the tabs (there is only one tab)
let pages = await browser.pages();
let page = pages[0];
// open the url
await page.goto(args.url);
// wait and then click on login on page1
await page.waitForSelector("a[data-event-action='Login']");
await page.click("a[data-event-action='Login']");
// wait and then click on login on page2
await page.waitForSelector("a[href='https://www.hackerrank.com/login']");
await page.click("a[href='https://www.hackerrank.com/login']");
// type userid
//wait for 2 sec
await page.waitForTimeout(2000);
await page.waitForSelector("input[name='username']");
await page.type("input[name='username']" , configJSO.userId ,{delay:100});
// type password
await page.waitForSelector("input[name='password']");
await page.type("input[name='password']", configJSO.password, { delay: 20 });
// press click on page3
await page.waitForSelector("button[data-analytics='LoginPassword']");
await page.click("button[data-analytics='LoginPassword']");
await page.waitForTimeout(2000);
// click on compete
await page.waitForSelector("a[data-analytics='NavBarContests']");
await page.click("a[data-analytics='NavBarContests']");
await page.waitForTimeout(2000);
// click on manage contests
await page.waitForSelector("a[href='/administration/contests/']");
await page.click("a[href='/administration/contests/']");
await page.waitForTimeout(2000);
await page.waitForSelector("a[data-attr1='Last']");
let numberOfPages = await page.$eval("a[data-attr1='Last']", function(alast){
let countOfPages = parseInt(alast.getAttribute("data-page"));
return countOfPages;
})
await page.waitForTimeout(2000);
for(let i = 0; i < numberOfPages - 1; i++){
await page.waitForSelector("a.backbone.block-center");
let contestUrls = await page.$$eval("a.backbone.block-center", function(atags){
let urls = [];
for(let i = 0; i < atags.length; i++){
let url = atags[i].getAttribute('href');
urls.push(url);
}
return urls;
});
await page.waitForTimeout(2000);
for (let i = 0; i < contestUrls.length; i++) {
let cpage = await browser.newPage();
page.waitForTimeout(2000);
await saveModerator(args.url+contestUrls[i], cpage, configJSO.moderator);
await cpage.close();
await page.waitForTimeout(2000);
}
await page.waitForSelector("a[data-attr1='Right']");
await page.click("a[data-attr1='Right']");
}
})();
async function saveModerator(url, cpage, moderator) {
await cpage.bringToFront();
await cpage.goto(url);
await cpage.waitFor(3000);
// click on moderators tab
await cpage.waitForSelector("li[data-tab='moderators']");
await cpage.click("li[data-tab='moderators']");
//add all moderators in one test
for(let i = 0 ; i < configJSO.moderators.length ; i++){
await cpage.waitForSelector("input#moderator");
await cpage.type("input#moderator" , configJSO.moderators[i] , {delay:100});
await cpage.keyboard.press("Enter");
}
await cpage.keyboard.press("Enter");
};