신원형

Merge branch 'login' into 'master'

Login (Merge)



See merge request !1
{
"env": {
"browser": true,
"es2021": true
},
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"rules": {
}
}
......@@ -5,6 +5,23 @@
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Mocha All",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": [
"--timeout",
"999999",
"--colors",
"${workspaceFolder}/test"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"skipFiles": [
"<node_internals>/**/*.js"
]
},
{
"type": "pwa-node",
"request": "launch",
"name": "Launch Program",
......
......@@ -40,33 +40,45 @@ ex)
*/
export async function get_schedule(id, pw, target_date) {
login(id, pw).then(async driver => {
load(driver, target_date)
.then(it => {
console.log(it)
logout(driver)
})
return await using_selenium( async (driver) => {
return await login(driver, id, pw)
.then(async () => {
return await load(driver, target_date)
.then((data) => {
//logout(driver)
console.log(data)
return data
})
})
})
}
export async function login(id, pw) {
export async function using_selenium(next) {
const option = new firefox.Options()
option.setBinary('C:\\Program Files\\Mozilla Firefox\\firefox.exe')
option.addArguments("-headless");
const driver = new selenium.Builder()
.forBrowser('firefox')
.setFirefoxOptions(option)
.build()
next(driver).finally(() => {
driver.quit()
})
}
export async function login(driver, id, pw) {
await driver.get("https://khcanvas.khu.ac.kr/")
const idInput = await driver.findElement(selenium.By.xpath('//*[@id="login_user_id"]'));
const pwInput = await driver.findElement(selenium.By.xpath('//*[@id="login_user_password"]'));
const login_button = await driver.findElement(selenium.By.xpath('//*[@id="form1"]/div/div[3]'));
await idInput.sendKeys(id);
await pwInput.sendKeys(pw);
await login_button.click();
return driver
}
......@@ -76,7 +88,7 @@ export async function load(driver, until) {
await driver.get(`https://khcanvas.khu.ac.kr/api/v1/planner/items?start_date=${start_date}`);
await sleep(1000)
await driver.findElement(selenium.By.xpath('/html/body/div/div/nav/ul/li[2]')).click();
const data = await driver.findElement(selenium.By.xpath('/html/body/div/div/div/div[2]/div/div/div[2]/pre'));
const text = JSON.parse((await data.getText()).slice(9))
......@@ -87,7 +99,7 @@ export async function load(driver, until) {
export async function logout(driver) {
await driver.get("https://khcanvas.khu.ac.kr/")
const logoutPanel = await driver.findElement(selenium.By.xpath('html/body/div[2]/header[2]/div[1]/ul/li[1]/button/div[1]')).click();
await driver.findElement(selenium.By.xpath('html/body/div[2]/header[2]/div[1]/ul/li[1]/button/div[1]')).click();
await sleep(1000)
const logout = await driver.findElement(selenium.By.xpath('html/body/div[3]/span/span/div/div/div/div/div/span/form/button'));
logout.click()
......
import fs from 'fs/promises'
import { login, load, logout } from './khcanvas.js';
fs.readFile("asdffdsa.txt").then(it => {
const auth = it.toString().split('|')
login(auth[0], auth[1]).then(async driver => {
load(driver, new Date())
.then(it => {
console.log(it)
logout(driver)
})
}).catch(err => { console.log(err) })
})
\ No newline at end of file
......
This diff is collapsed. Click to expand it.
......@@ -14,12 +14,12 @@
"license": "",
"dependencies": {
"@types/selenium-webdriver": "^4.1.0",
"eslint": "^8.15.0",
"mocha": "^10.0.0",
"selenium-webdriver": "^4.1.2"
},
"type": "module",
"devDependencies": {
"@types/node": "^17.0.35"
"@types/node": "^17.0.35",
"eslint": "^8.16.0"
}
}
......
//@ts-check
//https://github.com/gatoona/AWS-Selenium
import * as canvas from '../khcanvas.js'
import * as rd from 'readline'
import * as mocha from 'mocha'
import process from 'node:process';
import util from 'util'
import { rejects } from 'assert';
mocha.describe('khcanvas', () => {
mocha.it('opening selenium', async () => {
await canvas.using_selenium(async (driver) => {
await driver.get("http://khuhub.khu.ac.kr");
await driver.getTitle().then( (title) => {
console.log(title);
});
});
})
});
mocha.describe('khcanvas', () => {
mocha.it('get schedule', async () => {
const rl = rd.createInterface({
input: process.stdin,
output: process.stdout
})
const question = util.promisify(rl.question).bind(rl);
const a = await question('a');
const b = await question('b');
return await canvas.get_schedule(a, b, new Date())
.then(it => console.log(it))
.catch(it => {
console.log(it)
rejects(it)
})
})
});
\ No newline at end of file
......