source: devel/testlink/automation2.0/src/test/java/org/expressolivre/cte/pages/common/Page.java @ 5025

Revision 5025, 6.6 KB checked in by luiz-fernando, 13 years ago (diff)

Ticket #1771 - Adicionado testes automatizados para modulo da agenda

Line 
1package org.expressolivre.cte.pages.common;
2
3import java.util.List;
4import java.util.concurrent.TimeUnit;
5
6import org.openqa.selenium.By;
7import org.openqa.selenium.NoSuchElementException;
8import org.openqa.selenium.RenderedWebElement;
9import org.openqa.selenium.WebDriver;
10import org.openqa.selenium.WebElement;
11
12/**
13 * @author L.F.Estivalet (Serpro)
14 *
15 *         Created on Jan 4, 2011 at 3:52:33 PM
16 *
17 */
18public class Page {
19
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        /**
32         * Procura e clica em um elemento de tela.
33         *
34         * @param by
35         */
36        public void clickElement(By by) {
37                driver.findElement(by).click();
38        }
39
40        /**
41         * @param by
42         */
43        public void findAndClickElement(By by) {
44                this.waitForElement(by);
45                this.clickElement(by);
46        }
47
48        /**
49         * Aguarda ate o elemento aparecer na tela.
50         *
51         * @param by
52         *            Element a ser pesquisado, podendo ser por id, xpath, name.
53         */
54        public void waitForElement(By by) {
55                isDisplayed(this.waitFindElement(by, 100000, 1000));
56        }
57
58        /**
59         * @param ms
60         */
61        public void waitAMoment(long ms) {
62                driver.manage().timeouts().implicitlyWait(ms, TimeUnit.MILLISECONDS);
63        }
64
65        /**
66         * @param e
67         */
68        public boolean isDisplayed(RenderedWebElement e) {
69                // Up to 10 times
70                for (int i = 0; i < 10; i++) {
71                        // Check whether our element is visible yet
72                        if (e.isDisplayed()) {
73                                return true;
74                        }
75
76                        try {
77                                Thread.sleep(1000);
78                        } catch (InterruptedException ex) {
79                                // Try again
80                        }
81                }
82                return false;
83        }
84
85        /**
86         * @param e
87         * @return
88         */
89        public boolean isNotDisplayed(RenderedWebElement e) {
90                // Up to 10 times
91                for (int i = 0; i < 10; i++) {
92                        // Check whether our element is visible yet
93                        if (!e.isDisplayed()) {
94                                return true;
95                        }
96
97                        try {
98                                Thread.sleep(1000);
99                        } catch (InterruptedException ex) {
100                                // Try again
101                        }
102                }
103                return false;
104        }
105
106        /**
107         * @param by
108         *            Elemento a ser pesquisado.
109         * @return Texto contido no elemento.
110         */
111        public String getDisplayedElementContent(By by) {
112                waitForElement(by);
113                return driver.findElement(by).getText();
114        }
115
116        /**
117         * Aguarda ate o elemento aparecer na tela.
118         *
119         * @param by
120         *            Informa o elemento a ser pesquisado.
121         * @param timeout
122         *            Tempo limite de espera pelo elemento aparecer.
123         * @param interval
124         *            Intervalo para fazer uma nova tentativa.
125         * @return O elemento se aparecer ou dispara um erro informando que o
126         *         elemento nao existe ou nao pode ser exibido.
127         */
128        public RenderedWebElement waitFindElement(By by, long timeout, long interval) {
129                long start = System.currentTimeMillis();
130                while (true) {
131                        try {
132                                return (RenderedWebElement) driver.findElement(by);
133                        } catch (NoSuchElementException nse) {
134                                if (System.currentTimeMillis() - start >= timeout) {
135                                        throw new Error("Timeout reached and element[" + by
136                                                        + "]not found");
137                                } else {
138                                        try {
139                                                synchronized (this) {
140                                                        wait(interval);
141                                                }
142                                        } catch (InterruptedException e) {
143                                                e.printStackTrace();
144                                        }
145                                }
146                        }
147                }
148        }
149
150        /**
151         * Seta o valor de uma combo box.
152         *
153         * @param element
154         *            Combo box.
155         * @param value
156         *            Valor a ser selecionado.
157         */
158        public boolean setComboValue(WebElement element, String value) {
159
160                try {
161                        List<WebElement> options = element.findElements(By
162                                        .tagName("option"));
163                        for (WebElement option : options) {
164                                if (option.getText().equals(value)) {
165                                        option.setSelected();
166                                        return true;
167                                }
168                        }
169                } catch (NoSuchElementException nsee) {
170                        System.out.println("Warning: ELEMENTO NAO ENCONTRADO!");
171                        return false;
172                }
173
174                // TODO Rever o tratamento da excecao quando um valor nao for encontrado
175                // na combobox.
176
177                try {
178                        throw new Exception("Valor " + value + " não encontrado em "
179                                        + element);
180                } catch (Exception e) {
181                        // TODO Auto-generated catch block
182                        e.printStackTrace();
183                }
184                return false;
185        }
186
187        /**
188         * @param element
189         *            Combo box.
190         * @return Retorna o elemento selecionado da combo box.
191         */
192        public String getComboValue(WebElement element) {
193                List<WebElement> options = element.findElements(By.tagName("option"));
194                for (WebElement option : options) {
195                        if (option.isSelected()) {
196                                return option.getText();
197                        }
198                }
199                return null;
200        }
201
202        public List<WebElement> getComboElements(WebElement element) {
203                return element.findElements(By.tagName("option"));
204        }
205
206        /**
207         * Seleciona todos os valores de um combo.
208         *
209         * @param element
210         */
211        public void selectAllComboValue(WebElement element) {
212                List<WebElement> options = element.findElements(By.tagName("option"));
213                for (WebElement option : options) {
214                        option.setSelected();
215                }
216        }
217
218        /**
219         * Verifica se o valor passado esta presente na combo box.
220         *
221         * @param element
222         *            Elemento a verificar.
223         * @param value
224         *            Valor a verificar.
225         * @param select
226         *            Se <code>true</code> seleciona o elemento.
227         * @return
228         */
229        public boolean isValueInCombo(WebElement element, String value,
230                        Boolean select) {
231
232                boolean found = false;
233                try {
234                        List<WebElement> options = element.findElements(By
235                                        .tagName("option"));
236                        for (WebElement option : options) {
237                                if (option.getText().equals(value)) {
238                                        found = true;
239                                        if (select) {
240                                                option.setSelected();
241                                        }
242
243                                }
244                        }
245                } catch (NoSuchElementException nsee) {
246                        System.out.println("Warning: ELEMENTO NAO ENCONTRADO!");
247                        found = false;
248                }
249
250                // TODO Rever o tratamento da excecao quando um valor nao for encontrado
251                // na combobox.
252
253                try {
254                        throw new Exception("Valor " + value + " não encontrado em "
255                                        + element);
256                } catch (Exception e) {
257                        // TODO Auto-generated catch block
258                        e.printStackTrace();
259                }
260                return found;
261        }
262
263        /**
264         * Verifica se um element esta presente na tela.
265         *
266         * @param by
267         * @return
268         */
269        public Boolean isElementPresent(By by) {
270                boolean find = true;
271                try {
272                        driver.findElement(by);
273                } catch (NoSuchElementException nsee) {
274                        find = false;
275                }
276                return find;
277        }
278
279        /**
280         * Verifica se um texto esta presente no codigo-fonte da pagina.
281         *
282         * @param text
283         *            Texto a ser pesquisado.
284         * @return <code>true</code> se encontrar o texto, <code>false</code> caso
285         *         contrario.
286         */
287        public Boolean isTextPresent(String text) {
288                String pageSource = driver.getPageSource();
289                return pageSource.contains(text);
290        }
291
292        /**
293         * Gets the Xpath count for a given locator
294         *
295         * @param sLocator
296         *            The XPath you want to count
297         * @return int XPath Count
298         */
299        public int getXPathCount(String sLocator) {
300                return driver.findElements(By.xpath(sLocator)).size();
301
302        }
303}
Note: See TracBrowser for help on using the repository browser.