diff --git a/src/main/java/org/bench4q/master/scriptrecord/httpcapture/generator/AbstractCodeGenerator.java b/src/main/java/org/bench4q/master/scriptrecord/httpcapture/generator/AbstractCodeGenerator.java index cc8d311f..82471592 100644 --- a/src/main/java/org/bench4q/master/scriptrecord/httpcapture/generator/AbstractCodeGenerator.java +++ b/src/main/java/org/bench4q/master/scriptrecord/httpcapture/generator/AbstractCodeGenerator.java @@ -347,7 +347,7 @@ public abstract class AbstractCodeGenerator implements IScriptGenerator, if (contentType.toLowerCase().compareTo("text/html") == 0) { doTidyCode(HttpTestCase.staticUrlDecode(header.url)); - doParseHtmlContent(respStr); + doParseHtmlContent(respStr, header.url); if (cpRspToStdout) doResponseForStdOut(HttpTestCase.staticUrlDecode( header.url).trim()); @@ -425,7 +425,7 @@ public abstract class AbstractCodeGenerator implements IScriptGenerator, public abstract void doTidyCode(String paramString) throws Utils.UserException; - public abstract void doParseHtmlContent(String responseContent); + public abstract void doParseHtmlContent(String responseContent, String url); public abstract void doHeaders(HeaderValue[] paramArrayOfHeaderValue); diff --git a/src/main/java/org/bench4q/master/scriptrecord/httpcapture/generator/Bench4qCodeGenerator.java b/src/main/java/org/bench4q/master/scriptrecord/httpcapture/generator/Bench4qCodeGenerator.java index 98a3dfb9..65d0ed0c 100644 --- a/src/main/java/org/bench4q/master/scriptrecord/httpcapture/generator/Bench4qCodeGenerator.java +++ b/src/main/java/org/bench4q/master/scriptrecord/httpcapture/generator/Bench4qCodeGenerator.java @@ -16,6 +16,7 @@ import org.bench4q.share.models.agent.scriptrecord.UsePluginModel; import org.bench4q.share.models.agent.scriptrecord.UserBehaviorModel; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; import org.jsoup.select.Elements; public class Bench4qCodeGenerator extends AbstractCodeGenerator { @@ -46,9 +47,9 @@ public class Bench4qCodeGenerator extends AbstractCodeGenerator { private int unit = 1; private static Logger logger = Logger.getLogger(Bench4qCodeGenerator.class); // FOR TEST - public Elements linkElements; - public Elements imgElements; - public Elements scriptElements; + public Elements links; + public Elements medias; + public Elements imports; private static String escapeXmlString(String str) { for (int i = 0; i < escapes.length; ++i) @@ -202,7 +203,7 @@ public class Bench4qCodeGenerator extends AbstractCodeGenerator { } @Override - public void doParseHtmlContent(String responseContent) { + public void doParseHtmlContent(String responseContent, String url) { // Fulfill it here, extract the dom and static resources int htmlStart = responseContent.indexOf(""); int htmlEnd = responseContent.indexOf(""); @@ -211,12 +212,21 @@ public class Bench4qCodeGenerator extends AbstractCodeGenerator { } String htmlContent = responseContent.substring(htmlStart, htmlEnd + 8); Document document = Jsoup.parse(htmlContent); - Elements linkElements = document.getElementsByTag("link"); - Elements imgElements = document.getElementsByTag("img"); - Elements scriptElements = document.getElementsByTag("script"); - this.linkElements = linkElements; - this.imgElements = imgElements; - this.scriptElements = scriptElements; + document.setBaseUri(url); + Elements links = document.select("a[href]"); + Elements medias = document.select("[src]"); + Elements imports = document.select("link[href]"); + this.links = links; + this.medias = medias; + this.imports = imports; + for (Element element : medias) { + String childAbsoluteUrl = element.absUrl("src"); + this.getScriptAdapter().getChildrenUrls() + .add(ChildrenUrl.createChilldUrl(childAbsoluteUrl, 0)); + System.out.println(childAbsoluteUrl); + } + + // this.getScriptAdapter().getChildrenUrls().add() } public void doResponseForStdOut(String url) { diff --git a/src/main/java/org/bench4q/master/scriptrecord/httpcapture/generator/ChildrenUrl.java b/src/main/java/org/bench4q/master/scriptrecord/httpcapture/generator/ChildrenUrl.java index 02b4a6e2..bfa18b19 100644 --- a/src/main/java/org/bench4q/master/scriptrecord/httpcapture/generator/ChildrenUrl.java +++ b/src/main/java/org/bench4q/master/scriptrecord/httpcapture/generator/ChildrenUrl.java @@ -20,7 +20,7 @@ public class ChildrenUrl { this.parentBatchId = parentBatchId; } - public ChildrenUrl createChilldUrl(String url, int parentBatchId) { + public static ChildrenUrl createChilldUrl(String url, int parentBatchId) { ChildrenUrl ret = new ChildrenUrl(); ret.setParentBatchId(parentBatchId); ret.setUrl(url); diff --git a/src/test/java/org/bench4q/master/test/recordscript/TestDomGenerator.java b/src/test/java/org/bench4q/master/test/recordscript/TestDomGenerator.java index 446da9ad..7d38fc25 100644 --- a/src/test/java/org/bench4q/master/test/recordscript/TestDomGenerator.java +++ b/src/test/java/org/bench4q/master/test/recordscript/TestDomGenerator.java @@ -23,14 +23,18 @@ public class TestDomGenerator { @Test public void testDomParser() throws IOException { URL url = this.getClass().getResource("testcase.html"); - System.out.println(url.getPath()); this.getCodeGenerator().doParseHtmlContent( - FileUtils.readFileToString(new File(url.getPath()))); - assertNotNull(this.getCodeGenerator().imgElements); - assertNotNull(this.getCodeGenerator().linkElements); - assertNotNull(this.getCodeGenerator().scriptElements); - assertTrue(this.getCodeGenerator().imgElements.size() == 3); - assertTrue(this.getCodeGenerator().linkElements.size() == 3); - assertTrue(this.getCodeGenerator().scriptElements.size() == 2); + FileUtils.readFileToString(new File(url.getPath())), + "http://localhost:8080/Bench4QTestCase/testcase.html"); + assertNotNull(this.getCodeGenerator().medias); + assertNotNull(this.getCodeGenerator().links); + assertNotNull(this.getCodeGenerator().imports); + assertTrue(this.getCodeGenerator().medias.size() == 5); + assertTrue(this.getCodeGenerator().links.size() == 0); + assertTrue(this.getCodeGenerator().imports.size() == 3); + + assertTrue(this.getCodeGenerator().getScriptAdapter().getChildrenUrls() + .size() == 5); } + }