Saturday, 18 March 2017

how does it.each work in mocha?

I am trying to understand how it.each function work in mocha.js I have the following code :

describe('fetch all the selected topics',function(){
        it.each([1,2,3,4], 'should loop over each element in an array', function(count){
            fetchSelectedTopics(driver,count); // function that does something
        });

    }); 

        describe('Coach Sign In',function(){
            it('log in as coach',function(done){
                driver.get('https://dev.pluma.co/coach-sign-in');
                done();
            });
            it('login as coach',function(done){
                login.username(coach,driver);
                login.password(coachPassword,driver);
                driver.findElement(webdriver.By.className("client-onboarding-signin-btn")).click().then(function() {
                    done();
                });
            });
        });

Now ideally i am expecting the fetchSelectedTopics to run 4 times and then run the next set of describes. But it doesn't work that way, I want to make the process asynchronous currently instead of running the fetchSelectedTopics 4 times it calls coach sign in url and coach login. this is my fetchSelectedTopics code:

function fetchSelectedTopics(driver,count) {
        describe('',function(){
            it('Topic:: '+count,function(done){
                driver.sleep(2000);
                selectedTopic = driver.findElement(webdriver.By.xpath("//*[@id='selectedModules']/div["+count+"]")).getText().then(function(text){
                    console.log(text);
                    selectedTopics.push(text);
                    //console.log(selectedTopics[i]);
                    //i = Number(i) + 1;
                    done();
                });
            });
        });
}



via user2987322

No comments:

Post a Comment