now, i can get the static resources' absolute uri.

Next step, i will create appropriate childrenUrl,
and put it into the ScriptAdapter.
The key point is to get the parent's batch id.
This commit is contained in:
Tienan Chen 2013-12-05 17:29:24 +08:00
parent cfe5512763
commit d2784c7f7d
4 changed files with 35 additions and 21 deletions

View File

@ -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);

View File

@ -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("<html>");
int htmlEnd = responseContent.indexOf("</html>");
@ -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) {

View File

@ -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);

View File

@ -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);
}
}