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

Revision 3782, 3.0 KB checked in by luiz-fernando, 13 years ago (diff)

Ticket #1402 - Mudança no nome dos pacotes das classes

RevLine 
[3782]1package org.expressolivre.cte.pages.common;
[3663]2
[3745]3import java.util.List;
[3663]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;
[3745]10import org.openqa.selenium.WebElement;
[3663]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 {
[3766]19
[3663]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        /**
[3766]32         * Procura e clica em um elemento de tela.
[3663]33         *
[3766]34         * @param by
[3663]35         */
[3745]36        public void clickElement(By by) {
37                driver.findElement(by).click();
38        }
39
[3663]40        /**
[3708]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) {
[3766]47                this.waitForElement(By.id(id));
48                this.clickElement(By.id(id));
[3708]49        }
50
51        /**
[3766]52         * Aguarda ate o elemento aparecer na tela.
[3663]53         *
[3766]54         * @param by
55         *            Element a ser pesquisado, podendo ser por id, xpath, name.
[3663]56         */
[3766]57        public void waitForElement(By by) {
58                isDisplayed(this.waitFindElement(by, 5000, 1000));
[3663]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         */
[3708]71        public boolean isDisplayed(RenderedWebElement e) {
[3663]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()) {
[3708]76                                return true;
[3663]77                        }
78
79                        try {
80                                Thread.sleep(1000);
81                        } catch (InterruptedException ex) {
82                                // Try again
83                        }
84                }
[3708]85                return false;
[3663]86        }
87
88        /**
89         * @param id
90         * @return
91         */
92        public String getElementContent(String id) {
93                return driver.findElement(By.id(id)).getText();
94        }
95
96        public String getDisplayedElementContent(String id) {
[3766]97                waitForElement(By.id(id));
[3663]98                return driver.findElement(By.id(id)).getText();
99        }
100
101        public RenderedWebElement waitFindElement(By by, long timeout, long interval) {
102                long start = System.currentTimeMillis();
103                while (true) {
104                        try {
105                                return (RenderedWebElement) driver.findElement(by);
106                        } catch (NoSuchElementException nse) {
107                                if (System.currentTimeMillis() - start >= timeout) {
108                                        throw new Error("Timeout reached and element[" + by
109                                                        + "]not found");
110                                } else {
111                                        try {
112                                                synchronized (this) {
113                                                        wait(interval);
114                                                }
115                                        } catch (InterruptedException e) {
116                                                e.printStackTrace();
117                                        }
118                                }
119                        }
120                }
121        }
122
[3745]123        /**
[3766]124         * Seta o valor de uma combo box.
125         *
[3745]126         * @param element
[3766]127         *            Combo box.
[3745]128         * @param value
[3766]129         *            Valor a ser selecionado.
[3745]130         */
131        public void setComboValue(WebElement element, String value) {
132                List<WebElement> options = element.findElements(By.tagName("option"));
133                for (WebElement option : options) {
134                        if (option.getText().equals(value)) {
135                                option.setSelected();
136                                break;
137                        }
138                }
139        }
[3663]140}
Note: See TracBrowser for help on using the repository browser.