Selenium中同一测试方法内复用Page类实例与创建新实例的合理性探讨——基于Rediff网站PageFactory测试场景
Great question—this is a common point of confusion when working with the Page Object Model (POM) and PageFactory in Selenium. Let's break this down for your specific Rediff test and the broader principle.
For Your Rediff Test Scenario
In your current code, reusing the existing rhp instance (from Step 2) in Step 4 is not only acceptable but also the cleaner, more efficient approach. Here's why:
- PageFactory uses lazy loading for elements annotated with
@FindByby default. This means elements aren't located on the page until you actually call a method that interacts with them. - When you navigate back to the Rediff homepage in Step 4, the page context matches what the
RediffHomePageclass represents. When you callrhp.enterSearchText().sendKeys("demo"), PageFactory will re-locate the search bar element on the current page (not use a stale reference from earlier).
Creating a new RediffHomePage instance like rhp1 works too, but it's unnecessary—it adds redundant code and extra object creation overhead without any benefit in this scenario.
Optimized Code Snippet
Your original approach is fine, but you can make it even cleaner by moving PageFactory initialization into a base page class, so you don't have to call PageFactory.initElements() every time:
// BasePage.java public class BasePage { public BasePage(WebDriver driver) { PageFactory.initElements(driver, this); } } // RediffHomePage.java public class RediffHomePage extends BasePage { @FindBy(linkText = "Sign in") private WebElement signInLink; @FindBy(id = "srchword") private WebElement searchBox; @FindBy(className = "newsrchbtn") private WebElement searchButton; public RediffHomePage(WebDriver driver) { super(driver); } public WebElement signIn() { return signInLink; } public WebElement enterSearchText() { return searchBox; } public WebElement submitSearch() { return searchButton; } } // Then in your test class: @Test public void loginAndSearch() throws InterruptedException { // Step-1 getDriver().navigate().to("https://www.rediff.com"); // Step-2 RediffHomePage rhp = new RediffHomePage(getDriver()); rhp.signIn().click(); // Step-3 RediffLoginPage rlp = new RediffLoginPage(getDriver()); rlp.enterEmailID().sendKeys("hello"); rlp.enterPassword().sendKeys("demopass"); rlp.submitLogin().submit(); // Step-4 (reuse existing rhp instance) rlp.goToHomePage().click(); rhp.enterSearchText().sendKeys("demo"); rhp.submitSearch().click(); }
General Principle: Reusing Page Instances in Selenium
In a single test method, reusing the same Page class instance is absolutely appropriate whenever you return to the same page context. Here are the key guidelines:
- Do reuse instances when navigating back to a page that matches the Page class's structure (like your Rediff homepage return). Lazy loading ensures elements are re-located as needed.
- Create a new instance only if:
- The page's structure has changed drastically (e.g., the same URL but a completely different UI after an action).
- You need a different configuration or state for the page (rare in most test scenarios).
- You're using
@CacheLookupon elements (this caches the element reference, which can lead to stale element exceptions if the page refreshes—avoid@CacheLookupunless you're sure the element won't change).
Reusing instances keeps your code concise, reduces unnecessary object creation, and maintains consistency in your test flow.
内容的提问来源于stack exchange,提问作者Sandesh Sawant

