package org.expressolivre.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; } /** * Procura e clica em um elemento de tela. * * @param by */ 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.waitForElement(By.id(id)); this.clickElement(By.id(id)); } /** * Aguarda ate o elemento aparecer na tela. * * @param by * Element a ser pesquisado, podendo ser por id, xpath, name. */ public void waitForElement(By by) { isDisplayed(this.waitFindElement(by, 5000, 1000)); } /** * @param ms */ public void waitAMoment(long ms) { driver.manage().timeouts().implicitlyWait(ms, TimeUnit.MILLISECONDS); } /** * @param e */ public boolean isDisplayed(RenderedWebElement e) { // Up to 10 times for (int i = 0; i < 10; i++) { // Check whether our element is visible yet if (e.isDisplayed()) { return true; } try { Thread.sleep(1000); } catch (InterruptedException ex) { // Try again } } return false; } public boolean isNotDisplayed(RenderedWebElement e) { // Up to 10 times for (int i = 0; i < 10; 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) { waitForElement(By.id(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(); } } } } } /** * Seta o valor de uma combo box. * * @param element * Combo box. * @param value * Valor a ser selecionado. */ public boolean setComboValue(WebElement element, String value) { try { List options = element.findElements(By .tagName("option")); for (WebElement option : options) { if (option.getText().equals(value)) { option.setSelected(); return true; } } } catch (NoSuchElementException nsee) { System.out.println("Warning: ELEMENTO NAO ENCONTRADO!"); return false; } // TODO Rever o tratamento da excecao quando um valor nao for encontrado // na combobox. try { throw new Exception("Valor " + value + " não encontrado em " + element); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return false; } public String getComboValue(WebElement element) { List options = element.findElements(By.tagName("option")); for (WebElement option : options) { if (option.isSelected()) { return option.getText(); } } return null; } /** * Verifica se um element esta presente na tela. * * @param by * @return */ public Boolean isElementPresent(By by) { boolean find = true; try { driver.findElement(by); } catch (NoSuchElementException nsee) { find = false; } return find; } }