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

Revision 4902, 5.0 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, 5000, 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        /**
203         * Verifica se um element esta presente na tela.
204         *
205         * @param by
206         * @return
207         */
208        public Boolean isElementPresent(By by) {
209                boolean find = true;
210                try {
211                        driver.findElement(by);
212                } catch (NoSuchElementException nsee) {
213                        find = false;
214                }
215                return find;
216        }
217
218        /**
219         * Verifica se um texto esta presente no codigo-fonte da pagina.
220         *
221         * @param text
222         *            Texto a ser pesquisado.
223         * @return <code>true</code> se encontrar o texto, <code>false</code> caso
224         *         contrario.
225         */
226        public Boolean isTextPresent(String text) {
227                String pageSource = driver.getPageSource();
228                return pageSource.contains(text);
229        }
230}
Note: See TracBrowser for help on using the repository browser.