com.crawljax.plugins.testcasegenerator.OfflineTestGenerator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of testcasegenerator Show documentation
Show all versions of testcasegenerator Show documentation
Generates test cases from the crawl session.
The newest version!
package com.crawljax.plugins.testcasegenerator;
import com.crawljax.browser.EmbeddedBrowser.BrowserType;
import com.crawljax.core.CrawlPathInfo;
import com.crawljax.core.configuration.BrowserConfiguration;
import com.crawljax.core.configuration.CrawljaxConfiguration;
import com.crawljax.core.configuration.CrawljaxConfiguration.CrawljaxConfigurationBuilder;
import com.crawljax.core.plugin.HostInterfaceImpl;
import com.crawljax.core.state.CrawlPath;
import com.crawljax.core.state.Eventable;
import com.crawljax.core.state.Eventable.EventType;
import com.crawljax.core.state.Identification;
import com.crawljax.core.state.Identification.How;
import com.crawljax.core.state.StateVertex;
import com.crawljax.core.state.StateVertexImpl;
import com.crawljax.plugins.crawloverview.model.Edge;
import com.crawljax.plugins.crawloverview.model.OutPutModel;
import com.crawljax.plugins.crawloverview.model.Serializer;
import com.crawljax.plugins.testcasegenerator.TestConfiguration.StateEquivalenceAssertionMode;
import com.crawljax.plugins.testcasegenerator.crawlPlugins.AddressbookCleanup;
import com.crawljax.plugins.testcasegenerator.crawlPlugins.ClarolineCleanup;
import com.crawljax.plugins.testcasegenerator.util.GsonUtils;
import com.crawljax.util.DomUtils;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.InstanceCreator;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonIOException;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.internal.LinkedTreeMap;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class OfflineTestGenerator {
public static final String JSON_STATES = TestSuiteGenerator.TEST_SUITE_PATH + "states.json";
public static final String JSON_EVENTABLES = TestSuiteGenerator.TEST_SUITE_PATH + "eventables.json";
private static final Logger LOGGER = LoggerFactory.getLogger(OfflineTestGenerator.class.getName());
private static final Logger LOG = LoggerFactory.getLogger(OfflineTestGenerator.class.getName());
// public final static class ImmutableMapDeserializer implements
// JsonDeserializer> {
// @Override
// public ImmutableMap,?> deserialize(final JsonElement json, final Type type,
// final JsonDeserializationContext context) throws JsonParseException
// {
// final Type type2 =
// ParameterizedTypeImpl.make(Map.class, ((ParameterizedType)
// type).getActualTypeArguments(), null);
// final Map,?> map = context.deserialize(json, type2);
// return ImmutableMap.copyOf(map);
// }
//
//
// }
public static boolean verifyCrawlPathInfo(List path, CrawlPathInfo pathInfo) {
try {
String pathSign =
pathInfo.getPathString().substring(pathInfo.getPathString().indexOf("_"));
String printPath = printCrawlPath(path, false)
.substring(printCrawlPath(path, false).indexOf("_"));
if (pathSign.equalsIgnoreCase(printPath)) {
return true;
}
} catch (Exception ex) {
ex.printStackTrace();
}
return false;
}
/*
* Replica of com.crawljax.core.Crawler.printCrawlPath Replicating to handle
* problem with Json Deserialization.
*/
public static String printCrawlPath(List path, boolean print) {
StringBuilder builder = new StringBuilder();
/*
* if(path instanceof CrawlPath) { addPathInfo((CrawlPath)path, builder); }
*/
String target = null;
String source = null;
for (Eventable event : path) {
try {
int sourceid = (int) Double.parseDouble(
((LinkedTreeMap, ?>) event.getEdgeSource()).get("id").toString());
source = sourceid == 0 ? "index" : "state" + sourceid;
// source = event.getSourceStateVertex().getName();
if (target != null && !source.equalsIgnoreCase(target)) {
LOG.error("Something wrong with crawlpath {} != {}", target, source);
}
builder.append(source);
builder.append("_");
builder.append(event.getId());
builder.append("_");
int targetid = (int) Double.parseDouble(
((LinkedTreeMap, ?>) event.getEdgeTarget()).get("id").toString());
target = targetid == 0 ? "index" : "state" + targetid;
builder.append(target);
} catch (Exception ex) {
LOG.error("Exception while printing crawlPath", ex.getMessage());
}
}
String pathString = builder.toString();
if (print) {
LOG.info(pathString);
}
return pathString;
}
public static Collection> getCrawlPathObjects(
Collection> crawlPaths, String absPath) {
File crawlPathsInfoJson = new File(absPath, "crawlPathsInfo.json");
Collection> returnPaths = new ArrayList<>();
Gson gson = new Gson();
Type typeOfObjectsList = new TypeToken>() {}.getType();
try {
List pathsInfo = gson.fromJson(new FileReader(crawlPathsInfoJson), typeOfObjectsList);
int index = 0;
for (List crawlPath : crawlPaths) {
if (!verifyCrawlPathInfo(crawlPath, pathsInfo.get(index))) {
System.err.println("Error building CrawlPath Object for " + crawlPath + ":" + pathsInfo.get(index));
return null;
}
CrawlPath newPath = new CrawlPath(pathsInfo.get(index).getBacktrackTarget());
newPath.addAll(crawlPath);
newPath.setBacktrackSuccess(pathsInfo.get(index).isBacktrackSuccess());
newPath.setReachedNearDup(pathsInfo.get(index).isReachedNearDup());
returnPaths.add(newPath);
index += 1;
}
} catch (JsonIOException | JsonSyntaxException | FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
return returnPaths;
}
public static String generateTestCases(
Collection> crawlPaths, OutPutModel outputModel, JsonObject configObject, String absPath) {
// TODO: Provide pathInfoMap to get the backtracking info
Collection> pathObjects = getCrawlPathObjects(crawlPaths, absPath);
if (pathObjects != null) {
crawlPaths = pathObjects;
}
List testMethods = TestSuiteGeneratorHelper.getTestMethods(crawlPaths);
HostInterfaceImpl overviewHostInterface = null;
if (overviewHostInterface == null) {
overviewHostInterface = new HostInterfaceImpl(new File(absPath), null);
}
HostInterfaceImpl testgenHostInterface = null;
if (testgenHostInterface == null) {
Map params = new HashMap();
params.put("testRecordsDir", new File(absPath, "test-results").getAbsolutePath());
testgenHostInterface = new HostInterfaceImpl(new File(absPath), params);
absPath = testgenHostInterface.getOutputDirectory().getAbsolutePath() + File.separator;
}
// GsonBuilder builder = new GsonBuilder();
// Gson gson = builder.create();
String browserType = configObject
.get("browserConfig")
.getAsJsonObject()
.get("browserType")
.getAsString();
BrowserConfiguration browserConfig = null;
switch (browserType) {
case "FIREFOX":
browserConfig = new BrowserConfiguration(BrowserType.FIREFOX);
default:
browserConfig = new BrowserConfiguration(BrowserType.CHROME);
}
// gson.fromJson(, BrowserConfiguration.class);
TestConfiguration testConfiguration = null;
if (testConfiguration == null) {
testConfiguration = new TestConfiguration(StateEquivalenceAssertionMode.BOTH, browserConfig);
}
String initialURL = configObject.get("url").getAsString();
CrawljaxConfigurationBuilder builder = CrawljaxConfiguration.builderFor(initialURL);
JsonArray pluginArray = configObject.getAsJsonArray("plugins");
for (JsonElement plugin : pluginArray) {
switch (plugin.getAsString().toLowerCase()) {
case "addressbookcleanup":
builder.addPlugin(new AddressbookCleanup());
break;
case "clarolinecleanup":
builder.addPlugin(new ClarolineCleanup());
break;
default:
break;
}
}
/*
*
* "clickOnce" : false, "randomizeCandidateElements" : false,
* "crawlHiddenAnchors" : true, "waitAfterReloadUrl" : 1000, "waitAfterEvent" :
* 1000, "crawlPriorityMode" : "NORMAL", "crawlNearDuplicates" : true,
* "delayNearDuplicateCrawling" : true,
*/
/* crawling rules. */
// builder.crawlRules().clickElementsInRandomOrder(false);
// builder.crawlRules().crawlHiddenAnchors(true);
// builder.crawlRules().crawlFrames(false);
// builder.crawlRules().clickOnce(false);
//
// builder.crawlRules().setFormFillMode(FormFillMode.RANDOM);
//
// builder.crawlRules().dontClick("a").withText("php-addressbook");
// builder.crawlRules().dontClick("a").withText("v9.0.0.1");
/* set timeouts. */
// int maximumStates = configObject.get("maximumStates").getAsInt();
// int maximumDepth = configObject.get("maximumDepth").getAsInt();
// int maximumRuntime = configObject.get("maximumRuntime").getAsInt();
//
// if(maximumDepth == 0)
// builder.setUnlimitedCrawlDepth();
// else
// builder.setMaximumDepth(maximumDepth);
// // builder.setMaximumRunTime(30, TimeUnit.MINUTES);
//
// if(maximumStates == 0)
// builder.setUnlimitedStates();
// else
// builder.setMaximumStates(maximumStates);
//
// //builder.setMaximumStates(150);
// //builder.setUnlimitedRuntime();
// builder.setMaximumRunTime(120, TimeUnit.MINUTES);
CrawljaxConfiguration config = builder.build();
int WAIT_TIME_AFTER_RELOAD = configObject
.get("crawlRules")
.getAsJsonObject()
.get("waitAfterReloadUrl")
.getAsInt();
int WAIT_TIME_AFTER_EVENT = configObject
.get("crawlRules")
.getAsJsonObject()
.get("waitAfterEvent")
.getAsInt();
builder.crawlRules().waitAfterReloadUrl(WAIT_TIME_AFTER_RELOAD, TimeUnit.MILLISECONDS);
builder.crawlRules().waitAfterEvent(WAIT_TIME_AFTER_EVENT, TimeUnit.MILLISECONDS);
try {
JavaTestGenerator generator = new JavaTestGenerator(
TestSuiteGenerator.CLASS_NAME,
initialURL,
testMethods,
config, // Config,
absPath,
// absPath+ TestSuiteGenerator.TEST_SUITE_PATH,
// overviewHostInterface.getOutputDirectory().getAbsolutePath(),
// testgenHostInterface.getParameters().get("testRecordsDir"),
testConfiguration);
// testSuiteGeneratorHelper.writeStateVertexTestDataToJSON(absPath +
// JSON_STATES);
// testSuiteGeneratorHelper.writeEventableTestDataToJSON(absPath +
// JSON_EVENTABLES);
generator.useJsonInsteadOfDB(absPath + JSON_STATES, absPath + JSON_EVENTABLES);
String generatedFileName = generator.generate(
DomUtils.addFolderSlashIfNeeded(absPath + TestSuiteGenerator.TEST_SUITE_PATH),
TestSuiteGenerator.FILE_NAME_TEMPLATE);
if (null != generatedFileName) {
generator.copyExecutionScripts(
absPath,
TestSuiteGenerator.TEST_SUITE_SRC_FOLDER,
TestSuiteGenerator.TEST_SUITE_PACKAGE_NAME,
TestSuiteGenerator.CLASS_NAME);
}
LOGGER.info("Generated : " + generatedFileName);
} catch (Exception e) {
System.out.println("Error generating testsuite: " + e.getMessage());
e.printStackTrace();
}
return null;
}
public static void main(String args[]) {
// String crawlLocation =
// "/Users/rahulkrishna/git/art_fork/art/crawljax/plugins/testcasegenerator-plugin/src/test/resources/petclinic_HYBRID_0.0_5mins/localhost/crawl0/";
// String crawlLocation =
// "/Users/rahulkrishna/git/art_fork/art/crawljax/plugins//testcasegenerator-plugin/src/test/resources/crawls/crawl1/";
// "/Users/rahulkrishna/git/art_fork/art/crawljax/plugins/testcasegenerator-plugin/src/test/resources/crawls/addressbook_HYBRID_0.0_60mins/localhost/crawl0/";
if (args.length != 1) {
LOG.info("Usage: provide absolute location of the crawl folder as argument.");
}
String crawlLocation = args[0];
// "/Users/rahulkrishna/git/tackle-test-generator-ui-level/out/crawl0";
boolean crawlPathsAvailable = false;
File crawlPathsJson = new File(crawlLocation, "CrawlPaths.json");
File resultJson = new File(crawlLocation + File.separator + "result.json");
File jsonEventables = new File(crawlLocation + File.separator + JSON_EVENTABLES);
Map mapEventables = null;
if (Files.exists(jsonEventables.toPath(), LinkOption.NOFOLLOW_LINKS)) {
try {
Gson gson = (new GsonBuilder())
.registerTypeAdapter(ImmutableMap.class, new GsonUtils.ImmutableMapDeserializer())
.registerTypeAdapter(ImmutableList.class, new GsonUtils.ImmutableListDeserializer())
.create();
mapEventables = gson.fromJson(
new BufferedReader(new FileReader(jsonEventables)),
new TypeToken