Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- const utils = require('../step-utils');
- const Logger = require('../../logger').log;
- const DEBUG = require('../../debug');
- const GLOBALS = require('../../globals');
- module.exports = {
- initConfig,
- run,
- removeStyles,
- forceDisplayable,
- delayBeforeFillingCredentialsForm,
- typingUsername,
- typingPassword,
- typingAccountNumber,
- transformAccountNumber
- };
- let _config;
- function initConfig(newConfig) {
- _config = newConfig;
- };
- function getConfig() {
- return _config;
- }
- function* run() {
- const config = getConfig();
- try {
- const nightmare = GLOBALS.getNightmare();
- const username = arguments[0];
- const password = arguments[1];
- const accountNumber = arguments[2];
- yield removeStyles(nightmare, config);
- yield forceDisplayable(nightmare, config);
- yield delayBeforeFillingCredentialsForm(nightmare, config);
- yield typingUsername(nightmare, config, username);
- yield typingPassword(nightmare, config, password);
- yield typingAccountNumber(nightmare, config, accountNumber);
- Logger.info(config.log);
- if (config.debug || DEBUG.isDebugUser()) {
- yield DEBUG.upload(config.name, config.debugWait);
- }
- yield utils.markAsDone(config);
- } catch (error) {
- error.configName = config.name;
- throw error;
- }
- };
- function* removeStyles(nightmare, config) {
- if (config.selectors.styleRemove) {
- yield nightmare.use(utils.removeElementStyle(config.selectors.styleRemove));
- Logger.info('remove style from elements');
- }
- }
- function* forceDisplayable(nightmare, config) {
- if (config.selectors.forceDisplayable) {
- yield nightmare.use(utils.setDisplayBlockOnAllElements(config.selectors.forceDisplayable));
- Logger.info('forceDisplayable section done');
- }
- }
- function* delayBeforeFillingCredentialsForm(nightmare, config) {
- if (config.delayBeforeFillingCredentialsForm) {
- Logger.debug('Delay before filling credentials form...');
- yield nightmare.wait(config.delayBeforeFillingCredentialsForm);
- Logger.debug('Delay before filling credentials form... end');
- }
- }
- function* typingUsername(nightmare, config, username) {
- if (config.delayedType) { // only in OTP because the site contains validation and analytic
- yield nightmare.wait(config.delayBeforeUsernameTyping);
- for (let index = 0; index < username.length; index++) {
- yield nightmare.type(config.selectors.userNameTextBox, username[index]);
- yield nightmare.wait(config.delayBetweenUsernameCharacters);
- }
- } else {
- // we checked this elements in LoadLoginPage step
- yield nightmare.type(config.selectors.userNameTextBox, username);
- }
- const usernameValue = yield nightmare.use(utils.getElementValue(config.selectors.userNameTextBox));
- if (usernameValue !== username) {
- throw new Error('TXT_LOGIN_VALIDATION_ERROR');
- }
- Logger.info('username entered');
- }
- function* typingPassword(nightmare, config, password) {
- if (config.passwordRepeatedly) {
- Logger.debug('Typing password repeatedly...');
- yield utils.enterTextRepeatedly(nightmare, config.selectors.passwordTextBox, password, config.numberOfPasswordTypingTries);
- Logger.debug('Typing password repeatedly...end');
- } else {
- Logger.debug('Typing password with nightmare.type...');
- yield nightmare.type(config.selectors.passwordTextBox, password);
- Logger.debug('Typing password with nightmare.type...end');
- }
- const passwordValue = yield nightmare.use(utils.getElementValue(config.selectors.passwordTextBox));
- if (passwordValue !== password) {
- throw new Error('TXT_LOGIN_VALIDATION_ERROR');
- }
- Logger.info('password entered');
- }
- function* typingAccountNumber(nightmare, config, accountNumber) {
- if (config.selectors.accountNoTextBox) {
- Logger.info('Waiting before typing accountNumber');
- yield nightmare.wait(config.delayBeforeAccountNoTyping);
- Logger.info('Typing accountNumber');
- const { accountNoTextBox, delayBeforeAccountNoTyping, numberOfAccountNumberTypingTries } = config;
- yield enterTextRepeatedly(nightmare, accountNoTextBox, accountNumber, numberOfAccountNumberTypingTries, delayBeforeAccountNoTyping);
- Logger.info('accountNumber entered: ' + accountNumber);
- }
- }
- function* enterTextRepeatedly(nightmare, selector, text, maxTries, waitBeforeType) {
- let textValue;
- let i = 0;
- do {
- yield nightmare.wait(waitBeforeType);
- yield nightmare.type(selector, text);
- textValue = yield nightmare.use(utils.getElementValue(selector));
- } while (textValue !== transformAccountNumber(text) && i++ < maxTries);
- Logger.debug('enterTextRepeatedly number of tries: ' + i);
- if (textValue !== transformAccountNumber(text)) {
- throw new Error('TXT_LOGIN_VALIDATION_ERROR');
- }
- }
- function transformAccountNumber(accountNumber) {
- const shortAccountNumberLenght = 12; // 13
- const longAccountNumberLenght = 20; // 21
- switch (accountNumber) {
- case shortAccountNumberLenght:
- return transformShortAccountNumber(accountNumber);
- case longAccountNumberLenght:
- return transformLongAccountNumber(accountNumber);
- default:
- throw new Error('TXT_INVALID_ACCOUNT_NUMBER_LENGTH');
- }
- }
- function transformShortAccountNumber(accountNumber) {
- const firstSection = accountNumber.slice(0, 5);
- const secondSection = accountNumber.slice(5);
- return `${firstSection}-${secondSection}-`;
- }
- function transformLongAccountNumber(accountNumber) {
- const firstSection = accountNumber.slice(0, 5);
- const secondSection = accountNumber.slice(5, 13);
- const thirdSection = accountNumber.slice(13);
- return `${firstSection}-${secondSection}-${thirdSection}`;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement