source: devel/testlink/automation2.0/src/test/java/br/gov/serpro/expresso/cte/pages/common/Page.java @ 3745

Revision 3745, 3.3 KB checked in by luiz-fernando, 13 years ago (diff)

Ticket #1402 - Novos casos de teste implementados usando WebDriver?

Line 
1package br.gov.serpro.expresso.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        /** Implementacao do driver a ser utilizado no teste da pagina. */
20        protected WebDriver driver;
21
22        /**
23         * @param driver
24         *            Implementacao do driver (Firefox, IE, etc)
25         */
26        public Page(WebDriver driver) {
27                this.driver = driver;
28        }
29
30        /**
31         * Clica em um elemento.
32         *
33         * @param id
34         *            Id do elemento a ser clicado.
35         */
36        public void clickElement(String id) {
37                driver.findElement(By.id(id)).click();
38        }
39
40        public void clickElement(By by) {
41                driver.findElement(by).click();
42        }
43
44        /**
45         * Aguarda elemento aparecer e depois clica.
46         *
47         * @param id
48         *            Id do elemento a ser clicado.
49         */
50        public void findAndClickElement(String id) {
51                this.waitForElementById(id);
52                this.clickElement(id);
53        }
54
55        /**
56         * Aguarda por um elemento.
57         *
58         * @param id
59         *            Id do elemento a ser aguardado.
60         */
61        public void waitForElementById(String id) {
62                isDisplayed(this.waitFindElement(By.id(id), 5000, 1000));
63        }
64
65        /**
66         * @param name
67         */
68        public void waitForElementByName(String name) {
69                isDisplayed(this.waitFindElement(By.name(name), 5000, 1000));
70        }
71
72        /**
73         * @param xpath
74         */
75        public void waitForElementByXpath(String xpath) {
76                isDisplayed(this.waitFindElement(By.xpath(xpath), 5000, 1000));
77        }
78
79        /**
80         * @param ms
81         */
82        public void waitAMoment(long ms) {
83                driver.manage().timeouts().implicitlyWait(ms, TimeUnit.MILLISECONDS);
84        }
85
86        /**
87         * @param e
88         */
89        public boolean isDisplayed(RenderedWebElement e) {
90                System.out.println("starting tentatives");
91                // Up to 10 times
92                for (int i = 0; i < 10; i++) {
93                        System.out.println("tentative " + i);
94                        // Check whether our element is visible yet
95                        if (e.isDisplayed()) {
96                                return true;
97                        }
98
99                        try {
100                                Thread.sleep(1000);
101                        } catch (InterruptedException ex) {
102                                // Try again
103                        }
104                }
105                return false;
106        }
107
108        /**
109         * @param id
110         * @return
111         */
112        public String getElementContent(String id) {
113                return driver.findElement(By.id(id)).getText();
114        }
115
116        public String getDisplayedElementContent(String id) {
117                waitForElementById(id);
118                return driver.findElement(By.id(id)).getText();
119        }
120
121        public RenderedWebElement waitFindElement(By by, long timeout, long interval) {
122                long start = System.currentTimeMillis();
123                while (true) {
124                        try {
125                                return (RenderedWebElement) driver.findElement(by);
126                        } catch (NoSuchElementException nse) {
127                                if (System.currentTimeMillis() - start >= timeout) {
128                                        throw new Error("Timeout reached and element[" + by
129                                                        + "]not found");
130                                } else {
131                                        try {
132                                                synchronized (this) {
133                                                        wait(interval);
134                                                }
135                                        } catch (InterruptedException e) {
136                                                e.printStackTrace();
137                                        }
138                                }
139                        }
140                }
141        }
142
143        /**
144         * @param element
145         * @param value
146         */
147        public void setComboValue(WebElement element, String value) {
148                List<WebElement> options = element.findElements(By.tagName("option"));
149                for (WebElement option : options) {
150                        if (option.getText().equals(value)) {
151                                option.setSelected();
152                                break;
153                        }
154                }
155        }
156}
Note: See TracBrowser for help on using the repository browser.