Nội
dung bài học
Khởi chạy Browser tùy chỉnh (multi browser) không cần chạy bên file XML
BaseSetup.class
public WebDriver setupDriver(String browserType) {
switch (browserType.trim().toLowerCase()) {
case "chrome":
driver = initChromeDriver();
break;
case "firefox":
driver = initFirefoxDriver();
break;
case "opera":
driver = initOperaDriver();
break;
case "edge":
driver = initEdgeDriver();
break;
default:
System.out.println("Browser: " + browserType + " is invalid, Launching Chrome as browser of choice...");
driver = initChromeDriver();
}
return driver;
}
private WebDriver initChromeDriver() {
System.out.println("Launching Chrome browser...");
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
return driver;
}
private WebDriver initEdgeDriver() {
System.out.println("Launching Edge browser...");
WebDriverManager.edgedriver().setup();
driver = new EdgeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
return driver;
}
private WebDriver initFirefoxDriver() {
System.out.println("Launching Firefox browser...");
WebDriverManager.firefoxdriver().setup();
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
return driver;
}
private WebDriver initOperaDriver() {
System.out.println("Launching Opera browser...");
WebDriverManager.operadriver().setup();
driver = new OperaDriver();
driver.manage().window().maximize();
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
return driver;
}
Gọi lại
driver = new BaseSetup().setupDriver("opera");
Anh Tester
facebook.com/anhtester
Đường dẫu khó chân vẫn cần bước đi
Đời dẫu khổ tâm vẫn cần nghĩ thấu