0

I have some specs that are using the same functions, I would like to make one single file only for functions and read from this file while executing my scripts, would be that possible? if so.. how to do that?

During google searchers I found the "exports" to add in config file but didn't work (also I don't know how to call it from the config)

For example, I would like to add 2 functions in my config file (or separated file only for functions) and during any point of my execution, call it from the spec file:

function loginAdminUser(userElement, passWordElement, userName, password){
    var loginButton = element(by.id('logIn'));
    browser.get('https://( ͡° ͜ʖ ͡°).com/');
    userElement.sendKeys(userName);
    passWordElement.sendKeys(password);
    loginButton.click();
}

function accessHistoryViewDetail(){
    menuForViews.then(function(selectview) {
    selectview[3].click();
    browser.sleep(500);
    });
}

1 - How can I do that? (using "Suites" would be an option?)

2 - How to call them in my specs?

Thank you and have a good day!

Alexandre
  • 432
  • 1
  • 3
  • 14

1 Answers1

1

As far as I know you cannot add utility functions that you want to use in your tests in the config file. The options in the config file are generally for setting up the testing environment.

You can however put your functions in a separate file and import that to use the functions. Below is an example of how to do that using js and Node's module exports, you can do something similar with ts using classes.

// utils.js

function loginAdminUser(userElement, passWordElement, userName, password){
  var loginButton = element(by.id('logIn'));
  browser.get('https://( ͡° ͜ʖ ͡°).com/'); // nice Lenny face :)
  userElement.sendKeys(userName);
  passWordElement.sendKeys(password);
  loginButton.click();
}

function accessHistoryViewDetail() {
  menuForViews.then(function(selectview) {
  selectview[3].click();
  browser.sleep(500);
  });
}

module.exports = {
  loginAdminUserloginAdminUser: loginAdminUser,
  accessHistoryViewDetail: accessHistoryViewDetail
}

Then in your spec file

import * as utils from './utils.js';

...

  it('should ...', () => {
    ...
    utils.accessHistoryViewDetail();
    ...
  });
});

I hope that helps.

MkMan
  • 1,779
  • 1
  • 14
  • 27
  • Hi Mansour thanks for the answer =) I am getting this error: [13:14:02] E/launcher - Error: C:\.......\Spec.js:7 import * as utils from './utils.js'; ^^^^^^ SyntaxError: Unexpected token import – Alexandre Aug 08 '18 at 10:24
  • This could be caused by your node version. Take a look [here](https://stackoverflow.com/questions/39436322/node-js-syntaxerror-unexpected-token-import). Try replacing the import line with this `const utils = require('./utils.js'); // <- the string inside the require is the path to the file`. – MkMan Aug 08 '18 at 10:34
  • 1
    Perfect! i updated the node version to the last one but still with the error message but now for the *... then I changed to the const as you said and its working perfectly! thank you very much!! – Alexandre Aug 08 '18 at 10:45