1

When I write Cucumber scenario's like:-

User.java Feature File

    Scenario: Add a new user profile
    Given User is on the Add user profile page
    When user enters profile information and clicks save button
    Then a new User profile is added successfully
    
    
    Scenario: Delete a user profile
    Given User is on the Add user profile page
    When user clicks on the Delete User button
    Then user should be deleted successfully

======================================================================
public class UserProfile{   //PageObject class having WebElements and Methods
Webdriver driver; //driver reference

@FindBy(linkText = "Add User")
WebElement addUserButton

@FindBy(linkText = "Delete User")
WebElement deleteUserButton

public void addNewUser(){
//do stuff
}

public void deleteUser(){
//do stuff
}

This is fine. Now, when I create a Step Definitions file, UserStepDef.java

public class UserStepDefinition{ //Step Definition .Java Class

UserProfile user; // pageObject class reference

@Given(^Given User is on the Add user profile page$)
public void addUser(){
user = PageFactory.initElements(driver, UserProfile.class)
user.addnewUser()...
}


@Then(...)
void enterUserProfileInfo(){
user = PageFactory.initElements(driver, UserProfile.class) // as i need to user PageObject elements, methods again , so i initialise again
}


Next Scenario:
@Given(...)
void deleteUser(){
user = PageFactory.initElements(driver, UserProfile.class) // as i need to user PageObject elements, methods again , so i initialise again
user.deleteUserProfile();
}

This goes on and on... I need to initialize again and again. Why can't I just initialize a PageObject class once and use it in every scenario, @Given, @Then. If I don't do it I get all kinds of StaleElement, NullPointer exceptions.

If I initialize a Class in @Given, then is there no need to initialize in its corresponding @Then, @When, @And methods?

Your Answer

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

Browse other questions tagged or ask your own question.