Search code examples
javascriptexportdetoxmodule-export

TypeError: loginScreen.visibleOfWelcome is not a function


I want to access method defined in the javascript class. While running the code, I am getting error as

TypeError: loginScreen.visibleOfWelcome is not a function

This is my code : firstPage.CCC.js

module.exports = class LoginScreen {

    get welcome() {
      return element(by.id("welcome"));
    }


    visibleOfWelcome() {
       expect(this.welcome).toBeVisible();
    }

  }

firstTest.specs.js

var loginScreen = require("./firstPage.CCC.js");

describe('Example', () => {
  beforeEach(async () => {
    await device.reloadReactNative();
  });

    it('should have welcome screen', async () => {
      await loginScreen.visibleOfWelcome();
    });
  })

If I write await loginScreen.visibleOfWelcome(); then I get an error as

TypeError: loginScreen.visibleOfWelcome is not a function

if write await loginScreen.visibleOfWelcome; then my code works file.

Any idea why ? I want to call as await loginScreen.visibleOfWelcome();


Solution

  • Try below solution:

    export class LoginScreen {
    
        get welcome() {
          return element(by.id("welcome"));
        }
    
    
        visibleOfWelcome() {
           expect(this.welcome).toBeVisible();
        }
    
      }
    

    Now here you can do this:

    var LoginScreen = require("./firstPage.CCC");
    var loginScreen= new LoginScreen();
    
    describe('Example', () => {
      beforeEach(async () => {
        await device.reloadReactNative();
      });
    
        it('should have welcome screen', async () => {
          await loginScreen.visibleOfWelcome();
        });
      })
    

    Hope it will work for you.