public WebDriver getDriver() throws IOException, URISyntaxException {
    switch (driverType) {
      case FIREFOX:
        {
          FirefoxDriver wd = new FirefoxDriver();
          return wd;
        }
      case CHROME:
        {
          DesiredCapabilities desired = DesiredCapabilities.chrome();
          ChromeDriverService chromeService;
          ChromeDriverService.Builder builder =
              new ChromeDriverService.Builder().usingAnyFreePort();
          if (new File(chromeBinPath).exists()) {
            logger.info("using file location [{}]", chromeBinPath);
            System.setProperty("webdriver.chrome.driver", chromeBinPath);
            builder = builder.usingDriverExecutable(new File(chromeBinPath));
          } else if (chromeBinPath != null) {
            logger.info("finding classpath resource at :[{}]", chromeBinPath);
            File chromeDriver = new ClassPathResource(chromeBinPath).getFile();
            System.setProperty("webdriver.chrome.driver", chromeDriver.getAbsolutePath());
            builder = builder.usingDriverExecutable(chromeDriver);
          }

          chromeService = builder.build();
          logger.info("Starting Chrome Driver Server...");
          chromeService.start();
          ChromeDriver wd = new ChromeDriver(chromeService, desired);
          return wd;
        }
    }
    throw new RuntimeException("no driver specified");
  }
  @Test
  public void shouldBeAbleToCallQuitBrowserMultipleTimes() throws Exception {
    ChromeDriver mockChromeDriver = mock(ChromeDriver.class);
    ChromeDriverService mockService = mock(ChromeDriverService.class);
    when(mockService.isRunning()).thenReturn(true);
    config.getServices().put(config.currentThreadName(), mockService);

    config.quitBrowser(mockChromeDriver);
    config.quitBrowser(mockChromeDriver);

    assertThat(config.getServices(), is(Collections.<String, ChromeDriverService>emptyMap()));
    verify(mockService, times(1)).stop();
  }
  @Test
  public void shouldNotStopServiceIfNotRunningWhenQuitBrowserIsInvoked() throws Exception {
    ChromeDriver mockChromeDriver = mock(ChromeDriver.class);
    ChromeDriverService mockService = mock(ChromeDriverService.class);
    when(mockService.isRunning()).thenReturn(false);
    config.getServices().put(config.currentThreadName(), mockService);

    config.quitBrowser(mockChromeDriver);

    verify(mockChromeDriver).quit();
    assertThat(config.getServices(), is(Collections.<String, ChromeDriverService>emptyMap()));
    verify(mockService, times(0)).stop();
  }
 public void closeBrowser() {
   log.info(" BROWSER CLOSING ");
   if (driver != null) {
     driver.quit();
     if (chromeService != null) {
       chromeService.stop();
     }
   }
 }
Example #5
0
  @Before
  public void SetUp() {
    System.out.print("Using " + Browser + SystemHelper.LineSeparator);

    switch (Browser) {
      case Chrome:
        {
          System.setProperty("webdriver.chrome.driver", Settings.ChromeDriverPath);
          ChromeCapabilities =
              ChromeCapabilities == null ? DesiredCapabilities.chrome() : ChromeCapabilities;
          ChromeService =
              ChromeService == null ? ChromeDriverService.createDefaultService() : ChromeService;
          Driver = new ChromeDriver(ChromeService, ChromeCapabilities);
        }
        break;
      case Firefox:
        {
          InitializeFirefoxDriver();
        }
        break;
      case IE:
        {
          System.setProperty("webdriver.ie.driver", Settings.IEDriverPath);
          IECapabilities =
              IECapabilities == null ? DesiredCapabilities.internetExplorer() : IECapabilities;
          IEService =
              IEService == null ? InternetExplorerDriverService.createDefaultService() : IEService;
          Driver = new InternetExplorerDriver(IEService, IECapabilities);
        }
        break;
      default:
        {
          InitializeFirefoxDriver();
        }
        break;
    }

    Driver.manage().window().maximize();
    SmallWait = new WebDriverWait(Driver, 3);
    Wait = new WebDriverWait(Driver, 10);
    LongWait = new WebDriverWait(Driver, 30);
  }