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

br.com.behaviortest.api.engine.PageObject Maven / Gradle / Ivy

There is a newer version: 1.0.1
Show newest version
package br.com.behaviortest.api.engine;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.InvalidSelectorException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriver.TargetLocator;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;

import br.com.behaviortest.api.engine.loader.ConfigurationLoader;
import br.com.behaviortest.model.dto.Configuration;
import br.com.behaviortest.model.exception.FailureException;
import br.com.behaviortest.util.DateUtil;
import junit.framework.Assert;

/**
 * @author 		Felipe Rudolfe
 * @since		8 de jan de 2020
 */
public abstract class PageObject {

	private WebDriver driver;
	private static WaitUntil wait;
	private Configuration configuration;

	public PageObject() {
        this.configuration = ConfigurationLoader.getConfiguration();
		if (PageObject.wait == null) {
			PageObject.wait = new WaitUntil();
		}
	}

	public void setDriver(WebDriver driver) {
		this.driver = driver;
	}

	public WebDriver getDriver() {
        return this.driver;
    }

	public String getCurrentUrl() {
		return this.driver.getCurrentUrl();
	}

	public TargetLocator switchTo() {
		return this.driver.switchTo();
	}

	public String getPageSource() {
		return this.driver.getPageSource();
	}

	public String getWindowHandle() {
		return this.driver.getWindowHandle();
	}

	public Set getWindowHandles() {
		return this.driver.getWindowHandles();
	}

	protected void waitTitleIs(String titulo) {
		waiting().until(ExpectedConditions.titleIs(titulo));
	}

	protected void waitTitleContains(String titulo) {
		waiting().until(ExpectedConditions.titleContains(titulo));
	}

	protected WebElement waitPresenceOfElementLocatedByClassName(String className) {
		return waitPresenceOfElementLocated(By.className(className));
	}

	protected void waitPresenceOfElementLocatedByCssSelector(String selector) {
		waitPresenceOfElementLocated(By.cssSelector(selector));
	}

	protected WebElement waitPresenceOfElementLocatedByLinkText(String linkText) {
		return waitPresenceOfElementLocated(By.linkText(linkText));
	}

	protected WebElement waitPresenceOfElementLocatedByPartialLinkText(String linkText) {
		return waitPresenceOfElementLocated(By.partialLinkText(linkText));
	}

	protected WebElement waitPresenceOfElementLocatedByTagName(String name) {
		return waitPresenceOfElementLocated(By.tagName(name));
	}

	protected WebElement waitPresenceOfElementLocatedByXPathExpression(String xpathExpression) {
		return waitPresenceOfElementLocated(By.xpath(xpathExpression));
	}

	protected WebElement waitPresenceOfElementLocatedById(String id) {
		return waitPresenceOfElementLocated(By.id(id));
	}

	protected void waitPresenceOfElementLocatedByName(String name) {
		waitPresenceOfElementLocated(By.name(name));
	}

	protected WebElement waitPresenceOfElementLocated(By localor) {
		WebElement element = null;
		try {
			element = waiting().until(ExpectedConditions.presenceOfElementLocated(localor));
		} catch (InvalidSelectorException e) {
			throw new FailureException(e.getMessage());
		}
		return element;
	}

	protected WebElement waitElementToBeClickableByClassName(String className) {
		WebElement element = null;
		try {
			element = waitElementToBeClickable(By.className(className));
		} catch (InvalidSelectorException e) {
			throw new FailureException(e.getMessage());
		}
		return element;
	}

	protected WebElement waitElementToBeClickableByCssSelector(String selector) {
		WebElement element = null;
		try {
			element = waitElementToBeClickable(By.cssSelector(selector));
		} catch (InvalidSelectorException e) {
			throw new FailureException(e.getMessage());
		}
		return element;
	}

	protected WebElement waitElementToBeClickableById(String id) {
		WebElement element = null;
		try {
			element = waitElementToBeClickable(By.id(id));
		} catch (InvalidSelectorException e) {
			throw new FailureException(e.getMessage());
		}
		return element;
	}

	protected WebElement waitElementToBeClickableByLinkText(String linkText) {
		WebElement element = null;
		try {
			element = waitElementToBeClickable(By.linkText(linkText));
		} catch (InvalidSelectorException e) {
			throw new FailureException(e.getMessage());
		}
		return element;
	}

	protected WebElement waitElementToBeClickableByName(String name) {
		WebElement element = null;
		try {
			element = waitElementToBeClickable(By.name(name));
		} catch (InvalidSelectorException e) {
			throw new FailureException(e.getMessage());
		}
		return element;
	}

	protected WebElement waitElementToBeClickableByPartialLinkText(String linkText) {
		WebElement element = null;
		try {
			element = waitElementToBeClickable(By.partialLinkText(linkText));
		} catch (InvalidSelectorException e) {
			throw new FailureException(e.getMessage());
		}
		return element;
	}

	protected WebElement waitElementToBeClickableByTagName(String name) {
		WebElement element = null;
		try {
			element = waitElementToBeClickable(By.tagName(name));
		} catch (InvalidSelectorException e) {
			throw new FailureException(e.getMessage());
		}
		return element;
	}

	protected WebElement waitElementToBeClickableByXPathExpression(String xpathExpression) {
		WebElement element = null;
		try {
			element = waitElementToBeClickable(By.xpath(xpathExpression));
		} catch (InvalidSelectorException e) {
			throw new FailureException(e.getMessage());
		}
		return element;
	}

	protected WebElement waitElementToBeClickable(By localor) {
		WebElement element = null;
		try {
			element = waiting().until(ExpectedConditions.elementToBeClickable(localor));
		} catch (InvalidSelectorException e) {
			throw new FailureException(e.getMessage());
		}
		return element;
	}

	protected WebElement waitElementToBeClickable(WebElement elemento) {
		WebElement element = null;
		try {
			element = waiting().until(ExpectedConditions.elementToBeClickable(elemento));
		} catch (InvalidSelectorException e) {
			throw new FailureException(e.getMessage());
		}
		return element;
	}

	protected void waitElementToBeSelectedByClassName(String className) {
		waitElementToBeSelected(By.className(className));
	}

	protected void waitElementToBeSelectedByCssSelector(String selector) {
		waitElementToBeSelected(By.cssSelector(selector));
	}

	protected void waitElementToBeSelectedById(String id) {
		waitElementToBeSelected(By.id(id));
	}

	protected void waitElementToBeSelectedByLinkText(String linkText) {
		waitElementToBeSelected(By.linkText(linkText));
	}

	protected void waitElementToBeSelectedByName(String name) {
		waitElementToBeSelected(By.name(name));
	}

	protected void waitElementToBeSelectedByPartialLinkText(String linkText) {
		waitElementToBeSelected(By.partialLinkText(linkText));
	}

	protected void waitElementToBeSelectedByTagName(String name) {
		waitElementToBeSelected(By.tagName(name));
	}

	protected void waitElementToBeSelectedByXPathExpression(String xpathExpression) {
		waitElementToBeSelected(By.xpath(xpathExpression));
	}

	protected void waitElementToBeSelected(By localor) {
		try {
			waiting().until(ExpectedConditions.elementToBeSelected(localor));
		} catch (InvalidSelectorException e) {
			throw new FailureException(e.getMessage());
		}
	}

	protected void waitElementToBeSelected(WebElement elemento) {
		try {
			waiting().until(ExpectedConditions.elementToBeSelected(elemento));
		} catch (InvalidSelectorException e) {
			throw new FailureException(e.getMessage());
		}
	}

	protected void waitElementSelectionStateToBeByClassName(String className, boolean selecionado) {
		waitElementSelectionStateToBe(By.className(className), selecionado);
	}

	protected void waitElementSelectionStateToBeByCssSelector(String selector, boolean selecionado) {
		waitElementSelectionStateToBe(By.cssSelector(selector), selecionado);
	}

	protected void waitElementSelectionStateToBeById(String id, boolean selecionado) {
		waitElementSelectionStateToBe(By.id(id), selecionado);
	}

	protected void waitElementSelectionStateToBeByLinkText(String linkText, boolean selecionado) {
		waitElementSelectionStateToBe(By.linkText(linkText), selecionado);
	}

	protected void waitElementSelectionStateToBeByName(String name, boolean selecionado) {
		waitElementSelectionStateToBe(By.name(name), selecionado);
	}

	protected void waitElementSelectionStateToBeByPartialLinkText(String linkText, boolean selecionado) {
		waitElementSelectionStateToBe(By.partialLinkText(linkText), selecionado);
	}

	protected void waitElementSelectionStateToBeByTagName(String name, boolean selecionado) {
		waitElementSelectionStateToBe(By.tagName(name), selecionado);
	}

	protected void waitElementSelectionStateToBeByXPathExpression(String xpathExpression, boolean selecionado) {
		waitElementSelectionStateToBe(By.xpath(xpathExpression), selecionado);
	}

	protected void waitElementSelectionStateToBe(By by, boolean selecionado) {
		try {
			waiting().until(ExpectedConditions.elementSelectionStateToBe(by, selecionado));
		} catch (InvalidSelectorException e) {
			throw new FailureException(e.getMessage());
		}
	}

	protected void waitElementSelectionStateToBe(WebElement elemento, boolean selecionado) {
		try {
			waiting().until(ExpectedConditions.elementSelectionStateToBe(elemento, selecionado));
		} catch (InvalidSelectorException e) {
			throw new FailureException(e.getMessage());
		}
	}

	protected void waitInvisibilityOfElementLocatedByClassName(String className) {
		waitInvisibilityOfElementLocated(By.className(className));
	}

	protected void waitInvisibilityOfElementLocatedByCssSelector(String selector) {
		waitInvisibilityOfElementLocated(By.cssSelector(selector));
	}

	protected void waitInvisibilityOfElementLocatedById(String id) {
		waitInvisibilityOfElementLocated(By.id(id));
	}

	protected void waitInvisibilityOfElementLocatedByLinkText(String linkText) {
		waitInvisibilityOfElementLocated(By.linkText(linkText));
	}

	protected void waitInvisibilityOfElementLocatedByName(String name) {
		waitInvisibilityOfElementLocated(By.name(name));
	}

	protected void waitInvisibilityOfElementLocatedByPartialLinkText(String linkText) {
		waitInvisibilityOfElementLocated(By.partialLinkText(linkText));
	}

	protected void waitInvisibilityOfElementLocatedByTagName(String name) {
		waitInvisibilityOfElementLocated(By.tagName(name));
	}

	protected void waitInvisibilityOfElementLocatedByXPathExpression(String xpathExpression) {
		waitInvisibilityOfElementLocated(By.xpath(xpathExpression));
	}

	protected void waitInvisibilityOfElementLocated(By localor) {
		try {
			waiting().until(ExpectedConditions.invisibilityOfElementLocated(localor));
		} catch (InvalidSelectorException e) {
			throw new FailureException(e.getMessage());
		}
	}

	protected void waitInvisibilityOfElementWithTextByClassName(String className, String texto) {
		waitInvisibilityOfElementWithText(By.className(className), texto);
	}

	protected void waitInvisibilityOfElementWithTextByCssSelector(String selector, String texto) {
		waitInvisibilityOfElementWithText(By.cssSelector(selector), texto);
	}

	protected void waitInvisibilityOfElementWithTextById(String id, String texto) {
		waitInvisibilityOfElementWithText(By.id(id), texto);
	}

	protected void waitInvisibilityOfElementWithTextByLinkText(String linkText, String texto) {
		waitInvisibilityOfElementWithText(By.linkText(linkText), texto);
	}

	protected void waitInvisibilityOfElementWithTextByName(String name, String texto) {
		waitInvisibilityOfElementWithText(By.name(name), texto);
	}

	protected void waitInvisibilityOfElementWithTextByPartialLinkText(String linkText, String texto) {
		waitInvisibilityOfElementWithText(By.partialLinkText(linkText), texto);
	}

	protected void waitInvisibilityOfElementWithTextByTagName(String name, String texto) {
		waitInvisibilityOfElementWithText(By.tagName(name), texto);
	}

	protected void waitInvisibilityOfElementWithTextByXPathExpression(String xpathExpression, String texto) {
		waitInvisibilityOfElementWithText(By.xpath(xpathExpression), texto);
	}

	protected void waitInvisibilityOfElementWithText(By localor, String texto) {
		try {
			waiting().until(ExpectedConditions.invisibilityOfElementWithText(localor, texto));
		} catch (InvalidSelectorException e) {
			throw new FailureException(e.getMessage());
		}
	}

	protected void waitTextToBePresentInElementLocatedByClassName(String className, String texto) {
		waitTextToBePresentInElementLocated(By.className(className), texto);
	}

	protected void waitTextToBePresentInElementLocatedByCssSelector(String selector, String texto) {
		waitTextToBePresentInElementLocated(By.cssSelector(selector), texto);
	}

	protected void waitTextToBePresentInElementLocatedById(String id, String texto) {
		waitTextToBePresentInElementLocated(By.id(id), texto);
	}

	protected void waitTextToBePresentInElementLocatedByLinkText(String linkText, String texto) {
		waitTextToBePresentInElementLocated(By.linkText(linkText), texto);
	}

	protected void waitTextToBePresentInElementLocatedByName(String name, String texto) {
		waitTextToBePresentInElementLocated(By.name(name), texto);
	}

	protected void waitTextToBePresentInElementLocatedByPartialLinkText(String linkText, String texto) {
		waitTextToBePresentInElementLocated(By.partialLinkText(linkText), texto);
	}

	protected void waitTextToBePresentInElementLocatedByTagName(String name, String texto) {
		waitTextToBePresentInElementLocated(By.tagName(name), texto);
	}

	protected void waitTextToBePresentInElementLocatedByXPathExpression(String xpathExpression, String texto) {
		waitTextToBePresentInElementLocated(By.xpath(xpathExpression), texto);
	}

	protected void waitTextToBePresentInElementLocated(By localor, String texto) {
		try {
			waiting().until(ExpectedConditions.textToBePresentInElementLocated(localor, texto));
		} catch (InvalidSelectorException e) {
			throw new FailureException(e.getMessage());
		}
	}

	protected void waitTextToBePresentInElement(WebElement elemento, String texto) {
		try {
			waiting().until(ExpectedConditions.textToBePresentInElement(elemento, texto));
		} catch (InvalidSelectorException e) {
			throw new FailureException(e.getMessage());
		}
	}

	protected void waitTextToBePresentInElementValueByClassName(String className, String texto) {
		waitTextToBePresentInElementValue(By.className(className), texto);
	}

	protected void waitTextToBePresentInElementValueByCssSelector(String selector, String texto) {
		waitTextToBePresentInElementValue(By.cssSelector(selector), texto);
	}

	protected void waitTextToBePresentInElementValueById(String id, String texto) {
		waitTextToBePresentInElementValue(By.id(id), texto);
	}

	protected void waitTextToBePresentInElementValueByLinkText(String linkText, String texto) {
		waitTextToBePresentInElementValue(By.linkText(linkText), texto);
	}

	protected void waitTextToBePresentInElementValueByName(String name, String texto) {
		waitTextToBePresentInElementValue(By.name(name), texto);
	}

	protected void waitTextToBePresentInElementValueByPartialLinkText(String linkText, String texto) {
		waitTextToBePresentInElementValue(By.partialLinkText(linkText), texto);
	}

	protected void waitTextToBePresentInElementValueByTagName(String name, String texto) {
		waitTextToBePresentInElementValue(By.tagName(name), texto);
	}

	protected void waitTextToBePresentInElementValueByXPathExpression(String xpathExpression, String texto) {
		waitTextToBePresentInElementValue(By.xpath(xpathExpression), texto);
	}

	protected void waitTextToBePresentInElementValue(By localor, String texto) {
		try {
			waiting().until(ExpectedConditions.textToBePresentInElementValue(localor, texto));
		} catch (InvalidSelectorException e) {
			throw new FailureException(e.getMessage());
		}
	}

	protected void waitTextToBePresentInElementValue(WebElement elemento, String texto) {
		try {
			waiting().until(ExpectedConditions.textToBePresentInElementValue(elemento, texto));
		} catch (InvalidSelectorException e) {
			throw new FailureException(e.getMessage());
		}
	}

	protected void waitVisibilityOfElementLocatedByClassName(String className) {
		waitVisibilityOfElementLocated(By.className(className));
	}

	protected void waitVisibilityOfElementLocatedByCssSelector(String selector) {
		waitVisibilityOfElementLocated(By.cssSelector(selector));
	}

	protected void waitVisibilityOfElementLocatedById(String id) {
		waitVisibilityOfElementLocated(By.id(id));
	}

	protected void waitVisibilityOfElementLocatedByLinkText(String linkText) {
		waitVisibilityOfElementLocated(By.linkText(linkText));
	}

	protected void waitVisibilityOfElementLocatedByName(String name) {
		waitVisibilityOfElementLocated(By.name(name));
	}

	protected void waitVisibilityOfElementLocatedByPartialLinkText(String linkText) {
		waitVisibilityOfElementLocated(By.partialLinkText(linkText));
	}

	protected void waitVisibilityOfElementLocatedByTagName(String name) {
		waitVisibilityOfElementLocated(By.tagName(name));
	}

	protected void waitVisibilityOfElementLocatedByXPathExpression(String xpathExpression) {
		waitVisibilityOfElementLocated(By.xpath(xpathExpression));
	}

	protected void waitVisibilityOfElementLocated(By localor) {
		try {
			waiting().until(ExpectedConditions.visibilityOfElementLocated(localor));
		} catch (Exception e) {
			throw new FailureException(e.getMessage());
		}
	}

	protected void waitVisibilityOf(WebElement elemento) {
		waiting().until(ExpectedConditions.visibilityOf(elemento));
	}

	protected String getTitle() {
		String retorno = null;
		try {
			retorno = driver.getTitle();
		} catch (Exception e) {
			throw new FailureException(e.getMessage());
		}

		return retorno;
	}

	protected void get(String url) {
		try {
			driver.get(url);
		} catch (Exception e) {
			throw new FailureException(e.getMessage());
		}
	}

	protected void clickByClassName(String className) {
		click(By.className(className));
	}

	protected void clickByCssSelector(String selector) {
		click(By.cssSelector(selector));
	}

	protected void clickById(String id) {
		click(By.id(id));
	}

	protected void clickByLinkText(String linkText) {
		click(By.linkText(linkText));
	}

	protected void clickByName(String name) {
		click(By.name(name));
	}

	protected void clickByPartialLinkText(String linkText) {
		click(By.partialLinkText(linkText));
	}

	protected void clickByTagName(String name) {
		click(By.tagName(name));
	}

	protected void clickByXPathExpression(String xpathExpression) {
		click(By.xpath(xpathExpression));
	}

	protected void click(By localor) {
		try {
			waitUntil().delayClickTimeout();
			driver.findElement(localor).click();
		} catch (Exception e) {
			throw new FailureException(e.getMessage());
		}
	}

	protected void click(WebElement element) {
		try {
			waitUntil().delayClickTimeout();
			element.click();
		} catch (Exception e) {
			throw new FailureException(e.getMessage());
		}
	}

	protected void submitByClassName(String className) {
		submit(By.className(className));
	}

	protected void submitByCssSelector(String selector) {
		submit(By.cssSelector(selector));
	}

	protected void submitById(String id) {
		submit(By.id(id));
	}

	protected void submitByLinkText(String linkText) {
		submit(By.linkText(linkText));
	}

	protected void submitByName(String name) {
		submit(By.name(name));
	}

	protected void submitByPartialLinkText(String linkText) {
		submit(By.partialLinkText(linkText));
	}

	protected void submitByTagName(String name) {
		submit(By.tagName(name));
	}

	protected void submitByXPathExpression(String xpathExpression) {
		submit(By.xpath(xpathExpression));
	}

	protected void submit(By localor) {
		try {
			waitUntil().delayClickTimeout();
			driver.findElement(localor).submit();
		} catch (Exception e) {
			throw new FailureException(e.getMessage());
		}
	}

	protected void submit(WebElement element) {
		try {
			waitUntil().delayClickTimeout();
			element.submit();
		} catch (Exception e) {
			throw new FailureException(e.getMessage());
		}
	}

	protected void clearByClassName(String className) {
		clear(By.className(className));
	}

	protected void clearByCssSelector(String selector) {
		clear(By.cssSelector(selector));
	}

	protected void clearByLinkText(String linkText) {
		clear(By.linkText(linkText));
	}

	protected void clearByName(String name) {
		clear(By.name(name));
	}

	protected void clearByPartialLinkText(String linkText) {
		clear(By.partialLinkText(linkText));
	}

	protected void clearByTagName(String name) {
		clear(By.tagName(name));
	}

	protected void clearByXPathExpression(String xpathExpression) {
		clear(By.xpath(xpathExpression));
	}

	protected void clearById(String id) {
		clear(By.id(id));
	}

	protected void clear(By localor) {
		try {
			waitUntil().delayClickTimeout();
			driver.findElement(localor).clear();
		} catch (Exception e) {
			throw new FailureException(e.getMessage());
		}
	}

	protected void clear(WebElement element) {
		waitUntil().delayClickTimeout();
		element.clear();
	}

	protected void sendKeysByClassName(String className, String input) {
		sendKeys(By.className(className), input);
	}

	protected void sendKeysByCssSelector(String selector, String input) {
		sendKeys(By.cssSelector(selector), input);
	}

	protected void sendKeysById(String id, String input) {
		sendKeys(By.id(id), input);
	}

	protected void sendKeysByLinkText(String linkText, String input) {
		sendKeys(By.linkText(linkText), input);
	}

	protected void sendKeysByName(String name, String input) {
		sendKeys(By.name(name), input);
	}

	protected void sendKeysByPartialLinkText(String linkText, String input) {
		sendKeys(By.partialLinkText(linkText), input);
	}

	protected void sendKeysByTagName(String name, String input) {
		sendKeys(By.tagName(name), input);
	}

	protected void sendKeysByXPathExpression(String xpathExpression, String input) {
		sendKeys(By.xpath(xpathExpression), input);
	}

	protected void sendKeys(By localor, String input) {
		try {
			waitUntil().delayWritingTimeout();
			driver.findElement(localor).sendKeys(input);
		} catch (Exception e) {
			throw new FailureException(e.getMessage());
		}
	}

	protected void sendKeys(WebElement element, String input) {
		try {
			waitUntil().delayWritingTimeout();
			element.sendKeys(input);
		} catch (Exception e) {
			throw new FailureException(e.getMessage());
		}
	}

	protected void clearAndSendKeysByClassName(String className, String input) {
		clearByClassName(className);
		sendKeysByClassName(className, input);
	}

	protected void clearAndSendKeysByCssSelector(String selector, String input) {
		clearByCssSelector(selector);
		sendKeysByCssSelector(selector, input);
	}

	protected void clearAndSendKeysById(String id, String input) {
		clearById(id);
		sendKeysById(id, input);
	}

	protected void clearAndSendKeysByLinkText(String linkText, String input) {
		clearByLinkText(linkText);
		sendKeysByLinkText(linkText, input);
	}

	protected void clearAndSendKeysByName(String name, String input) {
		clearByName(name);
		sendKeysByName(name, input);
	}

	protected void clearAndSendKeysByPartialLinkText(String linkText, String input) {
		clearByPartialLinkText(linkText);
		sendKeysByPartialLinkText(linkText, input);
	}

	protected void clearAndSendKeysByTagName(String name, String input) {
		clearByTagName(name);
		sendKeysByTagName(name, input);
	}

	protected void clearAndSendKeysByXPathExpression(String xpathExpression, String input) {
		clearByXPathExpression(xpathExpression);
		sendKeysByXPathExpression(xpathExpression, input);
	}

	protected void clearAndSendKeys(By localor, String input) {
		clear(localor);
		sendKeys(localor, input);
	}

	protected void clearAndSendKeys(WebElement element, String input) {
		clear(element);
		sendKeys(element, input);
	}

	protected boolean exists(By localor) {
		try {
			waitUntil().delayCheckTimeout();
			driver.findElement(localor);
			return true;
		} catch (NoSuchElementException e) {
			return false;
		}
	}

	protected boolean compare(Object obj1, Object obj2) {
		waitUntil().delayCheckTimeout();
		return obj1.equals(obj2);
	}

	protected void validateMessageByClassName(String className, String mensagem) {
		validateMessage(By.className(className), mensagem);
	}

	protected void validateMessageByCssSelector(String selector, String mensagem) {
		validateMessage(By.cssSelector(selector), mensagem);
	}

	protected void validateMessageById(String id, String mensagem) {
		validateMessage(By.id(id), mensagem);
	}

	protected void validateMessageByLinkText(String linkText, String mensagem) {
		validateMessage(By.linkText(linkText), mensagem);
	}

	protected void validateMessageByName(String name, String mensagem) {
		validateMessage(By.name(name), mensagem);
	}

	protected void validateMessageByPartialLinkText(String linkText, String mensagem) {
		validateMessage(By.partialLinkText(linkText), mensagem);
	}

	protected void validateMessageByTagName(String name, String mensagem) {
		validateMessage(By.tagName(name), mensagem);
	}

	protected void validateMessageByXPathExpression(String xpathExpression, String mensagem) {
		validateMessage(By.xpath(xpathExpression), mensagem);
	}

	protected void validateMessage(By localor, String mensagem) {
		waitUntil().delayCheckTimeout();
		Assert.assertEquals(driver.findElement(localor).getText(), mensagem);
	}

	protected void validateMessage(WebElement element, String mensagem) {
		waitUntil().delayCheckTimeout();
		Assert.assertEquals(element.getText(), mensagem);
	}

	protected void validateMessage(String atual, String esperada) {
		waitUntil().delayCheckTimeout();
		Assert.assertEquals(atual, esperada);
	}

	protected String getStringValueByClassName(String className) {
		return getStringValue(By.className(className));
	}

	protected String getStringValueByCssSelector(String selector) {
		return getStringValue(By.cssSelector(selector));
	}

	protected String getStringValueById(String id) {
		return getStringValue(By.id(id));
	}

	protected String getStringValueByLinkText(String linkText) {
		return getStringValue(By.linkText(linkText));
	}

	protected String getStringValueByName(String name) {
		return getStringValue(By.name(name));
	}

	protected String getStringValueByPartialLinkText(String linkText) {
		return getStringValue(By.partialLinkText(linkText));
	}

	protected String getStringValueByTagName(String name) {
		return getStringValue(By.tagName(name));
	}

	protected String getStringValueByXPathExpression(String xpathExpression) {
		return getStringValue(By.xpath(xpathExpression));
	}

	protected String getStringValue(By localor) {
		return driver.findElement(localor).getText();
	}

	protected String getStringValue(WebElement element) {
		return element.getText();
	}

	protected Integer getIntegerValue(WebElement element) {
		return Integer.parseInt(element.getText());
	}

	protected Integer getIntegerValueByClassName(String className) {
		return getIntegerValue(By.className(className));
	}

	protected Integer getIntegerValueByCssSelector(String selector) {
		return getIntegerValue(By.cssSelector(selector));
	}

	protected Integer getIntegerValueById(String id) {
		return getIntegerValue(By.id(id));
	}

	protected Integer getIntegerValueByLinkText(String linkText) {
		return getIntegerValue(By.linkText(linkText));
	}

	protected Integer getIntegerValueByName(String name) {
		return getIntegerValue(By.name(name));
	}

	protected Integer getIntegerValueByPartialLinkText(String linkText) {
		return getIntegerValue(By.partialLinkText(linkText));
	}

	protected Integer getIntegerValueByTagName(String name) {
		return getIntegerValue(By.tagName(name));
	}

	protected Integer getIntegerValueByXPathExpression(String xpathExpression) {
		return getIntegerValue(By.xpath(xpathExpression));
	}

	protected Integer getIntegerValue(By localor) {
		return Integer.parseInt(driver.findElement(localor).getText());
	}

	protected Double getDoubleValueByClassName(String className) {
		return getDoubleValue(By.className(className));
	}

	protected Double getDoubleValueByCssSelector(String selector) {
		return getDoubleValue(By.cssSelector(selector));
	}

	protected Double getDoubleValueById(String id) {
		return getDoubleValue(By.id(id));
	}

	protected Double getDoubleValueByLinkText(String linkText) {
		return getDoubleValue(By.linkText(linkText));
	}

	protected Double getDoubleValueByName(String name) {
		return getDoubleValue(By.name(name));
	}

	protected Double getDoubleValueByPartialLinkText(String linkText) {
		return getDoubleValue(By.partialLinkText(linkText));
	}

	protected Double getDoubleValueByTagName(String name) {
		return getDoubleValue(By.tagName(name));
	}

	protected Double getDoubleValueByXPathExpression(String xpathExpression) {
		return getDoubleValue(By.xpath(xpathExpression));
	}

	protected Double getDoubleValue(By localor) {
		return Double.parseDouble(driver.findElement(localor).getText());
	}

	protected Double getDoubleValue(WebElement element) {
		return Double.parseDouble(element.getText());
	}

	protected Float getFloatValueByClassName(String className) {
		return getFloatValue(By.className(className));
	}

	protected Float getFloatValueByCssSelector(String selector) {
		return getFloatValue(By.cssSelector(selector));
	}

	protected Float getFloatValueById(String id) {
		return getFloatValue(By.id(id));
	}

	protected Float getFloatValueByLinkText(String linkText) {
		return getFloatValue(By.linkText(linkText));
	}

	protected Float getFloatValueByName(String name) {
		return getFloatValue(By.name(name));
	}

	protected Float getFloatValueByPartialLinkText(String linkText) {
		return getFloatValue(By.partialLinkText(linkText));
	}

	protected Float getFloatValueByTagName(String name) {
		return getFloatValue(By.tagName(name));
	}

	protected Float getFloatValueByXPathExpression(String xpathExpression) {
		return getFloatValue(By.xpath(xpathExpression));
	}

	protected Float getFloatValue(By localor) {
		return Float.parseFloat(driver.findElement(localor).getText());
	}

	protected Float getFloatValue(WebElement element) {
		return Float.parseFloat(element.getText());
	}

	protected Date getDateValueByClassName(String className, String format) {
		return getDateValue(By.className(className), format);
	}

	protected Date getDateValueByCssSelector(String selector, String format) {
		return getDateValue(By.cssSelector(selector), format);
	}

	protected Date getDateValueById(String id, String format) {
		return getDateValue(By.id(id), format);
	}

	protected Date getDateValueByLinkText(String linkText, String format) {
		return getDateValue(By.linkText(linkText), format);
	}

