View Javadoc

1   /**
2    * Copyright (c) 2002-2015, JWebUnit team.
3    *
4    * This file is part of JWebUnit.
5    *
6    * JWebUnit is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Lesser General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * JWebUnit is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public License
17   * along with JWebUnit.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  /**
20   * 
21   */
22  package net.sourceforge.jwebunit.webdriver;
23  
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import net.sourceforge.jwebunit.api.IElement;
28  
29  import org.openqa.selenium.By;
30  import org.openqa.selenium.JavascriptExecutor;
31  import org.openqa.selenium.WebDriver;
32  import org.openqa.selenium.WebElement;
33  
34  /**
35   * Webdriver implementation of IElement wrapper.
36   * 
37   * @author henryju
38   * 
39   */
40  public class WebDriverElementImpl implements IElement {
41  
42      /**
43       * The wrapped element.
44       */
45      private WebElement element;
46      
47      /**
48       * A reference to the driver.
49       */
50      private WebDriver driver;
51  
52      public WebDriverElementImpl(WebDriver driver, WebElement element) {
53          this.driver = driver;
54          if (element == null)
55              throw new NullPointerException("Cannot create an IElement for a null element.");
56          this.element = element;
57      }
58  
59      /*
60       * (non-Javadoc)
61       * 
62       * @see net.sourceforge.jwebunit.api.IElement#attribute(java.lang.String)
63       */
64      public String getAttribute(String name) {
65          return element.getAttribute(name);
66      }
67  
68      /*
69       * (non-Javadoc)
70       * 
71       * @see net.sourceforge.jwebunit.api.IElement#name()
72       */
73      public String getName() {
74          return element.getTagName();
75      }
76  
77      /*
78       * (non-Javadoc)
79       * 
80       * @see net.sourceforge.jwebunit.api.IElement#getChildren()
81       */
82      public List<IElement> getChildren() {
83          List<IElement> children = new ArrayList<IElement>();
84          for (WebElement e : element.findElements(By.xpath("child::*"))) {
85              if (e != null)
86                  children.add(new WebDriverElementImpl(driver, e));
87          }
88          return children;
89      }
90  
91      /*
92       * (non-Javadoc)
93       * 
94       * @see net.sourceforge.jwebunit.api.IElement#getParent()
95       */
96      public IElement getParent() {
97          return new WebDriverElementImpl(driver, element.findElement(By.xpath("parent::*")));
98      }
99  
100     /*
101      * (non-Javadoc)
102      * 
103      * @see net.sourceforge.jwebunit.api.IElement#getTextContent()
104      */
105     public String getTextContent() {
106         return element.getText();
107     }
108 
109     /*
110      * (non-Javadoc)
111      * 
112      * @see net.sourceforge.jwebunit.api.IElement#getElement(java.lang.String)
113      */
114     public IElement getElement(String xpath) {
115         return new WebDriverElementImpl(driver, (WebElement) element.findElement(By.xpath(xpath)));
116     }
117 
118     /*
119      * (non-Javadoc)
120      * 
121      * @see net.sourceforge.jwebunit.api.IElement#getElements(java.lang.String)
122      */
123     public List<IElement> getElements(String xpath) {
124         List<IElement> elements = new ArrayList<IElement>();
125         for (WebElement o : element.findElements(By.xpath(xpath))) {
126             elements.add(new WebDriverElementImpl(driver, o));
127         }
128         return elements;
129     }
130 
131     public String toString() {
132         return "IElement[name=" + getName() + " wrapped=" + element + "]";
133     }
134 
135     /*
136      * (non-Javadoc)
137      * 
138      * @see net.sourceforge.jwebunit.api.IElement#setAttribute(java.lang.String)
139      */
140     public void setAttribute(String name) {
141         ((JavascriptExecutor) driver).executeScript("return arguments[0].setAttribute(arguments[1], true);", element, name);
142     }
143 
144     /*
145      * (non-Javadoc)
146      * 
147      * @see net.sourceforge.jwebunit.api.IElement#setAttribute(java.lang.String, java.lang.String)
148      */
149     public void setAttribute(String name, String value) {
150         if ("value".equals(name) && "input".equals(element.getTagName())) {
151             // for inputs, we want to run any onChange code if the value changes
152             element.sendKeys(value);
153         } else {
154             ((JavascriptExecutor) driver).executeScript("return arguments[0].setAttribute(arguments[1], arguments[2]);", element, name, value);
155         }
156     }
157 
158     /*
159      * (non-Javadoc)
160      * 
161      * @see net.sourceforge.jwebunit.api.IElement#setTextContent(java.lang.String)
162      */
163     public void setTextContent(String value) {
164         if (element.getTagName().equals("textarea")) {
165             element.clear();
166             element.sendKeys(value);
167         } else {
168             ((JavascriptExecutor) driver).executeScript(
169                 "var parent = arguments[0];" +
170                 "var children = parent.childNodes;" +
171                 "for (i=0; i< children.length; i++) {" +
172                 "  parent.removeChild(children[i]);" +
173                 "}" +
174                 "parent.appendChild(document.createTextNode(arguments[1]));"
175                 , element, value);
176         }
177     }
178 
179     @Override
180     public int hashCode() {
181         final int prime = 37;
182         int result = 1;
183         result = prime * result + ((element == null) ? 0 : element.hashCode());
184         return result;
185     }
186 
187     @Override
188     public boolean equals(Object obj) {
189         if (this == obj)
190             return true;
191         if (obj == null)
192             return false;
193         if (getClass() != obj.getClass())
194             return false;
195         final WebDriverElementImpl other = (WebDriverElementImpl) obj;
196         if (element == null) {
197             if (other.element != null)
198                 return false;
199         }
200         else if (!element.equals(other.element))
201             return false;
202         return true;
203     }
204 
205     /**
206      * Return the unwrapped Webdriver element that this IElement represents.
207      * 
208      * @return the Webdriver element this IElement represents.
209      */
210     public WebElement getHtmlElement() {
211         return element;
212     }
213 
214 }