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.html;
16  
17  import java.util.ArrayList;
18  import java.util.List;
19  
20  import org.htmlunit.CollectingAlertHandler;
21  import org.htmlunit.MockWebConnection;
22  import org.htmlunit.Page;
23  import org.htmlunit.SimpleWebTestCase;
24  import org.htmlunit.WebClient;
25  import org.htmlunit.http.HttpStatus;
26  import org.htmlunit.util.MimeType;
27  import org.junit.jupiter.api.Test;
28  
29  /**
30   * Tests for {@link XHtmlPage}.
31   *
32   * @author Daniel Gredler
33   */
34  public class XHtmlPageTest extends SimpleWebTestCase {
35  
36      /**
37       * Regression test for Bug #764. Originally located in {@link org.htmlunit.xml.XmlPageTest}.
38       * @throws Exception if an error occurs
39       */
40      @Test
41      public void xpath1() throws Exception {
42          final String html
43              = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
44              + "<!DOCTYPE html PUBLIC \n"
45              + "  \"-//W3C//DTD XHTML 1.0 Strict//EN\" \n"
46              + "  \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"
47              + "<html xmlns='http://www.w3.org/1999/xhtml' xmlns:xhtml='http://www.w3.org/1999/xhtml'>\n"
48              + "<body><DIV>foo</DIV></body>\n"
49              + "</html>";
50  
51          final WebClient client = getWebClient();
52          final List<String> actual = new ArrayList<>();
53          client.setAlertHandler(new CollectingAlertHandler(actual));
54  
55          final MockWebConnection conn = new MockWebConnection();
56          conn.setResponse(URL_FIRST, html, MimeType.TEXT_XML);
57          client.setWebConnection(conn);
58  
59          final XHtmlPage page = client.getPage(URL_FIRST);
60          final DomNode body = page.getDocumentElement().getFirstChild().getNextSibling();
61          final DomNode div = body.getFirstChild();
62  
63          assertEquals(HtmlBody.class, body.getClass());
64          assertEquals("body", body.getLocalName());
65          assertEquals("DIV", div.getLocalName());
66          assertNotNull(page.getFirstByXPath(".//xhtml:body"));
67          assertNotNull(page.getFirstByXPath(".//xhtml:DIV"));
68          assertNull(page.getFirstByXPath(".//xhtml:div"));
69      }
70  
71      /**
72       * Tests a simplified real-life response from Ajax4jsf. Originally located in
73       * {@link org.htmlunit.xml.XmlPageTest}.
74       * @throws Exception if an error occurs
75       */
76      @Test
77      public void a4jResponse() throws Exception {
78          final String content = "<html xmlns='http://www.w3.org/1999/xhtml'><head>\n"
79              + "<script src='//:'></script>\n"
80              + "</head><body><span id='j_id216:outtext'>Echo Hello World</span></body></html>";
81          final WebClient client = getWebClient();
82          final MockWebConnection webConnection = new MockWebConnection();
83          webConnection.setDefaultResponse(content, 200, "OK", MimeType.TEXT_XML);
84          client.setWebConnection(webConnection);
85          final Page page = client.getPage(URL_FIRST);
86          assertEquals(URL_FIRST, page.getUrl());
87          assertEquals("OK", page.getWebResponse().getStatusMessage());
88          assertEquals(HttpStatus.OK_200, page.getWebResponse().getStatusCode());
89          assertEquals(MimeType.TEXT_XML, page.getWebResponse().getContentType());
90          assertTrue(XHtmlPage.class.isInstance(page));
91      }
92  
93      /**
94       * @throws Exception if an error occurs
95       */
96      @Test
97      public void xpath2() throws Exception {
98          final String html =
99                "<html xmlns='http://www.w3.org/1999/xhtml' xmlns:xhtml='http://www.w3.org/1999/xhtml'>\n"
100             + "<body><xhtml:div>foo</xhtml:div></body></html>";
101 
102         final MockWebConnection conn = new MockWebConnection();
103         conn.setDefaultResponse(html, 200, "OK", "application/xhtml+xml");
104 
105         final WebClient client = getWebClient();
106         client.setWebConnection(conn);
107         final XHtmlPage page = client.getPage(URL_FIRST);
108 
109         assertEquals(1, page.getByXPath("//:body").size());
110         assertEquals(0, page.getByXPath("//:BODY").size());
111         assertEquals(0, page.getByXPath("//:bOdY").size());
112 
113         assertEquals(1, page.getByXPath("//xhtml:body").size());
114         assertEquals(0, page.getByXPath("//xhtml:BODY").size());
115         assertEquals(0, page.getByXPath("//xhtml:bOdY").size());
116 
117         assertEquals(1, page.getByXPath("//xhtml:div").size());
118         assertEquals(0, page.getByXPath("//xhtml:DIV").size());
119         assertEquals(0, page.getByXPath("//xhtml:dIv").size());
120     }
121 
122     /**
123      * @throws Exception if an error occurs
124      */
125     @Test
126     public void namespace() throws Exception {
127         final String html =
128               "<html xmlns='http://www.w3.org/1999/xhtml' xmlns:xhtml='http://www.w3.org/1999/xhtml'>\n"
129             + "<body><div>foo</div></body></html>";
130 
131         final MockWebConnection conn = new MockWebConnection();
132         conn.setDefaultResponse(html, 200, "OK", "application/xhtml+xml");
133 
134         final WebClient client = getWebClient();
135         client.setWebConnection(conn);
136         final XHtmlPage page = client.getPage(URL_FIRST);
137 
138         final HtmlDivision div = page.getFirstByXPath("//xhtml:div");
139         assertEquals("http://www.w3.org/1999/xhtml", div.getNamespaceURI());
140     }
141 
142 }