Src and credits: www.logigear.com |
WebDriver Factory Pattern is very useful in cases when you want to manage Multiple Browsers
Lets create a Java Enun for all the browsers
First Class (DriverType)
package main.java;
public enum DriverType {
CHROME,
FIREFOX,
IE,
SAFARI;
}
Second Class:
Now we will create our own abstract class to control the driver and any test class can get instance from it and use it while testing
Second Class (DriverManager)
package main.java;
import org.openqa.selenium.WebDriver;
public abstract class DriverManager {
protected WebDriver driver;
protected abstract void startService();
protected abstract void stopService();
protected abstract void createDriver();
public void quitDriver() {
if (null != driver) {
driver.quit();
driver = null;
}
}
public WebDriver getDriver() {
if (null == driver) {
startService();
createDriver();
}
return driver;
}
}
Third Class (DriverManagerFactory)
package main.java;
import main.java.DriverManager;
public class DriverManagerFactory {
public static DriverManager getManager(DriverType type) {
DriverManager driverManager = null;
switch (type) {
case CHROME:
driverManager = new ChromeDriverManager();
break;
case FIREFOX:
// driverManager = new FirefoxDriverManager(); break;
default:
//driverManager = new SafariDriverManager(); break;
}
return driverManager;
}
}
Fourth Class (ChromeDriverManager)
package main.java;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.io.File;
public class ChromeDriverManager extends DriverManager {
private ChromeDriverService chService;
@Override
public void startService() {
if (null == chService) {
try {
chService = new ChromeDriverService.Builder()
.usingDriverExecutable(new File("chromedriver.exe"))
.usingAnyFreePort()
.build();
chService.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override
public void stopService() {
if (null != chService && chService.isRunning())
chService.stop();
}
@Override
public void createDriver() {
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
options.addArguments("test-type");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(chService, capabilities);
}
}
Test Class
package test.java;
import main.java.DriverManager;
import main.java.DriverManagerFactory;
import main.java.DriverType;
import org.openqa.selenium.WebDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class FactoryPatternTest {
DriverManager driverManager;//Observe DriverManager is abstract class, so we can't create //object,now every class has to extend DriverManager Class
//example ChromeDriverManager extends DriverManager
WebDriver driver;
@BeforeTest
public void beforeTest() {
driverManager = DriverManagerFactory.getManager(DriverType.CHROME);
}
@BeforeMethod
public void beforeMethod() {
driver = driverManager.getDriver();
}
@AfterMethod
public void afterMethod() {
driverManager.quitDriver();
}
@Test
public void launchGoogleTest() {
driver.get("https://www.google.com");
Assert.assertEquals("Google", driver.getTitle());
}
}
No comments:
Post a Comment