-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHackerrankAutomation.js
More file actions
151 lines (127 loc) · 6.63 KB
/
HackerrankAutomation.js
File metadata and controls
151 lines (127 loc) · 6.63 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//CLI -->
//1.To fire the code --> node HackerrankAutomation.js --url="https://www.hackerrank.com" --config=config.json
//2.Libararies
//npm init -y
//npm install puppeteer
//npm install minimist
//let start with the code for Automating the process to add moderators in hackerrank contest <code>
let minimist = require("minimist");
let puppeteer=require("puppeteer");
let args=minimist(process.argv);
let fs=require("fs");
//console.log(clargs.url+" "+clargs.config); ekbar chweck krlo ki args shi se read horhe hai na
// now to need to open the browser and click on the first page
let configJSON=fs.readFileSync(args.config);
let configJSO=JSON.parse(configJSON);
//console.log(configJSO); ekbar print krake dekhlete hai ki jso shise read to hoparhi hai na
//calling the run function
run();//ye khule me(jab ye function kisi function k andar na ho) vha await nhi likhskte, simple call krdo function ko
//defin ing the run function
async function run()
{
//1.start/open the browser
let browser=await puppeteer.launch({
defaultViewport :null,//us browser k andar jo content hai vo full screen khulega
args:[
"--start-maximized"//full screen browser dikhega
],
headless:false //work hote hue hume dikhega
})
//2.get a tab
let pages=await browser.pages();
let page=pages[0];
//3.go to url
await page.goto(args.url);
//4.click on login on page 1
await page.waitForSelector("a[data-event-action='Login']");
await page.click("a[data-event-action='Login']");
//5.click on login on page 2
await page.waitForSelector("a[href='https://www.hackerrank.com/login']");
await page.click("a[href='https://www.hackerrank.com/login']");
//6.type username/userid on page 3
await page.waitForSelector("input[name='username']");
await page.type("input[name='username']",configJSO.userid,{delay:50});
//7.type password on page 3
await page.waitForSelector("input[name='password']");
await page.type("input[name='password']",configJSO.password,{delay:50});
await page.waitFor(3000);
//8.click on login on page 3
await page.waitForSelector("button[data-analytics='LoginPassword']");
await page.waitFor(3000);
await page.click("button[data-analytics='LoginPassword']");
await page.waitFor(3000);
//9.click on compete after successful logging in
await page.waitForSelector("a[data-analytics='NavBarContests']");
await page.click("a[data-analytics='NavBarContests']");
await page.waitFor(3000);
//click on manage contests
await page.waitForSelector("a[href='/administration/contests/']");
await page.click("a[href='/administration/contests/']");
//now , we need to check how many pages of contest we have in manage contest therefore , we need to click on double right triangle button
// we need to inspect double right angle triangle
await page.waitFor(3000);
await page.waitForSelector("a[data-attr1='Last']");//hume last page par jana hai
let numPages=await page.$eval("a[data-attr1='Last']",function(lastTag)//$eval wait for selector chlayega aur jo result aayega use given function me pass krdega
{
//we are selecting the value of data pages attribute from the selector
let numpages=parseInt(lastTag.getAttribute('data-page'));
//converting the string into integer format
return numpages
});
await page.waitFor(3000);
console.log(numPages);//ekbar num of pages print krake dekhlena
//we got numpages now , now we need on the next page button (numPages-1) times ,hume right vale page of n-1 times jana hai
await page.waitFor(3000);
for (let i = 0; i < numPages; i++)//move through all pages
{
await handlePage(browser,page);
}
}
//now we will handle the code for adding moderators in all contest of a page
async function handlePage(browser,page)
{
await page.waitForSelector("a.backbone.block-center");
let curls=await page.$$eval("a.backbone.block-center",function(atags){//curls mtlb contest url
let iurls=[];//inner urls
for(let i=0;i<atags.length;i++)
{
let url=atags[i].getAttribute("href");
iurls.push(url);
}
return iurls;
});
console.log(curls); //ekbar urls print krke dekhlene chahiye of all contests in pages
for(let i=0;i<curls.length;i++)
{
await handlecontest(browser,page,curls[i]);
}
//to move on the next page
await page.waitFor(1500);//1.5 seconds ka wait krlete hai next page me jane se phle taki current page thoda set hojaye
await page.waitForSelector("a[data-attr1='Right']");
await page.click("a[data-attr1='Right']");
await page.waitFor(3000);
}
async function handlecontest(browser,page,curl)
{
let npage=await browser.newPage();
await npage.goto(args.url + curl);
await npage.waitFor(3000);
//click on moderators tab
await npage.waitForSelector("li[data-tab='moderators']");
await npage.click("li[data-tab='moderators']");
await npage.waitFor(3000);
//type the moderator ids
for(let i=0;i<configJSO.moderators.length;i++)
{
let moderator=configJSO.moderators[i];
await npage.waitForSelector("input#moderator");
await npage.type("input#moderator",moderator,{delay:50});
await npage.waitFor(3000);
//now we need to press enter to save the moderator
await npage.keyboard.press("Enter");
await npage.waitFor(3000);
}
//close the current contest to continue the loop for all contest of all pages
await npage.close();
await page.waitFor(3000);
}