View Javadoc
1   /*
2    * Copyright (c) 2002-2026 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.htmlunit.WebDriverTestCase;
20  import org.htmlunit.junit.annotation.Alerts;
21  import org.junit.jupiter.api.BeforeAll;
22  import org.junit.jupiter.api.Test;
23  import org.openqa.selenium.By;
24  import org.openqa.selenium.WebDriver;
25  import org.openqa.selenium.WebElement;
26  
27  /**
28   * Tests for 2.2 version of <a href="http://www.extjs.com/">Ext JS</a>.
29   *
30   * @author Ahmed Ashour
31   * @author Ronald Brill
32   */
33  public class ExtJS22Test extends WebDriverTestCase {
34  
35      /**
36       * @throws Exception if an error occurs
37       */
38      @BeforeAll
39      public static void startServer() throws Exception {
40          startWebServer("src/test/resources/libraries/ExtJS/" + getVersion(), null);
41      }
42  
43      /**
44       * Returns the Ext JS version being tested.
45       * @return the Ext JS version being tested
46       */
47      protected static String getVersion() {
48          return "2.2";
49      }
50  
51      /**
52       * Loads the Ext JS test page using the specified example.
53       *
54       * @param example the example name
55       * @param htmlName the page name
56       * @return the loaded page
57       * @throws Exception if an error occurs
58       */
59      protected WebDriver getPage(final String example, final String htmlName) throws Exception {
60          final WebDriver driver = getWebDriver();
61          driver.get(URL_FIRST + "examples/" + example + "/" + htmlName + ".html");
62          return driver;
63      }
64  
65      /**
66       * @throws Exception if an error occurs
67       */
68      @Test
69      public void core_templates() throws Exception {
70          final WebDriver driver = getPage("core", "templates");
71          final List<WebElement> buttons = driver.findElements(By.xpath("//button"));
72          final List<WebElement> divs = driver.findElements(By.xpath("//div[@class='x-panel-body']"));
73          assertEquals(2, buttons.size());
74          assertEquals(2, divs.size());
75          assertEquals("Apply the template to see results here", divs.get(0).getText());
76          assertEquals("Apply the template to see results here", divs.get(1).getText());
77  
78          buttons.get(0).click();
79          assertEquals("Name: Jack Slocum\n" + "Company: Ext JS, LLC\n"
80              + "Location: Cleveland, Ohio", divs.get(0).getText());
81          assertEquals("Apply the template to see results here", divs.get(1).getText());
82  
83          buttons.get(1).click();
84          assertEquals("Name: Jack Slocum\n" + "Company: Ext JS, LLC\n"
85              + "Location: Cleveland, Ohio", divs.get(0).getText());
86          assertEquals("Name: Jack Slocum\n" + "Company: Ext JS, LLC\n"
87              + "Location: Cleveland, Ohio\n"
88              + "Kids:\n" + "1. Jack Slocum's kid - Sara Grace\n"
89              + "2. Jack Slocum's kid - Zachary", divs.get(1).getText());
90      }
91  
92      /**
93       * @throws Exception if an error occurs
94       */
95      @Test
96      public void core_spotlight() throws Exception {
97          final WebDriver driver = getPage("core", "spotlight");
98          final List<WebElement> buttons = driver.findElements(By.xpath("//button"));
99          assertEquals(4, buttons.size());
100         assertEquals("Start", buttons.get(0).getText());
101         assertEquals("Next Panel", buttons.get(1).getText());
102         assertEquals("Next Panel", buttons.get(2).getText());
103         assertEquals("Done", buttons.get(3).getText());
104 
105         assertTrue(core_spotlight_isDisabled(buttons.get(1)));
106         assertTrue(core_spotlight_isDisabled(buttons.get(2)));
107         assertTrue(core_spotlight_isDisabled(buttons.get(3)));
108 
109         buttons.get(0).click();
110         Thread.sleep(200);
111         assertFalse(core_spotlight_isDisabled(buttons.get(1)));
112         assertTrue(core_spotlight_isDisabled(buttons.get(2)));
113         assertTrue(core_spotlight_isDisabled(buttons.get(3)));
114 
115         buttons.get(1).click();
116         Thread.sleep(200);
117         assertTrue(core_spotlight_isDisabled(buttons.get(1)));
118         assertFalse(core_spotlight_isDisabled(buttons.get(2)));
119         assertTrue(core_spotlight_isDisabled(buttons.get(3)));
120 
121         buttons.get(2).click();
122         Thread.sleep(200);
123         assertTrue(core_spotlight_isDisabled(buttons.get(1)));
124         assertTrue(core_spotlight_isDisabled(buttons.get(2)));
125         assertFalse(core_spotlight_isDisabled(buttons.get(3)));
126 
127         buttons.get(3).click();
128         Thread.sleep(200);
129         assertTrue(core_spotlight_isDisabled(buttons.get(1)));
130         assertTrue(core_spotlight_isDisabled(buttons.get(2)));
131         assertTrue(core_spotlight_isDisabled(buttons.get(3)));
132     }
133 
134     private static boolean core_spotlight_isDisabled(final WebElement button) {
135         final WebElement table = button.findElement(By.xpath("ancestor::table[1]"));
136         return table.getAttribute("class").contains("disabled");
137     }
138 
139     /**
140      * @throws Exception if an error occurs
141      */
142     @Test
143     @Alerts("Hello from the Ext console.")
144     public void debug_console() throws Exception {
145         final WebDriver driver = getPage("debug", "debug-console");
146 
147         final List<WebElement> anchors = driver.findElements(By.xpath("//a"));
148         assertEquals(2, anchors.size());
149         anchors.get(1).click();
150         assertEquals(getExpectedAlerts()[0],
151                 driver.findElement(By.xpath("//div[starts-with(text(), 'Hello')][1]")).getText());
152     }
153 
154     /**
155      * @throws Exception if an error occurs
156      */
157     @Test
158     public void desktop_desktop() throws Exception {
159         final WebDriver driver = getPage("desktop", "desktop");
160         driver.findElement(By.xpath("//button[1]")).click();
161     }
162 
163     /**
164      * @throws Exception if an error occurs
165      */
166     @Test
167     public void form_absform() throws Exception {
168         final WebDriver driver = getPage("form", "absform");
169         final String content = driver.findElement(By.xpath("//html/body")).getText();
170         assertTrue(content.contains("Resize Me"));
171         assertTrue(content.contains("Send To:"));
172         assertTrue(content.contains("Subject:"));
173         assertTrue(content.contains("Cancel"));
174     }
175 
176     /**
177      * @throws Exception if an error occurs
178      */
179     @Test
180     public void form_anchoring() throws Exception {
181         final WebDriver driver = getPage("form", "anchoring");
182         final String content = driver.findElement(By.xpath("//html/body")).getText();
183         assertTrue(content.contains("Send To:"));
184         assertTrue(content.contains("Subject:"));
185         assertTrue(content.contains("Send"));
186         assertTrue(content.contains("Cancel"));
187     }
188 
189     /**
190      * @throws Exception if an error occurs
191      */
192     @Test
193     public void grid_binding() throws Exception {
194         final WebDriver driver = getPage("grid", "binding");
195 
196         // usually this need 1s but sometimes our build machine is slower
197         // this is not a performance test, we only like to ensure that all
198         // functionality is running
199         Thread.sleep(DEFAULT_WAIT_TIME.multipliedBy(2).toMillis());
200 
201         final WebElement detailPanel = driver.findElement(By.id("detailPanel"));
202         final WebElement resultsDiv = detailPanel.findElement(By.xpath("div/div[1]"));
203         assertEquals("Please select a book to see additional details.", resultsDiv.getText());
204 
205         final WebElement firstRowDiv = driver.findElement(By.xpath("//div[@class='x-grid3-body']/div[1]"));
206 
207         firstRowDiv.click();
208         assertEquals("Title: Master of the Game\n"
209                 + "Author: Sidney Sheldon\n"
210                 + "Manufacturer: Warner Books\n"
211                 + "Product Group: Book", resultsDiv.getText());
212     }
213 
214 }