Java – how to copy / pass webdriver instances, is it dangerous?
I've been working on a team that has developed selenium webdriver infrastructure for several months, and the way we access driver objects from test cases and page objects annoys me
Our test case creates a new webdriver instance and opens the browser This new instance is stored in the test case class
Then, the test case instantiates the page object Following selenium's page object pattern, these page objects take webdriver as a parameter in their constructor (although I noticed in our version that it is not final) Various page object methods use the driver set in the constructor of the page object to perform their operations If the page object method navigates to a new page object, the webdriver passes it Like 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 @R_502_1750@ 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 the webdriver instance seem unique and important, just like the torch that must be passed from the page object to the page object The style of the code makes me feel like I always want to make sure I use the same instance of the driver I used in the last operation
However, when there are multiple page objects on the page, this "torch relay" becomes complex or impossible, and the page object method will not return the page object you plan to use on the next page When you have two page objects on the screen, how do you operate on the same webdriver instance and need to alternate between them without switching to a new page or creating a new page object?
All this confusion makes me believe that the torch relay is actually unnecessary and may not even happen (all these page objects store references to the same webdriver instance?), But later I don't know why this pattern appeared, as described by selenium
So, do I need to worry about "through the torch?" Or can any page object run normally after instantiation with its webdriver, even if other page objects use their own version of the same webdriver during the transition?
Is it easier / better to make webdrivers a single instance that everyone can access, because we won't use multiple webdrivers for each JVM at any given time? Then we don't need to pass webdriver. In the constructor at all Thank you in advance for any input
Solution
It is best to mark webdriver as a single instance of the entire framework I've been using this mode and it works normally Note: be careful when dealing with parallel execution
@For example, take a class, generallibrary, and create a webdriver
Use the generallibrary as a superclass to extend the classes in the page object model