@Override public void run() { String str; // 创建一个webclient WebClient webClient = new WebClient(); // htmlunit 对css和javascript的支持不好,所以请关闭之 webClient.getOptions().setJavaScriptEnabled(false); webClient.getOptions().setCssEnabled(false); Cookie cookie = new Cookie( "www.qixin.com", "login_returnurl", "http%3A//www.qixin.com/search/prov/SH%3Fpage%3D2"); Cookie cookie1 = new Cookie( "www.qixin.com", "userKey", "QXBAdmin-Web2.0_5tUrhr/6EVtLT+GVfE+vU8k330y+oPICCM6jhUGEoLc%3D"); Cookie cookie2 = new Cookie("www.qixin.com", "userValue", "4a68111b-0cfa-457f-91bd-b6fda97fa524"); Cookie cookie3 = new Cookie( "www.qixin.com", "gr_session_id_955c17a7426f3e98", "d25fe84e-fb1d-4ef8-8b4e-b530e5004b30"); Cookie cookie4 = new Cookie("www.qixin.com", "_alicdn_sec", "5732cf53d99e48a838049be355d47a44000895ae"); CookieManager cookieManager = new CookieManager(); cookieManager.addCookie(cookie); cookieManager.addCookie(cookie2); cookieManager.addCookie(cookie3); cookieManager.addCookie(cookie1); cookieManager.addCookie(cookie4); webClient.setCookieManager(cookieManager); // 获取页面 HtmlPage page = null; try { page = webClient.getPage("http://www.qixin.com/search/prov/SH?page=20"); } catch (IOException e) { e.printStackTrace(); } // 获取页面的XML代码 List<HtmlAnchor> hbList = (List<HtmlAnchor>) page.getByXPath("//a"); Iterator iterator = hbList.iterator(); while (iterator.hasNext()) { HtmlAnchor ha = (HtmlAnchor) iterator.next(); if ("search-result-title".equals(ha.getAttribute("class"))) { System.out.println(ha.asText()); System.out.println("http://www.qixin.com" + ha.getAttribute("href")); } } // 关闭webclient webClient.close(); }
@Test(dependsOnMethods = {"testLogout"}) public void testSaveRequestWithFallbackUri() throws Exception { CookieManager cookieManager = webClient.getCookieManager(); cookieManager.clearCookies(); boolean original = cookieManager.isCookiesEnabled(); cookieManager.setCookiesEnabled(false); clickOnBasePage("about"); assertLoginPage(); loginAction(); assertTrue( getLocation().startsWith(BASEURI + "index"), "Request wasn't redirected to the default success url"); // note that all cookies are disabled so we don't have a session either and we are logged out in // practice cookieManager.setCookiesEnabled(original); }
@Test(groups = {"notLoggedIn"}) public void testInterceptComponentMethodWithAjaxDeny() throws Exception { CookieManager cookieManager = webClient.getCookieManager(); cookieManager.clearCookies(); clickOnBasePage("componentMethodInterceptorWithAjax"); // this executes window.location.replace so we have to wait for it. is there an event we could // listen instead? webClient.waitForBackgroundJavaScript(500); page = (HtmlPage) webClient.getCurrentWindow().getEnclosedPage(); assertLoginPage(); for (Cookie cookie : cookieManager.getCookies()) if (cookie.getName().equals("shiroSavedRequest")) { // test we've stored a page render request, not the component event request. Could also just // test for existence of a dot assertEquals(cookie.getPath() + "/", APP_CONTEXT); return; } fail(); }