Mikolaj Kondur
Mikolaj Kondur

Reputation: 41

node.js zombie.js multiple browser errors

I want to log in to a web application and start a simulation - i.e. visiting some pages and changing some values.

My code is:

var Browser = require("zombie");

fail = function (error) {console.log(error)}

function login(browser){
    //console.log("login")

    return browser.visit("http://example.com/login").then(function(){
        browser.fill('input[name="email"]', '[email protected]')
        browser.fill('input[name="password"]', 'example')
        browser.pressButton("#login")
        return browser.wait().then(function() {
            return browser;
        })

    })
}

function simpleScenario(browser, id) {
    //browser.wait().then(function() {

        browser.visit("http://example.com/next_page")
            .then(function ()  { 
                browser.wait(function() {

                    var n = 5;
                    var step = function () { 
                        var period = 100 + n;                       

                        browser.evaluate("m.productvalue(" + period + ")");

                        if(n > 0) {
                            setTimeout(step, 1000);
                        }                    
                        n--;
                    };
                    step()

                })
                .fail(fail)
            });
    //});
}

function sampleScenario(id) {
    var browser = new Browser({debug:false})
    login(browser)
        .then(function () {
            console.log("logged in");
            simpleScenario(browser, id);
        })
        .fail(fail);
}

function loadTest(numberOfThreads) {
    for(i = 0; i < numberOfThreads; i++) {
        sampleScenario(i)
    }
}

loadTest(50);

My problem is: when I execute my function in a loop once, everything is OK, but when numberOfThreads is 10 or more, I get an error:
[TypeError: Cannot use 'in' operator to search for 'compareDocumentPosition' in null]
It seems that if there are many Browser objects created, then zombie can't manage with it. Maybe the zombie library is too slow to work with many threads? Can I do the same things without using Zombie.js, only with node.js? Or maybe some part of my script needs to be optimized to run with Zombie.js?

Upvotes: 4

Views: 1829

Answers (2)

shahmanthan9
shahmanthan9

Reputation: 523

I also had similar problem and it got solved by removing debug option, while creating instance of browser. Also downgrade to v1.4.1 as 2.0 is in alpha stage

Upvotes: 0

shawnzhu
shawnzhu

Reputation: 7585

The error message [TypeError: Cannot use 'in' operator to search for 'compareDocumentPosition' in null] means zombie tries to load the response content into Document object but the response content is null, which means the content is not respond yet.

Notice that your login function is not designed to handle log in process correctly because there's nothing to measure login is successful or not. at least you should add something to wait function:

function login(browser) {

    return browser.visit('http://example.com/login').then(function() {
        browser.fill('input[name="email"]', '[email protected]')
            .fill('input[name="password"]', 'example')
            .pressButton("#login").then(function() {
                // check if h1 tag is there in 7 seconds.
                return browser.wait({waitDuration: '7s', element: "h1"});
            });
    })
}

According to your simpleScenario, it must return something not falsy value in wait. or zombie will keep wait until 5seconds.

Upvotes: 1

Related Questions