Dual WebDriver testing

The tricky part about running two or more WebDrivers at the same time is that you must keep track of which driver is getting the WebDriver events at any point in time. Otherwise, the current WebDriver, which is the last one that gets instantiated, gets all the events. How do we do that?

It's actually not that difficult. What needs to be done is this:

  1. Create the first WebDriver instance.
  2. Assign the first WebDriver instance to a variable.
  3. Create the second WebDriver instance.
  4. Assign the second WebDriver instance to a variable.
  5. Switch back and forth between the two drivers using the variables.
  6. Instantiate other page object classes against the correct drivers.

Let's take a look at an example of how this is done using a Chrome and Firefox driver at the same time:

@Test
public void tc001_multiWebDriver(String rowID,
String description)
throws Exception {

// create the first WebDriver instance
CreateDriver.getInstance().setDriver("chrome",
Global_VARS.DEF_ENVIRONMENT,
Global_VARS.DEF_PLATFORM);

// save the first WebDriver instance
WebDriver chromeDriver = CreateDriver.getInstance().getDriver();

// create the second WebDriver instance
CreateDriver.getInstance().setDriver("firefox",
Global_VARS.DEF_ENVIRONMENT,
Global_VARS.DEF_PLATFORM);

// save the second WebDriver instance
WebDriver firefoxDriver = CreateDriver.getInstance().getDriver();

// switch back to the chrome driver
CreateDriver.getInstance().setDriver(chromeDriver);

// create a page object class instance that will use this driver
GmailLoginPO gmail = new GmailLoginPO();
gmail.login("user1", "password1");

// switch back to the firefox driver
CreateDriver.getInstance().setDriver(firefoxDriver);
    //  create a page object class instance that will use this driver
GmailLoginPO gmail2 = new GmailLoginPO();
gmail2.login("user2", "password2");

// test sending mail back and forth to each user via the 2 clients

// switch back to chrome and quit driver
CreateDriver.getInstance().setDriver(chromeDriver);
chromeDriver.quit();

// switch back to firefox and quit driver
CreateDriver.getInstance().setDriver(firefoxDriver);
firefoxDriver.quit();
}

So, the actions are actually fairly easy to understand, but let's point out a number of things.

Once you instantiate both drivers, you must call the overloaded setDriver method created in Chapter 9, Building a Scalable Selenium Test Driver Class for Web and Mobile Applications, to switch to the current driver thread of choice. Remember, the driver class has multithreading built in, so every time a new driver is created, it exists on a separate thread.

When you instantiate page object classes, the driver is fetched on the fly by the page object hierarchy, so you do not have to pass in the driver type, it's done automatically for you. But you must create the instance of the page object class after you call setDriver to set the instance of the driver to use.

If you switch to a different driver than the one you instantiated the page object class on, and try to send an event to the page, you will get a runtime error saying that the driver doesn't exist.

Finally, to test out sending Gmail back and forth between the clients, you will need to call setDriver to do the switching and use the correct PO class instance to send and receive the email.

It's the same when quitting the driver, you must switch to the correct one before closing it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.244.201