|
1 | 1 | package com.semet;
|
2 | 2 |
|
| 3 | +import com.solvent.SolventLogger; |
| 4 | +import com.solvent.util.Configurator; |
| 5 | +import org.apache.log4j.Logger; |
| 6 | +import org.openqa.selenium.WebDriver; |
| 7 | +import org.openqa.selenium.firefox.FirefoxDriver; |
| 8 | +import org.openqa.selenium.ie.InternetExplorerDriver; |
| 9 | +import org.openqa.selenium.remote.BrowserType; |
| 10 | +import org.openqa.selenium.remote.DesiredCapabilities; |
| 11 | + |
| 12 | +import java.io.File; |
| 13 | + |
3 | 14 | /**
|
4 | 15 | * Created by reed on 2017/10/2.
|
5 | 16 | */
|
6 | 17 | public class WebDrivers {
|
| 18 | + private static final Logger log = SolventLogger.getLogger(WebDrivers.class); |
| 19 | + private static WebDriver driver; |
| 20 | + |
| 21 | + static { |
| 22 | + log.debug("called WebDrivers static"); |
| 23 | + setDriver(); |
| 24 | + } |
| 25 | + |
| 26 | + public static void setDriver() { |
| 27 | + String browserName = Configurator.getBrowserName(); |
| 28 | + DesiredCapabilities capabilities = new DesiredCapabilities(); |
| 29 | + capabilities.setBrowserName(browserName); |
| 30 | + driver = setDriver(capabilities); |
| 31 | + } |
7 | 32 |
|
| 33 | + private static WebDriver setDriver(DesiredCapabilities capabilities) { |
| 34 | + String browserType = capabilities.getBrowserName(); |
| 35 | + if (browserType.equals(BrowserType.FIREFOX)) { |
| 36 | + return setDriverFirefox(capabilities); |
| 37 | + } |
| 38 | + if (browserType.equals(BrowserType.IE)) { |
| 39 | + return setDriverIE(capabilities); |
| 40 | + } |
8 | 41 |
|
| 42 | + log.info("\n Browser type not supported! Using IE now"); |
| 43 | + return setDriverIE(capabilities); |
| 44 | + } |
9 | 45 |
|
| 46 | + public static WebDriver getDriver() { |
| 47 | + return driver; |
| 48 | + } |
| 49 | + |
| 50 | + public static WebDriver setDriverFirefox(DesiredCapabilities capabilities) { |
| 51 | + log.debug("\n set Firefox driver"); |
| 52 | + driver = new FirefoxDriver(capabilities); |
| 53 | + return driver; |
| 54 | + } |
| 55 | + |
| 56 | + public static WebDriver setDriverIE(DesiredCapabilities capabilities) { |
| 57 | + log.debug("\n set IE driver"); |
| 58 | + File file = Configurator.getIEDriver(); |
| 59 | + System.setProperty("webdriver.ie.driver", file.getAbsolutePath()); |
| 60 | + capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true); |
| 61 | + driver = new InternetExplorerDriver(capabilities); |
| 62 | + return driver; |
| 63 | + } |
10 | 64 | }
|
| 65 | + |
0 commit comments