I want to run automation scripts and require to start and stop appium server automatically through my scripts using java. Please provide all the files required for the same!!
public static final void startAppium() throws IOException, InterruptedException {
CommandLine command = new CommandLine("/Applications/Appium.app/Contents/Resources/node/bin/node");
command.addArgument("/Applications/Appium.app/Contents/Resources/node_modules/appium/bin/appium.js", false);
command.addArgument("–address", false);
command.addArgument(“127.0.0.1”);
command.addArgument("–port", false);
command.addArgument(“4723”);
command.addArgument("–log-level");
command.addArgument(“warn”);
command.addArgument("–session-override",true);
command.addArgument("–local-timezone",true);
//command.addArgument("–log-no-colors", true);
command.addArgument("–backend-retries");
command.addArgument(“6”);
//command.addArgument("–force-quit-instruments",true);
//command.addArgument("–log-g /Users/slinger/Desktop/appium.log");
//command.addArgument("–native-instruments-lib",true);
//command.addArgument("–automation-name");
//command.addArgument(“Jitu_iPad_Automation”);
//command.addArgument("–ignoreUnimportantViews",true);
//command.addArgument("–autoAcceptAlerts",false);
command.addArgument("–show-ios-log");
//command.addArgument("–full-reset", false);
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
DefaultExecutor executor = new DefaultExecutor();
executor.setExitValue(0);
executor.execute(command, resultHandler);
Thread.sleep(5000);
System.out.println(“Appium server started”);
}
public static void stopAppium()
{
Runtime r = Runtime.getRuntime();
try {
proc = r.exec(“sh stop_appium_server.sh”);
Thread.sleep(10000);
testLog.debug(“Appium server stopped”);
}catch(Exception e)
{
e.printStackTrace();
}
//loggerObj.info(“Appium server sttopped”);
}
It is not working… after calling the function nothing happens and it goes on the nxt part.
Also what should i initialize the proc variable to?
Then you need to set the capabilities bro
Above one will run the Appium server
capabilities = new DesiredCapabilities();
capabilities.setCapability("udid",config.getProperty("udid"));
capabilities.setCapability("platformVersion", config.getProperty("platformVersion"));
capabilities.setCapability("deviceName", config.getProperty("deviceName"));
capabilities.setCapability("newCommandTimeout",480000);
//capabilities.setCapability("waitForAppScript",true);
//capabilities.setCapability("ignoreUnimportantViews",true);
//capabilities.setCapability("autoDismissAlertss",true);
//capabilities.setCapability("autoAcceptAlerts", true);
//capabilities.setCapability("app", config.getProperty("appPath"));
capabilities.setCapability("bundleId", config.getProperty("appBundleID"));
testLog.info("Loading capabilities");
driver = new IOSDriver(new URL("http://127.0.0.1:4723/wd/hub"),capabilities);
testLog.info("Loading Capability Successful");
Thread.sleep(10000);
That i have already added and that is working as well for:
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setCapability(CapabilityType.BROWSER_NAME, “Android”);
desiredCapabilities.setCapability(CapabilityType.VERSION, “5.1”);
desiredCapabilities.setCapability(“deviceName”, “Nexus_4_API_22”);
//desiredCapabilities.setCapability(CapabilityType.PLATFORM, “Android”);
desiredCapabilities.setCapability(“appPackage”, “com.sonicwall.mysonicwall.qa”);
desiredCapabilities.setCapability(“appActivity”, “com.sonicwall.mysonicwall.Activities.LoginActivity”);
desiredCapabilities.setCapability(“platformName”, “Android”);
appdriver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"),desiredCapabilities);
return appdriver;
But the server is not starting using the function startAppium()
Install team viewer and share the id and password . I can directly look on your set up
can u please guide me as to what these true/ false flag means in those commands?
also the session is getting terminated before the script ends, what do u suggest for that?
This is a complete, start to finish Appium Test. Step 1 starts the appium server, step 6 shuts it down.
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import io.appium.java_client.remote.MobilePlatform;
import io.appium.java_client.service.local.AppiumDriverLocalService;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
public class startUpTest {
private static AppiumDriverLocalService service;
private static AndroidDriver driver;
private static DesiredCapabilities dc;
public static void main(String[] args) throws IOException, InterruptedException {
stepOne();
stepTwo();
stepThree();
stepFour();
stepFive();
stepSix();
}
//Start the Appium Server
private static void stepOne() {
service = AppiumDriverLocalService.buildDefaultService();
service.start();
}
//Define your desired capabilities
private static void stepTwo() {
dc = new DesiredCapabilities();
dc.setCapability(MobileCapabilityType.DEVICE_NAME, "SAMSUNG_Android");
dc.setCapability(MobileCapabilityType.UDID, "0123456789nc88hh");
dc.setCapability(MobileCapabilityType.PLATFORM_NAME, MobilePlatform.ANDROID);
dc.setCapability(MobileCapabilityType.APP, "com.something.yellow");
dc.setCapability(MobileCapabilityType.APP_ACTIVITY, "com.something.yellow.app_launch.activities.LaunchActivity");
dc.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, "300");
}
//Initiate your driver
private static void stepThree() throws MalformedURLException{
driver = new AndroidDriver(new URL("http://localhost:4723/wd/hub"), dc);
}
//Run your tests
private static void stepFour() throws InterruptedException{
//Run test
Thread.sleep(10000);
}
//Quit your driver
private static void stepFive() {
driver.quit();
}
//Stop the Appium server
private static void stepSix() {
service.stop();
}
}
Running server from your tests is a bad idea for a number of reasons. You tie up tests and infrastructure, so the problem in one place will affect the other and you would not be able detect where the problem fast. You should carefully treat all the unexpected things in your tests to be sure you shut down the server correctly, otherwise you will not be able to start it next time. Try to isolate as much as you can to get consistent test results.