我是否有办法在我为junit测试编写的代码中获得appium启动?由于appium只需要在我的测试运行时运行,因此让appium服务器始终保持运行是没有意义的。
现在我正在使用junit和maven来运行测试版本。由于appium的稳定性问题,它有时会在构建过程中死亡,从而导致所有剩余的测试失败。我想知道是否可以在连接WebDriver之前向@Before方法添加内容以启动appium服务器,然后在@After方法中终止它。这应解决appium失败的任何问题,因为它可以在开始下一次测试之前重置。
仍然在java中查看一般的开始和结束过程,看看这是否有效。如果我弄清楚这一点,我会更新这篇文章,以帮助其他有兴趣以这种方式进行测试的人。
答案 0 :(得分:2)
通过在代码
中运行terminal命令,弄清楚如何使其工作@Before
public void setUp() throws Exception {
closeSimulatorAndInstruments(); // also closes any appium servers
appium = Runtime.getRuntime().exec("/usr/local/bin/appium");
Thread.sleep(1000); // wait for appium to start up, not sure how to check the status
... // start test
}
@After
public void tearDown() throws Exception {
captureScreenshot(testName.getMethodName());
driver.quit();
appium.destroy(); // kills the appium server so it wont collide with the next run
}
我看到我的CI盒运行jenkins尝试这样做的问题,但它可能是无关的。在本地,这是非常有效的,因为我不必再记得单独运行appium或检查它是否已经死亡。但是,如果您需要查看可能包含重要错误的appium输出,则不建议这样做
答案 1 :(得分:1)
我为此写了一个库。
/**
*@author Raghu Nair
*/
public class Appium {
private static volatile Appium instance;
public static Appium getInstance(String outFile, String errFile) {
if (instance == null) {
synchronized (Appium.class) {
if (instance == null) {
instance = new Appium(outFile, errFile);
}
}
}
return instance;
}
Process process;
final String outFile;
final String errFile;
private Appium(String outFile, String errFile) {
this.outFile = outFile;
this.errFile = errFile;
}
public void start() throws IOException {
if (process != null) {
stop();
}
String processName = System.getProperty("appium.bin");
String processString = processName + " -lt 180000";
ProcessBuilder builder = new ProcessBuilder("bash");
process = builder.start();
OutputStream outStream = System.out;
if (outFile != null) {
outStream = new FileOutputStream(outFile);
}
OutputStream errStream = System.err;
if (errFile != null) {
errStream = new FileOutputStream(errFile);
}
handleStream(process.getInputStream(), new PrintWriter(outStream));
handleStream(process.getErrorStream(), new PrintWriter(errStream));
try (PrintWriter writer = new PrintWriter(process.getOutputStream())) {
//writer.println("kill -9 `ps -ef | grep appium | cut -d' ' -f2`");
writer.println("export PATH=$PATH:/usr/bin/:/usr/local/bin/");
writer.println(processString);
writer.flush();
}
}
private void handleStream(final InputStream processOut, final PrintWriter writer) {
Thread outHandler;
outHandler = new Thread(new Runnable() {
@Override
public void run() {
try {
BufferedReader stdout = new BufferedReader(
new InputStreamReader(processOut));
String line;
while ((line = stdout.readLine()) != null) {
writer.println(line);
writer.flush();
}
} catch (IOException ex) {
ex.printStackTrace(System.err);
}
}
});
outHandler.start();
}
public void stop() {
System.out.println("Stopping the process");
if (process != null) {
try {
process.destroy();
process.getErrorStream().close();
process.getInputStream().close();
process.getOutputStream().close();
} catch (IOException ex) {
ex.printStackTrace(System.err);
}
}
}
public static void main(String[] args) throws Exception {
System.setProperty("appium.bin", "/Applications/Appium.app//Contents/Resources/node_modules/.bin/appium");
Appium appium = Appium.getInstance("/Users/<user>/tmp/appium.out", "/Users/<user>/tmp/appium.err");
appium.start();
TimeUnit.SECONDS.sleep(30);
appium.stop();
}
答案 2 :(得分:0)
我使用DefaultExecutor
以非常类似的方式解决了这个问题,以便跟踪Appium进程,以便在测试结束时将其销毁。这也允许在测试期间使用DefaultExecuteResultHandler
注销Appium输出。
为避免使用sleep等待Appium启动,您可以在try-catch中创建WebDriver
实例
for (int i = 10; i > 0; i--) {
try {
driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
// If we successfully attach to appium, exit the loop.
i = 0;
} catch (UnreachableBrowserException e) {
LOGGER.info("Waiting for Appium to start");
}
}
如果你想减少轮询次数,你可以在catch块中添加一个睡眠。
答案 3 :(得分:0)
我发现我们可以在解决方案之上取得一些进展。