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.
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
上一篇: 在ASP.NET中使用AJAX的后台工作