为什么每次运行测试组时都需要重新启动Appium?

时间:2018-02-22 11:09:54

标签: java appium

为什么不一直设置Appium运行?从早上开始,在白天完成时停止,并在我们想要的时候运行测试脚本。

Mac,Appium,Java。

1 个答案:

答案 0 :(得分:0)

你可以这样做,只是在早上启动并在晚上停止它,但你也可以做一些更好的事情:检查你的测试脚本是否正在运行,如果没有,请启动它。然后,您还可以在代码中决定是否停止服务。

以下是我在许多其他人的帮助下为自己编写的课程,研究网络并将它们放在一起:

import java.io.IOException;
import java.net.ServerSocket;

import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.service.local.AppiumDriverLocalService;
import io.appium.java_client.service.local.AppiumServiceBuilder;
import io.appium.java_client.service.local.flags.GeneralServerFlag;

/**
 * This class handles Appium Server
 * 
 * @author Bill Hileman
 */
public class AppiumServer {

    private AppiumDriverLocalService service;
    private AppiumServiceBuilder builder;
    private DesiredCapabilities cap;

    private int port = 4723;

    public void startServer() {
        // Set Capabilities
        cap = new DesiredCapabilities();
        cap.setCapability("noReset", "false");

        // Build the Appium service
        builder = new AppiumServiceBuilder();
        builder.withIPAddress("127.0.0.1");
        builder.usingPort(port);
        builder.withCapabilities(cap);
        builder.withArgument(GeneralServerFlag.SESSION_OVERRIDE);
        builder.withArgument(GeneralServerFlag.LOG_LEVEL, "error");

        // Start the server with the builder
        service = AppiumDriverLocalService.buildService(builder);
        service.start();
    }

    public void stopServer() {
        service.stop();
    }

    public boolean serverIsRunnning() {

        boolean isServerRunning = false;
        ServerSocket serverSocket;
        try {
            serverSocket = new ServerSocket(port);
            serverSocket.close();
        } catch (IOException e) {
            // If control comes here, then it means that the port is in use
            isServerRunning = true;
        } finally {
            serverSocket = null;
        }
        return isServerRunning;
    }

}