2017-01-07 94 views
-2

我是新来的htmlunit(2.23),我不能得到这个测试的工作: 我得到这个ClassCastException抛出HtmlUnit,我不知道这是一个错误,或者如果我在做有问题。这个ClassCastException是一个HtmlUnit错误吗?

java.lang.ClassCastException:com.gargoylesoftware.htmlunit.TextPage不能在com.gargoylesoftware.htmlunit.WebClient.makeWebResponseForJavaScriptUrl被强制转换为com.gargoylesoftware.htmlunit.html.HtmlPage (WebClient.java:1241) (WebClient.getPage(WebClient.getPage))。 java:451) at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:436) at org.wyttenbach.dale.mlec.OutageTest.test(OutageTest.java:46) ...

代码

import java.awt.Desktop; 
import java.io.IOException; 
import java.net.MalformedURLException; 
import java.net.URI; 
import java.util.ArrayList; 
import java.util.Collection; 
import java.util.HashMap; 
import java.util.LinkedList; 
import java.util.List; 
import java.util.Map; 

import org.junit.Assert; 
import org.junit.Test; 

import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException; 
import com.gargoylesoftware.htmlunit.JavaScriptPage; 
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController; 
import com.gargoylesoftware.htmlunit.Page; 
import com.gargoylesoftware.htmlunit.TextPage; 
import com.gargoylesoftware.htmlunit.WebClient; 
import com.gargoylesoftware.htmlunit.html.DomElement; 
import com.gargoylesoftware.htmlunit.html.HtmlPage; 

public class OutageTest { 

    private static final String SITE_URL = "https://ebill.mlecmn.net/woViewer/"; 

    private static final String OUTAGE_MAP_URL = SITE_URL + "mapviewer.html?config=Outage+Web+Map"; 

    @Test 
    public void test() throws FailingHttpStatusCodeException, MalformedURLException, IOException { 
    try (final WebClient webClient = new WebClient()) { 
     webClient.waitForBackgroundJavaScript(20000); 
     webClient.setAjaxController(new NicelyResynchronizingAjaxController()); 
     webClient.getOptions().setUseInsecureSSL(true); 
     Map<String, Page> urls = new HashMap<String, Page>(); 
     LinkedList<String> urlsToVisit = new LinkedList<String>(); 
     urlsToVisit.add(OUTAGE_MAP_URL); 
     while (!urlsToVisit.isEmpty()) { 
     String url = urlsToVisit.remove(); 
     if (urls.containsKey(url)) { 
      continue; 
     } 
     Page page = webClient.getPage(url); 
     urls.put(url, page); 
     if (page instanceof HtmlPage) { 
      HtmlPage page2 = (HtmlPage) page; 
      System.err.println("================================================================"); 
      System.err.println(page2.asXml()); 
      System.err.println("================================================================"); 
      Assert.assertFalse("Outage in Nordland township: " + url, page2.asText().contains("Nordland")); 
      urlsToVisit.addAll(extractLinks(page2)); 
     } else if (page instanceof JavaScriptPage) { 
      JavaScriptPage page2 = (JavaScriptPage) page; 
      Assert.assertFalse("Outage in Nordland township: " + url, page2.getContent().contains("Nordland")); 
     } else if (page instanceof TextPage) { 
      TextPage page2 = (TextPage) page; 
      Assert.assertFalse("Outage in Nordland township: " + url, page2.getContent().contains("Nordland")); 
     } else { 
      System.err.println(String.format("%s => %s", url, page.getClass().getName())); 
     } 
     } 
    } catch (AssertionError e) { 
     reportOutage(); 
     throw e; 
    } 
    } 

    private Collection<String> extractLinks(HtmlPage page) { 
    List<String> links = new ArrayList<String>(); 
    for (DomElement x : page.getElementsByTagName("script")) { 
     String src = x.getAttribute("src"); 
     if (!src.contains(":")) { 
     src = SITE_URL + src; 
     System.err.println("script src="+src); 
     } 
     links.add(src); 
    } 
    for (DomElement x : page.getElementsByTagName("link")) { 
     String href = x.getAttribute("href"); 
     if (!href.contains(":")) { 
     href = SITE_URL + href; 
     System.err.println("link href="+href); 
     } 
     links.add(href); 
    } 
    // Causes ClassCastException com.gargoylesoftware.htmlunit.TextPage cannot be cast to com.gargoylesoftware.htmlunit.html.HtmlPage 
    //at com.gargoylesoftware.htmlunit.WebClient.makeWebResponseForJavaScriptUrl(WebClient.java:1241) 
    for (DomElement x : page.getElementsByTagName("iframe")) { 
     String src = x.getAttribute("src"); 
     if (!src.contains(":")) { 
     src = SITE_URL + src; 
     System.err.println("iframe src="+src); 
     } 
     links.add(src); 
    } 
    return links; 
    } 

    private void reportOutage() { 
    try { 
     Desktop.getDesktop().browse(new URI(OUTAGE_MAP_URL)); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    } 
} 
+2

咦?当然,如果你有一个异常,你的代码不工作。请澄清。 – OldProgrammer

+0

考虑创建一个[_Minimal_,Complete,Verifiable Example](http://stackoverflow.com/help/mcve)。 – qxz

+0

其意图是,如果在网页或其链接页面的任何位置发现单词“Nordland”,则测试应该失败...但我不太了解如何使用htmlunit知道如何正确实施extractLinks方法,或者如果这甚至是可能的。而且我相当肯定我并没有详尽地搜索所有的链接,而例外就是这样的一个障碍。 – wytten

回答

1

或多或少是的 - 但我必须做更深入的分析。 但有一些希望;-)

您的代码尝试从给定的网页中提取网址。在此过程中,您要将网址“javascript:”“'添加到您的网址列表中以便进行处理。此网址导致此类转换异常。如果你不把这个URL添加到列表中,测试正在工作(至少对我而言)。