[3782] | 1 | package org.expressolivre.cte.pages.common; |
---|
[3663] | 2 | |
---|
[3745] | 3 | import java.util.List; |
---|
[3663] | 4 | import java.util.concurrent.TimeUnit; |
---|
| 5 | |
---|
| 6 | import org.openqa.selenium.By; |
---|
| 7 | import org.openqa.selenium.NoSuchElementException; |
---|
| 8 | import org.openqa.selenium.RenderedWebElement; |
---|
| 9 | import org.openqa.selenium.WebDriver; |
---|
[3745] | 10 | import org.openqa.selenium.WebElement; |
---|
[3663] | 11 | |
---|
| 12 | /** |
---|
| 13 | * @author L.F.Estivalet (Serpro) |
---|
| 14 | * |
---|
| 15 | * Created on Jan 4, 2011 at 3:52:33 PM |
---|
| 16 | * |
---|
| 17 | */ |
---|
| 18 | public class Page { |
---|
[3766] | 19 | |
---|
[3663] | 20 | /** Implementacao do driver a ser utilizado no teste da pagina. */ |
---|
| 21 | protected WebDriver driver; |
---|
| 22 | |
---|
| 23 | /** |
---|
| 24 | * @param driver |
---|
| 25 | * Implementacao do driver (Firefox, IE, etc) |
---|
| 26 | */ |
---|
| 27 | public Page(WebDriver driver) { |
---|
| 28 | this.driver = driver; |
---|
| 29 | } |
---|
| 30 | |
---|
| 31 | /** |
---|
[3766] | 32 | * Procura e clica em um elemento de tela. |
---|
[3663] | 33 | * |
---|
[3766] | 34 | * @param by |
---|
[3663] | 35 | */ |
---|
[3745] | 36 | public void clickElement(By by) { |
---|
| 37 | driver.findElement(by).click(); |
---|
| 38 | } |
---|
| 39 | |
---|
[3663] | 40 | /** |
---|
[3708] | 41 | * Aguarda elemento aparecer e depois clica. |
---|
| 42 | * |
---|
| 43 | * @param id |
---|
| 44 | * Id do elemento a ser clicado. |
---|
| 45 | */ |
---|
| 46 | public void findAndClickElement(String id) { |
---|
[3766] | 47 | this.waitForElement(By.id(id)); |
---|
| 48 | this.clickElement(By.id(id)); |
---|
[3708] | 49 | } |
---|
| 50 | |
---|
| 51 | /** |
---|
[3766] | 52 | * Aguarda ate o elemento aparecer na tela. |
---|
[3663] | 53 | * |
---|
[3766] | 54 | * @param by |
---|
| 55 | * Element a ser pesquisado, podendo ser por id, xpath, name. |
---|
[3663] | 56 | */ |
---|
[3766] | 57 | public void waitForElement(By by) { |
---|
| 58 | isDisplayed(this.waitFindElement(by, 5000, 1000)); |
---|
[3663] | 59 | } |
---|
| 60 | |
---|
| 61 | /** |
---|
| 62 | * @param ms |
---|
| 63 | */ |
---|
| 64 | public void waitAMoment(long ms) { |
---|
| 65 | driver.manage().timeouts().implicitlyWait(ms, TimeUnit.MILLISECONDS); |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | /** |
---|
| 69 | * @param e |
---|
| 70 | */ |
---|
[3708] | 71 | public boolean isDisplayed(RenderedWebElement e) { |
---|
[3663] | 72 | // Up to 10 times |
---|
| 73 | for (int i = 0; i < 10; i++) { |
---|
| 74 | // Check whether our element is visible yet |
---|
| 75 | if (e.isDisplayed()) { |
---|
[3708] | 76 | return true; |
---|
[3663] | 77 | } |
---|
| 78 | |
---|
| 79 | try { |
---|
| 80 | Thread.sleep(1000); |
---|
| 81 | } catch (InterruptedException ex) { |
---|
| 82 | // Try again |
---|
| 83 | } |
---|
| 84 | } |
---|
[3708] | 85 | return false; |
---|
[3663] | 86 | } |
---|
| 87 | |
---|
[4344] | 88 | public boolean isNotDisplayed(RenderedWebElement e) { |
---|
| 89 | // Up to 10 times |
---|
| 90 | for (int i = 0; i < 10; i++) { |
---|
| 91 | // Check whether our element is visible yet |
---|
| 92 | if (!e.isDisplayed()) { |
---|
| 93 | return true; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | try { |
---|
| 97 | Thread.sleep(1000); |
---|
| 98 | } catch (InterruptedException ex) { |
---|
| 99 | // Try again |
---|
| 100 | } |
---|
| 101 | } |
---|
| 102 | return false; |
---|
| 103 | } |
---|
| 104 | |
---|
[3663] | 105 | /** |
---|
| 106 | * @param id |
---|
| 107 | * @return |
---|
| 108 | */ |
---|
| 109 | public String getElementContent(String id) { |
---|
| 110 | return driver.findElement(By.id(id)).getText(); |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | public String getDisplayedElementContent(String id) { |
---|
[3766] | 114 | waitForElement(By.id(id)); |
---|
[3663] | 115 | return driver.findElement(By.id(id)).getText(); |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | public RenderedWebElement waitFindElement(By by, long timeout, long interval) { |
---|
| 119 | long start = System.currentTimeMillis(); |
---|
| 120 | while (true) { |
---|
| 121 | try { |
---|
| 122 | return (RenderedWebElement) driver.findElement(by); |
---|
| 123 | } catch (NoSuchElementException nse) { |
---|
| 124 | if (System.currentTimeMillis() - start >= timeout) { |
---|
| 125 | throw new Error("Timeout reached and element[" + by |
---|
| 126 | + "]not found"); |
---|
| 127 | } else { |
---|
| 128 | try { |
---|
| 129 | synchronized (this) { |
---|
| 130 | wait(interval); |
---|
| 131 | } |
---|
| 132 | } catch (InterruptedException e) { |
---|
| 133 | e.printStackTrace(); |
---|
| 134 | } |
---|
| 135 | } |
---|
| 136 | } |
---|
| 137 | } |
---|
| 138 | } |
---|
| 139 | |
---|
[3745] | 140 | /** |
---|
[3766] | 141 | * Seta o valor de uma combo box. |
---|
| 142 | * |
---|
[3745] | 143 | * @param element |
---|
[3766] | 144 | * Combo box. |
---|
[3745] | 145 | * @param value |
---|
[3766] | 146 | * Valor a ser selecionado. |
---|
[3745] | 147 | */ |
---|
| 148 | public void setComboValue(WebElement element, String value) { |
---|
| 149 | List<WebElement> options = element.findElements(By.tagName("option")); |
---|
| 150 | for (WebElement option : options) { |
---|
| 151 | if (option.getText().equals(value)) { |
---|
| 152 | option.setSelected(); |
---|
[4620] | 153 | return; |
---|
[3745] | 154 | } |
---|
| 155 | } |
---|
[4620] | 156 | |
---|
| 157 | // TODO Rever o tratamento da excecao quando um valor nao for encontrado |
---|
| 158 | // na combobox. |
---|
| 159 | |
---|
| 160 | try { |
---|
| 161 | throw new Exception("Valor " + value + " não encontrado em " |
---|
| 162 | + element); |
---|
| 163 | } catch (Exception e) { |
---|
| 164 | // TODO Auto-generated catch block |
---|
| 165 | e.printStackTrace(); |
---|
| 166 | } |
---|
[3745] | 167 | } |
---|
[4620] | 168 | |
---|
| 169 | public String getComboValue(WebElement element) { |
---|
| 170 | List<WebElement> options = element.findElements(By.tagName("option")); |
---|
| 171 | for (WebElement option : options) { |
---|
| 172 | if (option.isSelected()) { |
---|
| 173 | return option.getText(); |
---|
| 174 | } |
---|
| 175 | } |
---|
| 176 | return null; |
---|
| 177 | } |
---|
| 178 | |
---|
[3663] | 179 | } |
---|