nrs
nrs

Reputation: 945

How to call a function in another function in protractor

first function

describe('Shortlisting page', function () {
    it('Click on candidate status Screened', function () {
        element(by.css('i.flaticon-leftarrow48')).click();
            browser.sleep(5000);
            browser.executeScript('window.scrollTo(0,250);');
            element(by.partialButtonText('Initial Status')).click();
            browser.sleep(2000);
            var screen = element.all(by.css('[ng-click="setStatus(choice, member)"]')).get(1);
            screen.click();
            element(by.css('button.btn.btn-main.btn-sm')).click();
            browser.executeScript('window.scrollTo(250,0);');
            browser.sleep(5000);

        });
    })

Second function

it('Click on candidate status Screened', function () {
       //Here i need to call first function 

    });

I want to call "first function" in "second function", how to do it please help me

Upvotes: 3

Views: 12775

Answers (1)

giri-sh
giri-sh

Reputation: 6962

What you have written as the first function is not something that you can call or invoke. describe is a global Jasmine function that is used to group test specs to create a test suite, in an explanatory/human readable way. You have to write a function to call it in your test spec or it. Here's an example -

//Write your function in the same file where test specs reside
function clickCandidate(){
    element(by.css('i.flaticon-leftarrow48')).click();
    //All your code that you want to include that you want to call from test spec
};

Call the function defined above in your test spec -

it('Click on candidate status Screened', function () {
    //Call the first function 
    clickCandidate();
});

You can also write this function in a page object file and then invoke it from your test spec. Here's an example -

//Page object file - newPage.js
newPage = function(){
    function clickCandidate(){
        //All your code that you want to call from the test spec
    });
};
module.exports = new newPage();

//Test Spec file - test.js
var newPage = require('./newPage.js'); //Write the location of your javascript file
it('Click on candidate status Screened', function () {
    //Call the function
    newPage.clickCandidate();
});

Hope it helps.

Upvotes: 5

Related Questions