1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.html;
16
17 import java.io.File;
18 import java.io.InputStream;
19 import java.net.URL;
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.Collections;
23 import java.util.List;
24
25 import org.apache.commons.io.FileUtils;
26 import org.apache.commons.io.IOUtils;
27 import org.htmlunit.MockWebConnection;
28 import org.htmlunit.SimpleWebTestCase;
29 import org.htmlunit.junit.annotation.Alerts;
30 import org.htmlunit.util.NameValuePair;
31 import org.junit.jupiter.api.Test;
32
33
34
35
36
37
38
39
40
41 public class HtmlImageInput2Test extends SimpleWebTestCase {
42
43
44
45
46 @Test
47 @Alerts({"button.x#100", "button.y#200"})
48 public void click_WithPosition() throws Exception {
49 final String html = DOCTYPE_HTML
50 + "<html><head><title>foo</title></head><body>\n"
51 + "<form id='form1' method='post'>\n"
52 + "<input type='image' name='aButton' value='foo'/>\n"
53 + "<input type='image' name='button' value='foo'/>\n"
54 + "<input type='image' name='anotherButton' value='foo'/>\n"
55 + "</form></body></html>";
56 final HtmlPage page = loadPage(html);
57 final MockWebConnection webConnection = getMockConnection(page);
58
59 final HtmlForm form = page.getHtmlElementById("form1");
60
61 final HtmlImageInput imageInput = form.getInputByName("button");
62 final HtmlPage secondPage = imageInput.click(100, 200);
63 assertNotNull(secondPage);
64
65 final List<String> params = new ArrayList<>();
66 for (final NameValuePair nameValuePair : webConnection.getLastParameters()) {
67 params.add(nameValuePair.getName() + "#" + nameValuePair.getValue());
68 }
69 assertEquals(getExpectedAlerts(), params);
70 }
71
72
73
74
75
76
77 @Test
78 public void noNameClick_WithPosition() throws Exception {
79 final String html = DOCTYPE_HTML
80 + "<html><head><title>foo</title></head><body>\n"
81 + "<form id='form1' method='post'>\n"
82 + "<input type='image' value='foo'/>\n"
83 + "</form></body></html>";
84 final HtmlPage page = loadPageWithAlerts(html);
85 final MockWebConnection webConnection = getMockConnection(page);
86
87 final HtmlForm form = page.getHtmlElementById("form1");
88
89 final HtmlImageInput imageInput = form.getInputByValue("foo");
90 final HtmlPage secondPage = imageInput.click(100, 200);
91 assertNotNull(secondPage);
92
93 final List<NameValuePair> expectedPairs = Arrays.asList(new NameValuePair[]{
94 new NameValuePair("x", "100"),
95 new NameValuePair("y", "200")
96 });
97
98 assertEquals(expectedPairs, webConnection.getLastParameters());
99 }
100
101
102
103
104 @Test
105 public void saveAs() throws Exception {
106 try (InputStream is = getClass().getClassLoader().
107 getResourceAsStream("testfiles/tiny-jpg.img")) {
108 final byte[] directBytes = IOUtils.toByteArray(is);
109 final URL urlImage = new URL(URL_FIRST, "img.jpg");
110 getMockWebConnection().setResponse(urlImage, directBytes, 200, "ok", "image/jpg", Collections.emptyList());
111 }
112
113 final String html = DOCTYPE_HTML
114 + "<html><head>\n"
115 + "</head>\n"
116 + "<body>\n"
117 + " <input type='image' src='img.jpg' >\n"
118 + "</body></html>";
119
120 final HtmlPage page = loadPage(html);
121
122 final HtmlImageInput input = page.querySelector("input");
123 final File tempFile = File.createTempFile("img", ".tmp");
124 input.saveAs(tempFile);
125 FileUtils.deleteQuietly(tempFile);
126 }
127
128
129
130
131 @Test
132 @Alerts({"4x7.jpg", "§§URL§§4x7.jpg"})
133 public void src() throws Exception {
134 final String html = DOCTYPE_HTML
135 + "<html>\n"
136 + "<head></head>\n"
137 + "<body>\n"
138 + " <input type='image' id='myImage' src='4x7.jpg' >\n"
139 + "</body></html>";
140
141 final HtmlPage page = loadPage(html);
142 final HtmlImageInput img = page.getHtmlElementById("myImage");
143
144 expandExpectedAlertsVariables(URL_FIRST);
145 assertEquals(getExpectedAlerts()[0], img.getSrcAttribute());
146 assertEquals(getExpectedAlerts()[1], img.getSrc());
147 }
148 }