Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. const utils = require('../step-utils');
  4. const Logger = require('../../logger').log;
  5. const DEBUG = require('../../debug');
  6. const GLOBALS = require('../../globals');
  7.  
  8. module.exports = {
  9.   initConfig,
  10.   run,
  11.   removeStyles,
  12.   forceDisplayable,
  13.   delayBeforeFillingCredentialsForm,
  14.   typingUsername,
  15.   typingPassword,
  16.   typingAccountNumber,
  17.   transformAccountNumber
  18. };
  19.  
  20. let _config;
  21.  
  22. function initConfig(newConfig) {
  23.   _config = newConfig;
  24. };
  25.  
  26. function getConfig() {
  27.   return _config;
  28. }
  29.  
  30. function* run() {
  31.   const config = getConfig();
  32.  
  33.   try {
  34.     const nightmare = GLOBALS.getNightmare();
  35.     const username = arguments[0];
  36.     const password = arguments[1];
  37.     const accountNumber = arguments[2];
  38.  
  39.     yield removeStyles(nightmare, config);
  40.     yield forceDisplayable(nightmare, config);
  41.     yield delayBeforeFillingCredentialsForm(nightmare, config);
  42.     yield typingUsername(nightmare, config, username);
  43.     yield typingPassword(nightmare, config, password);
  44.     yield typingAccountNumber(nightmare, config, accountNumber);
  45.  
  46.     Logger.info(config.log);
  47.     if (config.debug || DEBUG.isDebugUser()) {
  48.       yield DEBUG.upload(config.name, config.debugWait);
  49.     }
  50.  
  51.     yield utils.markAsDone(config);
  52.   } catch (error) {
  53.     error.configName = config.name;
  54.     throw error;
  55.   }
  56. };
  57.  
  58. function* removeStyles(nightmare, config) {
  59.   if (config.selectors.styleRemove) {
  60.     yield nightmare.use(utils.removeElementStyle(config.selectors.styleRemove));
  61.     Logger.info('remove style from elements');
  62.   }
  63. }
  64.  
  65. function* forceDisplayable(nightmare, config) {
  66.   if (config.selectors.forceDisplayable) {
  67.     yield nightmare.use(utils.setDisplayBlockOnAllElements(config.selectors.forceDisplayable));
  68.     Logger.info('forceDisplayable section done');
  69.   }
  70. }
  71.  
  72. function* delayBeforeFillingCredentialsForm(nightmare, config) {
  73.   if (config.delayBeforeFillingCredentialsForm) {
  74.     Logger.debug('Delay before filling credentials form...');
  75.     yield nightmare.wait(config.delayBeforeFillingCredentialsForm);
  76.     Logger.debug('Delay before filling credentials form... end');
  77.   }
  78. }
  79.  
  80. function* typingUsername(nightmare, config, username) {
  81.   if (config.delayedType) { // only in OTP because the site contains validation and analytic
  82.     yield nightmare.wait(config.delayBeforeUsernameTyping);
  83.     for (let index = 0; index < username.length; index++) {
  84.       yield nightmare.type(config.selectors.userNameTextBox, username[index]);
  85.       yield nightmare.wait(config.delayBetweenUsernameCharacters);
  86.     }
  87.   } else {
  88.     // we checked this elements in LoadLoginPage step
  89.     yield nightmare.type(config.selectors.userNameTextBox, username);
  90.   }
  91.  
  92.   const usernameValue = yield nightmare.use(utils.getElementValue(config.selectors.userNameTextBox));
  93.   if (usernameValue !== username) {
  94.     throw new Error('TXT_LOGIN_VALIDATION_ERROR');
  95.   }
  96.  
  97.   Logger.info('username entered');
  98. }
  99.  
  100. function* typingPassword(nightmare, config, password) {
  101.   if (config.passwordRepeatedly) {
  102.     Logger.debug('Typing password repeatedly...');
  103.     yield utils.enterTextRepeatedly(nightmare, config.selectors.passwordTextBox, password, config.numberOfPasswordTypingTries);
  104.     Logger.debug('Typing password repeatedly...end');
  105.   } else {
  106.     Logger.debug('Typing password with nightmare.type...');
  107.     yield nightmare.type(config.selectors.passwordTextBox, password);
  108.     Logger.debug('Typing password with nightmare.type...end');
  109.   }
  110.  
  111.   const passwordValue = yield nightmare.use(utils.getElementValue(config.selectors.passwordTextBox));
  112.   if (passwordValue !== password) {
  113.     throw new Error('TXT_LOGIN_VALIDATION_ERROR');
  114.   }
  115.  
  116.   Logger.info('password entered');
  117. }
  118.  
  119. function* typingAccountNumber(nightmare, config, accountNumber) {
  120.   if (config.selectors.accountNoTextBox) {
  121.     Logger.info('Waiting before typing accountNumber');
  122.     yield nightmare.wait(config.delayBeforeAccountNoTyping);
  123.  
  124.     Logger.info('Typing accountNumber');
  125.     const { accountNoTextBox, delayBeforeAccountNoTyping, numberOfAccountNumberTypingTries } = config;
  126.     yield enterTextRepeatedly(nightmare, accountNoTextBox, accountNumber, numberOfAccountNumberTypingTries, delayBeforeAccountNoTyping);
  127.     Logger.info('accountNumber entered: ' + accountNumber);
  128.   }
  129. }
  130.  
  131. function* enterTextRepeatedly(nightmare, selector, text, maxTries, waitBeforeType) {
  132.   let textValue;
  133.   let i = 0;
  134.  
  135.   do {
  136.     yield nightmare.wait(waitBeforeType);
  137.     yield nightmare.type(selector, text);
  138.     textValue = yield nightmare.use(utils.getElementValue(selector));
  139.   } while (textValue !== transformAccountNumber(text) && i++ < maxTries);
  140.  
  141.   Logger.debug('enterTextRepeatedly number of tries: ' + i);
  142.   if (textValue !== transformAccountNumber(text)) {
  143.     throw new Error('TXT_LOGIN_VALIDATION_ERROR');
  144.   }
  145. }
  146.  
  147. function transformAccountNumber(accountNumber) {
  148.   const shortAccountNumberLenght = 12; // 13
  149.   const longAccountNumberLenght = 20; // 21
  150.  
  151.   switch (accountNumber) {
  152.     case shortAccountNumberLenght:
  153.       return transformShortAccountNumber(accountNumber);
  154.     case longAccountNumberLenght:
  155.       return transformLongAccountNumber(accountNumber);
  156.     default:
  157.       throw new Error('TXT_INVALID_ACCOUNT_NUMBER_LENGTH');
  158.   }
  159. }
  160.  
  161. function transformShortAccountNumber(accountNumber) {
  162.   const firstSection = accountNumber.slice(0, 5);
  163.   const secondSection = accountNumber.slice(5);
  164.   return `${firstSection}-${secondSection}-`;
  165. }
  166.  
  167. function transformLongAccountNumber(accountNumber) {
  168.   const firstSection = accountNumber.slice(0, 5);
  169.   const secondSection = accountNumber.slice(5, 13);
  170.   const thirdSection = accountNumber.slice(13);
  171.   return `${firstSection}-${secondSection}-${thirdSection}`;
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement