Advertisement
Guest User

Untitled

a guest
Mar 10th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. import { launch } from "puppeteer";
  2.  
  3. export const login = async function(host: string, username: string, password: string) {
  4. const browser = await launch();
  5.  
  6. try {
  7. const page = await browser.newPage();
  8.  
  9. var puppeteerTransferedStorage = {};
  10.  
  11. //setup console logging
  12. page.on("console", consoleObj => {
  13. const transferPrefix = "TRANSFER:";
  14. if (consoleObj.text().startsWith(transferPrefix)) {
  15. puppeteerTransferedStorage = JSON.parse(
  16. consoleObj.text().substring(transferPrefix.length)
  17. );
  18. } else {
  19. console.log(`BROWSER: ${consoleObj.text()}`);
  20. }
  21. });
  22.  
  23. console.log("navigating to page");
  24. await page.goto(host);
  25.  
  26. console.log("waiting for 1 second");
  27. //todo: find a hook that works instead of waiting
  28. await page.waitFor(2000);
  29.  
  30. console.log("typing in username");
  31. await page.type("#i0116", username, { delay: 20 });
  32.  
  33. console.log("clicking 'next' button");
  34. await page.click("#idSIButton9");
  35.  
  36. console.log("waiting 5 seconds");
  37. //todo: find a hook that works instead of waiting
  38. await page.waitFor(5000);
  39.  
  40. console.log("typing in email");
  41. await page.type("#user_email", username, { delay: 20 });
  42.  
  43. console.log("typing in password");
  44. await page.type("#user_password", password, { delay: 20 });
  45.  
  46. console.log("clicking submit");
  47. await page.click("#user_submit");
  48.  
  49. console.log("waiting 1 second");
  50. //todo: find a hook that works instead of waiting
  51. await page.waitFor(1000);
  52.  
  53. console.log("taking screenshot of page");
  54. await page.screenshot({ path: "example.png" });
  55.  
  56. console.log("getting session storage");
  57. await page.evaluate(() => {
  58. let puppeteerSessionStorage = {};
  59. for (let i = 0; i < sessionStorage.length; i++) {
  60. puppeteerSessionStorage[sessionStorage.key(i)] = sessionStorage.getItem(
  61. sessionStorage.key(i)
  62. );
  63. }
  64.  
  65. console.log("TRANSFER:" + JSON.stringify(puppeteerSessionStorage));
  66. });
  67.  
  68. return puppeteerTransferedStorage;
  69. } finally {
  70. console.log("closing browser");
  71. await browser.close();
  72. }
  73. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement