Jenkins做集成测试是不错的工具,那么UI自动化是否可以,appium与testng结合能否集成到jenkins中去做定时运行。如果要执行,先解决jenkins怎么识别testng:
今天只演示通过main方式执行testng
import org.dom4j.Document;import org.dom4j.DocumentHelper;import org.dom4j.Element;import org.dom4j.io.OutputFormat;import org.dom4j.io.XMLWriter;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.util.Properties;
/** * @author liwen * @Title: createTestngXml * @Description: 生存xml文件 * @date 2019/11/24 / 16:15 */public class createTestngXml {
static Properties sysProperty = System.getProperties();
public static void createTestngXml(String classname) { Document document = DocumentHelper.createDocument(); Element root = DocumentHelper.createElement("suite"); document.setRootElement(root); root.addAttribute("name", "automation"); root.addAttribute("thread-count", String.valueOf("1")); //生成监听器 Element listeners = root.addElement("listeners"); Element listener1 = listeners.addElement("listener"); listener1.addAttribute("class-name", "appout.reporter.ReporterListener"); Element test = root.addElement("test"); //增加设备号与端口号: Element paramUuid = test.addElement("parameter"); paramUuid.addAttribute("name", "udid"); //根据实际情况修改 paramUuid.addAttribute("value", "127.0.0.1:62001"); Element paramPort = test.addElement("parameter"); paramPort.addAttribute("name", "port"); //根据实际情况修改 paramPort.addAttribute("value", "4723"); test.addAttribute("name", "127.0.0.1"); Element classes = test.addElement("classes"); Element classNode = classes.addElement("class"); classNode.addAttribute("name", classname); OutputFormat format = new OutputFormat(" ", true); XMLWriter xmlWrite2; try { xmlWrite2 = new XMLWriter(new FileOutputStream(sysProperty.getProperty("user.dir") +"/comsevenday/src/main/resources/mytestng.xml"), format); xmlWrite2.write(document); } catch ( UnsupportedEncodingException e) { e.printStackTrace(); } catch ( FileNotFoundException e) { e.printStackTrace(); } catch ( IOException e) { e.printStackTrace(); } }
}
import appout.utils.createTestngXml;import org.testng.TestNG;import java.util.ArrayList;import java.util.List;import java.util.Properties;
/** * @author liwen * @Title: MainTest * @Description: 主方法 * @date 2019/11/24 / 19:49 */public class MainTest {
static Properties sysProperty = System.getProperties(); private static final String ESCAPE_PROPERTY = "org.uncommons.reportng.escape-output";
public static void main(String[] args) { try { //执行的类 createTestngXml.createTestngXml("appout.appcase.CartTest"); } catch (Exception e) { e.printStackTrace(); } System.setProperty(ESCAPE_PROPERTY, "false"); List<String> suites = new ArrayList<String>(); System.out.println(suites); suites.add(sysProperty.getProperty("user.dir") + "/comsevenday/src/main/resources/appxml/mytestng.xml"); TestNG tng = new TestNG(); tng.setTestSuites(suites); tng.run(); }}
<?xml version="1.0" encoding="UTF-8"?>
<suite name="automation" parallel="Automatically generate xml" thread-count="1"> <listeners> <listener class-name="appout.reporter.ReporterListener"/> </listeners> <test name="127.0.0.1"> <parameter name="udid" value="127.0.0.1:62001"/> <parameter name="port" value="4723"/> <classes> <class name="appout.appcase.CartTest"/> </classes> </test></suite>
如果使用命令行执行,需要了解下appium启动命令,这样咱们才可以拼接命令,启动服务
先试下编写好的命令appium -p 4327 -bp 11 -U 127.0.0.1:62001
/** * 启动或者关闭appium servers * kill server with appium servers * * @return */ public static boolean killServer() { String command = null; if (osName.toLowerCase().contains("mac")) { command = "killall node"; } else if (osName.toLowerCase().contains("win")) { command = "taskkill -F -PID node.exe"; } else { command = "taskkill -F -PID node.exe"; } if (execCmd(command)) { log.debug("kill server node Succeed"); return true; } else { log.error("kill server node Failure"); return false; } }
/** * 启动appium服务 */ public void startAppium() { String deviceName = getDeviceName(); String cmmd = "appium -p 4327 -bp 11 -U " + deviceName + ">" + deviceName + "_log.log"; execCmd(cmmd); }
/** * 执行操作命令 * * @param cmdString * @return */ public static boolean execCmd(String cmdString) { if (cmdString == null) { return false; } //获取当前执行环境 Runtime p = Runtime.getRuntime(); Process process = null; try { if (osName.toLowerCase().contains("mac")) { String[] command = {"/bin/sh", "-c", cmdString}; process = p.exec(command); } else if (osName.toLowerCase().contains("win")) { process = p.exec("cmd /c " + cmdString); } return true; } catch (Exception e) { e.printStackTrace(); return false; } }
/** * 获取设备号 * * @return name of the device * @throws IOException */ public static String getDeviceName() { return runCmdCommand(GET_DEVICE_NAME).trim(); } /** * 执行命令 * * @param command * @return string * @throws IOException */ public static String runCmdCommand(String command) { ProcessBuilder builder; if (SYSTEM_TYPE_UPPER.indexOf("WINDOWS") != -1) { builder = new ProcessBuilder("cmd.exe", "/c", command); } else { builder = new ProcessBuilder("/bin/sh", "-c", command); } StringBuffer content = null; try { builder.redirectErrorStream(true); Process p = builder.start(); BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream())); content = new StringBuffer(); String line; while ((line = r.readLine()) != null) { content.append(line); } } catch (IOException e) { e.printStackTrace(); } return content.toString(); }
如果存在就kill
/** * 启动或者关闭appium servers * kill server with appium servers * * @return */ public static boolean killServer() { String command = null; if (osName.toLowerCase().contains("mac")) { command = "killall node"; } else if (osName.toLowerCase().contains("win")) { command = "taskkill -F -PID node.exe"; } else { command = "taskkill -F -PID node.exe"; } if (execCmd(command)) { LogUtil.debug("kill server node Succeed"); return true; } else { LogUtil.error("kill server node Failure"); return false; } }
import appout.utils.OperationalCmd;import appout.utils.createTestngXml;import org.testng.TestNG;import java.util.ArrayList;import java.util.List;import java.util.Properties;
/** * @author liwen * @Title: MainTest * @Description: 主方法 * @date 2019/11/24 / 19:49 */public class MainTest {
static Properties sysProperty = System.getProperties(); private static final String ESCAPE_PROPERTY = "org.uncommons.reportng.escape-output";
public static void main(String[] args) {
//先执行kill OperationalCmd.killServer(); //在启动服务 OperationalCmd.startAppium();
try { //执行的类 createTestngXml.createTestngXml("appout.appcase.CartTest"); } catch (Exception e) { e.printStackTrace(); } System.setProperty(ESCAPE_PROPERTY, "false"); List<String> suites = new ArrayList<String>(); System.out.println(suites); suites.add(sysProperty.getProperty("user.dir") + "/comsevenday/src/main/resources/appxml/mytestng.xml"); TestNG tng = new TestNG(); tng.setTestSuites(suites); tng.run(); }}
自己想一想
操作:略
通过上面主方法调用检查appium服务,并且执行appium服务,上面不足的地方就是生成xml中设备号与udid需要手动,其实也是可以通过自动生成并且赋值给appium执行命令与生存testng.xml中去。暂时就不演示怎么加上去,其实在工作中确实是自动获取设备号生存xml,全部一键执行。