【问题标题】:How to start appium sever from the Java code in Eclipse如何从 Eclipse 中的 Java 代码启动 appium 服务器
【发布时间】:2016-02-22 19:26:10
【问题描述】:

我正在尝试从我的测试用例代码以编程方式启动 appium 服务器,但没有任何帮助。控制台总是给我“org.openqa.selenium.remote.UnreachableBrowserException:无法启动新会话。可能的原因是远程服务器地址无效或浏览器启动失败”错误消息。到目前为止我尝试了什么:

CommandLine command = new CommandLine("cmd");
command.addArgument("/c");
command.addArgument("C:/Program Files (x86)/Appium/node.exe");
command.addArgument("C:/Program Files (x86)/Appium/node_modules/appium/bin/appium.js");
        command.addArgument("--address");
        command.addArgument("127.0.0.1");
        command.addArgument("--bootstrap-port");
        command.addArgument("5001");
        command.addArgument("--no-reset");
        command.addArgument("--log");

不起作用。下一个:

DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
        DefaultExecutor executor = new DefaultExecutor();
        executor.setExitValue(1);
        executor.execute(new CommandLine("C:/Program Files (x86)/Appium/node.exe"), resultHandler);
        executor.execute(new CommandLine("C:/Program Files (x86)/Appium/node_modules/appium/bin/Appium.js --address 127.0.0.1 --chromedriver-port 9516 --bootstrap-port 4725 --selendroid-port 8082 --no-reset --local-timezone"), resultHandler);

不起作用。下一个:

ProcessBuilder pb = new ProcessBuilder("C:/Program Files(x86)/Appium/node.exe/");
        ProcessBuilder pb1 = new ProcessBuilder("C:/Program Files(x86)/Appium/node_modules/appium/bin/Appium.js --address 127.0.0.1 --chromedriver-port 9516 --bootstrap-port 5002 --no-reset --local-timezone");
        pb.start();
        pb1.start();

不起作用。下一个:

String path = "cmd /c start C:/Users/jamesrobinson/Desktop/Run automation servers.bat";
        Runtime rn = Runtime.getRuntime();
        Process process = rn.exec(path);

我可以从 UI 手动启动它的唯一方法。任何如何解决该问题的想法将不胜感激。

【问题讨论】:

  • 您能否分享成功运行的UI General settings 的屏幕截图。我可以在您的尝试中看到很多端口更改。

标签: java selenium appium


【解决方案1】:
                Explanation: 
                1. Create a command string and then write this command in some .bat file and then execute this .bat file.
                2. (Code not posted here for this ==> )After executing this, you need to put a wait until appium server is started, you can do this by sending request to http://localhost:4723, wait until you get response from this url and then you can initiate the driver.

                    //Getting temp dir
                    String tempDir = System.getProperty("java.io.tmpdir").toString();
                    logFile = new File(tempDir+"\\applog.txt");

                    String commandFile = "C:\\appium_commands.bat";

                    String nodeExe = "C:\\node.exe";
                    String appiumJs = "C:\\node_modules\\appium\\bin\\Appium.js");

                    String strText = "start /B " + nodeExe + "  " + appiumJs + " -g " + logFile.toString() + " --full-reset --command-timeout 90 ";

                    logger.info("****** STARTING APPIUM SERVER USING COMMAND: "+strText);

                    WriteTextInFile(commandFile, strText);
                    Runtime.getRuntime().exec(commandFile);

            *********************  Code to Write Text in File  ***********
            WriteTextInFile(String commandFile, String strText)
           {
            FileWriter writer = new FileWriter(file);
            writer.write(strText);
            writer.close();
          }

          ************ Code to Send GET Request and Check Response *******
    //In this code, you can keep on checking until you get NON-Empty //result.tostring(); Also download and add apache httpclient-4.XXX.jar and //httpcore-4.XXX.jar to your project.

    import java.io.BufferedReader;
    import java.io.InputStreamReader;

    import org.apache.http.client.methods.CloseableHttpResponse;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.conn.HttpHostConnectException;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;

    public class Test {

        public static void main(String[] args) {

            String ServerURL = "http://localhost:4723";
            StringBuffer result = new StringBuffer();
            CloseableHttpClient httpclient = null;
            CloseableHttpResponse response = null;

            try 
            {
                httpclient = HttpClients.createDefault();

                ServerURL = ServerURL.replace("%%", "").trim();
                HttpGet GetRequest = new HttpGet(ServerURL);
                try{
                    response = httpclient.execute(GetRequest);
                }catch(HttpHostConnectException h){
                    System.out.println("Couldn't connect to host: "+ServerURL);
                }

                BufferedReader rd = null;
                try{
                    rd = new BufferedReader( new InputStreamReader(response.getEntity().getContent()));
                }catch(NullPointerException e){
                    System.out.println("no response received.. ");
                }

                if(rd != null)
                {
                    String line = "";
                    while ((line = rd.readLine()) != null)
                    {
                        result.append(line);
                    }
                }
            } 
            catch(Exception e)
            {
            }
            finally 
            {
                try{
                    response.close();
                    httpclient.close();
                }
                catch(Exception e){
                }

            System.out.println("Final Response: "+result.toString());
            }
        }

    }

