1
@Add User
Scenario: Add a new User profile in the System
Given user is on the Add User profile page
Then Add a new user profile in the system
Then verify that user has been added successfully

Now, for example, i want call the methods of "Login" over here. If i dont initialize the elements using the page factory in this scenario itself, then i get null pointer exception.

Login login = PageFactory.initElements(driver, Login.class);

if i have initialized the class in some other scenario, then also i get null pointer error. Should i initialize the class globally outside of any methods?

0

In cucumber to share states between steps you need to use DI mechanisms. Your steps definitions can be (and usually are) spread over several classes.

Here you may find general concept description https://cucumber.io/docs/cucumber/state. Which is stating:

Cucumber will create a new instance of each of your glue code classes before each scenario.

So you need to pass the object (which you need for your test - page object or anything else) reference to each of your classes where you need that and they will persist within a scenario.

There are two examples.

4
  • Thanks for the answer. I will refer these pages and learn something new. But if i just intialize all PageObject classes at the Starting of my StepDefinition Class , then is that also fine?
    – user50752
    Sep 7 at 8:15
  • That depends on the details. It might be OK and might be not OK.
    – Alexey R.
    Sep 7 at 8:27
  • @user50752 if all your step defs are defined in one class then technically it should be okay. But not from design point of view.
    – Alexey R.
    Sep 7 at 8:32
  • Thanks for the clarification.
    – user50752
    Sep 7 at 9:54

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.