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

com.g2forge.gearbox.browser.selenium.SeleniumBrowser Maven / Gradle / Ivy

There is a newer version: 0.0.9
Show newest version
package com.g2forge.gearbox.browser.selenium;

import java.util.List;
import java.util.stream.Collectors;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.g2forge.alexandria.java.function.IConsumer1;
import com.g2forge.alexandria.java.function.IFunction1;
import com.g2forge.gearbox.browser.IBrowser;
import com.g2forge.gearbox.browser.IElement;
import com.g2forge.gearbox.browser.IForm;
import com.g2forge.gearbox.browser.operation.IOperationBuilder;

public class SeleniumBrowser implements IBrowser {
	protected final WebDriver driver = new FirefoxDriver();

	protected boolean open = true;

	protected void assertOpen() {
		if (!open) throw new IllegalStateException();
	}

	public void close() {
		assertOpen();
		open = false;
		driver.close();
	}

	protected  T convert(final WebElement element, Class type) {
		final Object retVal;
		if (IElement.class.equals(type)) retVal = new Element(element, this);
		else if (IForm.class.equals(type)) retVal = new Form(element, this);
		else throw new IllegalArgumentException(type.toString());

		@SuppressWarnings("unchecked")
		final T cast = (T) retVal;
		return cast;
	}

	@Override
	public Object executeJavascript(String script, Object... args) {
		return ((JavascriptExecutor) driver).executeScript(script, args);
	}

	public IElement find(By by) {
		return find(by, IElement.class);
	}

	@Override
	public  T find(By by, Class type) {
		return convert(driver.findElement(by), type);
	}

	@Override
	public List findAll(By by) {
		return findAll(by, IElement.class);
	}

	@Override
	public  List findAll(By by, Class type) {
		final List elements = driver.findElements(by);
		return elements.stream().map(e -> convert(e, type)).collect(Collectors.toList());
	}

	@Override
	public IBrowser getBrowser() {
		return this;
	}

	@Override
	public String getTitle() {
		return driver.getTitle();
	}

	@Override
	public IBrowser go(String url) {
		driver.get(url);
		return this;
	}

	@Override
	public IOperationBuilder operation() {
		return new IOperationBuilder() {
			@Override
			public IBrowser execute(IConsumer1 operation) {
				operation.accept(SeleniumBrowser.this);
				return SeleniumBrowser.this;
			}

			@Override
			public  V until(IFunction1 function) {
				return new WebDriverWait(driver, 30).until(d -> function.apply(SeleniumBrowser.this));
			}
		};
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy