View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.libraries;
16  
17  import java.util.List;
18  
19  import org.eclipse.jetty.server.Server;
20  import org.htmlunit.WebDriverTestCase;
21  import org.htmlunit.WebServerTestCase;
22  import org.htmlunit.junit.BrowserRunner;
23  import org.htmlunit.junit.annotation.Alerts;
24  import org.junit.AfterClass;
25  import org.junit.BeforeClass;
26  import org.junit.Test;
27  import org.junit.runner.RunWith;
28  import org.openqa.selenium.By;
29  import org.openqa.selenium.WebDriver;
30  import org.openqa.selenium.WebElement;
31  
32  /**
33   * Tests for 2.2 version of <a href="http://www.extjs.com/">Ext JS</a>.
34   *
35   * @author Ahmed Ashour
36   * @author Ronald Brill
37   */
38  @RunWith(BrowserRunner.class)
39  public class ExtJS22Test extends WebDriverTestCase {
40  
41      private static Server SERVER_;
42  
43      /**
44       * @throws Exception if an error occurs
45       */
46      @BeforeClass
47      public static void startSesrver() throws Exception {
48          SERVER_ = WebServerTestCase.createWebServer("src/test/resources/libraries/ExtJS/" + getVersion(), null);
49      }
50  
51      /**
52       * @throws Exception if an error occurs
53       */
54      @AfterClass
55      public static void stopServer() throws Exception {
56          if (SERVER_ != null) {
57              SERVER_.stop();
58              SERVER_.destroy();
59              SERVER_ = null;
60          }
61      }
62  
63      /**
64       * Returns the Ext JS version being tested.
65       * @return the Ext JS version being tested
66       */
67      protected static String getVersion() {
68          return "2.2";
69      }
70  
71      /**
72       * Loads the Ext JS test page using the specified example.
73       *
74       * @param example the example name
75       * @param htmlName the page name
76       * @return the loaded page
77       * @throws Exception if an error occurs
78       */
79      protected WebDriver getPage(final String example, final String htmlName) throws Exception {
80          final WebDriver driver = getWebDriver();
81          driver.get(URL_FIRST + "examples/" + example + "/" + htmlName + ".html");
82          return driver;
83      }
84  
85      /**
86       * @throws Exception if an error occurs
87       */
88      @Test
89      public void core_templates() throws Exception {
90          final WebDriver driver = getPage("core", "templates");
91          final List<WebElement> buttons = driver.findElements(By.xpath("//button"));
92          final List<WebElement> divs = driver.findElements(By.xpath("//div[@class='x-panel-body']"));
93          assertEquals(2, buttons.size());
94          assertEquals(2, divs.size());
95          assertEquals("Apply the template to see results here", divs.get(0).getText());
96          assertEquals("Apply the template to see results here", divs.get(1).getText());
97  
98          buttons.get(0).click();
99          assertEquals("Name: Jack Slocum\n" + "Company: Ext JS, LLC\n"
100             + "Location: Cleveland, Ohio", divs.get(0).getText());
101         assertEquals("Apply the template to see results here", divs.get(1).getText());
102 
103         buttons.get(1).click();
104         assertEquals("Name: Jack Slocum\n" + "Company: Ext JS, LLC\n"
105             + "Location: Cleveland, Ohio", divs.get(0).getText());
106         assertEquals("Name: Jack Slocum\n" + "Company: Ext JS, LLC\n"
107             + "Location: Cleveland, Ohio\n"
108             + "Kids:\n" + "1. Jack Slocum's kid - Sara Grace\n"
109             + "2. Jack Slocum's kid - Zachary", divs.get(1).getText());
110     }
111 
112     /**
113      * @throws Exception if an error occurs
114      */
115     @Test
116     public void core_spotlight() throws Exception {
117         final WebDriver driver = getPage("core", "spotlight");
118         final List<WebElement> buttons = driver.findElements(By.xpath("//button"));
119         assertEquals(4, buttons.size());
120         assertEquals("Start", buttons.get(0).getText());
121         assertEquals("Next Panel", buttons.get(1).getText());
122         assertEquals("Next Panel", buttons.get(2).getText());
123         assertEquals("Done", buttons.get(3).getText());
124 
125         assertTrue(core_spotlight_isDisabled(buttons.get(1)));
126         assertTrue(core_spotlight_isDisabled(buttons.get(2)));
127         assertTrue(core_spotlight_isDisabled(buttons.get(3)));
128 
129         buttons.get(0).click();
130         Thread.sleep(200);
131         assertFalse(core_spotlight_isDisabled(buttons.get(1)));
132         assertTrue(core_spotlight_isDisabled(buttons.get(2)));
133         assertTrue(core_spotlight_isDisabled(buttons.get(3)));
134 
135         buttons.get(1).click();
136         Thread.sleep(200);
137         assertTrue(core_spotlight_isDisabled(buttons.get(1)));
138         assertFalse(core_spotlight_isDisabled(buttons.get(2)));
139         assertTrue(core_spotlight_isDisabled(buttons.get(3)));
140 
141         buttons.get(2).click();
142         Thread.sleep(200);
143         assertTrue(core_spotlight_isDisabled(buttons.get(1)));
144         assertTrue(core_spotlight_isDisabled(buttons.get(2)));
145         assertFalse(core_spotlight_isDisabled(buttons.get(3)));
146 
147         buttons.get(3).click();
148         Thread.sleep(200);
149         assertTrue(core_spotlight_isDisabled(buttons.get(1)));
150         assertTrue(core_spotlight_isDisabled(buttons.get(2)));
151         assertTrue(core_spotlight_isDisabled(buttons.get(3)));
152     }
153 
154     private static boolean core_spotlight_isDisabled(final WebElement button) {
155         final WebElement table = button.findElement(By.xpath("ancestor::table[1]"));
156         return table.getAttribute("class").contains("disabled");
157     }
158 
159     /**
160      * @throws Exception if an error occurs
161      */
162     @Test
163     @Alerts("Hello from the Ext console.")
164     public void debug_console() throws Exception {
165         final WebDriver driver = getPage("debug", "debug-console");
166 
167         final List<WebElement> anchors = driver.findElements(By.xpath("//a"));
168         assertEquals(2, anchors.size());
169         anchors.get(1).click();
170         assertEquals(getExpectedAlerts()[0],
171                 driver.findElement(By.xpath("//div[starts-with(text(), 'Hello')][1]")).getText());
172     }
173 
174     /**
175      * @throws Exception if an error occurs
176      */
177     @Test
178     public void desktop_desktop() throws Exception {
179         final WebDriver driver = getPage("desktop", "desktop");
180         driver.findElement(By.xpath("//button[1]")).click();
181     }
182 
183     /**
184      * @throws Exception if an error occurs
185      */
186     @Test
187     public void form_absform() throws Exception {
188         final WebDriver driver = getPage("form", "absform");
189         final String content = driver.findElement(By.xpath("//html/body")).getText();
190         assertTrue(content.contains("Resize Me"));
191         assertTrue(content.contains("Send To:"));
192         assertTrue(content.contains("Subject:"));
193         assertTrue(content.contains("Cancel"));
194     }
195 
196     /**
197      * @throws Exception if an error occurs
198      */
199     @Test
200     public void form_anchoring() throws Exception {
201         final WebDriver driver = getPage("form", "anchoring");
202         final String content = driver.findElement(By.xpath("//html/body")).getText();
203         assertTrue(content.contains("Send To:"));
204         assertTrue(content.contains("Subject:"));
205         assertTrue(content.contains("Send"));
206         assertTrue(content.contains("Cancel"));
207     }
208 
209     /**
210      * @throws Exception if an error occurs
211      */
212     @Test
213     public void grid_binding() throws Exception {
214         final WebDriver driver = getPage("grid", "binding");
215 
216         // usually this need 1s but sometimes our build machine is slower
217         // this is not a performance test, we only like to ensure that all
218         // functionality is running
219         Thread.sleep(DEFAULT_WAIT_TIME.multipliedBy(2).toMillis());
220 
221         final WebElement detailPanel = driver.findElement(By.id("detailPanel"));
222         final WebElement resultsDiv = detailPanel.findElement(By.xpath("div/div[1]"));
223         assertEquals("Please select a book to see additional details.", resultsDiv.getText());
224 
225         final WebElement firstRowDiv = driver.findElement(By.xpath("//div[@class='x-grid3-body']/div[1]"));
226 
227         firstRowDiv.click();
228         assertEquals("Title: Master of the Game\n"
229                 + "Author: Sidney Sheldon\n"
230                 + "Manufacturer: Warner Books\n"
231                 + "Product Group: Book", resultsDiv.getText());
232     }
233 
234 }