=UTF-8
5 | encoding/config.properties=UTF-8
6 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5 | org.eclipse.jdt.core.compiler.compliance=1.8
6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate
8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
12 | org.eclipse.jdt.core.compiler.source=1.8
13 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.m2e.core.prefs:
--------------------------------------------------------------------------------
1 | activeProfiles=
2 | eclipse.preferences.version=1
3 | resolveWorkspaceProjects=true
4 | version=1
5 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # AppiumXM
2 | AppiumXM自动化测试框架基于 Appium + testNG + maven 二次封装,采用java语言进行开发,适用于Android、iOS自动化测试,采用Excel关键字驱动实现无需编写代码即可进行自动化测试,支持jenkins持续集成,支持Android/iOS在真机或模拟器进行自动化测试,支持H5、Hybrid 、Native测试
3 | # 环境要求
4 | Appium >=1.6.3
5 | maven >=3.3.9
6 | OSX >=10.11.6
7 | Xcode >=8.2.1
8 | # demo
9 | **测试用例**
10 | 
11 | **测试步骤描述**
12 | 
13 | **测试报告(普通)**
14 | 
15 | **测试报告(性能监控)**
16 | 
17 | # 存在问题
18 | 1、测试报告展示测试结束时间有误
19 | 2、其他未知问题
20 | # 如何运行?
21 | 1、Devices文件夹下添加相应的设备信息
22 | 2、testcase文件夹下编辑测试步骤流程
23 | 3、支持操作:看源码。。。
24 | 4、安装testng插件运行StartTest.xml或定位到工程目录下运行mvn test,ant编译中文会乱码不建议
25 | 5、多设备参照StartTest.xml注释
26 | 6、持续集成,Jenkins + mvn 亲测运行完美
27 | 7、iOS真机需自行前往 /usr/local/lib/node_modules/appium/node_modules/appium-xcuitest-driver/WebDriverAgent 对 WebDriverAgent.xcodeproj 进行重签名(个人证书即可)
28 | # 缺陷
29 | 1、灵活性真的不高。。。
30 |
--------------------------------------------------------------------------------
/StartTest.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/apps/wangyi.apk:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mingming2513953126/AppiumXM/d6cf7f5f1efe0e849db70cca630213511456dc8b/apps/wangyi.apk
--------------------------------------------------------------------------------
/apps/webview.apk:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mingming2513953126/AppiumXM/d6cf7f5f1efe0e849db70cca630213511456dc8b/apps/webview.apk
--------------------------------------------------------------------------------
/config.properties:
--------------------------------------------------------------------------------
1 | #appium安装路径,win系统无视
2 | APPIUM_JS_PATH=/usr/local/lib/node_modules/appium/build/lib/main.js
3 | #adb安装路径,win系统无视
4 | ADB_PATH=/Users/xiaoM/Desktop/OSX/xiaoMWork/sdk/platform-tools/adb
5 | #工程路径
6 | WORKSPAC_PATH=D:/xiaoMWork/workspace/AppiumXM
7 | #测试设备系统(Android\iOS)
8 | DEVICE_TYPE=Android
9 | #测试设备(线程数必须小于设备数)
10 | DEVICES=device1
11 | #测试报告标题
12 | REPORT_TITLE=自动化测试报告
13 | #被测试应用
14 | APP_NAME=wangyi.apk
15 | #测试应用包名
16 | PACKAGE_NAME=com.netease.mail
17 | #测试应用Activity
18 | ACTIVITY=com.netease.mobimail.activity.LaunchActivity
19 | #测试应用bundleId(IOS用)
20 | BUNDIEID=com.tencent.qqmail
21 | #测试类型(RM:资源监控(只支持Android);FT:功能测试)
22 | TEST_TYPE=FT
23 | #不清除缓存重置应用(暂时只针对Android)
24 | NORESET_APP=FALSE
25 | #测试用例
26 | TESTCASE=TestCase.xlsx
27 |
--------------------------------------------------------------------------------
/devices/AndroidDevices.xlsx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mingming2513953126/AppiumXM/d6cf7f5f1efe0e849db70cca630213511456dc8b/devices/AndroidDevices.xlsx
--------------------------------------------------------------------------------
/devices/iOSDevices.xlsx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mingming2513953126/AppiumXM/d6cf7f5f1efe0e849db70cca630213511456dc8b/devices/iOSDevices.xlsx
--------------------------------------------------------------------------------
/img/585.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mingming2513953126/AppiumXM/d6cf7f5f1efe0e849db70cca630213511456dc8b/img/585.png
--------------------------------------------------------------------------------
/img/58home.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mingming2513953126/AppiumXM/d6cf7f5f1efe0e849db70cca630213511456dc8b/img/58home.png
--------------------------------------------------------------------------------
/img/zaku.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mingming2513953126/AppiumXM/d6cf7f5f1efe0e849db70cca630213511456dc8b/img/zaku.jpg
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 | com.xiaoM
5 | 0.0.1-SNAPSHOT
6 | jar
7 | AppiumXM
8 | https://github.com/xiaoMGitHub
9 |
10 | UTF-8
11 | 1.8
12 | 1.8
13 |
14 | AppiumXM
15 | This is App test framework base on appium
16 |
17 |
18 | The Apache License, Version 2.0
19 | http://www.apache.org/licenses/LICENSE-2.0.txt
20 |
21 |
22 |
23 |
24 | xiaoM Zhou
25 | zxmyhd@163.com
26 |
27 |
28 |
29 |
30 | org.seleniumhq.selenium
31 | selenium-server
32 | 3.4.0
33 |
34 |
35 | org.seleniumhq.selenium
36 | selenium-remote-driver
37 | 3.4.0
38 |
39 |
40 | io.appium
41 | java-client
42 | 5.0.0-BETA8
43 |
44 |
45 | log4j
46 | log4j
47 | 1.2.17
48 |
49 |
50 | org.testng
51 | testng
52 | 6.11
53 |
54 |
55 | org.apache.poi
56 | poi-ooxml
57 | 3.16
58 |
59 |
60 | org.jfree
61 | jfreechart
62 | 1.0.19
63 |
64 |
65 | com.aventstack
66 | extentreports
67 | 3.0.5
68 |
69 |
70 | org.bytedeco
71 | javacv-platform
72 | 1.3.2
73 |
74 |
75 |
76 |
77 |
78 | org.apache.maven.plugins
79 | maven-surefire-plugin
80 | 2.20
81 |
82 | once
83 | -Dfile.encoding=UTF-8
84 |
85 | StartTest.xml
86 |
87 |
88 |
89 |
90 |
91 |
92 |
--------------------------------------------------------------------------------
/report1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mingming2513953126/AppiumXM/d6cf7f5f1efe0e849db70cca630213511456dc8b/report1.png
--------------------------------------------------------------------------------
/report2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mingming2513953126/AppiumXM/d6cf7f5f1efe0e849db70cca630213511456dc8b/report2.png
--------------------------------------------------------------------------------
/src/main/java/com/xiaoM/BeginScript/BeginScript.java:
--------------------------------------------------------------------------------
1 | package com.xiaoM.BeginScript;
2 |
3 | import java.io.IOException;
4 |
5 | import org.testng.annotations.DataProvider;
6 | import org.testng.annotations.Test;
7 |
8 | import com.xiaoM.Common.Utils.Run;
9 | import com.xiaoM.Common.Utils.UseDevices;
10 | import com.xiaoM.Report.utils.TestListener;
11 |
12 |
13 | public class BeginScript{
14 |
15 | @DataProvider(parallel = true)
16 | public Object[][]Testcases() throws IOException{
17 | return TestListener.RunCase;
18 | }
19 |
20 | @Test(dataProvider = "Testcases")
21 | public void run(String CaseType,String CaseName) throws Exception{
22 | String getDevice = UseDevices.Device();
23 | Run run = new Run();
24 | run.testCase(CaseType,CaseName,getDevice);
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/main/java/com/xiaoM/Common/Utils/Assertion.java:
--------------------------------------------------------------------------------
1 | package com.xiaoM.Common.Utils;
2 |
3 | import java.text.SimpleDateFormat;
4 | import java.util.Date;
5 | import org.testng.Assert;
6 |
7 | import com.xiaoM.Report.utils.TestListener;
8 | /**
9 | * Assert 验证类
10 | * @author xiaoM
11 | *
12 | */
13 | public class Assertion {
14 | private static Log log=new Log(Assertion.class);
15 | //断言成功日志内容
16 | private static void AssertPassLog(String driverName){
17 | log.info("设备: "+driverName+" "+"【Assert验证:pass!】");
18 | }
19 | //断言失败日志内容
20 | private static void AssertFailedLog(String driverName){
21 | log.error("设备: "+driverName+" "+"【Assert验证:failed!】");
22 | }
23 | public static String formatDate(Date date){
24 | SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HHmmssSSS");
25 | return formatter.format(date).toString();
26 | }
27 |
28 | public static void Verity(String checkWay,String actualText,String exceptText,String driverName,String sdkVersion){
29 | if(checkWay.equals("文本校验")){
30 | VerityText(actualText,exceptText,driverName,sdkVersion);
31 | }
32 | }
33 |
34 | /**
35 | * 验证某元素文本值是否与预期值exceptText一样
36 | * @param exceptText 预期文本值
37 | */
38 | public static void VerityText(String actualText,String exceptText,String driverName,String sdkVersion){
39 | String verityStr= "设备: "+driverName+" "+"Assert验证:某文本值是否与预期值一致{"+"实际值:"+actualText+","+"预期值:"+exceptText+"}";
40 | log.info(verityStr);
41 | try {
42 | Assert.assertEquals(actualText, exceptText);
43 | AssertPassLog(driverName);
44 | } catch (Error e) {
45 | TestListener.messageList.add(driverName+"(安卓版本:"+sdkVersion+")::"+"Assert验证失败:{"+"实际值:"+actualText+","+"预期值:"+exceptText+"} 不一致");
46 | AssertFailedLog(driverName);
47 | throw e;
48 | }
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/src/main/java/com/xiaoM/Common/Utils/BaseDriver.java:
--------------------------------------------------------------------------------
1 | package com.xiaoM.Common.Utils;
2 |
3 | import java.io.File;
4 | import java.io.IOException;
5 | import java.net.URL;
6 |
7 | import org.openqa.selenium.remote.CapabilityType;
8 | import org.openqa.selenium.remote.DesiredCapabilities;
9 |
10 | import com.xiaoM.Report.utils.TestListener;
11 | import com.xiaoM.appium.utils.AppiumServerUtils;
12 | import com.xiaoM.appium.utils.PortProber;
13 |
14 | import io.appium.java_client.AppiumDriver;
15 | import io.appium.java_client.MobileElement;
16 | import io.appium.java_client.remote.AndroidMobileCapabilityType;
17 | import io.appium.java_client.remote.AutomationName;
18 | import io.appium.java_client.remote.IOSMobileCapabilityType;
19 | import io.appium.java_client.remote.MobileCapabilityType;
20 |
21 | public class BaseDriver {
22 | public Log log=new Log(this.getClass());
23 | AppiumDriver driver ;
24 | AppiumServerUtils AppiumServer = null;
25 | public URL url;
26 | public AppiumDriver setUpApp(String device,String devicesPath) throws IOException {
27 | String driverName = null;
28 | try {
29 | Object[][] testBase = IOMananger.readExcelData(device,devicesPath);
30 | driverName = testBase[1][2].toString();
31 | String ipAddress = testBase[2][2].toString();
32 | String platformName =testBase[3][2].toString();
33 | String deviceId =testBase[4][2].toString();
34 | String deviceName =testBase[5][2].toString();
35 | String sdkVersion =testBase[6][2].toString();
36 | int Port = PortProber.getFreePort();
37 | String bootstrapPort = String.valueOf(PortProber.getFreePort());
38 | log.info("设备: "+driverName+" "+"开始执行测试");
39 | log.info("设备: "+driverName+" "+"启动appium server");
40 | log.info("设备: "+driverName+" "+"配置信息:Mobile Driver:"+driverName);
41 | log.info("设备: "+driverName+" "+"Appium Server:"+"http://"+ipAddress+":"+Port+"/wd/hub");
42 | log.info("设备: "+driverName+" "+"设备Id:"+deviceId);
43 | try {
44 | DesiredCapabilities capabilities = new DesiredCapabilities();
45 | if(TestListener.DeviceType.equals("Android")){
46 | AppiumServer = new AppiumServerUtils(ipAddress,Port,bootstrapPort);
47 | url = AppiumServer.startServer();
48 | File appDir=new File(TestListener.ProjectPath,"apps");
49 | File app =new File(appDir,TestListener.appName);
50 | String appMainPackage =TestListener.PackageName;
51 | String appActivity =TestListener.Activity;
52 | capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, AutomationName.APPIUM);
53 | capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, platformName);
54 | capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, deviceName);
55 | capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, sdkVersion);
56 | capabilities.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());
57 | capabilities.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, appMainPackage);
58 | capabilities.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, appActivity);
59 | capabilities.setCapability(MobileCapabilityType.NO_RESET, TestListener.ResetApp);
60 | capabilities.setCapability(MobileCapabilityType.UDID, deviceId);
61 | capabilities.setCapability("unicodeKeyboard", "True");
62 | capabilities.setCapability("resetKeyboard", "True");
63 | capabilities.setCapability("noSign", "True");
64 | }else{
65 | AppiumServer = new AppiumServerUtils();
66 | url = AppiumServer.startServer(ipAddress,Port);
67 | String bundleId = TestListener.bundleId;
68 | capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, AutomationName.IOS_XCUI_TEST);
69 | capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, platformName);
70 | capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, deviceName);
71 | capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, sdkVersion);
72 | capabilities.setCapability(IOSMobileCapabilityType.LAUNCH_TIMEOUT, 500000);
73 | capabilities.setCapability(MobileCapabilityType.UDID, deviceId);
74 | capabilities.setCapability(IOSMobileCapabilityType.BUNDLE_ID,bundleId);
75 | }
76 | driver = new AppiumDriver(url, capabilities);
77 | } catch (Exception e) {
78 | e.printStackTrace();
79 | log.error("设备: "+driverName+" "+"appium环境配置失败");
80 | }
81 | } catch (Exception e) {
82 | log.error("设备: "+driverName+" "+"读取TestBase配置文件失败");
83 | e.printStackTrace();
84 | }
85 | return driver;
86 | }
87 |
88 |
89 | public AppiumDriver setUpWap(String device,String devicesPath) throws IOException {
90 | String driverName =null;
91 | try {
92 | Object[][] testBase = IOMananger.readExcelData(device,devicesPath);
93 | driverName = testBase[1][2].toString();
94 | String ipAddress = testBase[2][2].toString();
95 | String platformName =testBase[3][2].toString();
96 | String deviceId =testBase[4][2].toString();
97 | String deviceName =testBase[5][2].toString();
98 | String sdkVersion =testBase[6][2].toString();
99 | String Browser = testBase[7][2].toString();
100 | int Port = PortProber.getFreePort();
101 | String bootstrapPort = String.valueOf(PortProber.getFreePort());
102 | log.info("设备: "+driverName+" "+"开始执行测试");
103 | log.info("设备: "+driverName+" "+"启动appium server");
104 | try {
105 | log.info("设备: "+driverName+" "+"配置信息:Mobile Driver:"+driverName);
106 | log.info("设备: "+driverName+" "+"Appium Server:"+"http://"+ipAddress+":"+Port+"/wd/hub");
107 | log.info("设备: "+driverName+" "+"设备Id:"+deviceId);
108 | DesiredCapabilities capabilities = new DesiredCapabilities();
109 | if(TestListener.DeviceType.equals("Android")){
110 | AppiumServer = new AppiumServerUtils(ipAddress,Port,bootstrapPort);
111 | url = AppiumServer.startServer();
112 | capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, AutomationName.APPIUM);
113 | capabilities.setCapability(CapabilityType.BROWSER_NAME, Browser);// Browser
114 | capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, platformName);
115 | capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, deviceName);
116 | capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, sdkVersion);
117 | capabilities.setCapability(MobileCapabilityType.UDID, deviceId);
118 | capabilities.setCapability("unicodeKeyboard", "True");
119 | capabilities.setCapability("resetKeyboard", "True");
120 | }else{
121 | AppiumServer = new AppiumServerUtils();
122 | url = AppiumServer.startServer(ipAddress,Port);
123 | capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, AutomationName.IOS_XCUI_TEST);
124 | capabilities.setCapability(CapabilityType.BROWSER_NAME, Browser);// Browser
125 | capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, platformName);
126 | capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, deviceName);
127 | capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, sdkVersion);
128 | capabilities.setCapability(MobileCapabilityType.UDID, deviceId);
129 | }
130 | driver = new AppiumDriver (url, capabilities);
131 | } catch (Exception e) {
132 | e.printStackTrace();
133 | log.error("设备: "+driverName+" "+"appium环境配置失败");
134 | }
135 | } catch (Exception e) {
136 | log.error("设备: "+driverName+" "+"读取TestBase配置文件失败");
137 | e.printStackTrace();
138 | }
139 | return driver;
140 | }
141 | }
142 |
--------------------------------------------------------------------------------
/src/main/java/com/xiaoM/Common/Utils/CommonUtils.java:
--------------------------------------------------------------------------------
1 | package com.xiaoM.Common.Utils;
2 |
3 | import java.io.IOException;
4 |
5 | /**
6 | * 公共调用类
7 | * @author XiaoM
8 | *
9 | */
10 | public class CommonUtils {
11 | /**
12 | * 执行cmd命令
13 | */
14 | public static void executeCmd(String cmd) throws IOException {
15 | Runtime runtime=Runtime.getRuntime();
16 | runtime.exec("cmd /c start "+cmd);
17 | }
18 |
19 | /**
20 | * 显式等待,程序休眠暂停
21 | * @param time 以秒为单位
22 | */
23 | public static void sleep(long time){
24 | try {
25 | Thread.sleep(time*1000);
26 | } catch (InterruptedException e) {
27 | e.printStackTrace();
28 | }
29 | }
30 | }
--------------------------------------------------------------------------------
/src/main/java/com/xiaoM/Common/Utils/CopyFileForReport.java:
--------------------------------------------------------------------------------
1 | package com.xiaoM.Common.Utils;
2 |
3 | import java.io.*;
4 |
5 | /**
6 | * Created by zhengshuheng on 2016/8/18 0018.
7 | */
8 | public class CopyFileForReport {
9 |
10 | public static void main(String[] args){
11 | CopyFileForReport copyReportResources=new CopyFileForReport();
12 | copyReportResources.copyResources();
13 | }
14 | public void copyResources(){
15 | this.copyFile("resources/back.gif", "test-output/back.gif");
16 | }
17 | /**
18 | * 复制图片及其他文件
19 | * @param sourceRelativePath 源文件相对路径
20 | * @param targetRelativePath 目标文件相对路径
21 | */
22 | private void copyFile(String sourceRelativePath,String targetRelativePath){
23 | //读取流字节流
24 | FileInputStream fileInputStream=null;
25 | //写入流字节流
26 | OutputStream imageOutputStream=null;
27 | try {
28 | fileInputStream=new FileInputStream(new File(sourceRelativePath));
29 | //imageInputStream=CopyReportResources.class.getClassLoader().getResourceAsStream(sourceRelativePath);// 与复制的图片关联起来
30 | imageOutputStream=new FileOutputStream(targetRelativePath);// 与复制到的目的关联起来,这里的图片的名称可以与原来的相同,也可以不一样
31 | byte[] b = new byte[1024];// 定义字节数组,并指定长度
32 | int startbyte = -1;
33 | while ((startbyte= fileInputStream.read(b)) != -1) {// 读取
34 | imageOutputStream.write(b, 0, startbyte);// 写入,读多少写入多少,所以用 write(b,0,len)
35 | }
36 | } catch (IOException e) {
37 | e.printStackTrace();
38 | }finally{
39 | if (fileInputStream!=null) {
40 | try {
41 | fileInputStream.close();
42 |
43 | } catch (IOException e2) {
44 | System.out.println("读取流关闭失败");
45 | }
46 | }
47 | if (imageOutputStream!=null) {
48 | try {
49 | imageOutputStream.close();
50 | } catch (Exception e2) {
51 | System.out.println("输出流关闭失败");
52 | }
53 | }
54 | }
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/src/main/java/com/xiaoM/Common/Utils/DataFormat.java:
--------------------------------------------------------------------------------
1 | package com.xiaoM.Common.Utils;
2 |
3 | import java.text.ParseException;
4 | import java.text.SimpleDateFormat;
5 | import java.util.Date;
6 |
7 | /**
8 | * 格式化时间工具类
9 | * @author XiaoM
10 | *
11 | */
12 | public class DataFormat {
13 |
14 | public static String formatDate(Date date,String format){
15 | SimpleDateFormat formatter = new SimpleDateFormat(format);
16 | System.out.println(formatter.format(date).toString());
17 | return formatter.format(date).toString();
18 |
19 | }
20 | public static String formatDate(long date,String format){
21 | SimpleDateFormat formatter = new SimpleDateFormat(format);
22 | System.out.println(formatter.format(date));
23 | return formatter.format(date);
24 | }
25 | //支持YY-MM-DD转换成YYYY-MM-DD
26 | public static String formatDate(String date,String format){
27 | SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd");
28 | SimpleDateFormat sdf2 = new SimpleDateFormat(format);
29 | String sss = null;
30 | try {
31 | sss = sdf2.format(sdf.parse(date));
32 | System.out.println(sss);
33 | } catch (ParseException e) {
34 | // TODO Auto-generated catch block
35 | e.printStackTrace();
36 | }
37 | return sss;
38 | }
39 | public static void main(String[] args) {
40 | // TODO 自动生成的方法存根
41 |
42 | }
43 |
44 | }
45 |
--------------------------------------------------------------------------------
/src/main/java/com/xiaoM/Common/Utils/FileManger.java:
--------------------------------------------------------------------------------
1 | package com.xiaoM.Common.Utils;
2 |
3 | import java.io.*;
4 |
5 | /**
6 | * 文件管理类
7 | * @author XiaoM
8 | *
9 | */
10 | public class FileManger {
11 |
12 | /**
13 | * 递归删除文件
14 | * @param file
15 | */
16 | private void deleteFile(File file){
17 | if (file.isDirectory()) {
18 | File[] files=file.listFiles();
19 | //递归条用删除方法
20 | for (int i = 0; i < files.length; i++) {
21 | deleteFile(files[i]);
22 | }
23 | }
24 | file.delete();
25 | }
26 | /**
27 | * 删除文件夹和密码
28 | * @param workspaceRootPath 文件目录
29 | */
30 | public void clearFile(String workspaceRootPath){
31 | File file=new File(workspaceRootPath);
32 | if (file.exists()) {
33 | deleteFile(file);
34 | }
35 |
36 | }
37 |
38 | /**
39 | *
40 | * @param path 写入文件目标路径
41 | * @param encode 写入文件编码
42 | * @param append 是否文本追加模式
43 | * @param content 写入文本
44 | */
45 | public void writeWithEncode(String path,String encode,boolean append,String content){
46 | File file=new File(path);
47 | if (file.exists()){
48 | file.delete();
49 | }
50 | try {
51 | file.createNewFile();
52 | BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter( new FileOutputStream(file,append),encode));
53 | bufferedWriter.write(content);
54 | bufferedWriter.close();
55 | } catch (IOException e) {
56 | e.printStackTrace();
57 | }
58 |
59 | }
60 | /**
61 | *
62 | * @param file 写入文件文件
63 | * @param encode 写入文件编码
64 | * @param append 是否文本追加模式
65 | * @param content 写入文本
66 | */
67 | public static void writeWithEncode(File file,String encode,boolean append,String content){
68 | try {
69 | file.createNewFile();
70 | BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter( new FileOutputStream(file,append),encode));
71 | bufferedWriter.write(content);
72 | bufferedWriter.close();
73 | } catch (IOException e) {
74 | e.printStackTrace();
75 | }
76 |
77 | }
78 |
79 |
80 | public static void main(String[] args) {
81 |
82 | }
83 | }
84 |
--------------------------------------------------------------------------------
/src/main/java/com/xiaoM/Common/Utils/IOMananger.java:
--------------------------------------------------------------------------------
1 | package com.xiaoM.Common.Utils;
2 |
3 | import java.io.BufferedWriter;
4 | import java.io.File;
5 | import java.io.FileInputStream;
6 | import java.io.FileNotFoundException;
7 | import java.io.FileOutputStream;
8 | import java.io.IOException;
9 | import java.io.InputStream;
10 | import java.io.OutputStreamWriter;
11 | import java.util.ArrayList;
12 | import java.util.LinkedList;
13 | import java.util.List;
14 | import java.util.Scanner;
15 |
16 | import org.apache.poi.hssf.usermodel.HSSFRow;
17 | import org.apache.poi.hssf.usermodel.HSSFSheet;
18 | import org.apache.poi.hssf.usermodel.HSSFWorkbook;
19 | import org.apache.poi.ss.usermodel.Cell;
20 | import org.apache.poi.ss.usermodel.CellType;
21 | import org.apache.poi.ss.usermodel.Row;
22 | import org.apache.poi.xssf.usermodel.XSSFRow;
23 | import org.apache.poi.xssf.usermodel.XSSFSheet;
24 | import org.apache.poi.xssf.usermodel.XSSFWorkbook;
25 |
26 | import com.xiaoM.Report.utils.TestListener;
27 |
28 |
29 | /**
30 | * IO类
31 | * @author XiaoM
32 | *
33 | */
34 | public class IOMananger {
35 | /**
36 | * 读取excel
37 | * @param sheetName
38 | * @param path
39 | * @return
40 | * @throws IOException
41 | */
42 | public static Object[][] readExcelData(String sheetName,String path) throws IOException{
43 | Object[][] test = null;
44 | String[] xlsx = path.split("\\.");
45 | if(xlsx[1].toString().contains("xlsx")){
46 | test = readExcelDataXlsx(sheetName, path);
47 | }else{
48 | test = readExcelDataXls(sheetName, path);
49 | }
50 | return test;
51 | }
52 | /**
53 | * 定位Excel单元格,获取一行内容
54 | * @param sheetName
55 | * @param path
56 | * @return
57 | * @throws IOException
58 | */
59 | public static List checkExcelData(String cellContent) throws IOException{
60 | List test = null;
61 | String[] xlsx = TestListener.CasePath.split("\\.");
62 | if(xlsx[1].toString().contains("xlsx")){
63 | test = checkExcelDataXlsx(cellContent);
64 | }else{
65 | test = checkExcelDataXls(cellContent);
66 | }
67 | return test;
68 | }
69 |
70 |
71 |
72 | public static Object[][] readExcelDataXlsx(String sheetName,String path) throws IOException {
73 | InputStream is = new FileInputStream(path);
74 | XSSFWorkbook workbook = new XSSFWorkbook(is);//读取Excel
75 | XSSFSheet sheet = workbook.getSheet(sheetName);//读取sheet
76 | if(sheet!=null){
77 | int lastrowNum = sheet.getLastRowNum()+1;//获取总行数
78 | int collNum = sheet.getRow(0).getLastCellNum();//获取列数
79 | Object[][] user = new Object[lastrowNum][collNum];
80 | for(int rowNum=0;rowNum checkExcelDataXls( String cellContent) throws IOException {
125 | InputStream is = new FileInputStream(TestListener.CasePath);
126 | List list = new LinkedList();
127 | HSSFWorkbook workbook = new HSSFWorkbook(is);//读取Excel
128 | HSSFSheet sheet = workbook.getSheet("TestCases");//读取sheet
129 | if(sheet!=null){
130 | for (Row row : sheet) {
131 | for (Cell cell : row) {
132 | if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
133 | if (cell.getRichStringCellValue().getString().trim().equals(cellContent)) {
134 | for(Cell cell2:row){
135 | list.add(cell2.toString());
136 | }
137 | workbook.close();
138 | return list;
139 | }
140 | }
141 | }
142 | }
143 | }
144 | workbook.close();
145 | return null;
146 | }
147 |
148 | public static List checkExcelDataXlsx( String cellContent) throws IOException {
149 | InputStream is = new FileInputStream(TestListener.CasePath);
150 | List list = new LinkedList();
151 | XSSFWorkbook workbook = new XSSFWorkbook(is);//读取Excel
152 | XSSFSheet sheet = workbook.getSheet("TestCases");//读取sheet
153 | if(sheet!=null){
154 | for (Row row : sheet) {
155 | for (Cell cell : row) {
156 | if (cell.getCellTypeEnum() == CellType.STRING) {
157 | if (cell.getRichStringCellValue().getString().trim().equals(cellContent)) {
158 | for(Cell cell2:row){
159 | list.add(cell2.toString());
160 | }
161 | workbook.close();
162 | return list;
163 | }
164 | }
165 | }
166 | }
167 | }
168 | workbook.close();
169 | return null;
170 | }
171 |
172 | public static Object[][] runTime(String sheetName,String path) throws IOException{
173 | Object[][] Date = readExcelData(sheetName,path);
174 | List