Java Code Examples for com.gargoylesoftware.htmlunit.html.HtmlAnchor#getHrefAttribute()

The following examples show how to use com.gargoylesoftware.htmlunit.html.HtmlAnchor#getHrefAttribute() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HTMLAnchorElement.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the value of this link's {@code href} property.
 * @return the value of this link's {@code href} property
 */
@JsxGetter
public String getHref() {
    final HtmlAnchor anchor = (HtmlAnchor) getDomNodeOrDie();
    final String hrefAttr = anchor.getHrefAttribute();

    if (hrefAttr == DomElement.ATTRIBUTE_NOT_DEFINED) {
        return "";
    }

    try {
        return getUrl().toString();
    }
    catch (final MalformedURLException e) {
        return hrefAttr;
    }
}
 
Example 2
Source File: HTMLAnchorElement.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Sets the {@code username} attribute.
 * @param username {@code username} attribute
 */
@JsxSetter({CHROME, FF, FF68, FF60})
public void setUsername(final String username) {
    try {
        final HtmlAnchor anchor = (HtmlAnchor) getDomNodeOrDie();
        final String href = anchor.getHrefAttribute();
        if (href == ATTRIBUTE_NOT_DEFINED) {
            return;
        }

        final URL url = ((HtmlPage) anchor.getPage()).getFullyQualifiedUrl(href);
        setUrl(UrlUtils.getUrlWithNewUserName(url, username));
    }
    catch (final MalformedURLException e) {
        // ignore
    }
}
 
Example 3
Source File: HTMLAnchorElement.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Sets the {@code password} attribute.
 * @param password {@code password} attribute
 */
@JsxSetter({CHROME, FF, FF68, FF60})
public void setPassword(final String password) {
    try {
        final HtmlAnchor anchor = (HtmlAnchor) getDomNodeOrDie();
        final String href = anchor.getHrefAttribute();
        if (href == ATTRIBUTE_NOT_DEFINED) {
            return;
        }

        final URL url = ((HtmlPage) anchor.getPage()).getFullyQualifiedUrl(href);
        setUrl(UrlUtils.getUrlWithNewUserPassword(url, password));
    }
    catch (final MalformedURLException e) {
        // ignore
    }
}
 
Example 4
Source File: HTMLAnchorElement.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the value of this link's {@code href} property.
 * @return the value of this link's {@code href} property
 */
@JsxGetter
public String getHref() {
    final HtmlAnchor anchor = (HtmlAnchor) getDomNodeOrDie();
    final String hrefAttr = anchor.getHrefAttribute();

    if (hrefAttr == DomElement.ATTRIBUTE_NOT_DEFINED) {
        return "";
    }

    try {
        return getUrl().toString();
    }
    catch (final MalformedURLException e) {
        return hrefAttr;
    }
}
 
Example 5
Source File: TvMaoCrawler.java    From MyTv with Apache License 2.0 6 votes vote down vote up
/**
 * 抓取指定城市下的所有电视台
 * 
 * @param htmlPage
 * @param city
 * @return
 */
private List<TvStation> getAllTvStationOfCity(HtmlPage htmlPage, String city) {
	List<TvStation> resultList = new ArrayList<TvStation>();
	List<?> elements = htmlPage
			.getByXPath("//div[@class='chlsnav']//div[@class='plst']/parent::*");
	for (int i = 0, size = elements == null ? 0 : elements.size(); i < size; i++) {
		try {
			HtmlAnchor anchor = (HtmlAnchor) elements.get(i);
			String href = anchor.getHrefAttribute();
			if (!href.startsWith("/program/")) {
				continue;
			}
			logger.debug(anchor.getTextContent()
					+ " program table of tvmao: " + ", url: " + href);
			TimeUnit.MILLISECONDS.sleep(getRandomSleepTime());
			HtmlPage p = (HtmlPage) WebCrawler.crawl(TV_MAO_URL_PREFIX
					+ href);
			resultList.addAll(getTvStations(p, city));
		} catch (Exception e) {
			logger.error("error occur while get all tv station of city: "
					+ city, e);
			continue;
		}
	}
	return resultList;
}
 
Example 6
Source File: HTMLAnchorElement.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the focus to this element.
 */
@Override
public void focus() {
    final HtmlAnchor anchor = (HtmlAnchor) getDomNodeOrDie();
    final String hrefAttr = anchor.getHrefAttribute();

    if (hrefAttr != DomElement.ATTRIBUTE_NOT_DEFINED) {
        anchor.focus();
    }
}
 
Example 7
Source File: HTMLAnchorElement.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the focus to this element.
 */
@Override
public void focus() {
    final HtmlAnchor anchor = (HtmlAnchor) getDomNodeOrDie();
    final String hrefAttr = anchor.getHrefAttribute();

    if (hrefAttr != DomElement.ATTRIBUTE_NOT_DEFINED) {
        anchor.focus();
    }
}
 
Example 8
Source File: TestResultLinksTest.java    From junit-plugin with MIT License 5 votes vote down vote up
@LocalData
@Test
public void testFailureLinks() throws Exception {
    FreeStyleBuild build = project.scheduleBuild2(0).get(10, TimeUnit.SECONDS);
    rule.assertBuildStatus(Result.UNSTABLE, build);

    TestResult theOverallTestResult =   build.getAction(TestResultAction.class).getResult();
    CaseResult theFailedTestCase = theOverallTestResult.getFailedTests().get(0);
    String relativePath = theFailedTestCase.getRelativePathFrom(theOverallTestResult);
    System.out.println("relative path seems to be: " + relativePath); 

    WebClient wc = rule.createWebClient();

    String testReportPageUrl =  project.getLastBuild().getUrl() + "/testReport";
    HtmlPage testReportPage = wc.goTo( testReportPageUrl );

    Page packagePage = testReportPage.getAnchorByText("tacoshack.meals").click();
    rule.assertGoodStatus(packagePage); // I expect this to work; just checking that my use of the APIs is correct.

    // Now we're on that page. We should be able to find a link to the failed test in there.
    HtmlAnchor anchor = testReportPage.getAnchorByText("tacoshack.meals.NachosTest.testBeanDip");
    String href = anchor.getHrefAttribute();
    System.out.println("link is : " + href);
    Page failureFromLink = anchor.click();
    rule.assertGoodStatus(failureFromLink);

    // Now check the >>> link -- this is harder, because we can't do the javascript click handler properly
    // The summary page is just tack on /summary to the url for the test

}