2

I am wring a PageFactory framework for a website using maven+TestNG, I have page wise PageObject classes where all web elements and actions specific to page are present like LoginPageObject, AccountSelectionPageObject...
I have a class "Base" where the common elements like WebDriver, Logger are present. I have a class "BasePage" where the common actions like click, scroll, select, refresh... are present

MyTestng.xml is having separate <class> entry for both all individual pages. It's just that I am initializing the browser object in @BeforeSuiit and stored/placed it in the Base class which is being extended in my Test classes Below is the flow/arch I came up for my project. enter image description here Issue:

I have multiple @Test in each of my test classes. When my Test classes are executed individually, all @Test script executed, but when I execute them continuously, i.e. my testng file have separate entries for all my test classes, my execution fails. Error says unable to find element on page, I have wait statements, but still it's not working.

I have tried debugging code, but not able to find the reason as the flow stops on starting of second page with exception saying element not found

Code:

@FindBy(id="listAccounts")
WebElement accountDropdown;

public void selectAccount(){
    logger.info("Selecting Account");
    implicitwait(10);

    Select dropdown = new Select(accountDropdown);
    logger.info("Drop down is multiple::"+ dropdown.isMultiple());
}

Expected:
Code should execute completely even when separated code page wise.

Actual:
When I have all pages code in one test class, code executed. But when I place them separately in page wise test class, element not found exception is thrown.

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"listAccounts"}
Kamal
  • 2,384
  • 1
  • 13
  • 25
Rajaji
  • 133
  • 2
  • 14
  • I can't tell from your limited code and your chart if you're implementing pageobject factory properly. Do you pass a driver object from your test class to the pageobject class, and do you call the base pageobjectclass super, passing it the driver to initialize the page objects? – Bill Hileman Jul 09 '19 at 17:17
  • @BillHileman ** I am passing the driver object to Instantiate Page Object using Page Factory but i am not passing driver as i am not using the Constructors in my classes as i did not see the need of it for passing the driver object to super class because I have a Utility/Helper class called Browser which have a singleton method to return browser object any where its called. I have kept the Page Factory initialize as first statement in side the TestNG Test method it self and i am assigning it globally ** – Rajaji Jul 15 '19 at 04:50
  • When i debugged,the PageObject has to be initialized first and then the methods to be called, but , it seems that object is not getting initialized and so the element not found exception is thrown – Rajaji Jul 15 '19 at 04:57

2 Answers2

1

In POM using PageFactory framework, one should initialise PageFactory in Constructor of PageClasses. Please find below code snippet which might work in your case.

public class LoginPage extends TestBase {

public LoginPage() {
    PageFactory.initElements(driver, this);//Here driver is initialised in TestBase class and inherited in LoginPage class
}
//your code

@FindBy(id="listAccounts")
WebElement accountDropdown;

public void selectAccount(){
logger.info("Selecting Account");
implicitwait(10);
Select dropdown = new Select(accountDropdown);
logger.info("Drop down is multiple::"+ dropdown.isMultiple());
}
}
Krishna Majgaonkar
  • 1,532
  • 14
  • 25
1

A complement to the Krishna answer:

PageFactory.initElements(driver, this); 

The code above can be moved to the base class and from the LoginPage, you just pass the webdriver on the constructor like this.

public class LoginPage extends Base {

public LoginPage(Webdriver driver) {
    super(driver);
}
...
public class Base {

public Base(Webdriver driver) {
    PageFactory.initElements(driver, this);
}
...
Spencer Melo
  • 410
  • 4
  • 14