最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to call a function in another function in protractor - Stack Overflow

programmeradmin5浏览0评论

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

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

Share Improve this question asked Oct 23, 2015 at 11:40 nrsnrs 9453 gold badges18 silver badges42 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

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.

发布评论

评论列表(0)

  1. 暂无评论