下面列出了org.jsoup.nodes.Comment#org.jsoup.nodes.FormElement 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Test fetching a form, and submitting it with a file attached.
*/
@Test
public void postHtmlFile() throws IOException {
Document index = Jsoup.connect("http://direct.infohound.net/tidy/").get();
FormElement form = index.select("[name=tidy]").forms().get(0);
Connection post = form.submit();
File uploadFile = ParseTest.getFile("/htmltests/google-ipod.html");
FileInputStream stream = new FileInputStream(uploadFile);
Connection.KeyVal fileData = post.data("_file");
fileData.value("check.html");
fileData.inputStream(stream);
Connection.Response res;
try {
res = post.execute();
} finally {
stream.close();
}
Document out = res.parse();
assertTrue(out.text().contains("HTML Tidy Complete"));
}
/**
* Test fetching a form, and submitting it with a file attached.
*/
@Test
public void postHtmlFile() throws IOException {
Document index = Jsoup.connect("http://direct.infohound.net/tidy/").get();
FormElement form = index.select("[name=tidy]").forms().get(0);
Connection post = form.submit();
File uploadFile = ParseTest.getFile("/htmltests/google-ipod.html");
FileInputStream stream = new FileInputStream(uploadFile);
Connection.KeyVal fileData = post.data("_file");
fileData.value("check.html");
fileData.inputStream(stream);
Connection.Response res;
try {
res = post.execute();
} finally {
stream.close();
}
Document out = res.parse();
assertTrue(out.text().contains("HTML Tidy Complete"));
}
/**
* Test fetching a form, and submitting it with a file attached.
*/
@Test
public void postHtmlFile() throws IOException {
Document index = Jsoup.connect("http://direct.infohound.net/tidy/").get();
FormElement form = index.select("[name=tidy]").forms().get(0);
Connection post = form.submit();
File uploadFile = ParseTest.getFile("/htmltests/google-ipod.html");
FileInputStream stream = new FileInputStream(uploadFile);
Connection.KeyVal fileData = post.data("_file");
fileData.value("check.html");
fileData.inputStream(stream);
Connection.Response res;
try {
res = post.execute();
} finally {
stream.close();
}
Document out = res.parse();
assertTrue(out.text().contains("HTML Tidy Complete"));
}
FormElement insertForm(Token.StartTag startTag, boolean onStack) {
Tag tag = Tag.valueOf(startTag.name(), settings);
FormElement el = new FormElement(tag, baseUri, startTag.attributes);
setFormElement(el);
insertNode(el);
if (onStack)
stack.add(el);
return el;
}
@Test public void createsFormElements() {
String html = "<body><form><input id=1><input id=2></form></body>";
Document doc = Jsoup.parse(html);
Element el = doc.select("form").first();
assertTrue("Is form element", el instanceof FormElement);
FormElement form = (FormElement) el;
Elements controls = form.elements();
assertEquals(2, controls.size());
assertEquals("1", controls.get(0).id());
assertEquals("2", controls.get(1).id());
}
@Test public void associatedFormControlsWithDisjointForms() {
// form gets closed, isn't parent of controls
String html = "<table><tr><form><input type=hidden id=1><td><input type=text id=2></td><tr></table>";
Document doc = Jsoup.parse(html);
Element el = doc.select("form").first();
assertTrue("Is form element", el instanceof FormElement);
FormElement form = (FormElement) el;
Elements controls = form.elements();
assertEquals(2, controls.size());
assertEquals("1", controls.get(0).id());
assertEquals("2", controls.get(1).id());
assertEquals("<table><tbody><tr><form></form><input type=\"hidden\" id=\"1\"><td><input type=\"text\" id=\"2\"></td></tr><tr></tr></tbody></table>", TextUtil.stripNewlines(doc.body().html()));
}
FormElement insertForm(Token.StartTag startTag, boolean onStack) {
Tag tag = Tag.valueOf(startTag.name(), settings);
FormElement el = new FormElement(tag, baseUri, startTag.attributes);
setFormElement(el);
insertNode(el);
if (onStack)
stack.add(el);
return el;
}
@Test public void createsFormElements() {
String html = "<body><form><input id=1><input id=2></form></body>";
Document doc = Jsoup.parse(html);
Element el = doc.select("form").first();
assertTrue("Is form element", el instanceof FormElement);
FormElement form = (FormElement) el;
Elements controls = form.elements();
assertEquals(2, controls.size());
assertEquals("1", controls.get(0).id());
assertEquals("2", controls.get(1).id());
}
@Test public void associatedFormControlsWithDisjointForms() {
// form gets closed, isn't parent of controls
String html = "<table><tr><form><input type=hidden id=1><td><input type=text id=2></td><tr></table>";
Document doc = Jsoup.parse(html);
Element el = doc.select("form").first();
assertTrue("Is form element", el instanceof FormElement);
FormElement form = (FormElement) el;
Elements controls = form.elements();
assertEquals(2, controls.size());
assertEquals("1", controls.get(0).id());
assertEquals("2", controls.get(1).id());
assertEquals("<table><tbody><tr><form></form><input type=\"hidden\" id=\"1\"><td><input type=\"text\" id=\"2\"></td></tr><tr></tr></tbody></table>", TextUtil.stripNewlines(doc.body().html()));
}
FormElement getFormElement() {
return formElement;
}
void setFormElement(FormElement formElement) {
this.formElement = formElement;
}
FormElement getFormElement() {
return formElement;
}
void setFormElement(FormElement formElement) {
this.formElement = formElement;
}