All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.rapidpm.vaadin.addons.webdriver.WebDriverFunctions Maven / Gradle / Ivy

/**
 * Copyright © 2017 Sven Ruppert ([email protected])
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.rapidpm.vaadin.addons.webdriver;

import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;

import static java.util.Optional.ofNullable;
import static org.openqa.selenium.By.id;
import static org.rapidpm.frp.matcher.Case.match;
import static org.rapidpm.frp.matcher.Case.matchCase;
import static org.rapidpm.frp.model.Result.success;

/**
 *
 */
public interface WebDriverFunctions {

  static Function webdriverName() {
    return driver -> match(
        matchCase(() -> success(driver.toString())),
        matchCase(() -> driver instanceof RemoteWebDriver, () -> success(formatRemoteWebDriverName().apply((RemoteWebDriver) driver)))
    )
        .getOrElse(() -> " Mr NoName.... B-) ");
  }


  static Function formatRemoteWebDriverName() {
    return (webDriver) -> webDriver.getCapabilities().getBrowserName()
                          + " "
                          + webDriver.getCapabilities().getVersion()
                          + " / "
                          + webDriver.getCapabilities().getPlatform();
  }

  static BiFunction> elementFor() {
    return (driver, id) -> ofNullable(driver.findElement(id(id)));
  }

  static Consumer takeScreenShot() {
    return (webDriver) -> {
      //take Screenshot
      ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
      try {
        outputStream.write(((TakesScreenshot) webDriver).getScreenshotAs(OutputType.BYTES));
        //write to target/screenshot-[timestamp].jpg
        final FileOutputStream out = new FileOutputStream("target/screenshot-" + LocalDateTime.now() + ".png");
        out.write(outputStream.toByteArray());
        out.flush();
        out.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    };
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy