package br.gov.serpro.expresso.cte.pages.common; import java.util.List; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.NoSuchElementException; import org.openqa.selenium.RenderedWebElement; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; /** * @author L.F.Estivalet (Serpro) * * Created on Jan 4, 2011 at 3:52:33 PM * */ public class Page { /** Implementacao do driver a ser utilizado no teste da pagina. */ protected WebDriver driver; /** * @param driver * Implementacao do driver (Firefox, IE, etc) */ public Page(WebDriver driver) { this.driver = driver; } /** * Clica em um elemento. * * @param id * Id do elemento a ser clicado. */ public void clickElement(String id) { driver.findElement(By.id(id)).click(); } public void clickElement(By by) { driver.findElement(by).click(); } /** * Aguarda elemento aparecer e depois clica. * * @param id * Id do elemento a ser clicado. */ public void findAndClickElement(String id) { this.waitForElementById(id); this.clickElement(id); } /** * Aguarda por um elemento. * * @param id * Id do elemento a ser aguardado. */ public void waitForElementById(String id) { isDisplayed(this.waitFindElement(By.id(id), 5000, 1000)); } /** * @param name */ public void waitForElementByName(String name) { isDisplayed(this.waitFindElement(By.name(name), 5000, 1000)); } /** * @param xpath */ public void waitForElementByXpath(String xpath) { isDisplayed(this.waitFindElement(By.xpath(xpath), 5000, 1000)); } /** * @param ms */ public void waitAMoment(long ms) { driver.manage().timeouts().implicitlyWait(ms, TimeUnit.MILLISECONDS); } /** * @param e */ public boolean isDisplayed(RenderedWebElement e) { System.out.println("starting tentatives"); // Up to 10 times for (int i = 0; i < 10; i++) { System.out.println("tentative " + i); // Check whether our element is visible yet if (e.isDisplayed()) { return true; } try { Thread.sleep(1000); } catch (InterruptedException ex) { // Try again } } return false; } /** * @param id * @return */ public String getElementContent(String id) { return driver.findElement(By.id(id)).getText(); } public String getDisplayedElementContent(String id) { waitForElementById(id); return driver.findElement(By.id(id)).getText(); } public RenderedWebElement waitFindElement(By by, long timeout, long interval) { long start = System.currentTimeMillis(); while (true) { try { return (RenderedWebElement) driver.findElement(by); } catch (NoSuchElementException nse) { if (System.currentTimeMillis() - start >= timeout) { throw new Error("Timeout reached and element[" + by + "]not found"); } else { try { synchronized (this) { wait(interval); } } catch (InterruptedException e) { e.printStackTrace(); } } } } } /** * @param element * @param value */ public void setComboValue(WebElement element, String value) { List options = element.findElements(By.tagName("option")); for (WebElement option : options) { if (option.getText().equals(value)) { option.setSelected(); break; } } } }