Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i load/run an external .js script from Protractor

I am running a Protractor test that will verify various elements on a website, but before doing this I want to run an external JavaScript script with arguments using node which will set up some prerequisites beforehand. However, I am completely stuck as to how I would run the "javascript_code.js" script from the protractor.

I know the protractor code below is completely wrong, I have just used the describe/it layout to show where I want to do what and where:

describe('Validate elements on website',function(){

    // reference and run the javascript_code.js
    require('C:\<file path>\Javascript_code.js');
    Run "node javascript_code.js arg1 arg2 arg3"

    it('Validate onscreen elements', function(){
        
        <Do website verification>
     });
    });     
like image 377
Jessa Brandy Avatar asked Jun 21 '26 22:06

Jessa Brandy


1 Answers

The approach will depend on what you're doing in your js file. If your code is synchronous, the easiest way is to make your javascript_code.js a function, that takes parameters and then export it, like so

module.exports = function(arg1, arg2) {
    console.log(arg1);
    console.log(arg2);
};

and then require and invoke your function

let your_func = require('C:\<file path>\Javascript_code.js');
your_func(arg1, arg2);

describe('Validate elements on website',function(){

    it('Validate onscreen elements', function(){

    });
});     

However, if your js code is asynchronous the approach should be different. Let me know

like image 59
Sergey Pleshakov Avatar answered Jun 23 '26 12:06

Sergey Pleshakov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!