本文整理匯總了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