	protected Date getDateValueByName(String name, String format) {
		return getDateValue(By.tagName(name), format);
	}

	protected Date getDateValueByPartialLinkText(String linkText, String format) {
		return getDateValue(By.partialLinkText(linkText), format);
	}

	protected Date getDateValueByTagName(String name, String format) {
		return getDateValue(By.tagName(name), format);
	}

	protected Date getDateValueByXPathExpression(String xpathExpression, String format) {
		return getDateValue(By.xpath(xpathExpression), format);
	}

	protected Date getDateValue(By localor, String format) {
		Date data = null;
		try {
			data = DateUtil.converterEmDate(getStringValue(localor), format);
		} catch (ParseException e) {
			throw new FailureException(e.getMessage());
		}

		return data;
	}

	protected Date getDateValue(WebElement elemento, String format) {
		Date data = null;
		try {
			data = DateUtil.converterEmDate(elemento.getText(), format);
		} catch (ParseException e) {
			throw new FailureException(e.getMessage());
		}
		return data;
	}

	protected WebElement findByClassName(String className) {
		return find(By.className(className));
	}

	protected WebElement findByCssSelector(String selector) {
		return find(By.cssSelector(selector));
	}

	protected WebElement findById(String id) {
		return find(By.id(id));
	}

	protected WebElement findByLinkText(String linkText) {
		return find(By.linkText(linkText));
	}

	protected WebElement findByName(String name) {
		return find(By.name(name));
	}

	protected WebElement findByPartialLinkText(String linkText) {
		return find(By.partialLinkText(linkText));
	}

	protected WebElement findByTagName(String name) {
		return find(By.tagName(name));
	}

	protected WebElement findByXPathExpression(String xpathExpression) {
		return find(By.xpath(xpathExpression));
	}

	protected WebElement find(By by) {

		WebElement element = null;
		try {

			waitUntil().delayCheckTimeout();
			element = this.driver.findElement(by);

		} catch (NoSuchElementException e) {
			throw new FailureException(e.getMessage());
		}

		return element;
	}

	protected List findsByClassName(String className) {
		return finds(By.className(className));
	}

	protected List findsByCssSelector(String selector) {
		return finds(By.cssSelector(selector));
	}

	protected List findsById(String id) {
		return finds(By.id(id));
	}

	protected List findsByLinkText(String linkText) {
		return finds(By.linkText(linkText));
	}

	protected List findsByName(String name) {
		return finds(By.name(name));
	}

	protected List findsByPartialLinkText(String linkText) {
		return finds(By.partialLinkText(linkText));
	}

	protected List findsByTagName(String name) {
		return finds(By.tagName(name));
	}

	protected List findsByXPathExpression(String xpathExpression) {
		return finds(By.xpath(xpathExpression));
	}

	protected List finds(By by) {

		List elements = null;
		try {

			waitUntil().delayCheckTimeout();
			elements = this.driver.findElements(by);

		} catch (NoSuchElementException e) {
			throw new FailureException(e.getMessage());
		}

		return elements;
	}

	protected class WaitUntil {

		private void delayClickTimeout() {
			try {
				Thread.sleep(getDelayClickTimeout());
			} catch (InterruptedException e) {
				throw new FailureException(e.getMessage());
			}
		}

		private void delayWritingTimeout() {
			try {
				Thread.sleep(getDelayWritingTimeout());
			} catch (InterruptedException e) {
				throw new FailureException(e.getMessage());
			}
		}

		private void delayCheckTimeout() {
			try {
				Thread.sleep(getDelayCheckTimeout());
			} catch (InterruptedException e) {
				throw new FailureException(e.getMessage());
			}
		}
	}

	private WaitUntil waitUntil() {
		return PageObject.wait;
	}

	@SuppressWarnings("deprecation")
	private Wait waiting() {

		Integer timeout = getWaitingtime();

		// TODO rever implementação de wait com new WebDriverWait(driver, 10)

		return new FluentWait(driver).withTimeout(timeout, TimeUnit.MILLISECONDS).pollingEvery(2,
				TimeUnit.MILLISECONDS);
	}

	private Integer getWaitingtime() {
		return this.configuration.getDelay().getWaitingTimeout();
	}

	private Integer getDelayClickTimeout() {
		return this.configuration.getDelay().getClickTimeout();
	}

	private Integer getDelayWritingTimeout() {
		return this.configuration.getDelay().getWritingTimeout();
	}

	private Integer getDelayCheckTimeout() {
		return this.configuration.getDelay().getCheckTimeout();
	}


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy