-
Notifications
You must be signed in to change notification settings - Fork 0
/
applyleaves.js
142 lines (123 loc) · 3.71 KB
/
applyleaves.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const { Builder, By, Key, until } = require('selenium-webdriver');
let driver, username, password;
const args = () => process.argv
.slice(2)
.map(arg => arg.split('='))
.reduce((args, [value, key]) => {
args[value] = key;
return args;
}, {});
async function waitForPageToload() {
await driver.wait(function () {
return driver.executeScript('return document.readyState').then(function (readyState) {
return readyState === 'complete';
});
});
}
async function waitForELementToLoadAndsend(selector, text) {
var element = await getElement(selector)
await waitForElementToLoad(element);
element.sendKeys(text);
}
async function getElement(selector) {
return await driver.findElement(selector);
}
async function waitForElementToLoad(element) {
await driver.wait(until.elementIsEnabled(element));
}
async function waitForUrlToContain(url) {
await new Promise((resolve, reject) => {
var urlLoaded = async () => {
var loadedUrl = await driver.getCurrentUrl();
console.log("called start", loadedUrl)
if (loadedUrl.indexOf(url) > -1) {
console.log("called in")
resolve();
}
else {
setTimeout(() => {
console.log("called tineout")
urlLoaded();
}, 2000)
}
}
urlLoaded()
})
await waitForPageToload();
}
async function waitForModelToClose() {
await new Promise((resolve, reject) => {
var modelClosed = async () => {
var model = await driver.findElement(By.id("processing_modal"));
var modelDisplay = await model.getCssValue('display')
console.log(modelDisplay)
if (modelDisplay == 'none') {
resolve();
}
else {
setTimeout(() => {
modelClosed();
}, 2000)
}
}
setTimeout(() => {
modelClosed();
}, 2000)
})
await waitForPageToload();
}
async function logout() {
await driver.get("http://103.206.50.15:81/campsystems/logout.php");
}
async function save() {
var element = await driver.findElement(By.id("savebtn"));
await waitForElementToLoad(element);
await element.click();
}
async function selectOod() {
var element = await driver.findElement(By.css('#LEVID > option:nth-child(5)'));
element.click();
}
async function loadLeavePage() {
await waitForUrlToContain("home")
var leaveIcon = await getElement(By.css(".col-sm-2 > :nth-child(2) > img"));
await leaveIcon.click();
await waitForUrlToContain("apply_leave");
}
async function login() {
await loadLogin();
await waitForELementToLoadAndsend(By.id("username"), username);
await waitForELementToLoadAndsend(By.id("password"), password);
await driver.findElement(By.id("btn-login")).click()
}
async function loadLogin() {
await driver.get('http://103.206.50.15:81/campsystems/login.php');
await waitForPageToload();
}
async function start() {
driver = await new Builder().forBrowser('chrome').build();
await driver.manage().window().maximize();
await login();
await loadLeavePage();
await selectOod();
await save();
await waitForModelToClose();
await logout();
}
(async function () {
try {
var input = args();
username = input.username;
password = input.password;
if (!username || !password) {
throw new Error("please provide username and password")
}
await start();
}
catch (e) {
console.log(e)
}
finally {
await driver.quit();
}
})();