本文整理汇总了Java中com.gargoylesoftware.htmlunit.html.HtmlPage.getFormByName方法的典型用法代码示例。如果您正苦于以下问题:Java HtmlPage.getFormByName方法的具体用法?Java HtmlPage.getFormByName怎么用?Java HtmlPage.getFormByName使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.gargoylesoftware.htmlunit.html.HtmlPage
的用法示例。
在下文中一共展示了HtmlPage.getFormByName方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: searchInBaidu
import com.gargoylesoftware.htmlunit.html.HtmlPage; //导入方法依赖的package包/类
public void searchInBaidu() throws Exception {
HtmlPage page = webClient.getPage("https://www.baidu.com/");
HtmlForm form = page.getFormByName("f");
HtmlTextInput input = form.getInputByName("wd");
HtmlSubmitInput button = form.getInputByValue("百度一下");
input.setValueAttribute("无锡");
HtmlPage nextPage = button.click();
//System.out.println(nextPage.asXml());
// hit next page
HtmlAnchor next = null;
List list = nextPage.getByXPath("//a");
for(Object obj : list) {
if(obj instanceof HtmlAnchor) {
HtmlAnchor ha = (HtmlAnchor)obj;
//System.out.println(ha.getTextContent());
if(ha.getTextContent().indexOf("百度百科") != -1) {
next = ha;
break;
}
}
}
System.out.println(next.asXml());
System.out.println("--------------------------");
HtmlPage p = next.click();
System.out.println(p.asXml());
}
示例2: download
import com.gargoylesoftware.htmlunit.html.HtmlPage; //导入方法依赖的package包/类
/**
* return the html of url
* @param url : form data
* @param inputs : input name -> input value
* @return
*/
public String download(URL url, Map<String, String> inputs) {
HtmlSubmitInput button = null;
HtmlPage nextPage = null;
try {
HtmlPage page = webClient.getPage(url.getURLValue());
for(Map.Entry<String, String> input : inputs.entrySet()) {
String form_name = input.getKey().split("\\.")[0];
String input_name = input.getKey().split("\\.")[1];
HtmlForm form = page.getFormByName(form_name);
if(input_name.equals("button")) {
button = form.getInputByValue(input.getValue());
}
else {
HtmlTextInput text_input = form.getInputByName(input_name);
text_input.setValueAttribute(input.getValue());
}
}
nextPage = button.click();
} catch(IOException ioe) {
ioe.printStackTrace();
}
return nextPage.asXml();
}
示例3: testEnglishValidationMessage
import com.gargoylesoftware.htmlunit.html.HtmlPage; //导入方法依赖的package包/类
@Test
public void testEnglishValidationMessage() throws Exception {
HtmlPage page1 = webClient.getPage(webUrl + "resources/validation?lang=en");
HtmlForm form1 = page1.getFormByName("form");
form1.getInputByName("name").setValueAttribute("foo");
HtmlPage page2 = form1.getInputByName("button").click();
assertThat(page2.getWebResponse().getContentAsString(),
CoreMatchers.containsString("size must be between 5 and 10"));
}
示例4: testGermanValidationMessage
import com.gargoylesoftware.htmlunit.html.HtmlPage; //导入方法依赖的package包/类
@Test
public void testGermanValidationMessage() throws Exception {
HtmlPage page1 = webClient.getPage(webUrl + "resources/validation?lang=de");
HtmlForm form1 = page1.getFormByName("form");
form1.getInputByName("name").setValueAttribute("foo");
HtmlPage page2 = form1.getInputByName("button").click();
assertThat(page2.getWebResponse().getContentAsString(),
CoreMatchers.containsString("muss zwischen 5 und 10 liegen"));
}
示例5: testFrenchValidationMessage
import com.gargoylesoftware.htmlunit.html.HtmlPage; //导入方法依赖的package包/类
@Test
public void testFrenchValidationMessage() throws Exception {
HtmlPage page1 = webClient.getPage(webUrl + "resources/validation?lang=fr");
HtmlForm form1 = page1.getFormByName("form");
form1.getInputByName("name").setValueAttribute("foo");
HtmlPage page2 = form1.getInputByName("button").click();
assertThat(page2.getWebResponse().getContentAsString(),
CoreMatchers.containsString("entre 5 et 10"));
}
示例6: test
import com.gargoylesoftware.htmlunit.html.HtmlPage; //导入方法依赖的package包/类
@Test
public void test() throws Exception {
HtmlPage unauthenticatedPage = webClient.getPage(base + "/");
logCurrentPage();
assertThat(unauthenticatedPage.getTitleText(), equalTo("Unauthenticated page"));
assertCookiesPresent(false, false, false);
HtmlPage loginPage = unauthenticatedPage.getAnchorByName("authenticatedPageLink").click();
logCurrentPage();
assertThat(loginPage.getTitleText(), equalTo("Login Page"));
assertCookiesPresent(false, true, true);
assertThat(getSavedRequestUrl(), equalTo(base + "/authenticated"));
String preLoginCsrfToken = getCsrfToken();
HtmlForm loginForm = loginPage.getFormByName("f");
loginForm.getInputByName("username").setValueAttribute("user");
loginForm.getInputByName("password").setValueAttribute("password");
HtmlPage authenticatedPage = loginForm.getInputByName("submit").click();
logCurrentPage();
assertThat(authenticatedPage.getTitleText(), equalTo("Authenticated page"));
assertCookiesPresent(true, false, true);
String postLoginCsrfToken = getCsrfToken();
assertThat(preLoginCsrfToken, not(equalTo(postLoginCsrfToken)));
HtmlForm logoutForm = authenticatedPage.getFormByName("logoutForm");
HtmlPage pageAfterLogout = logoutForm.getInputByName("logout").click();
logCurrentPage();
assertThat(pageAfterLogout.getTitleText(), equalTo("Unauthenticated page"));
assertCookiesPresent(false, false, false);
}
示例7: getNextUrl
import com.gargoylesoftware.htmlunit.html.HtmlPage; //导入方法依赖的package包/类
/**
* 通过htmlunit来获得一些搜狗的网址。
* @param key
* @return
* @throws Exception
*/
public String getNextUrl(String key){
String page = new String();
try {
WebClient webClient = new WebClient();
webClient.getOptions().setCssEnabled(false);
webClient.getOptions().setJavaScriptEnabled(false);
//去拿网页
HtmlPage htmlPage = webClient.getPage("http://pic.sogou.com/");
//得到表单
HtmlForm form = htmlPage.getFormByName("searchForm");
//得到提交按钮
HtmlSubmitInput button = form.getInputByValue("搜狗搜索");
//得到输入框
HtmlTextInput textField = form.getInputByName("query");
//输入内容
textField.setValueAttribute(key);
//点一下按钮
HtmlPage nextPage = button.click();
String str = nextPage.toString();
page = cutString(str);
webClient.close();
} catch (Exception e) {
// TODO: handle exception
} finally{
}
System.out.println();
return page;
}
示例8: testCategoryOperations
import com.gargoylesoftware.htmlunit.html.HtmlPage; //导入方法依赖的package包/类
@Test
public void testCategoryOperations() throws Exception {
logger.info("start insert category");
HtmlPage adminPage = webClient.getPage(base + "views/admin/index.xhtml");
HtmlAnchor button = adminPage.getAnchors().get(2);
HtmlPage page = button.click();
HtmlForm addCategory = page.getFormByName("addCategoryForm");
String categoryToInsert = "ccccc";
addCategory.getInputByName("addCategoryForm:Category").setValueAttribute(categoryToInsert);
HtmlSubmitInput submitButton = addCategory.getInputByName("addCategoryForm:editinline");
page = submitButton.click();
assertTrue("The category is created", page.asText().contains(categoryToInsert));
HtmlAnchor adminPanel = adminPage.getAnchors().get(1);
page = adminPanel.click();
button = page.getAnchors().get(6);
page = button.click();
HtmlForm editCategory = page.getForms().get(4);
String categoryToUpdate = "aaaaaa";
String firstCategory = "Dummy demo category";
editCategory.getInputByValue(categoryToInsert).setValueAttribute(categoryToUpdate);
submitButton = editCategory.getInputByValue(resourceBundle.getString("Update"));
page = submitButton.click();
assertTrue("The category is updated", page.asText().contains(categoryToUpdate));
button = page.getAnchors().get(7);
page = button.click();
assertTrue("The category is arrowed up",
page.asText().indexOf(categoryToUpdate) < page.asText().indexOf(firstCategory));
button = page.getAnchors().get(4);
page = button.click();
assertTrue("The category is arrowed down",
page.asText().indexOf(categoryToUpdate) > page.asText().indexOf(firstCategory));
button = page.getAnchors().get(8);
page = button.click();
HtmlForm deleteCategory = page.getForms().get(1);
submitButton = deleteCategory.getInputByValue(resourceBundle.getString("Cancel"));
page = submitButton.click();
assertTrue("The delete is canceled",
page.asText().contains(firstCategory) && page.asText().contains(categoryToUpdate));
button = page.getAnchors().get(0);
page = button.click();
assertTrue("In the home page there are two categories",
page.asText().contains(firstCategory) && page.asText().contains(categoryToUpdate));
button = page.getAnchors().get(1);
page = button.click();
button = page.getAnchors().get(8);
page = button.click();
deleteCategory = page.getForms().get(1);
submitButton = deleteCategory.getInputByValue(resourceBundle.getString("Confirm"));
page = submitButton.click();
assertTrue("The delete is confirmed", page.asText()
.contains("\"" + categoryToUpdate + "\" " + resourceBundle.getString("Category_deleted_1")));
}
开发者ID:PacktPublishing,项目名称:Mastering-Java-EE-Development-with-WildFly,代码行数:53,代码来源:ApplicationTestCase.java