Search code examples
javascriptseleniumautomated-testscypresswebdriver-io

How to import and use static javascript object in webdriverIO testing?


I am trying to test some values present in elements, the values are stored in a static object, this is the approach I am taking.

File - someObjects.js (stores the static objects)

class SomeObjects {
    static headers = {
        header1: "Something",
        header2: "Something else",
    };
}
export default SomeObjects;

Now I want to check whether some element matches header1. So this is the test file that I am running

import SomeObjects from "../../SomeObjects";

const HomePage = require('../../HomePage');

describe("", () => {

    it("", () => {
        HomePage.open()
        const title = browser.$('#title');
        expect(title).toHaveText(SomeObjects.headers.header1);
    });
});

However, this is not working out at all. Any help regarding this would be appreciated, thanks. P.S. This is the error message I am getting on running the test. [0-0] Error: Unable to load spec files quite likely because they rely on browser object that is not fully initialised.


Solution

  • Looks like you have missed new keyword and braces () while exporting class, your code should look something like this..

    class SomeObjects {
       static headers = {
           header1: "Something",
           header2: "Something else",
        };
    }
    export default new SomeObjects();
    

    Also this can be achieved using constants class or in using json file. Few example you will find in my personal webdriverio git repo WDIO6_TypeScript_BDD