*********** node.json to be used with appium ***********



{
 "capabilities":
  [
{
"version":"4.4.2",
"maxInstances": 3,
"platformName":"ANDROID"
}
],
"configuration":
{
"cleanUpCycle":2000,
"timeout":30000,
"proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
"url":"http://WHERE_APPIUM_RUNNING_IP:4723/wd/hub",
"host": "WHERE_APPIUM_RUNNING_IP",
"port": 4723,
"maxSession": 6,
"register": true,
"registerCycle": 5000,
"hubPort": 4444,
"hubHost": "WHERE_GRID_RUNNING_IP"
}
}

【讨论】:

  • 如何创建命令字符串并在 bat 文件中写入命令。我了解如何将任何文本文件保存为 .bat 可能是您的意思。另外,您能否解释一下如何向localhost:4723 发送请求。任何形式的回应将不胜感激。
  • 我在回答中添加了代码,可以用来发送GET请求和检查响应。
  • 好吧,我的意思和你说的一样,将文本保存为 .bat 文件。
  • 我正在尝试在 Selnium Grid 中注册一个 Appium 节点,但总是报错:node.exe node_modules\appium\bin\appium.js --nodeconfig "C:\XXX\ IXXX\xxx.json" -p 4723 -U d66d8f10 信息:欢迎使用 Appium v​​1.4.16 (REV ae6877eff263066b26328d457bd285c0cc62430d) 信息:Appium REST http 接口监听器开始于 0.0.0.0:4723 信息:[调试] 非默认服务器参数:{ "udid":"d66d8f10","nodeconfig":"C:\\XXX\\XXX\\xxx.json"} 信息:控制台日志级别:调试错误:无法加载节点配置文件以向网格注册我能够成功启动 Selenium Grid 服务器并访问它。
  • 我能够将 appium 节点注册到网格,您能否检查您的 node.json 是否实际存在于给定位置,或者它是否为正确的 json 格式,只需粘贴 node.json 的内容到 jsonlint.com 看看是否可以,我将整个 node.json 内容粘贴到我的答案中,它可能会对你有所帮助。谢谢。
【解决方案2】:

在您分享屏幕截图之前,我只是猜测更改为default 端口可能会帮助您成功创建会话:

command.addArgument("--bootstrap-port");
command.addArgument("4723");

一般的一行命令如下:

String command = "/Applications/Appium.app/Contents/Resources/node/bin/node /Applications/Appium.app/Contents/Resources/node_modules/appium/lib/server/main.js --command-timeout 120 --session-override" + nativelib + " --platform-name " + mobileOS + " --log-timestamp -a 0.0.0.0 -p "+4723; //+" --chromedriver-port "+chromePort+" -bp "+bootstrapPort;

在哪里

nativelib = " --native-instruments-lib"; //for iOS
nativelib = ""; //for android

mobileOS = "iOS"; //for iOS
mobileOS = "Android"; //for Android

【讨论】:

  • 我想我明白问题出在哪里,我可以以编程方式启动 appium 服务器,但是由于 UnreachableBrowserException 导致该代码失败............驱动程序信息:驱动程序版本:安卓驱动。但神奇的是,如果我从 UI 手动启动 appium 服务器,则不会发生故障。
  • 是的,我可以分享截图。
  • 你可以在问题中附上同样的内容
【解决方案3】:

前置条件:

  1. 确保 nodejs 已安装在您的系统中
  2. 确保您的系统中安装了 appium。

--在下面给定的脚本中输入两者的位置。

public class mainClass{

AppiumDriverLocalService service;

@Test

public void  startSErver(){


    service=AppiumDriverLocalService.buildService(new AppiumServiceBuilder()
            .usingDriverExecutable(new File("C:\\Program Files\\nodejs\\node.exe"))
            .withAppiumJS(new File("C:\\Program Files (x86)\\Appium\\node_modules\\appium\\bin\\appium.js"))
            .withIPAddress("127.0.0.1").usingPort(4723));

 service.start(); // To start the appium  server

【讨论】:

    猜你喜欢
    • 2023-02-12
    • 1970-01-01
    • 2020-07-21
    • 2017-10-15
    • 2017-05-18
    • 1970-01-01
    • 1970-01-01
    • 2015-10-30
    • 2018-02-17
    相关资源
    最近更新 更多