Java Control Appium Server Start/stop

Source: Internet
Author: User
Tags appium

Believe that many people will encounter this scenario, in the Appium automation with the Windows OS, not a good background run, every time you start Appium server:

    • Manually click with Appium GUI edition
    • is to start appium on Cmd line.

If you want to implement CI, it is not feasible to use the Appium GUI, because if the appium session cannot be created during the run case, you must restart Appium server, and you cannot automatically get the corresponding parameters to start directly appium

Then you can only use command line at this time.

PS: Use command line to add Appium related to environment variables

Add in Path

; C:\Program Files (x86) \appium\node_modules\.bin;

and verify it on command line.

If this occurs, the Appium starts successfully with the default parameters

In fact, this default startup is: C:\Program Files (x86) \appium\node_modules\.bin\appium.bat

Use the default parameters, if you want to use the specified IP and port log and other content needs to add parameters

For example, use:

C:\users\test>appium-a 127.0.0.1-p 1235info:welcome to Appium v1.4.16 (REV ae6877eff263066b26328d457bd285c0cc62430d) Info:appium REST HTTP interface listener started on 127.0.0.1:1235info: [ Debug] Non-default server args: {"address": "127.0.0.1", "Port": 1235}info:console Loglevel:debug

Specific parameters are no longer detailed here, you can use Appium--help

Well, it's good to call this command directly with Java.

And so it was written:

  Public voidexcutecmd (String comand) {Runtime rt=Runtime.getruntime (); Runtimeexec Rte=Newruntimeexec ();        Streamwrapper error, output; Try{Process proc=rt.exec (comand); Error= Rte.getstreamwrapper (Proc.geterrorstream (), "ERROR"); Output= Rte.getstreamwrapper (Proc.getinputstream (), "OUTPUT"); BufferedReader Stdinput=NewBufferedReader (NewInputStreamReader (Proc.getinputstream ()));            String s;  while((s = stdinput.readline ())! =NULL) {System.out.println (s); if(S.contains ("Appium REST http") {System.out.println ("Started!");            }} error.start ();            Output.start (); Error.join (3000); Output.join (3000); System.out.println ("Output:" + output.message + "\nerror:" +error.message); } Catch(IOException e) {e.printstacktrace (); } Catch(interruptedexception e) {e.printstacktrace (); }    }

Using the Java Runtime class

Pass in the appropriate command, then execute and display the input

And then find that it doesn't work because you can't execute the following code

You know, it's called thread blocking.

Then only one method of non-threading blocking can be found

This time to find a commons-exec project to solve the problem

According to the official documentation, if you use non-blocking execution, you can do this:

    • First, create a non-blocking handler Defaultexecuteresulthandler, which is specifically designed to handle non-blocking
    • When creating a watchdog to monitor the output, set timeout time to 60s
    • Create an actuator setting exit code of 1, which represents a successful execution

Note that a waitfor time must be set here, and if not set, an error will be

  Resulthandler.waitfor (5000);
 Public StaticString Appiumserverstart = "C:\\Program Files (x86) \\Appium\\node_modules\\.bin\\appium.cmd";  Public Static voidStartServer ()throwsIOException, interruptedexception {StartServer ("4723"); //runtimeexec appiumobj = new Runtimeexec (); //Appiumobj.excutecmd (appiumserverstart);Defaultexecuteresulthandler Resulthandler =NewDefaultexecuteresulthandler (); CommandLine CommandLine=Commandline.parse (Appiumserverstart); Executewatchdog Dog=NewExecutewatchdog (60 * 1000); Executor Executor=NewDefaultexecutor (); Executor.setexitvalue (1);        Executor.setwatchdog (dog);        Executor.execute (CommandLine, Resulthandler); Resulthandler.waitfor (5000); System.out.println ("Appium Server Start"); }

The above code is implemented using the default port to start Appium server, if you encounter Appium port is occupied, how to start failure?

My strategy is to kill the process that consumes this port first:

You can try to use command line

cmd/c echo off & for/f "Usebackq tokens=5"%a in (' Netstat-nao ^| findstr/r/C: "4723" ') Do (for/f "USEBACKQ"%b I N (' tasklist/fi ' PID eq%a "^| findstr/i node.exe ') do taskkill/f/pid%a)

    /**     * @authorYoung *@paramAppiumserviceport *@throwsexecuteexception *@throwsIOException*/     Public Static voidStopappiumserver (String appiumserviceport)throwsexecuteexception, IOException {exectorutils.runwithwatchdog ("Cmd/c echo off & for/f \" Usebackq tokens=5\ "%a in" + "(' Netstat-nao ^| FINDSTR/R/c:\ "" + Appiumserviceport + "\" ') Do (for/f \ "usebackq\"%b in "+" (' tasklist/fi \ ' PID eq%a \" ^| findstr/i node.exe ') do taskkill/f/pid%a) "); }

This allows you to start the appropriate Appium server in each test case and give the specified parameters.

Related information: http://commons.apache.org/proper/commons-exec/tutorial.html

Java Control Appium Server Start/stop

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.