2

I'm struggling with how to apply inheritance and composition with Java for scraping sample data.

App driver:

package dur.bounceme.net.SeleniumBase;

import java.io.IOException;
import java.util.Properties;
import java.util.logging.Logger;
import org.openqa.selenium.WebDriver;

public class App {

    private static final Logger LOG = Logger.getLogger(App.class.getName());

    public static void main(String[] args) throws IOException {
        new App().initSelenium();
    }

    private void initSelenium() throws IOException {
        LOG.fine("starting selenium initialization..");

        Properties properties = new Properties();
        properties.loadFromXML(App.class.getResourceAsStream("/selenium.xml"));
        LOG.fine(properties.toString());
        BookScraper bookScraper = new BookScraper(properties);
        WebDriver webDriver = bookScraper.getWebDriver();
        WelcomePage welcomePage = new WelcomePage(webDriver);
        welcomePage.populateCatalogue();
        bookScraper.close();
    }
}

within WelcomePage I'm looking to utilize OOP principles in relation to the Page Object Model which Selenium utilizes.

My focus is populating the catalogue. As the catologue is a division within the page it seems there should be a corresponding class. What would this class extend and implement?

Is it not customary to instantiate a single WebDriver instance which is then passed around? I would think that Selenium Grid would be employed for multiple instances, and wouldn't expect a single user to concurrently be on different pages. (A user, myself, might switch between open tabs or pages, but not, literally, concurrently. Like most humans I have only the one set of eyes and one set of fingers (for Braille).)

Or, perhaps each page and page component gets its own, new, WebDriver!?

Admittedly, this PageFactory looks intriguing, and I see that Page Fragment is well established:

image

2

The fragment idea is what you should use. As the Selenium Wiki page says, a page object should provide the services to interact and retrieve information.

Since you don't want to create dependencies between the catalogue fragment and the pages, you should pass the fragment location as a dependency: a WebElement in its construtor - baseElement.

Inside it, you can do findElement on this baseElement, rather than on a WebDriver. This way, the pages pass their knowledge of the fragment location to the object which encapsulates the knowledge of the fragment behavior.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.