Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does copying/passing instances of a WebDriver work, and is it dangerous?

I've been working on a team that's been developing a Selenium WebDriver infrastructure for a few months now, and something about the way we access the driver object from test cases and page objects is bugging me.

Our test cases create a new WebDriver instance and open the browser. This new instance is stored in the test case class.

Then, the test case instantiates a page object. Following along with Selenium's Page Object Pattern, these page objects take the WebDriver as a parameter in their constructor (though I notice in our version it isn't final). The various page object methods use the driver that was set in the page object's constructor to do what they do. If a page object method navigates to a new page object, then the WebDriver is passed on to it. Just like in Selenium's example:

public class LoginPage {
    private final WebDriver driver;

    public LoginPage(WebDriver driver) {
        this.driver = driver;

        // Check that we're on the right page.
        if (!"Login".equals(driver.getTitle())) {
            // Alternatively, we could navigate to the login page, perhaps logging out first
            throw new IllegalStateException("This is not the login page");
        }
    }

    // Conceptually, the login page offers the user the service of being able to "log into"
    // the application using a user name and password. 
    public HomePage loginAs(String username, String password) {
        // This is the only place in the test code that "knows" how to enter these details
        driver.findElement(By.id("username")).sendKeys(username);
        driver.findElement(By.id("passwd")).sendKeys(password);
        driver.findElement(By.id("login")).submit();

        // Return a new page object representing the destination. Should the login page ever
        // go somewhere else (for example, a legal disclaimer) then changing the method signature
        // for this method will mean that all tests that rely on this behaviour won't compile.
        return new HomePage(driver);
    }
}

This makes it seem as though the WebDriver instance is unique and important, like a torch that must be passed from page object to page object. The style of the code led me to think that I always had to make sure I was using the same instance of the driver that was used in the last operation.

But this "passing of the torch" becomes complicated or impossible with situations where there are multiple page objects on a page, and page object methods don't return the page object that you are planning on working with next. How can you operate on the same WebDriver instance when you've got two page objects on the screen, and you need to alternate between them, without switching to a new page or creating new page objects?

All this confusion leads me to believe that no passing of the torch is actually necessary, or maybe even happening (are all these page objects storing references to the same WebDriver instance?), but then I don't know why that pattern was suggested in the description given by Selenium.

So, do I need to worry about "passing the torch?" Or will any page object operate fine once instantiated with its WebDriver, even if other page objects perform operations with their own versions of the same WebDriver in the interim?

Would it be easier/better to just make the WebDriver a singleton, accessible to all, since we won't be using more than one WebDriver per JVM at any given time? Then we wouldn't need to pass the WebDriver around in constructors at all. Thanks in advance for any input.

like image 803
James Martineau Avatar asked Oct 31 '12 19:10

James Martineau


People also ask

Is WebDriver thread safe?

WebDriver instance has now become thread-safe, and as the output shows it is being used by the multiple threads concurrently thanks to ThreadLocal class.

How does Selenium handle multiple driver instances?

You can invoke multiple browser sessions by just creating multiple driver objects, and managing them. Each session will be separate if you want them to be. Grid is for scaling as there is a limitation on the no of browser instances you can run keeping your machine performance intact and tests stable.

What is a WebDriver how it works?

Selenium WebDriver is a web framework that permits you to execute cross-browser tests. This tool is used for automating web-based application testing to verify that it performs expectedly. Selenium WebDriver allows you to choose a programming language to create test scripts.


1 Answers

It is better to mark the webdriver as a singleton for the entire framework. I had been currently using this pattern and it works fine.
Note: Care should be taken when dealing with parallel execution.

@CodeEnthusiastic For example take a class GeneralLibrary, in that create a WebDriver.

Extend the classes in Page Object Model with GeneralLibrary as super class

like image 150
Harshavardhan Konakanchi Avatar answered Sep 28 '22 20:09

Harshavardhan Konakanchi