-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
85 lines (77 loc) · 3.01 KB
/
main.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
const puppeteer = require("puppeteer");
const credObj = require("./cred");
const fs = require("fs");
async function AutomationFunction() {
const browserRepresentativeObj = await puppeteer.launch({
headless: false,
executablePath: "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe",
defaultViewport: null,
args: ["--start-maximized", "--start-in-incognito"],
slowMo: 20
});
const tab = await browserRepresentativeObj.newPage();
await tab.goto("https://www.hackerrank.com/auth/login");
await tab.type("input[type='text']", credObj.email, { delay: 20 });
await tab.type("input[type='password']", credObj.password, { delay: 20 });
await tab.keyboard.press("Enter");
await waitAndClickTopic("Java", tab);
await waitAndClickQuestion("Java Stdin and Stdout I", tab)
let code = await fs.promises.readFile("code.java", "utf-8");
await copyPasteQuestion(code, tab);
await submitCode(tab);
}
AutomationFunction();
async function waitAndClickTopic(name, tab) {
await tab.waitForSelector(".topics-list", { visible: true });
await tab.evaluate(findAndClick, name);
function findAndClick(name) {
let alltopics = document.querySelectorAll(".topics-list .topic-card a");
let idx;
for (idx = 0; idx < alltopics.length; idx++) {
let cTopic = alltopics[idx].textContent.trim();
console.log(cTopic);
if (cTopic == name) {
break;
}
}
alltopics[idx].click();
}
}
async function waitAndClickQuestion(name, tab) {
await tab.waitForSelector(".challenges-list", { visible: true });
let questions = await tab.evaluate(findAndClick, name);
console.log(questions);
function findAndClick(name) {
let allQuestions = document.querySelectorAll(".challenges-list .challengecard-title");
let idx;
let textContent = []
for (idx = 0; idx < allQuestions.length; idx++) {
let cTopic = allQuestions[idx].textContent.trim();
textContent.push(cTopic);
if (cTopic.includes(name.trim())) {
break;
}
}
allQuestions[idx].click();
}
}
async function copyPasteQuestion(code, tab) {
await tab.waitForSelector('input[type="checkbox"]', { visible: true });
await tab.click('input[type="checkbox"]');
await tab.waitForSelector("textarea[id='input-1']", { visible: true });
await tab.type("textarea[id='input-1']", code);
await tab.keyboard.down('ControlLeft')
await tab.keyboard.press('KeyA')
await tab.keyboard.press('KeyX');
await tab.keyboard.up('ControlLeft');
await tab.waitForSelector(".monaco-editor");
await tab.click(".monaco-editor");
await tab.keyboard.down('ControlLeft')
await tab.keyboard.press('KeyA')
await tab.keyboard.press('KeyV');
await tab.keyboard.up('ControlLeft');
}
async function submitCode(tab) {
await tab.waitForSelector(".hr-monaco-submit");
await tab.click(".hr-monaco-submit");
}