test.js 5.2 KB
const express = require("express");
const router = express.Router();
const puppeteer = require("puppeteer")
const { getPeed, getSubjects, login, close } = require("./E_Campus.js");

async function runrun() {
    page = await login("lorem", "lorem");
    await getPeed(page);
    await getSubjects(page);
    Info = await close();
    console.log(Info);
    console.log(Info.subjects)
    
  }


  async function attendanceCheck(subject, code) {
    try {
      //for visibility, headless: false
      browser = await puppeteer.launch({
        headless: false,
        args: [
          "--window-size=1920x1080",
          "--start-maximized",
          "--no-sandbox",
          "--disable-dev-shm-usage",
          "--lang=ko",
        ],
      });
  
      const page = await browser.newPage();
      await page.setViewport({
        width: 1920,
        height: 1080,
      });
      await page.goto(
        "https://info21.khu.ac.kr/com/LoginCtr/login.do?returnurl=https://portal.khu.ac.kr/ksign/index.jsp?ssoGb=ptfol&sso=ok"
      );
      await page.type("#userId", Info.id);
      await page.type("#userPw", Info.pw);
  
      //press the login button on E_Campus homepage
      await Promise.all([
        page.click("#loginFrm > div > div.wrap > div.login_box > div.m_box.idx02.fr > button.btn.loginbtn1"),
        page.waitForNavigation(),
      ]);
      await page.goto(
        "https://portal.khu.ac.kr/haksa/clss/clss/elctCrtf/index.do"
      );
      multipleClick(page);
      
      await page.type("#crtfNo", code);
      await page.click("#crtfBtn");
      await page.click("body > div:nth-child(23) > div.ui-dialog-buttonpane.ui-widget-content.ui-helper-clearfix > div > button.di_btn_conf.ui-button.ui-widget.ui-state-default.ui-corner-all.ui-button-text-only");
      await page.goto("https://portal.khu.ac.kr/haksa/clss/clss/elctCrtf/index.do");
      
      result = await checkStatus(page);
      console.log(result);
      browser.close();
      
    } catch (err) {
      console.log(err);
    }
  }
  
  async function multipleClick(page) {
    let data = [];
    let selector = "#baseForm > div:nth-child(3) > div.table_wrap.pc_view > table > tbody";
    const number = await page.$$eval(selector, (data) => data.length);
    try{
      for (let index = 2; index < number; index++) {
        page.click(`#baseForm > div:nth-child(3) > div.table_wrap.pc_view > table > tbody > tr:nth-child("+ ${index} +") > td.first.input_td > a`)
      }
      page.click("#baseForm > div:nth-child(3) > div.table_wrap.pc_view > table > tbody > tr.first > td.first.input_td > a");
      page.click("#baseForm > div:nth-child(3) > div.table_wrap.pc_view > table > tbody > tr.last > td.first.input_td > a");
    }
    catch(err){
      console.log("error!");
    }
    
  }
  
 async function checkStatus(page) {
  let selector = "#baseForm > div:nth-child(3) > div.table_wrap.pc_view > table > tbody";
  const number = await page.$$eval(selector, (data) => data.length);
  
  for (let index = 2; index < number; index++) {
    if (await getOne(page, index) == "인증완료"){
      return "success";
    }    
    else{
      return "failed";
    }
  }
  
  //exception(first context)
  let temp = await page.$(
    "#baseForm > div:nth-child(3) > div.table_wrap.pc_view > table > tbody > tr.first > td.first"
  );
  checking = await page.evaluate((data) => {
    if (data.textContent == "인증완료"){
      return "success";
    }
    else{
      return "failed";
    }
  }, temp);
  

  //exception(last context)
  let templast = await page.$(
    "#baseForm > div:nth-child(3) > div.table_wrap.pc_view > table > tbody > tr.last > td.first"
  );
  checking = await page.evaluate((data) => {
    if (data.textContent == "인증완료"){
      return "success";
    }
    else{
      return "failed";
    }
  }, templast);
  
 }


 
async function getOne(page, index) {
  let temp = await page.$(
    "#baseForm > div:nth-child(3) > div.table_wrap.pc_view > table > tbody > tr:nth-child("+index+ ") > td.first"
  );
  checking = await page.evaluate((data) => {
    return data.textContent;
  }, temp);
  
  return Promise.resolve(checking);
}


async function test() {
  await runrun();
  await attendanceCheck("오픈소스SW개발", 2022);
}
test();





// //인증하기
// #baseForm > div:nth-child(3) > div.table_wrap.pc_view > table > tbody > tr.first > td.first.input_td > a
// #baseForm > div:nth-child(3) > div.table_wrap.pc_view > table > tbody > tr.first > td.first.input_td > a > span > b
// #baseForm > div:nth-child(3) > div.table_wrap.pc_view > table > tbody > tr.last > td.first
// //입력하기
// #crtfNo

// //인증 버튼 누르기
// #crtfBtn

// //확인
// body > div:nth-child(23) > div.ui-dialog-buttonpane.ui-widget-content.ui-helper-clearfix > div > button.di_btn_conf.ui-button.ui-widget.ui-state-default.ui-corner-all.ui-button-text-only

// //닫기
// body > div:nth-child(23) > div.ui-dialog-buttonpane.ui-widget-content.ui-helper-clearfix > div > button.di_btn_conf.ui-button.ui-widget.ui-state-default.ui-corner-all.ui-button-text-only


// //인증완료
// #baseForm > div:nth-child(3) > div.table_wrap.pc_view > table > tbody > tr.first > td.first
// #baseForm > div:nth-child(3) > div.table_wrap.pc_view > table > tbody > tr.last > td.first
// .name 사용

// page