com.seleniumtests.util.ide.SeleniumIdeParser Maven / Gradle / Ivy
package com.seleniumtests.util.ide;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import com.github.javaparser.JavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.expr.AnnotationExpr;
import com.github.javaparser.ast.stmt.BlockStmt;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
import com.seleniumtests.core.SeleniumTestsContext;
import com.seleniumtests.util.logging.SeleniumRobotLogger;
public class SeleniumIdeParser {
private static final Logger logger = SeleniumRobotLogger.getLogger(SeleniumIdeParser.class);
private StringBuilder testCode;
private StringBuilder webPageCode;
private File javaFile;
private String className;
public static final String PAGE_OBJECT_HEADER = "package com.infotel.selenium.ide;\n" +
"\n" +
"import java.io.IOException;\n" +
"\n" +
"import com.seleniumtests.uipage.PageObject;\n" +
"import org.openqa.selenium.JavascriptExecutor;\n" +
"import static org.testng.Assert.*;\n" +
"import static org.hamcrest.MatcherAssert.*;\n" +
"import static org.hamcrest.CoreMatchers.is;\n" +
"import static org.hamcrest.core.IsNot.not;\n" +
"import org.openqa.selenium.By;\n" +
"import org.openqa.selenium.Dimension;\n" +
"import org.openqa.selenium.WebElement;\n" +
"import org.openqa.selenium.interactions.Actions;\n" +
"import org.openqa.selenium.support.ui.ExpectedConditions;\n" +
"import org.openqa.selenium.support.ui.WebDriverWait;\n" +
"import org.openqa.selenium.JavascriptExecutor;\n" +
"import org.openqa.selenium.Alert;\n" +
"import org.openqa.selenium.Keys;\n" +
"import com.seleniumtests.core.TestVariable;\n" +
"import java.util.Map.Entry;\n" +
"import java.util.*;\n" +
"\n" +
"public class %sPage extends PageObject {\n" +
"\n" +
" private Map vars;\n" +
" private JavascriptExecutor js;\n" +
"\n" +
" public %sPage() throws IOException {\n" +
" super();\n" +
" js = (JavascriptExecutor) driver;\n" +
" vars = new HashMap();\n" +
" for (Entry entry: robotConfig().getConfiguration().entrySet()) {\n" +
" vars.put(entry.getKey(), entry.getValue().getValue());\n" +
" }\n" +
" }\n";
private static final String FOOTER = "}";
public static final String TEST_HEADER = "package com.infotel.selenium.ide;\n" +
"\n" +
"import java.io.IOException;\n" +
"import com.seleniumtests.core.runner.SeleniumTestPlan;\n" +
"import org.testng.annotations.Test;\n" +
"\n" +
"public class %s extends SeleniumTestPlan {\n\n";
public SeleniumIdeParser(String filePath) {
javaFile = new File(filePath);
className = javaFile.getName().replace(".java", "");
testCode = new StringBuilder(String.format(TEST_HEADER, className));
webPageCode = new StringBuilder(String.format(PAGE_OBJECT_HEADER, className, className));
}
/**
* Do some pre-computing to help execution
* - transform "CALL:" operation to java call
* - in the CALL line, replace escaped quotes by quote itself
*/
private void prepareJavaFile(File javaFile) {
Pattern pattern = Pattern.compile(".*System.out.println\\(\"CALL:(.*)\"\\);$");
try {
StringBuilder newContent = new StringBuilder();
String content = FileUtils.readFileToString(javaFile, StandardCharsets.UTF_8);
for (String line: content.split("\n")) {
line = line.replace("\r", "");
Matcher matcher = pattern.matcher(line);
if (matcher.matches()) {
newContent.append(matcher.group(1).replace("\\\"", "\"") + "\n");
} else {
newContent.append(line + "\n");
}
}
FileUtils.writeStringToFile(javaFile, newContent.toString(), StandardCharsets.UTF_8);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Map parseSeleniumIdeFile() throws FileNotFoundException {
prepareJavaFile(javaFile);
Map classInfo = new HashMap<>();
// parse the file
CompilationUnit cu = JavaParser.parse(javaFile);
cu.accept(new TestMethodVisitor(), new StringBuilder[] {testCode, webPageCode});
webPageCode.append(FOOTER);
testCode.append(FOOTER);
classInfo.put("com.infotel.selenium.ide." + className, testCode.toString().replace("new WebPage().", String.format("new %sPage().", className)));
classInfo.put("com.infotel.selenium.ide." + className + "Page", webPageCode.toString());
logger.info(String.format("generated class %s", className));
logger.info("\n" + testCode.toString());
logger.info("------------------------------------------");
logger.info(String.format("generated class %sPage", className));
logger.info("\n" + webPageCode.toString());
return classInfo;
}
private static class TestMethodVisitor extends VoidVisitorAdapter {
@Override
public void visit(MethodDeclaration n, StringBuilder[] codes) {
// only keep test code
Optional beforeAnnotation = n.getAnnotationByName("Before");
Optional afterAnnotation = n.getAnnotationByName("After");
if (beforeAnnotation.isPresent() || afterAnnotation.isPresent()) {
return;
}
StringBuilder tCode = codes[0];
StringBuilder pageCode = codes[1];
// code is always copied to PageObject
pageCode.append(n.getDeclarationAsString());
Optional optBody = n.getBody();
if (optBody.isPresent()) {
String body = optBody.get().toString().replace("\r", "");
for (String line: body.split("\n")) {
if (line.contains("System.out.println(\"STEP:") && "true".equals(System.getProperty(SeleniumTestsContext.MANUAL_TEST_STEPS))) {
pageCode.append(line.replace("System.out.println(\"STEP:", "addStep(\"") + "\n");
} else if (line.contains("System.out.println(")) {
pageCode.append(line.replace("System.out.println(", "logger.info(") + "\n");
} else {
pageCode.append(line + "\n");
}
}
pageCode.append("\n\n");
// in case of a Test method, create a reference to PageObject method
Optional testAnnotation = n.getAnnotationByName("Test");
if (testAnnotation.isPresent()) {
tCode.append(" @Test\n");
tCode.append(String.format(" %s throws IOException {\n", n.getDeclarationAsString()));
tCode.append(String.format(" new WebPage().%s();\n", n.getNameAsString()));
tCode.append(" }\n\n");
}
}
// TODO: steps
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy