/*
* Created on Mar 12, 2006
*
*/
package org.openqa.selenium.server.testgenerator;
import java.io.*;
import java.util.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
/**
* Given an HTML file containing a Selenese test case, generate equivalent Java code w/ calls
* to the Selenium object to execute that same test case.
*
* @author nsproul
*
*/
public class XlateHtmlSeleneseToJava {
static Set generatedJavaClassNames = new HashSet();
private static Map funcTypes = null;
static final String BEGIN_SELENESE = ">>>>>";
static final String END_SELENESE = "<<<<<";
static final String SELENESE_TOKEN_DIVIDER = "//////";
static HashMap declaredVariables = new HashMap();
private static int varNameSeed = 1;
private static String EOL = "\n\t\t";
private static int timeOut = 30000;
private static String domain;
private static boolean silentMode = false;
private static boolean dontThrowOnTranslationDifficulties = false;
public static void main(String[] args) throws IOException {
boolean generateSuite = false;
if (args.length < 2) {
Usage("too few args");
return;
}
HashMap skipList = new HashMap();
String javaSeleneseFileDirectoryName = args[0];
for (int j = 1; j < args.length; j++) {
if (args[j].equals("-silent")) {
silentMode = true;
}
else if (args[j].equals("-skip")) {
skipList.put(args[++j], Boolean.TRUE);
}
else if (args[j].equals("-dontThrowOnTranslationDifficulties")) {
dontThrowOnTranslationDifficulties = true;
}
else if (args[j].equals("-suite")) {
generateSuite = true;
}
else if (args[j].equals("-dir")) {
String dirName = args[++j];
File dir = new File(dirName);
if (!dir.isDirectory()) {
Usage("-dir is not a directory: " + dirName);
}
String children[] = dir.list();
for (int k = 0; k < children.length; k++) {
String fileName = children[k];
if (!skipList.containsKey(fileName) && fileName.indexOf(".htm")!=-1 && fileName.indexOf("Suite")==-1) {
generateJavaClassFromSeleneseHtml(dirName + "/" + fileName, javaSeleneseFileDirectoryName);
}
}
}
else {
String htmlSeleneseFileName = args[j];
generateJavaClassFromSeleneseHtml(htmlSeleneseFileName, javaSeleneseFileDirectoryName);
}
}
if (generateSuite) {
generateSuite(javaSeleneseFileDirectoryName);
}
}
private static void initializeFuncTypes() {
if (funcTypes != null) return;
funcTypes = new HashMap();
InputStream stream = XlateHtmlSeleneseToJava.class.getResourceAsStream("/core/iedoc.xml");
if (stream==null) {
throw new RuntimeException("could not find /core/iedoc.xml on the class path");
}
try {
Document d = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(stream);
NodeList functions = d.getElementsByTagName("function");
for (int i = 0; i < functions.getLength(); i++) {
Element function = (Element) functions.item(i);
String funcName = function.getAttribute("name");
NodeList returnElements = function.getElementsByTagName("return");
if (returnElements.getLength() == 0) {
funcTypes.put(funcName, void.class);
} else {
Element ret = (Element) returnElements.item(0);
String retType = ret.getAttribute("type");
if ("boolean".equals(retType)) {
funcTypes.put(funcName, boolean.class);
} else if ("string".equals(retType)) {
funcTypes.put(funcName, String.class);
}
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private static boolean isBoolean(String op) {
return (boolean.class.equals(getOpType(op)));
}
private static Class getOpType(String op) {
initializeFuncTypes();
if (funcTypes.get(op) != null) {
return (Class) funcTypes.get(op);
}
String swappedOp = op.replaceFirst("store", "get");
if (funcTypes.get(swappedOp) != null) {
return (Class) funcTypes.get(swappedOp);
}
swappedOp = op.replaceFirst("store", "is");
if (funcTypes.get(swappedOp) != null) {
return (Class) funcTypes.get(swappedOp);
}
// if we get here, apparently op has no direct analog in Selenium. So just look at the name and guess:
if (op.matches(".*(Present|Visible|Editable)$")) {
return boolean.class;
}
return String.class;
}
private static void generateSuite(String javaSeleneseFileDirectoryName) throws IOException {
if (generatedJavaClassNames.size()==1) {
return; // this is a test run focusing on a single file, so a suite wouldn't be useful
}
String beginning = "package com.thoughtworks.selenium.corebased;\n" +
"\n" +
"import junit.framework.Test;\n" +
"import junit.framework.TestSuite;\n" +
"\n" +
"public class SeleneseSuite {\n" +
" static public Test suite() {\n" +
" TestSuite suite = new TestSuite();\n";
String ending = " return suite;\n" +
" }\n" +
"}\n";
StringBuffer middle = new StringBuffer();
Iterator i = generatedJavaClassNames.iterator();
while (i.hasNext()) {
String generatedJavaClassName = (String) i.next();
if (!generatedJavaClassName.equals("TestJavascriptParameters")){
middle.append(" suite.addTestSuite(")
.append(generatedJavaClassName)
.append(".class);\n");
}
}
WriteFileContents(beginning + middle + ending, openFile(javaSeleneseFileDirectoryName + "/SeleneseSuite.java"));
}
private static void generateJavaClassFromSeleneseHtml(String htmlSeleneseFileName,
String javaSeleneseFileDirectoryName) throws IOException {
String base = htmlSeleneseFileName;
base = base.replaceAll(".*/", "");
base = base.replaceAll("\\.html?$", "");
String javaSeleneseFileName = javaSeleneseFileDirectoryName + "/" + base + ".java";
generatedJavaClassNames.add(base);
System.out.println("Generating test class " + base + " from\t"
+ htmlSeleneseFileName + "...");
File htmlSeleneseFile = openFile(htmlSeleneseFileName);
File javaSeleneseFile = openFile(javaSeleneseFileName);
try {
javaSeleneseFile.createNewFile();
} catch (IOException e) {
Usage(e.toString());
}
if (!htmlSeleneseFile.canRead()) {
Usage("can't read " + htmlSeleneseFileName);
}
if (!javaSeleneseFile.canWrite()) {
Usage("can't write " + javaSeleneseFileName);
}
String htmlSelenese = ReadFileContents(htmlSeleneseFile);
String javaSelenese = XlateString(base, htmlSeleneseFileName, htmlSelenese);
WriteFileContents(javaSelenese, javaSeleneseFile);
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
selenium-remote-control-0.8.0.zip_Selenium web 测试_remote_seleniu (744个子文件)
GenerateNAntSchema.build 392B
Changes 3KB
Selenium.chm 129KB
.classpath 540B
.classpath 233B
NAnt.exe.config 47KB
nunit-gui.exe.config 3KB
nunit-console.exe.config 3KB
nunit.tests.dll.config 3KB
mock-assembly.dll.config 2KB
DefaultSelenium.cs 40KB
ISelenium.cs 30KB
HttpCommandProcessor.cs 10KB
DefaultSeleniumTest.cs 4KB
ICommandProcessor.cs 4KB
DefaultSeleneseCommand.cs 3KB
DefaultSelenesCommandTest.cs 2KB
I18nTest.cs 2KB
SeleniumIntegrationTest.cs 2KB
GoogleTest.cs 1KB
SeleniumException.cs 1KB
ISeleneseCommand.cs 878B
SingleEntryBlockingQueueTest.cs 813B
CommonAssemblyInfo.cs 656B
AssemblyInfo.cs 360B
AssemblyInfo.cs 359B
AssemblyInfo.cs 352B
IntegrationTests.csproj 5KB
Core.csproj 5KB
UnitTests.csproj 5KB
MSDN.css 8KB
epydoc.css 8KB
rdoc-style.css 4KB
stylesheet.css 1KB
stylesheet.css 1KB
tree.css 1KB
NDoc.Documenter.NativeHtmlHelp2.dll 448KB
NDoc.Documenter.Msdn2.dll 432KB
NAnt.Core.dll 344KB
NDoc.Documenter.Msdn.dll 324KB
NDoc.Documenter.Msdn.dll 324KB
NDoc.Documenter.Msdn.dll 324KB
NDoc.Documenter.Msdn.dll 324KB
NDoc.Documenter.Msdn.dll 311KB
NDoc.Documenter.Msdn.dll 311KB
NDoc.Documenter.LinearHtml.dll 260KB
NDoc.Core.dll 204KB
NDoc.Core.dll 204KB
NDoc.Core.dll 204KB
NDoc.Core.dll 204KB
log4net.dll 200KB
nunit.tests.dll 184KB
NDoc.Core.dll 171KB
NDoc.Core.dll 171KB
ICSharpCode.SharpCvsLib.dll 148KB
nunit.uikit.dll 140KB
NAnt.VSNetTasks.dll 132KB
NDoc.Documenter.NAnt.dll 128KB
ICSharpCode.SharpZipLib.dll 124KB
NAnt.DotNetTasks.dll 104KB
ICSharpCode.SharpCvsLib.Console.dll 96KB
nunit.util.dll 84KB
nunit.util.dll 84KB
nunit.util.dll 84KB
nunit.util.dll 80KB
NDoc.Documenter.JavaDoc.dll 76KB
nunit.core.dll 60KB
nunit.core.dll 60KB
nunit.core.dll 60KB
nunit.core.dll 60KB
nunit.core.dll 60KB
nunit.util.dll 57KB
nunit.util.dll 57KB
NDoc.Test.dll 48KB
nmock.dll 48KB
nunit.core.dll 45KB
nunit.core.dll 45KB
NUnitCore.dll 40KB
NAnt.SourceControlTasks.dll 40KB
NDoc.Documenter.Latex.dll 36KB
NAnt.NUnit1Tasks.dll 36KB
NAnt.VisualCppTasks.dll 36KB
NAnt.NUnit2Tasks.dll 32KB
NAnt.CompressionTasks.dll 32KB
ThoughtWorks.Selenium.Core.dll 28KB
NDoc.ExtendedUI.dll 28KB
NDoc.ExtendedUI.dll 28KB
nunit.framework.dll 28KB
NAnt.Win32Tasks.dll 28KB
nunit.framework.dll 28KB
nunit.framework.dll 28KB
nunit.framework.dll 28KB
NDoc.ExtendedUI.dll 28KB
nunit.framework.dll 28KB
NDoc.ExtendedUI.dll 28KB
nunit.mocks.dll 20KB
nunit.framework.dll 17KB
NDoc.ExtendedUI.dll 17KB
nunit.framework.dll 17KB
NDoc.ExtendedUI.dll 17KB
共 744 条
- 1
- 2
- 3
- 4
- 5
- 6
- 8
资源评论
Kinonoyomeo
- 粉丝: 72
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功