How to start the Appium server through the java code. I have tried some code but that failed. Suggestions are welcome.
CODE
package startup;
import io.appium.java_client.AppiumDriver;
import java.io.IOException;
import java.net.URL;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecuteResultHandler;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
public class Startup {
static RemoteWebDriver driver = null;
public void startAppiumServer() throws IOException, InterruptedException {
CommandLine command = new CommandLine("cmd");
command.addArgument("/c");
command.addArgument("D:\\SOFTWARES\\AppiumForWindows-1.2.4.1\\Appium\\node.exe");
command.addArgument("D:\\SOFTWARES\\AppiumForWindows-1.2.4.1\\Appium\\node_modules\\appium\\lib\\appium.js");
command.addArgument("--address", false);
command.addArgument("127.0.0.1");
command.addArgument("--port", false);
command.addArgument("4723");
command.addArgument("--full-reset", false);
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
DefaultExecutor executor = new DefaultExecutor();
executor.setExitValue(1);
executor.execute(command, resultHandler);
}
public void stopAppiumServer() throws IOException {
CommandLine command = new CommandLine("cmd");
command.addArgument("/c");
command.addArgument("Taskkill /F /IM node.exe");
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
DefaultExecutor executor = new DefaultExecutor();
executor.setExitValue(1);
executor.execute(command, resultHandler);
}
public static void startApplication() throws Exception{
System.out.println("application has started...................");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
capabilities.setCapability("deviceName","emulator-5554");
capabilities.setCapability("platformVersion","4.3");
capabilities.setCapability("platformName","Android");
capabilities.setCapability("app","D:/SOFTWARES/Apks/autoInsurancelatest.apk");
driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
System.out.println("Appium SetUp for Android is successful and Appium Driver is launched successfully");
}
public static void main(String as[]) throws Exception
{
Startup aps = new Startup();
System.out.println("Starting Server...");
aps.startAppiumServer();
System.out.println("Server Started....");
try{Thread.sleep(5000);}catch(Exception e){}
Startup.startApplication();
try{Thread.sleep(5000);}catch(Exception e){}
System.out.println("Stopping Server");
aps.stopAppiumServer();
System.out.println("Server Stopped");
}
}
CONSOLE ERROR:
Starting Server…
Server Started…
application has started…
Exception in thread “main” org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.
Build info: version: ‘2.45.0’, revision: ‘32a636c’, time: ‘2015-03-05 22:01:35’
System info: host: ‘IMPC1388’, ip: ‘172.16.11.2’, os.name: ‘Windows 7’, os.arch: ‘amd64’, os.version: ‘6.1’, java.version: ‘1.7.0_51’
Driver info: driver.version: RemoteWebDriver
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:593)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:240)
at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:126)
at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:153)
at startup.Startup.startApplication(Startup.java:63)
at startup.Startup.main(Startup.java:82)
Caused by: org.apache.http.conn.HttpHostConnectException: Connect to 127.0.0.1:4723 [/127.0.0.1] failed: Connection refused: connect
at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:142)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:319)
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:363)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:219)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:86)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:72)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)
at org.openqa.selenium.remote.internal.ApacheHttpClient.fallBackExecute(ApacheHttpClient.java:126)
at org.openqa.selenium.remote.internal.ApacheHttpClient.execute(ApacheHttpClient.java:72)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:133)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:572)
… 5 more
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:72)
at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:125)
… 18 more