1

enter image description here

As on the image I have few elements on the POMLogin.java class (which i think I am following POM). And I need to access these elements from another class which is jUnitLogintest.java

  1. How am i going to access the elements from other class?
  2. Am i following correct approach of POM?
2
  • 1
    Voting to leave this question open, it is very specific. My Summary: "How can I reach properties of another class, and how would I do this according to the pageObject pattern/model?" – Niels van Reijmersdal Mar 16 '17 at 9:27
  • If you need to access locators from another PageObject, you are doing it WRONG. You need to level up you OOP skills. – Peter M. - stands for Monica Mar 16 '17 at 14:40
1

You can make the class Static: http://www.javatpoint.com/static-keyword-in-java

And then add a getter method getUsername() to return the By.

Then you could use it like:

By username = POMLogin.getUsername();

No, it is not following the POM correctly. You should always instantiate the class with new. Also I think PageObjects should return page objects. Your login page would return the page after login. Also the tests or other PageObject should not know the internal elements of another page. You should use the actions to interact with the page. An example loginTest:

// Arrange
User admin = new User("admin");
Page loginPage = new POMLogin();
Page pageAfterLogin = loginPage.authenticate(admin.username, admin.password);

// Act
Boolean result = pageAfterLogin.checkLogoutButtonIsVisible();

// Assert
Assert.True(result)

Java is not my main language there could be better ways (an errors in my example code), maybe ask on StackOverflow, this question is a bit off-topic here as it is a programming question, not a testing.

Also read this about getting variables from a class without a getter: https://dzone.com/articles/java-properties-without

3
  • Yes you are correct, I make the variable static and it works. – ChathuD Mar 16 '17 at 9:54
  • @ChathuD it works, but keep in mind it might not be the cleanest way to code it. You create a hardcoded dependencies between the two classes. If in doubt spar with your local Java developers. – Niels van Reijmersdal Mar 16 '17 at 9:57
  • Roger that niels – ChathuD Mar 16 '17 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.