How to call a function in another function in protractor

时光总嘲笑我的痴心妄想 提交于 2019-12-10 18:46:36

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/33301428/how-to-call-a-function-in-another-function-in-protractor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!