Skip to content

Commit

Permalink
Change connection using SF URL instead of learning hub URl
Browse files Browse the repository at this point in the history
Add support for Universal ID accounts
Add flag --no-deprecation for Buffer()
  • Loading branch information
AZn5ReD committed Jan 15, 2021
1 parent 9a1eb00 commit ec04f4d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 59 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
},
"scripts": {
"build": "webpack -p",
"start": "set NODE_ENV=production && node ./dist/index.js",
"webpack-dev": "set NODE_ENV=development && webpack && node ./dist/index.js",
"dev": "set NODE_ENV=development && nodemon --exec babel-node src/index.js"
"start": "set NODE_ENV=production && node --no-deprecation ./dist/index.js ",
"webpack-dev": "set NODE_ENV=development && webpack && node --no-deprecation ./dist/index.js",
"dev": "set NODE_ENV=development && nodemon --exec babel-node --no-deprecation src/index.js"
},
"repository": {
"type": "git",
Expand Down
7 changes: 4 additions & 3 deletions src/constants.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"LEARNINGHUB_URL": "https://learninghub.sap.com/login",
"SUCCESS_FACTOR_URL": "https://performancemanager.successfactors.eu/sf/learning?company=learninghub",
"LOGIN_SELECTOR": "input[id='j_username']",
"PASSWORD_SELECTOR": "input[id='j_password']",
"UNIVERSAL_ID_PASSWORD_SELECTOR": "input[id='password']",
"SUBMIT_SELECTOR": "button[id='logOnFormSubmit']",
"URL_CONNECTED": "https://learninghub.sap.com/?languagePrompt=true",
"SUCCESS_FACTOR_URL": "https://performancemanager.successfactors.eu/sf/learning?company=learninghub",
"UNIVERSAL_ID_SUBMIT_SELECTOR": "button.test-button",
"URL_CONNECTED": "https://saplearninghub.plateau.com/",
"SVG_URL": "xml/topic$$$.svg",
"TOPIC_VAR": "$$$"
}
3 changes: 1 addition & 2 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import puppeteer from "puppeteer";
import config from "./config";
import { login, getAuthorization } from "./navigation";
import { login } from "./navigation";
import { downloadFile, targetDirCheck } from "./file";
import processSend from "./process";

Expand Down Expand Up @@ -29,7 +29,6 @@ export default async function main() {
if (!targetDirCheck() || !(await login(page))) {
throw new Error("Error during init");
}
await getAuthorization(page);
await downloadFile(page);

process.on("exit", (code) => {
Expand Down
71 changes: 20 additions & 51 deletions src/navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ async function redirection(page) {

export async function login(page) {
try {
console.info("Going to:", constant.LEARNINGHUB_URL);
console.info("Going to:", constant.SUCCESS_FACTOR_URL);
processSend({ log: "Connecting..." });
await page.goto(constant.LEARNINGHUB_URL, { followRedirect: true });
await page.goto(constant.SUCCESS_FACTOR_URL, { followRedirect: true });
await page.waitForSelector(constant.LOGIN_SELECTOR, {
visible: true,
});
Expand All @@ -23,22 +23,29 @@ export async function login(page) {
await page.click(constant.SUBMIT_SELECTOR);

console.info("Entering password: ***");
await page.waitForSelector(constant.PASSWORD_SELECTOR, {
visible: true,
});
await page.type(constant.PASSWORD_SELECTOR, config.PASSWORD);
const password_input = await Promise.race([
page.waitForSelector(constant.PASSWORD_SELECTOR, {
visible: true,
}),
page.waitForSelector(constant.UNIVERSAL_ID_PASSWORD_SELECTOR, {
visible: true,
}),
]);
await password_input.type(config.PASSWORD);

console.info("Submiting...");
await Promise.all([
page.click(constant.SUBMIT_SELECTOR),
redirection(page),
const submit_button = await Promise.race([
page.waitForSelector(constant.SUBMIT_SELECTOR, {
visible: true,
}),
page.waitForSelector(constant.UNIVERSAL_ID_SUBMIT_SELECTOR, {
visible: true,
}),
]);
if (page.url().startsWith("https://accounts.sap.com/")) {
throw new Error("Login failed :(");
}
await submit_button.click();
await redirection(page);

if (page.url() === constant.URL_CONNECTED) {
if (page.url().startsWith(constant.URL_CONNECTED)) {
console.info("Connected :)");
return true;
} else {
Expand All @@ -49,41 +56,3 @@ export async function login(page) {
console.error("Error during login:", error);
}
}

async function cookiePopup(page) {
try {
const frame = await page
.mainFrame()
.childFrames()
.find((f) => {
return f.name().startsWith("pop-frame") ? f : null;
});
if (!frame) {
console.info("No cookie consent");
return;
}
await frame.waitForSelector("a[class='call']");
await frame.click("a[class='call']");
console.info("Cookie accepted");
await page.waitForNavigation();
} catch (error) {
console.error("Error during cookie consent:", error);
}
}

async function navToSF(page) {
try {
console.info(`Navigating to SuccessFactor`);
await page.goto(constant.SUCCESS_FACTOR_URL, { followRedirect: true });
await redirection(page);
await redirection(page);
} catch (error) {
console.error("Error while navToSF", error);
}
}

export async function getAuthorization(page) {
processSend({ log: "Authentication..." });
await cookiePopup(page);
await navToSF(page);
}

0 comments on commit ec04f4d

Please sign in to comment.