0

I am having issues when trying to create a function inside of a constructor. The function doesn't seem to understand variables that are also initialized inside of the constructor.

I need the variables and functions inside of the class.

Here is some sample code

class dLoginPage { 
  constructor() {   
    this.Username     = Aliases.browser.page_General_Login.formLoginform.textboxUsernameinput;
    this.Password     = Aliases.browser.page_General_Login.formLoginform.passwordboxPasswordinput;
    this.SubmitButton = Aliases.browser.page_General_Login.formLoginform.submitbuttonLogin;

    this.Login = function() {
      this.Username.SetText("admin");
      this.Password.SetText("admin");
      this.SubmitButton.ClickButton();
    }
  }
}

module.exports = dLoginPage;

When I try running the login function, it states that username and password and submitbutton have not been initialized. But if i take the login function outside of the class, everything works. But i need the function inside of the class.

4
  • why do you want to create function in the constructor? Commented May 9, 2018 at 17:16
  • I need to access the class from another class. For example I have a class called GeneralModule I want the ability to go GeneralModule.LoginPage.Login Commented May 9, 2018 at 17:17
  • Why is it that you are using this.Login Commented May 9, 2018 at 17:21
  • Because if i don't i get a runtime error. ReferenceError: Login is not defined Commented May 9, 2018 at 17:24

2 Answers 2

1

Why not simply add the function as a method to your class?

Constructor functions didn't support this, but ES6 classes do.

class dLoginPage {
  constructor() {
    this.Username = Aliases.browser.page_General_Login.formLoginform.textboxUsernameinput;
    this.Password = Aliases.browser.page_General_Login.formLoginform.passwordboxPasswordinput;
    this.SubmitButton = Aliases.browser.page_General_Login.formLoginform.submitbuttonLogin;
  }

  Login() {
    this.Username.SetText("admin");
    this.Password.SetText("admin");
    this.SubmitButton.ClickButton();
  }

}
Sign up to request clarification or add additional context in comments.

Comments

0

use arrow function

this.Login = ()=> {
  this.Username.SetText("admin");
  this.Password.SetText("admin");
  this.SubmitButton.ClickButton();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.