protractor icon indicating copy to clipboard operation
protractor copied to clipboard

Ability to add own extention files to config file

Open Hardcode37 opened this issue 12 years ago • 9 comments

I think it would be a great addition to protractor to be able to add (via the config) the ability to extend protractor. I have written some small extension functions which I would like to be able to use with newer versions of protractor as they are release without having to add these directly to lib/protractor.js.

This would enable users of protractor to write their own functions to stop replication of certain tests and then add require them in the config file.

Has something like this been implemented or could it be a possible implementation in the future.

Hardcode37 avatar Jan 22 '14 15:01 Hardcode37

Can you give an example of what type of functions you'd like to add? Could these be things that you store in a helper file that just gets required?

If they're more general and you'd want to share, what type of hooks would you need in to Protractor?

juliemr avatar Jan 22 '14 21:01 juliemr

I built an onPrepare.conf.js file where i started to put this kind of things, e.g.

// Convenient initial assumptions
browser.manage().window().maximize();
browser.get(browser.params.routes.index);

// Protractor extensions. TODO: Is this the proper place to put this?
browser.isInternetExplorer = function() {
    var browserName;

    browser.getCapabilities().then(function(s) { 
        browserName = s.caps_.browserName;
    });

    return /i.*explore/.test(browserName);
};

But i'm not sure the proper location to put these, any suggestions or link to related doc are welcome! :)

elgalu avatar Jan 22 '14 21:01 elgalu

Hey Julie,

I was looking at something along this line

/** click on a hidden element and return the promise/result */ Protractor.prototype.clickOnHiddenLink = function(locator) { var ptor = this; var hiddenElement = ptor.findElement(locator); return ptor.driver.executeScript("arguments[0].click()", hiddenElement); };

I know I can do this directly in protractor.js but is there some way of hooking into Protractor.prototype to be able to do this externally. I figure going the config way would enable users to require extended functionality on a more global level rather than on a per spec basis. Obviously this is a small test case but if I have a large amount of tests that use this it saves me time just using browser.clickOnHiddenElemement(by.id(''));

I also have test specs that use the same set of findElements to add addresses, make bookings etc, it would be neat to be able to put this in a function and add this file to the conf to be accessed in any spec for later use.

Hardcode37 avatar Jan 22 '14 23:01 Hardcode37

@Hardcode37 what I'd do is make a helper file like this:

// file helper.js
exports.clickOnHiddenLink = function(locator) {
  var elm = browser.findElement(locator);
  return browser.driver.executeScript("...", elm);
};

And then inside your test:

var helper = require('./helper.js');
describe('foo', function() {
  it('should do stuff', function() {
    helper.clickOnHiddenLink(by.id(''));
  });
});

juliemr avatar Jan 23 '14 00:01 juliemr

that's exactly what I need, but would be nice to be able to include the required file inside the conf file rather than per spec require() Thanks Julie

Hardcode37 avatar Jan 23 '14 08:01 Hardcode37

@Hardcode37 would putting the require statement in onPrepare work? You could add to the global instead of exporting, if importing into each test is a problem.

juliemr avatar Jan 23 '14 09:01 juliemr

I'll have a look and let you know if it works

Hardcode37 avatar Jan 24 '14 10:01 Hardcode37

@juliemr yes it can be done, if you use browser.nameOfFunction = function() { }; in the helper file and just require('nameOfFile') in the onPrepare you can extend protractor quite well.

Hardcode37 avatar Jan 24 '14 11:01 Hardcode37

OK - seems like all the functionality you want is already there, so I think that closes this issue then? Please re-open if there's some other request.

juliemr avatar Jan 24 '14 18:01 juliemr