From 17dd198cfc1debdc22d9dc4190ae1b9f96ca2c90 Mon Sep 17 00:00:00 2001 From: ali-lion Date: Tue, 17 Nov 2020 21:04:56 +0800 Subject: [PATCH] add java code --- chrome-extension/js/foreground.js | 14 ++- lib/start.js | 147 +++++++++++++++++++++++++++++- 2 files changed, 149 insertions(+), 12 deletions(-) diff --git a/chrome-extension/js/foreground.js b/chrome-extension/js/foreground.js index 9fdebd1..751b3c6 100644 --- a/chrome-extension/js/foreground.js +++ b/chrome-extension/js/foreground.js @@ -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){ diff --git a/lib/start.js b/lib/start.js index 99c3235..32bde5c 100644 --- a/lib/start.js +++ b/lib/start.js @@ -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:''; @@ -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 = ''; @@ -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){ @@ -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('.');