skip to Main Content

I have a test that works on webdriverio, but when I move the browser into a class this error comes out:

Unable to load spec files quite likely because they rely on `browser` object that is not fully initialised.

The class:

/* eslint-disable require-jsdoc */
/* eslint-disable no-trailing-spaces */
/* eslint-disable indent */
/* eslint-disable linebreak-style */

//import {browser} from '@wdio/globals';
//const browser = require('@wdio/globals');

/**
 * A class that handles DashboardPage
 */
//export default class DashboardPage {
class DashboardPage {
    /**
    * Opens the browser.
    * @param {int} num1 The first number.
    * @param {int} num2 The second number.
    * @return {int} The sum of the two numbers.
    */
    async open() {  
        await browser.url('https://www.demoblaze.com');
        await browser.pause(200);
    }
}

// export default new DashboardPage();
module.exports = DashboardPage;

The test:

//const DashboardPage = require('../../po/pages/dashboard.page');
import {expect} from 'chai';
import DashboardPage from '../../po/pages/dashboard.page';

const dashboardPage = new DashboardPage();

describe('CSS selectors section', () => {
    describe('DemoBlaze Home Page', () => {
        beforeEach(async () => {              
            await dashboardPage.open();
            // await browser.url('https://www.demoblaze.com');    
            // await browser.pause(200); 
        });

        it('Sony vaio i7 title on card should exist', async () => { 
            const form = await $('div:nth-child(9) > div > div > h4 > a');
            expect(form).to.exist;
        });        
    });
});

wdio.conf.js on specs
‘./src/tests/specs/**/*.js’,

package.json:

{
  "name": "automated-testing-with",
  "type": "module",
  "version": "1.0.0",
  "description": "init testing example",
  "main": "index.js",
  "scripts": {
    "test": "mocha",
    "wdio": "wdio run ./wdio.conf.js"
  },
  "repository": {
    "type": "git",
    "url": "https://git.epam.com/jhon_hernandez/automated-testing-with-js"
  },
  "author": "Jhon Hernandez",
  "license": "ISC",
  "devDependencies": {
    "@wdio/allure-reporter": "^8.15.0",
    "@wdio/browser-runner": "^8.14.6",
    "@wdio/cli": "^8.14.6",
    "@wdio/junit-reporter": "^8.15.0",
    "@wdio/local-runner": "^8.15.4",
    "@wdio/mocha-framework": "^8.14.0",
    "@wdio/spec-reporter": "^8.14.0",
    "eslint": "^8.46.0",
    "eslint-config-google": "^0.14.0",
    "geckodriver": "^4.2.0",
    "mocha": "^10.2.0",
    "wdio-geckodriver-service": "^5.0.2"
  },
  "dependencies": {
    "c8": "^8.0.1",
    "chai": "^4.3.7",
    "chromedriver": "^115.0.1",
    "husky": "^8.0.3",
    "mocha": "^10.2.0",
    "mochawesome": "^7.1.3",
    "wdio-chromedriver-service": "^8.1.1",
    "webdriverio": "^8.14.3"
  }
}

I checked the route, and I tried several ways of import the class, but the error remains

The console doesn’t show much real information that guides me.

When you use an imported file, how you do it?

2

Answers


  1. Chosen as BEST ANSWER

    I had to create a new proyect because the configuration of the reporters was apparently creating conflicts, any way, this works:

    const { browser } = require('@wdio/globals')
    
    
    class DashboardPage {
       
        openPage (path) {
            return browser.url(`https://www.demoblaze.com`)
        }   
    }
    
    
    module.exports = new DashboardPage();
    

    The test:

    const { expect } = require('@wdio/globals')
    const DashboardPage = require('../pageobjects/dashboard.page')
    
    describe('POM CSS section', () => {
        it('Sony vaio i7 title on card should exist', async () => {
            await DashboardPage.openPage('login');
            await browser.pause(200);
    
            const form = await $('div:nth-child(9) > div > div > h4 > a');
            expect(form).toBeExisting();
        })
    })
    

  2. Have you tried to const DashboardPage = require(‘../../po/pages/dashboard.page’);

    I seen that it was left as a comment instead.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search