@Test
  public void allowsToSpecifyCustomWebDriverConfiguration() {
    WebDriverRunner.closeWebDriver();
    Configuration.browser = "com.codeborne.selenide.WebDriverRunnerTest$CustomWebDriverProvider";

    assertSame(driver, WebDriverRunner.getWebDriver());
  }
  @Test
  public void allowsToSpecifyCustomWebDriverProgrammatically() {
    HtmlUnitDriver myDriver = new HtmlUnitDriver(true);
    WebDriverRunner.setWebDriver(myDriver);

    open(url);
    assertSame(myDriver, WebDriverRunner.getWebDriver());
  }
  public File takeScreenshot(WebElement element) {
    if (!WebDriverRunner.hasWebDriverStarted()) {
      log.warning("Cannot take screenshot because browser is not started");
      return null;
    }

    WebDriver webdriver = getWebDriver();
    if (!(webdriver instanceof TakesScreenshot)) {
      log.warning("Cannot take screenshot because browser does not support screenshots");
      return null;
    }

    File screen = ((TakesScreenshot) webdriver).getScreenshotAs(OutputType.FILE);

    Point p = element.getLocation();
    Dimension elementSize = element.getSize();

    try {
      BufferedImage img = ImageIO.read(screen);
      BufferedImage dest =
          img.getSubimage(p.getX(), p.getY(), elementSize.getWidth(), elementSize.getHeight());
      ImageIO.write(dest, "png", screen);
      File screenshotOfElement = new File(generateScreenshotFileName());
      FileUtils.copyFile(screen, screenshotOfElement);
      return screenshotOfElement;
    } catch (IOException e) {
      printOnce("takeScreenshotImage", e);
      return null;
    }
  }
 @Test
 public void userCanAddWebDriverListeners() {
   WebDriverEventListener listener1 = mock(WebDriverEventListener.class);
   WebDriverRunner.addListener(listener1);
   Configuration.browser = HTMLUNIT;
   open(url);
   verify(listener1).beforeNavigateTo(eq(url.toString()), any(WebDriver.class));
 }
  /**
   * Takes screenshot of current browser window. Stores 2 files: html of page, and (if possible)
   * image in PNG format.
   *
   * @param fileName name of file (without extension) to store screenshot to.
   * @return the name of last saved file, it's either my_screenshot.png or my_screenshot.html (if
   *     failed to create png)
   */
  public String takeScreenShot(String fileName) {
    if (!WebDriverRunner.hasWebDriverStarted()) {
      log.warning("Cannot take screenshot because browser is not started");
      return null;
    }

    WebDriver webdriver = getWebDriver();
    File pageSource = savePageSourceToFile(fileName, webdriver);
    File imageFile = savePageImageToFile(fileName, webdriver);

    String screenshot = firstNonNull(imageFile, pageSource).getAbsolutePath();
    return addToHistory(screenshot);
  }
  public File getScreenShotAsFile() {
    if (!WebDriverRunner.hasWebDriverStarted()) {
      log.warning("Cannot take screenshot because browser is not started");
      return null;
    }

    WebDriver webdriver = getWebDriver();
    // File pageSource = savePageSourceToFile(fileName, webdriver); - temporary not available
    File scrFile = getPageImage(webdriver);

    String screenshot = scrFile.getAbsolutePath();
    addToHistory(screenshot);

    return scrFile;
  }
 @After
 public void resetSettings() {
   WebDriverRunner.closeWebDriver();
   Configuration.browser = System.getProperty("browser", FIREFOX);
   WebDriverRunner.webdriverContainer = new WebDriverThreadLocalContainer();
 }
 @After
 public void tearDown() {
   WebDriverRunner.closeWebDriver();
 }
 @Before
 public void setUp() {
   WebDriverRunner.closeWebDriver();
   WebDriverRunner.setWebDriver(webdriver);
 }