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

Revision 3663, 2.7 KB checked in by luiz-fernando, 13 years ago (diff)

Ticket #1402 - Nova versão dos casos de testes, agora implementados com WebDriver? (Selenium 2.0)

Line 
1package br.gov.serpro.expresso.cte.pages.common;
2
3import java.util.concurrent.TimeUnit;
4
5import org.openqa.selenium.By;
6import org.openqa.selenium.NoSuchElementException;
7import org.openqa.selenium.RenderedWebElement;
8import org.openqa.selenium.WebDriver;
9
10/**
11 * @author L.F.Estivalet (Serpro)
12 *
13 *         Created on Jan 4, 2011 at 3:50:44 PM
14 *
15 */
16/**
17 * @author L.F.Estivalet (Serpro)
18 *
19 *         Created on Jan 4, 2011 at 3:52:33 PM
20 *
21 */
22public class Page {
23        /** Implementacao do driver a ser utilizado no teste da pagina. */
24        protected WebDriver driver;
25
26        /**
27         * @param driver
28         *            Implementacao do driver (Firefox, IE, etc)
29         */
30        public Page(WebDriver driver) {
31                this.driver = driver;
32        }
33
34        /**
35         * Clica em um elemento.
36         *
37         * @param id
38         *            Id do elemento a ser clicado.
39         */
40        public void clickElement(String id) {
41                driver.findElement(By.id(id)).click();
42        }
43
44        /**
45         * Aguarda por um elemento.
46         *
47         * @param id
48         *            Id do elemento a ser aguardado.
49         */
50        public void waitForElementById(String id) {
51                isDisplayed(this.waitFindElement(By.id(id), 5000, 1000));
52        }
53
54        /**
55         * @param name
56         */
57        public void waitForElementByName(String name) {
58                isDisplayed(this.waitFindElement(By.name(name), 5000, 1000));
59        }
60
61        /**
62         * @param xpath
63         */
64        public void waitForElementByXpath(String xpath) {
65                isDisplayed(this.waitFindElement(By.xpath(xpath), 5000, 1000));
66        }
67
68        /**
69         * @param ms
70         */
71        public void waitAMoment(long ms) {
72                driver.manage().timeouts().implicitlyWait(ms, TimeUnit.MILLISECONDS);
73        }
74
75        /**
76         * @param e
77         */
78        private void isDisplayed(RenderedWebElement e) {
79                System.out.println("starting tentatives");
80                // Up to 10 times
81                for (int i = 0; i < 10; i++) {
82                        System.out.println("tentative " + i);
83                        // Check whether our element is visible yet
84                        if (e.isDisplayed()) {
85                                return;
86                        }
87
88                        try {
89                                Thread.sleep(1000);
90                        } catch (InterruptedException ex) {
91                                // Try again
92                        }
93                }
94
95        }
96
97        /**
98         * @param id
99         * @return
100         */
101        public String getElementContent(String id) {
102                return driver.findElement(By.id(id)).getText();
103        }
104
105        public String getDisplayedElementContent(String id) {
106                waitForElementById(id);
107                return driver.findElement(By.id(id)).getText();
108        }
109
110        public RenderedWebElement waitFindElement(By by, long timeout, long interval) {
111                long start = System.currentTimeMillis();
112                while (true) {
113                        try {
114                                return (RenderedWebElement) driver.findElement(by);
115                        } catch (NoSuchElementException nse) {
116                                if (System.currentTimeMillis() - start >= timeout) {
117                                        throw new Error("Timeout reached and element[" + by
118                                                        + "]not found");
119                                } else {
120                                        try {
121                                                synchronized (this) {
122                                                        wait(interval);
123                                                }
124                                        } catch (InterruptedException e) {
125                                                e.printStackTrace();
126                                        }
127                                }
128                        }
129                }
130        }
131
132}
Note: See TracBrowser for help on using the repository browser.