1

I don't understand why the error "non-static method getLoginPage() cannot be referenced from a static context" here!? There is nothing static here! Seems trivial but I am not sure what I'm missing.

EDIT: I understand that by instantiating TestFramework I can get rid of the error but as recommended, I'm trying to avoid instantiating objects in my unit tests and pass the object creation to my framework if needed. On the other hand making getLoginPage() in TestFramework static, creates a whole bunch of similar errors in my TestFramework class!

Class Tests.java:

public class Tests {

@Test
public void User_Can_Login(){
    String username = "Jake";
    String password = "Jake";
    TestFramework.getLoginPage().Login(username, password);
    assertTrue(TestFramework.getLoginPage().CheckIfLoggedIn(username));
}

Class TestFramework.Java:

public class TestFramework {

private LoginPage loginPage;

public LoginPage getLoginPage() {
    loginPage = new LoginPage();
    PageFactory.initElements(Browser.getDriver(), loginPage);
    return loginPage;
}

public class LoginPage {

    String Url = "http://localhost:8080";
    String welcomeMsg = "Welcome";

    public void Login(String username, String password) {
        Browser.Goto(Url);
        Browser.Input(username,password);
        Browser.Submit();
    }
    public boolean CheckIfLoggedIn(String user){
        return Browser.getTextByxpath("/html/body/header/div/p").contains(welcomeMsg + " " + user);
    }
}
4
  • Probably because you have no instance of TestFramework... Commented Jul 17, 2014 at 11:05
  • getLoginPage() method is not static. So you cant call like TestFramework.getLoginPage(). You need to create object new TestFramework().getLoginPage(); Commented Jul 17, 2014 at 11:05
  • 2
    On a side note, you may wish to refer to Java naming conventions i.e. method names starting with lower case, no underscores etc Commented Jul 17, 2014 at 11:08
  • @JamesB right on! coming back to Java after a while I had forgotten the naming convention, thanks for reminding Commented Jul 17, 2014 at 11:46

3 Answers 3

3
TestFramework.getLoginPage().Login(username, password);

There in that line TestFramework is not an instance and compiler assuming it as a static type. Create an instance of TestFramework and call the method.

You might need

TestFramework tfWork = new TestFramework();
tfwork.getLoginPage().Login(username, password);
assertTrue(tfWork.getLoginPage().CheckIfLoggedIn(username));
Sign up to request clarification or add additional context in comments.

2 Comments

That seems to be the case but I'm trying to refrain from instantiating objects in my test class as it's not recommended. Does that mean I will need to make everything in my framework static instead to be able to call it from my unit test?
@SabaAhang That's not a good idea to make everything static. And remember that the varaibles and methods used in side a static method also be static. Do not do that. use instances.
1

What's TestFramework if not a static context (it's a class, not an instance).

Comments

1

You are trying to call the method using class name instead of instance.

TestFramework.getLoginPage().Login(username, password);

insted do it like this

new TestFramework().getLoginPage().Login(username, password);

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.