View Javadoc
1   /*
2    * Copyright (c) 2002-2024 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.HttpMethod;
21  import org.htmlunit.MockWebConnection;
22  import org.htmlunit.Page;
23  import org.htmlunit.SimpleWebTestCase;
24  import org.htmlunit.junit.BrowserRunner;
25  import org.htmlunit.util.NameValuePair;
26  import org.junit.Test;
27  import org.junit.runner.RunWith;
28  
29  /**
30   * Tests for {@link HtmlIsIndex}.
31   *
32   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
33   * @author Marc Guillemot
34   * @author Ahmed Ashour
35   */
36  @RunWith(BrowserRunner.class)
37  public class HtmlIsIndexTest extends SimpleWebTestCase {
38  
39      /**
40       * @throws Exception if the test fails
41       */
42      @Test
43      public void formSubmission() throws Exception {
44          final String html
45              = "<html><head><title>foo</title></head><body>\n"
46                  + "<form id='form1' method='post'>\n"
47                  + "  <isindex prompt='enterSomeText'></isindex>\n"
48                  + "  <input type='submit' id='clickMe'>\n"
49                  + "</form></body></html>";
50          final HtmlPage page = loadPage(html);
51          final MockWebConnection webConnection = getMockConnection(page);
52  
53          final HtmlForm form = page.getHtmlElementById("form1");
54  
55          final HtmlElement element = form.getElementsByAttribute(
56                  "isindex", "prompt", "enterSomeText").get(0);
57          if (element instanceof HtmlIsIndex) {
58              final HtmlIsIndex isInput = (HtmlIsIndex) element;
59              isInput.setValue("Flintstone");
60              final Page secondPage = page.getHtmlElementById("clickMe").click();
61  
62              final List<NameValuePair> expectedParameters = new ArrayList<>();
63              expectedParameters.add(new NameValuePair("enterSomeText", "Flintstone"));
64  
65              assertEquals("url", URL_FIRST, secondPage.getUrl());
66              assertSame("method", HttpMethod.POST, webConnection.getLastMethod());
67              assertEquals("parameters", expectedParameters, webConnection.getLastParameters());
68          }
69      }
70  }