Reputation: 91
I m using a Page Object Model for my automation and I am creating a page object in Test class like below.(Just a sample code)
public class Test1 extends TestBase {
Page1 page1;
Page2 page2;
Page3 page3;
// and continues as number of pages increases
@BeforeTest
public void initialise() {
this.page1 = new Page1(this);
this.page2 = new Page2(this);
this.page3 = new Page3(this);
// and continues as number of pages increases
}
@Test
public void firstTest() {
// my test script starts here
}
}
But as my number of pages increases, number of page object creation in test class increases. I want to know if there is any other optimize way to create page object or whatever I am doing is good ?
Upvotes: 0
Views: 243
Reputation: 29362
Yes there is.
Create a Page class
that will extends from TestBase
, and create specific Pages()
class that will be child class of Page class
.
I have created a basic project for Page object model. See the eclipse project structure below :
TestBase
class :
public class TestBase {
protected static WebDriver driver;
public TestBase() {
System.out.println("Test Base Bot has been activated.");
}
public Page getPageObj() {
return new Page();
}
@BeforeSuite
public void setUpSuite() {
System.setProperty("webdriver.chrome.driver", "C:\\Inc\\Desktop\\Selenium+Python\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
System.out.println("Before suite executed successfully");
}
@BeforeClass
public void setUpTest() {
PageFactory.initElements(driver, this);
driver.get("https://fuelinsights.gasbuddy.com/Charts");
}
@AfterClass
public void tearDownClass() {
//driver.close();
}
}
TestClass StackOverflowProblems.Java
public class StackOverflowProblems extends TestBase {
@Test
public void testSO() throws InterruptedException {
getPageObj().loginPage().loginWithUser();
}
}
Page
class
public class Page extends TestBase {
public Page(){
System.out.println("Page bots are now in action");
}
public LoginPage loginPage() {
return new LoginPage();
}
public HomePage homePage() {
return new HomePage();
}
public DashBoard dashBoardPage() {
return new DashBoard();
}
}
LoginPage.Java :
public class LoginPage extends Page {
@FindBy(xpath = "//div[@class='xxkkk20']")
WebElement ele;
public void loginWithUser() {
System.out.println("Finally Bot has called this method.");
driver.get("Your url here");
System.out.println("Web drivers operations can be performed here");
String html = ele.getAttribute("innerHTML");
System.out.println(html);
}
}
Upvotes: 1