在Windows机器中以编程方式启动appium服务器

时间:2017-05-23 06:28:31

标签: selenium-webdriver appium

我正在使用Windows 8.1并使用appium 1.4.16直到现在,我想升级appium所以我只是从控制面板卸载了1.4.16,然后安装了node.js之后使用

安装了最新的appium
npm install -g appium

当我跑

appium -v
它显示我1.6.4直到现在没有问题。 在我的Maven项目之后,我想以编程方式启动appium服务器,但appium不会保存在

C:/Program Files or C:/ProgramFile(x86)

如何以编程方式启动appium服务器?

我使用下面的代码来运行appium

Process p = Runtime.getRuntime().exec("\"C:/Program Files/Appium/node.exe\" \"C:/Program Files/Appium/node_modules/appium/bin/Appium.js\" --full-reset --local-timezone");

4 个答案:

答案 0 :(得分:2)

有三种方法可以实现这种情况。 1)使用AppiumDriverLocalService

 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(4723);
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();
}

2)将Appium.js与Node.exe一起使用

  public void startServer() {
CommandLine cmd = new CommandLine("C:\\Program Files (x86)\\Appium\\node.exe");
cmd.addArgument("C:\\Program Files (x86)\\Appium\\node_modules\\appium\\bin\\Appium.js");
cmd.addArgument("--address");
cmd.addArgument("127.0.0.1");
cmd.addArgument("--port");
cmd.addArgument("4723");

DefaultExecuteResultHandler handler = new DefaultExecuteResultHandler();
DefaultExecutor executor = new DefaultExecutor();
executor.setExitValue(1);
try {
    executor.execute(cmd, handler);
    Thread.sleep(10000);
} catch (IOException | InterruptedException e) {
    e.printStackTrace();
}
}

public void stopServer() {
Runtime runtime = Runtime.getRuntime();
try {
    runtime.exec("taskkill /F /IM node.exe");
} catch (IOException e) {
    e.printStackTrace();
}
}

3)使用命令提示符启动Appium服务器

public void startServer() {
Runtime runtime = Runtime.getRuntime();
try {
    runtime.exec("cmd.exe /c start cmd.exe /k \"appium -a 127.0.0.1 -p 4723 --session-override -dc \"{\"\"noReset\"\": \"\"false\"\"}\"\"");
    Thread.sleep(10000);
} catch (IOException | InterruptedException e) {
    e.printStackTrace();
}
}
public void stopServer() {
Runtime runtime = Runtime.getRuntime();
try {
    runtime.exec("taskkill /F /IM node.exe");
    runtime.exec("taskkill /F /IM cmd.exe");
} catch (IOException e) {
    e.printStackTrace();
}
}


我发现它很有用。希望它有所帮助。资料来源:http://www.automationtestinghub.com/3-ways-to-start-appium-server-from-java/

答案 1 :(得分:1)

您可以尝试以下代码:

AppiumServiceBuilder builder = new AppiumServiceBuilder()
            .withAppiumJS(new File("C:\Users\<Username>\node_modules\appium\build\lib\main.js"))
            .withArgument(GeneralServerFlag.APP,  path of your app );
appiumDriverLocalService = builder.build();
appiumDriverLocalService.start();

答案 2 :(得分:0)

如果它是通过npm安装的,那么Appium可以以编程方式运行。 Appium的安装位置无关紧要。下面的代码模仿了我们从命令提示符打开它的手动行为。

Runtime runtime = Runtime.getRuntime();
        try {
            runtime.exec("cmd.exe /c start cmd.exe /k \"appium -a 127.0.0.1 -p 4723 --session-override -dc \"{\"\"noReset\"\": \"\"false\"\"}\"\"");
            Thread.sleep(10000);
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }

还有其他几种方法,具体取决于您安装Appium的方式。您可以在此处查看这些内容 - Start Appium Server Programatically

答案 3 :(得分:0)

String nodePath = "C:/Appium/node.exe";
 // Set path of your appium.js file.
 String appiumJSPath = "C:/Progra~1/Appium/node_modules/appium/bin/appium.js";
 String cmd = nodePath + " " + appiumJSPath;
 AndroidDriver driver;

 // This method Is responsible for starting appium server.
 public void appiumStart() throws IOException, InterruptedException {
  // Execute command string to start appium server.
  p = Runtime.getRuntime().exec(cmd);
  // Provide wait time of 10 mins to start appium server properly.
  // If face any error(Could not start a new session...) then Increase
  // this time to 15 or 20 mins.
  Thread.sleep(10000);
  if (p != null) {
   System.out.println("Appium server Is started now.");
  }
 }

 // This method Is responsible for stopping appium server.
 public void appiumStop() throws IOException {
  if (p != null) {
   p.destroy();
  }
  System.out.println("Appium server Is stopped now.");
 }