本文整理汇总了Java中com.gargoylesoftware.htmlunit.html.HtmlElement类的典型用法代码示例。如果您正苦于以下问题:Java HtmlElement类的具体用法?Java HtmlElement怎么用?Java HtmlElement使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
HtmlElement类属于com.gargoylesoftware.htmlunit.html包,在下文中一共展示了HtmlElement类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testConversation
import com.gargoylesoftware.htmlunit.html.HtmlElement; //导入依赖的package包/类
@Test
public void testConversation() throws Exception {
HtmlPage page;
Iterator<HtmlElement> it;
page = webClient.getPage(webUrl + "resources/converse/start");
it = page.getDocumentElement().getHtmlElementsByTagName("p").iterator();
final String secret = it.next().asText();
System.out.println(secret);
page = webClient.getPage(webUrl + "resources/converse/" +
page.getDocumentElement().getHtmlElementsByTagName("a")
.iterator().next().getAttribute("href"));
it = page.getDocumentElement().getHtmlElementsByTagName("p").iterator();
assertTrue(secret.equals(it.next().asText()));
page = webClient.getPage(webUrl + "resources/converse/stop");
it = page.getDocumentElement().getHtmlElementsByTagName("p").iterator();
assertFalse(secret.equals(it.next().asText()));
}
示例2: testFormOk
import com.gargoylesoftware.htmlunit.html.HtmlElement; //导入依赖的package包/类
/**
* Retrieve a form and submit it making sure the CSRF hidden field is present
*
* @throws Exception an error occurs or validation fails.
*/
@Test
public void testFormOk() throws Exception {
HtmlPage page1 = webClient.getPage(webUrl + "resources/csrf");
HtmlForm form = (HtmlForm) page1.getDocumentElement().getHtmlElementsByTagName("form").get(0);
// Check hidden input field
HtmlElement input = form.getHtmlElementsByTagName("input").get(1);
assertTrue(input.getAttribute("type").equals("hidden"));
assertTrue(input.getAttribute("name").equals(CSRF_PARAM));
assertTrue(input.hasAttribute("value")); // token
// Submit form
HtmlSubmitInput button = (HtmlSubmitInput) form.getHtmlElementsByTagName("input").get(0);
HtmlPage page2 = button.click();
Iterator<HtmlElement> it = page2.getDocumentElement().getHtmlElementsByTagName("h1").iterator();
assertTrue(it.next().asText().contains("CSRF Protection OK"));
}
示例3: testFormFail
import com.gargoylesoftware.htmlunit.html.HtmlElement; //导入依赖的package包/类
/**
* Retrieves a form, removes CSRF hidden field and attempts to submit. Should
* result in a 403 error.
*
* @throws Exception an error occurs or validation fails.
*/
@Test
public void testFormFail() throws Exception {
HtmlPage page1 = webClient.getPage(webUrl + "resources/csrf");
HtmlForm form = (HtmlForm) page1.getDocumentElement().getHtmlElementsByTagName("form").get(0);
// Remove hidden input field to cause a CSRF validation failure
HtmlElement input = form.getHtmlElementsByTagName("input").get(1);
form.removeChild(input);
// Submit form - should fail
HtmlSubmitInput button = (HtmlSubmitInput) form.getHtmlElementsByTagName("input").get(0);
try {
button.click();
fail("CSRF validation should have failed!");
} catch (FailingHttpStatusCodeException e) {
// falls through
}
}
示例4: setAttribute
import com.gargoylesoftware.htmlunit.html.HtmlElement; //导入依赖的package包/类
public static boolean setAttribute(HtmlPage htmlPage, String xpath,
String key, String value) {
boolean flag = true;
try {
HtmlElement element = htmlPage.getFirstByXPath(xpath);
if (element != null) {
element.click();
element.setAttribute(key, "");
element.setAttribute(key, value);
} else {
logger.error("setAttribute element is null xpath " + xpath);
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
flag = false;
}
return flag;
}
示例5: testParseGoogle
import com.gargoylesoftware.htmlunit.html.HtmlElement; //导入依赖的package包/类
@Test
public void testParseGoogle() {
String link = "https://www.google.com/finance?q=NYSE%3ALCI&fstype=ii";
String datesText = "In Millions of USD (except for per share items)";
Document d;
try (WebClient wc = new WebClient()){
HtmlPage page = wc.getPage(link);
List<DomElement> balanceSheet = page.getElementsByIdAndOrName("Balance Sheet");
HtmlElement b = page.getBody();
d = Jsoup.connect(link).get();
Element body = d.body();
Elements datesRefElts = body.getElementsMatchingOwnText(datesText);
Element elementById = body.getElementById("fs-table");
if (datesRefElts.size() > 0) {
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
示例6: extractText
import com.gargoylesoftware.htmlunit.html.HtmlElement; //导入依赖的package包/类
/**
* Extracts the article text from an article page based on the tag defined
* in the news source and saves it in the article data
*
* @param page
* the article page the title of which needs to be extracted
* @param newsSource
* the news publisher
* @param articleData
* the article data where the extracted title will be stored
*/
public void extractText(HtmlPage page, NewsSourceDataModel newsSource, ArticleDataModel articleData) {
// It can happen that the text is dispersed into
// several section of similar properties, then combine
// everything
List<?> articleBody = page.getByXPath(newsSource.getTextTag());
if (articleBody != null && articleBody.size() >= 1) {
StringBuffer textBuffer = new StringBuffer(2048);
for (int n = 0; n < articleBody.size(); n++) {
HtmlElement article = (HtmlElement) articleBody.get(n);
textBuffer.append(" " + article.asText());
}
articleData.setText(textBuffer.toString());
}
}
示例7: postValidate
import com.gargoylesoftware.htmlunit.html.HtmlElement; //导入依赖的package包/类
@Override
public void postValidate() throws Exception
{
HtmlPage page = getHtmlPage();
//Common validator
CommonValidator.getInstance().validate(page);
//Check for the UDDIBrowser Portlet
String xpathToText0 = "id('portlets-left-column')/div[@class='portlet'][1]/div[@class='header'][1]/h2[@class='title'][1]";
HtmlElement txtElement0 = HtmlPageUtils.findSingleHtmlElementByXPath(page, xpathToText0);
Assert.assertTrue("Element does not contain text 'UDDIBrowser Portlet'", txtElement0.asText().contains("UDDIBrowser Portlet"));
//Check for the UDDISearch Portlet
String xpathToText1 = "id('portlets-right-column')/div[@class='portlet'][1]/div[@class='header'][1]/h2[@class='title'][1]";
HtmlElement txtElement1 = HtmlPageUtils.findSingleHtmlElementByXPath(page, xpathToText1);
Assert.assertTrue("Element does not contain text 'UDDISearch Portlet'", txtElement1.asText().contains("UDDISearch Portlet"));
//Check for the UDDISubscriptionNotification Portlet
String xpathToText2 = "id('portlets-left-column')/div[@class='portlet'][2]/div[@class='header'][1]/h2[@class='title'][1]";
HtmlElement txtElement2 = HtmlPageUtils.findSingleHtmlElementByXPath(page, xpathToText2);
Assert.assertTrue("Element does not contain text 'UDDISubscriptionNotification Portlet'", txtElement2.asText().contains("UDDISubscriptionNotification Portlet"));
}
示例8: getHtmlUnitElement
import com.gargoylesoftware.htmlunit.html.HtmlElement; //导入依赖的package包/类
private HtmlElement getHtmlUnitElement(HtmlUnitWebElement webElement){
try {
boolean changed=false;
Field field = HtmlUnitWebElement.class.getDeclaredField("element");
if (!field.isAccessible()) {
field.setAccessible(true);
changed=true;
}
HtmlElement htmlElement = (HtmlElement)field.get(webElement);
if(changed)
field.setAccessible(false);
return htmlElement;
} catch (Exception e) {
throw new Error(e.getMessage(), e);
}
}
示例9: testWeeklyOvertimeRuleMaint
import com.gargoylesoftware.htmlunit.html.HtmlElement; //导入依赖的package包/类
@Test
public void testWeeklyOvertimeRuleMaint() throws Exception {
HtmlPage maintPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), TkTestConstants.Urls.WEEKLY_OVERTIME_RULE_MAINT_URL);
Assert.assertTrue("Maintenance Page contains test WeeklyOvertimeRule",maintPage.asText().contains(TEST_CODE));
// test Convert from EarnCodeGroup has overtime earn codes error
HtmlPage newMaintPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), TkTestConstants.Urls.WEEKLY_OVERTIME_RULE_MAINT_NEW_URL);
HtmlUnitUtil.setFieldValue(newMaintPage, "document.documentHeader.documentDescription", "Test");
HtmlUnitUtil.setFieldValue(newMaintPage, "document.newMaintainableObject.add.lstWeeklyOvertimeRules.effectiveDate", "01/01/2010");
HtmlUnitUtil.setFieldValue(newMaintPage, "document.newMaintainableObject.add.lstWeeklyOvertimeRules.maxHoursEarnGroup", "REG");
HtmlUnitUtil.setFieldValue(newMaintPage, "document.newMaintainableObject.add.lstWeeklyOvertimeRules.convertFromEarnGroup", "OVT");
HtmlUnitUtil.setFieldValue(newMaintPage, "document.newMaintainableObject.add.lstWeeklyOvertimeRules.convertToEarnCode", "RGN");
HtmlUnitUtil.setFieldValue(newMaintPage, "document.newMaintainableObject.add.lstWeeklyOvertimeRules.step", "1");
HtmlUnitUtil.setFieldValue(newMaintPage, "document.newMaintainableObject.add.lstWeeklyOvertimeRules.maxHours", "8");
// HtmlPage finalPage = HtmlUnitUtil.clickInputContainingText(newMaintPage, "add");
HtmlElement element = (HtmlElement)newMaintPage.getElementById("methodToCall.addLine.lstWeeklyOvertimeRules.(!!org.kuali.kpme.tklm.time.rules.overtime.weekly.WeeklyOvertimeRule!!)");
HtmlPage finalPage = element.click();
Assert.assertTrue("Maintenance Page should contains EarnCodeGroup has overtime earn code error",
finalPage.asText().contains("Earn Group 'OVT' has overtime earn codes."));
}
示例10: testErrorMessages
import com.gargoylesoftware.htmlunit.html.HtmlElement; //导入依赖的package包/类
@Test
public void testErrorMessages() throws Exception {
String baseUrl = TkTestConstants.Urls.TIME_OFF_MAINT_NEW_URL;
HtmlPage page = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), baseUrl);
Assert.assertNotNull(page);
HtmlForm form = page.getFormByName("KualiForm");
Assert.assertNotNull("Search form was missing from page.", form);
HtmlUnitUtil.setFieldValue(page, "document.documentHeader.documentDescription", "System Scheduled Time Off - test");
// use past dates
HtmlUnitUtil.setFieldValue(page, "document.newMaintainableObject.effectiveDate", "04/01/2011");
HtmlUnitUtil.setFieldValue(page, "document.newMaintainableObject.accruedDate", "04/01/2011");
HtmlUnitUtil.setFieldValue(page, "document.newMaintainableObject.scheduledTimeOffDate", "04/01/2011");
HtmlInput input = HtmlUnitUtil.getInputContainingText(form, "methodToCall.route");
Assert.assertNotNull("Could not locate submit button", input);
HtmlElement element = page.getElementByName("methodToCall.route");
page = element.click();
Assert.assertTrue("page text does not contain:\n" + HrTestConstants.EFFECTIVE_DATE_ERROR, page.asText().contains(HrTestConstants.EFFECTIVE_DATE_ERROR));
Assert.assertTrue("page text does not contain:\n" + ACCRUED_DATE_PAST_ERROR, page.asText().contains(ACCRUED_DATE_PAST_ERROR));
Assert.assertTrue("page text does not contain:\n" + SCHEDULED_TO_DATE_PAST_ERROR, page.asText().contains(SCHEDULED_TO_DATE_PAST_ERROR));
}
示例11: testValidateLeaveCode
import com.gargoylesoftware.htmlunit.html.HtmlElement; //导入依赖的package包/类
@Test
//test for jiar1363
public void testValidateLeaveCode() throws Exception {
String baseUrl = TkTestConstants.Urls.TIME_OFF_MAINT_NEW_URL;
HtmlPage page = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), baseUrl);
Assert.assertNotNull(page);
HtmlForm form = page.getFormByName("KualiForm");
Assert.assertNotNull("Search form was missing from page.", form);
// add 150 days in the future, need to add dates instead of month
// because if we happen to be running the test on the 31 of a month, some future months do not have 31st
LocalDate validDate = LocalDate.now().plusDays(150);
String validDateString = TKUtils.formatDate(validDate);
HtmlUnitUtil.setFieldValue(page, "document.newMaintainableObject.effectiveDate", validDateString);
HtmlUnitUtil.setFieldValue(page, "document.newMaintainableObject.earnCode", "testLCL");
page = ((HtmlElement)page.getElementByName("methodToCall.route")).click();
HtmlUnitUtil.createTempFile(page);
Assert.assertTrue("page text does not contain:\n" + ERROR_LEAVE_CODE, page.asText().contains(ERROR_LEAVE_CODE));
}
示例12: testRequiredFields
import com.gargoylesoftware.htmlunit.html.HtmlElement; //导入依赖的package包/类
@Test
public void testRequiredFields() throws Exception {
String baseUrl = TkTestConstants.Urls.LEAVE_ADJUSTMENT_MAINT_NEW_URL;
HtmlPage page = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), baseUrl);
Assert.assertNotNull(page);
HtmlForm form = page.getFormByName("KualiForm");
Assert.assertNotNull("Search form was missing from page.", form);
HtmlInput input = HtmlUnitUtil.getInputContainingText(form, "methodToCall.route");
Assert.assertNotNull("Could not locate submit button", input);
HtmlElement element = page.getElementByName("methodToCall.route");
page = element.click();
Assert.assertTrue("page text does not contain:\n" + EFFECTIVE_DATE_REQUIRED, page.asText().contains(EFFECTIVE_DATE_REQUIRED));
Assert.assertTrue("page text does not contain:\n" + PRINCIPAL_ID_REQUIRED, page.asText().contains(PRINCIPAL_ID_REQUIRED));
Assert.assertTrue("page text does not contain:\n" + LEAVE_PLAN_REQUIRED, page.asText().contains(LEAVE_PLAN_REQUIRED));
Assert.assertTrue("page text does not contain:\n" + ACCRUAL_CATEGORY_REQUIRED, page.asText().contains(ACCRUAL_CATEGORY_REQUIRED));
Assert.assertTrue("page text does not contain:\n" + EARN_CODE_REQUIRED, page.asText().contains(EARN_CODE_REQUIRED));
Assert.assertTrue("page text does not contain:\n" + DES_REQUIRED, page.asText().contains(DES_REQUIRED));
HtmlUnitUtil.setFieldValue(page, "document.newMaintainableObject.adjustmentAmount", "");
element = page.getElementByName("methodToCall.route");
page = element.click();
Assert.assertTrue("page text does not contain:\n" + ADJUSTMENT_AMOUNT_REQUIRED, page.asText().contains(ADJUSTMENT_AMOUNT_REQUIRED));
}
示例13: testRequiredFields
import com.gargoylesoftware.htmlunit.html.HtmlElement; //导入依赖的package包/类
@Test
public void testRequiredFields() throws Exception {
String baseUrl = HrTestConstants.Urls.INSTITUTION_MAINT_NEW_URL;
HtmlPage page = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), baseUrl);
Assert.assertNotNull(page);
HtmlForm form = page.getFormByName("KualiForm");
Assert.assertNotNull("Search form was missing from page.", form);
HtmlInput input = HtmlUnitUtil.getInputContainingText(form, "methodToCall.route");
Assert.assertNotNull("Could not locate submit button", input);
HtmlElement element = page.getElementByName("methodToCall.route");
page = element.click();
Assert.assertTrue("page text does not contain:\n" + "Effective Date (Effective Date) is a required field.",
page.asText().contains("Effective Date (Effective Date) is a required field."));
Assert.assertTrue("page text does not contain:\n" + "Institution Code (Institution Code) is a required field.",
page.asText().contains("Institution Code (Institution Code) is a required field."));
}
示例14: testValidationOfLeavePlan
import com.gargoylesoftware.htmlunit.html.HtmlElement; //导入依赖的package包/类
@Test
public void testValidationOfLeavePlan() throws Exception {
HtmlPage accrualCategoryLookup = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), HrTestConstants.Urls.ACCRUAL_CATEGORY_MAINT_URL);
accrualCategoryLookup = HtmlUnitUtil.clickInputContainingText(accrualCategoryLookup, "search");
Assert.assertTrue("Page contains test Accrual Category", accrualCategoryLookup.asText().contains(ACCRUAL_CATEGORY));
HtmlPage maintPage = HtmlUnitUtil.clickAnchorContainingText(accrualCategoryLookup, "edit", "lmAccrualCategoryId=3000");
Assert.assertTrue("Maintenance Page contains test AccrualCategory",maintPage.asText().contains(ACCRUAL_CATEGORY));
HtmlForm form = maintPage.getFormByName("KualiForm");
Assert.assertNotNull("Search form was missing from page.", maintPage);
HtmlInput inputForDescription = HtmlUnitUtil.getInputContainingText(maintPage, "* Document Description");
inputForDescription.setValueAttribute("Test_KPME1355");
HtmlUnitUtil.setFieldValue(maintPage, "document.newMaintainableObject.leavePlan", "IU-SM-W");
maintPage = ((HtmlElement)maintPage.getElementByName("methodToCall.route")).click();
HtmlUnitUtil.createTempFile(maintPage);
Assert.assertTrue("page text does not contain:\n" + ERROR_LEAVE_PLAN, maintPage.asText().contains(ERROR_LEAVE_PLAN));
}
示例15: getUserNameFromUserId
import com.gargoylesoftware.htmlunit.html.HtmlElement; //导入依赖的package包/类
public String getUserNameFromUserId(int userId) {
try {
HtmlPage page = manager
.getPage("http://www.roblox.com/User.aspx?ID=" + userId);
HtmlElement avatarImg = (HtmlElement) page
.getElementById("ctl00_cphRoblox_rbxUserPane_AvatarImage");
if (page.getUrl().equals(
"http://www.roblox.com/Error/DoesntExist.aspx")) {
System.out.println("Username does not exist.");
} else {
manager.closeAllWindows();
return avatarImg.getAttribute("title").toString();
}
} catch (Exception e) {
System.out.println("Error getting user page");
}
manager.closeAllWindows();
return "";
}