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

Revision 4620, 3.8 KB checked in by luiz-fernando, 13 years ago (diff)

Ticket #1771 - Refactoring, criadas novas classes para Pasta, Filtro, Pesquisa

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         * Aguarda elemento aparecer e depois clica.
42         *
43         * @param id
44         *            Id do elemento a ser clicado.
45         */
46        public void findAndClickElement(String id) {
47                this.waitForElement(By.id(id));
48                this.clickElement(By.id(id));
49        }
50
51        /**
52         * Aguarda ate o elemento aparecer na tela.
53         *
54         * @param by
55         *            Element a ser pesquisado, podendo ser por id, xpath, name.
56         */
57        public void waitForElement(By by) {
58                isDisplayed(this.waitFindElement(by, 5000, 1000));
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         */
71        public boolean isDisplayed(RenderedWebElement e) {
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()) {
76                                return true;
77                        }
78
79                        try {
80                                Thread.sleep(1000);
81                        } catch (InterruptedException ex) {
82                                // Try again
83                        }
84                }
85                return false;
86        }
87
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
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) {
114                waitForElement(By.id(id));
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
140        /**
141         * Seta o valor de uma combo box.
142         *
143         * @param element
144         *            Combo box.
145         * @param value
146         *            Valor a ser selecionado.
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();
153                                return;
154                        }
155                }
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                }
167        }
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
179}
Note: See TracBrowser for help on using the repository browser.