Skip to content

Commit

Permalink
add java code
Browse files Browse the repository at this point in the history
  • Loading branch information
ali-lion committed Nov 17, 2020
1 parent e6d7a21 commit 17dd198
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 12 deletions.
14 changes: 6 additions & 8 deletions chrome-extension/js/foreground.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,19 +196,17 @@
var tempTestPath = '[data-testid="'+testidValue+'"]';
var tempIdPath = '#'+idValue;
var tempTextPath = `//${tagName}[text()="${textValue}"]`;
if(testidValue && checkUniqueSelector(rootNode, tempTestPath)){
return tempTestPath;
if(textValue && !reTextValueBlack.test(textValue) && textValue.length <= 50 && checkUniqueXPath(rootNode, tempTextPath, isAllDom)){
// text定位
return tempTextPath;
}
// 检查目标元素自身是否有唯一id
else if(idValue && reAttrValueBlack.test(idValue) === false && checkUniqueSelector(rootNode, tempIdPath, isAllDom)){
// id定位
return tempIdPath;
}
else if(textValue && !reTextValueBlack.test(textValue) && textValue.length <= 50 && checkUniqueXPath(rootNode, tempTextPath, isAllDom)){
// text定位
return tempTextPath;
}
else if(tagName === 'input'){
} else if(testidValue && checkUniqueSelector(rootNode, tempTestPath)){
return tempTestPath;
}else if(tagName === 'input'){
// 表单项特殊校验
tempPath = nameValue ? tagName + '[name="'+nameValue+'"]' : tagName;
if(valueValue){
Expand Down
147 changes: 143 additions & 4 deletions lib/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -1496,8 +1496,114 @@ function startRecorder(options){
i18n: __('chrome')
};
startRecorderServer(recorderConfig, onReady, onCommand, onEnd);
function saveTestFile(){
if(allCaseCount > 0){
function getArrRawCmdsTarget(arrRawCmdsTarget){
if (browserSize) {
arrRawCmdsTarget[0] = 'Configuration.browserSize = "' + browserSize[0] + 'x' + browserSize[1] + '";';
} else {
arrRawCmdsTarget[0] = 'Configuration.startMaximized=true;';
}
for (jj = 0, len = arrRawCmds.length; jj < len; jj++) {
// console.log('arrRawCmds[jj].type:'+arrRawCmds[jj].type);
switch (arrRawCmds[jj].type) {
case 'url':
arrRawCmds[jj].data = arrRawCmds[jj].data.replace(/\"/g, "'");
arrRawCmdsTarget[jj + 1] = 'actions.openUrl("' + arrRawCmds[jj].data + '");'+'//打开URL';
console.log('lion arrRawCmdsTarget[' + jj + 1 + ']:' + arrRawCmdsTarget[jj + 1]);
break;
case 'dblClick':
arrRawCmds[jj].data.path = arrRawCmds[jj].data.path.replace(/\"/g, "'");
arrRawCmdsTarget[jj + 1] = 'actions.click("' + arrRawCmds[jj].data.path + '");'+'//点击:'+arrRawCmds[jj].data.text;
console.log('lion arrRawCmdsTarget[' + jj + 1 + ']:' + arrRawCmdsTarget[jj + 1]);
break;
case 'click':
arrRawCmds[jj].data.path = arrRawCmds[jj].data.path.replace(/\"/g, "'");
if (arrRawCmds[jj].data.path.indexOf('radio') != -1 || arrRawCmds[jj].data.path.indexOf('checkbox') != -1 || arrRawCmds[jj].data.path.indexOf('combobox') != -1) {
arrRawCmdsTarget[jj + 1] = 'actions.clickByJS("' + arrRawCmds[jj].data.path + '");'+'//点击:'+arrRawCmds[jj].data.text;
} else {
arrRawCmdsTarget[jj + 1] = 'actions.click("' + arrRawCmds[jj].data.path + '");'+'//点击:'+arrRawCmds[jj].data.text;
}
console.log('lion arrRawCmdsTarget[' + jj + 1 + ']:' + arrRawCmdsTarget[jj + 1]);
break;
case 'switchWindow':
//arrRawCmds[jj].path = arrRawCmds[jj].data.replace(/\"/g, "'");
arrRawCmdsTarget[jj + 1] = 'actions.swtichToWindow(' + arrRawCmds[jj].data + ');'+'//切换窗口';
console.log('lion arrRawCmdsTarget[' + jj + 1 + ']:' + arrRawCmdsTarget[jj + 1]);
break;
case 'sendKeys':
// arrRawCmds[jj - 1].data.path = arrRawCmds[jj - 1].data.path.replace(/\"/g, "'");
// arrRawCmdsTarget[jj + 1] = 'actions.input("' + arrRawCmds[jj - 1].data.path + '","' + arrRawCmds[jj].data.keys + '");'+'//文本框'+arrRawCmds[jj - 1].data.text+'输入:'+arrRawCmds[jj].data.keys;
arrRawCmdsTarget[jj + 1] = 'actions.sendKeys("' + arrRawCmds[jj].data.keys + '");'+'//输入:'+arrRawCmds[jj].data.keys;
console.log('lion arrRawCmdsTarget[' + jj + 1 + ']:' + arrRawCmdsTarget[jj + 1]);
break;
case 'waitBody':
arrRawCmdsTarget[jj + 1] = 'actions.sleep(3000);';
console.log('lion arrRawCmdsTarget[' + jj + 1 + ']:' + arrRawCmdsTarget[jj + 1])+'//waitBody等待页面加载完成';
break;
// case 'switchFrame':
// arrRawCmds[jj].data = arrRawCmds[jj].data.replace(/\"/g,"'");
// arrRawCmdsTarget[jj]='actions.switchToFrame("'+arrRawCmds[jj].data+'");';
// console.log('lion arrRawCmdsTarget['+jj+']:'+arrRawCmdsTarget[jj]);
// break;
case 'switchFrame':
if (null == arrRawCmds[jj].data) {
arrRawCmdsTarget[jj + 1] = 'actions.switchToDefaultContent();'+'//退出frame';
} else {
arrRawCmds[jj].data = arrRawCmds[jj].data.replace(/#/g, "");
arrRawCmdsTarget[jj + 1] = 'actions.switchToFrame("' + arrRawCmds[jj].data + '");'+'//切换frame';
}
console.log('lion arrRawCmdsTarget[' + jj + 1 + ']:' + arrRawCmdsTarget[jj + 1]);
break;
case 'scrollTo':
arrRawCmdsTarget[jj + 1] = 'actions.scrollTo(' + arrRawCmds[jj].data.x + ',' + arrRawCmds[jj].data.y + ');'+'//滚动页面';
console.log('lion scrollTo[' + jj + 1 + ']:' + arrRawCmdsTarget[jj + 1]);
break;
case 'scrollElementTo':
arrRawCmdsTarget[jj + 1] = 'actions.scrollElementTo("' + arrRawCmds[jj].data.path + '",' + arrRawCmds[jj].data.x + ',' + arrRawCmds[jj].data.y + ');'+'//滚动元素';
console.log('lion scrollTo[' + jj + 1 + ']:' + arrRawCmdsTarget[jj + 1]);
break;
case 'closeWindow':
arrRawCmdsTarget[jj + 1] = 'actions.closeCurrentWindow();'+'//关闭当前窗口'
console.log('lion scrollTo[' + jj + 1 + ']:' + arrRawCmdsTarget[jj + 1]);
break;
case 'sleep':
arrRawCmdsTarget[jj + 1] = 'actions.sleep(' + arrRawCmds[jj].data + ');'+'//sleep';
console.log('lion scrollTo[' + jj + 1 + ']:' + arrRawCmdsTarget[jj + 1]);
break;
case 'mouseMove':
arrRawCmdsTarget[jj + 1] = 'actions.hover("' + arrRawCmds[jj].data.path + '");'+'//鼠标hover:'+arrRawCmds[jj].data.text;
console.log('lion scrollTo[' + jj + 1 + ']:' + arrRawCmdsTarget[jj + 1]);
break;
case 'expect':
switch (arrRawCmds[jj].data.type) {
case 'displayed' :
arrRawCmds[jj].data.params[0] = arrRawCmds[jj].data.params[0].replace(/\"/g, "'");
if ((arrRawCmds[jj].data.compare == 'equal') && (arrRawCmds[jj].data.to == 'true')) {
arrRawCmdsTarget[jj + 1] = 'Assert.assertTrue(actions.waitElementFound("' + arrRawCmds[jj].data.params[0] + '"));'+'//校验元素存在'
console.log('lion scrollTo[' + jj + 1 + ']:' + arrRawCmdsTarget[jj + 1]);
} else {
arrRawCmdsTarget[jj + 1] = 'Assert.assertFalse(actions.waitElementDisapear("' + arrRawCmds[jj].data.params[0] + '"));'+'//校验元素不存在'
console.log('lion scrollTo[' + jj + 1 + ']:' + arrRawCmdsTarget[jj + 1]);
}
break;
case 'text' :
arrRawCmds[jj].data.params[0] = arrRawCmds[jj].data.params[0].replace(/\"/g, "'");
if (arrRawCmds[jj].data.compare == 'equal') {
arrRawCmdsTarget[jj + 1] = 'Assert.assertTrue(actions.getText("' + arrRawCmds[jj].data.params[0] + '").equals("' + arrRawCmds[jj].data.to + '"));'+'//校验元素的文本值满足期望'
console.log('lion scrollTo[' + jj + 1 + ']:' + arrRawCmdsTarget[jj + 1]);
} else {
arrRawCmdsTarget[jj + 1] = 'Assert.assertFalse(actions.getText("' + arrRawCmds[jj].data.params[0] + '").equals("' + arrRawCmds[jj].data.to + '"));'+'//校验元素的文本值不满足期望'
console.log('lion scrollTo[' + jj + 1 + ']:' + arrRawCmdsTarget[jj + 1]);
}
break;
}
break;
}
}
return arrRawCmdsTarget;
}

function saveTestFile() {
if (allCaseCount > 0) {
var testFile = path.resolve(rootPath + '/' + fileName);
arrTestCodes = arrTestCodes.map(function(line){
return line?' '+ line:'';
Expand All @@ -1508,8 +1614,19 @@ function startRecorder(options){
return arrTestCodes.join('\r\n') + '\r\n' + all;
});
fs.writeFileSync(testFile, testContent);
}
else{

// console.log('lion sss arrRawCmds: ' + JSON.stringify(arrRawCmds));
// console.log('@@生成java代码开始@@');
// var arrRawCmdsTarget = [];
// arrRawCmdsTarget = getArrRawCmdsTarget(arrRawCmdsTarget);
// var javaContent = getJavaTemplateContent();
// javaContent = javaContent.replace(/\/\/java code ending/,arrRawCmdsTarget.join('\r\n'+' '));
// console.log('lion filename222: ' + rootPath + '/' + fileName + 'java');
// var testJavaFile = path.resolve(rootPath + '/' + 'JavaUITest.java');
// mkdirs(path.dirname(testJavaFile));
// fs.writeFileSync(testJavaFile, javaContent);
// console.log('@@生成java代码结束@@');
} else {
var templateContent = getTemplateContent(mobile);
mkdirs(path.dirname(testFile));
var sizeCode = '';
Expand Down Expand Up @@ -1553,6 +1670,18 @@ function startRecorder(options){
}
});
}

console.log('lion sss arrRawCmds: ' + JSON.stringify(arrRawCmds));
console.log('@@生成java代码开始@@');
var arrRawCmdsTarget = [];
arrRawCmdsTarget = getArrRawCmdsTarget(arrRawCmdsTarget);
var javaContent = getJavaTemplateContent();
javaContent = javaContent.replace(/\{\$(\w+)\}/g,arrRawCmdsTarget.join('\r\n'+' '));
var testJavaFile = path.resolve(rootPath + '/sample/' + 'JavaUITest.java');
console.log('lionssssssss:'+testJavaFile);
mkdirs(path.dirname(testJavaFile));
fs.writeFileSync(testJavaFile, javaContent);
console.log('@@生成java代码结束@@');
}

if(checkerBrowser){
Expand Down Expand Up @@ -1615,6 +1744,16 @@ function getTemplateContent(isMobile) {
return fs.readFileSync(tempalteFilePath).toString();
}

function getJavaTemplateContent() {
var templateName = 'javaTemplate.txt';
var tempalteFilePath = path.resolve(__dirname, '../template/' + templateName);
var customTemplateFilePath = path.join(rootPath, './template/', templateName);
if (fs.existsSync(customTemplateFilePath)) {
tempalteFilePath = customTemplateFilePath;
}
return fs.readFileSync(tempalteFilePath).toString();
}

// get test root
function getRootPath(){
var rootPath = path.resolve('.');
Expand Down

0 comments on commit 17dd198

Please sign in to comment.