garris / BackstopJS

Catch CSS curve balls.
http://backstopjs.org
MIT License
6.79k stars 604 forks source link

how to page-object in onReady.js #938

Open kvkulish opened 5 years ago

kvkulish commented 5 years ago

I need to do a lot of replacement on different pages. So i made page-object file with vars and replacement functions and trying to connect it to onReady. But there is a problem with require external *.js - code inside require promis is not executed. Here is my code, onReady.js:

`module.exports = async (page, scenario, vp) => {

await require('./clickAndHoverHelper')(page, scenario);

if (scenario.label == "catalog-page") {

    page.evaluateHandle(() => { 

        require(['/var/lib/jenkins/workspace/backstop-desktop/PageObjects/CatalogPage.js'], function (CatalogPage) {

            console.log('This code has newer run');
            let catalogPage = new CatalogPage();
            catalogPage.replaceTopBanner('topBannerReplacementURL');

        });                 
    }
}

};`

CatalogPage.js: `var CatalogPage = function() {

this.topBanner = document.querySelector('.bluefoot-wrapper img');

this.replaceTopBanner = (fakePicURL) => {
    try {
        topBanner.src = fakePicURL;                
    } catch (err) {
        console.log('Top banner picture has not changed: ' + err);
    }    
}

this.itemsList = document.querySelectorAll('.grid-item'); 

};

module.exports = CatalogPage;`

Where I made a mistake? Is there any another way to include external libraries?

brendonbribeiro commented 5 years ago

You should use page.evaluateHandle to execute browser code. The problem with your code is that require is from node.