I am making a Regression suite Automation project using Selenium Webdriver, Java, Cucumber, Maven etc.
Now, the problem is that I have multiple Page Object classes that I initialise again and again using PageFactory Class in almost every scenario Step Definition methods i.e. @Given
, @Then
, @And
etc. I am getting a lot of "StaleElement Exception
" ,"TimeoutException
" and "Null Pointer
" exception errors consistently?
Can I just initialize a PageObject class JUST ONCE and use it in all test classes, step definition methods?
I have a BaseClass that initializes the ChromeWebDriver instance and all other StepDefinitions java classes implement that BaseClass.
Cucumber project structure is something like this:
public class BaseClass{
public void setupWendriver(...)
}
public class User implements BaseClass{ // this is a stepDefinition class
Webdriver driver;
@AddUserProfile
Scenario: Add a new User Profile in Users Section
Given Navigate to Users Tab
Then Add a new User Profile
@Given("^Navigate to Users Tab$")
public void Login() throws Exception {
UserProfile user = PageFactory.initElements(BaseClass.driver, UserProfile.class);
user.enterUserName();
user.enterPwd();
etc...
}
@Then("^Add a new User Profile$")
UserProfile user = PageFactory.initElements(BaseClass.driver, UserProfile.class);
UserProfile user = PageFactory.initElements(BaseClass.driver, UserProfile.class);
user.addNewUser();
user.VerifyUser();
Now, you can see that I have to initialize the SAME PageObject Class, i.e UserProfile in all StepDefinition methods... If I don't it and just initialize once, still I get StaleElement, NullPointer, Timeout errors... If I initialize just once per StepDef class, still I get the Error.
NOTE: I don't want to initialize every PageObject in its constructor, but want to initialize it directly.
public UserProfile(){
driver = BaseClass.driver;
PageFactory.initElements(this, UserProfile.class)
// Dont want to do this to save one extra line of code, i am not creating object of pageObject class, im directly assigning it initElements() return type...
Please help, as I can't get my code to work correctly?