Appium mobile automation
  • About the Book
  • Introduction to Mobile Test Automation
  • Pre-requisites for Appium Verification
  • What is Appium
    • Appium 1.6.0
  • Installation Instructions
    • Installing Android SDK
    • Installing Appium using app
    • Install Appium using npm
    • Installing an App on Emulator
  • Appium For Beginners
    • Understanding Desired Capabilities
    • Desired Capabilities for Android
    • Cucumber-JVM-Appium - Gradle Project
    • Start Appium Server
  • Write your first Android Test
    • How to use UiAutomatorViewer
    • Appium Inspector
  • Execution of Android tests
    • Execute on GenyMotion Emulator
    • Execute on Real Device
  • Appium Advanced
    • How to automate gestures
    • How to Change contexts
    • Remote Debugging using chrome
    • Running multiple Appium Server for parallel execution
Powered by GitBook
On this page

Was this helpful?

  1. Appium For Beginners

Start Appium Server

Appium has recently made some changes and now you can actually write a couple line of code and start appium server.

Appium has introduced a class called AppiumDriverLocalService which will help you do this. There are two ways you can write code:

  1. If you have appium installed via npm then try the below lines of code.

     service = AppiumDriverLocalService.buildDefaultService();
     service.start();

    For stopping the appium server use:

     service.stop();
  2. If you don't have appium installed via npm then try the below lines of code. For your local machine you might want to change the path accordingly. In the below example, I am showing if the OS is mac then use the installed Appium to start the server. If the OS is windows use the npm installed appium. This is just for the demonstration, ideally you should have common strategy whether it is Mac or Windows.

    String osName = System.getProperty("os.name");

     if (osName.contains("Mac")) {
         service = AppiumDriverLocalService.buildService(new AppiumServiceBuilder()
                 .usingDriverExecutable(new File("/Applications/Appium.app/Contents/Resources/node/bin/node"))
                 .withAppiumJS(new File("/Applications/Appium.app/Contents/Resources/node_modules/appium/bin/appium.js"))
                 .withIPAddress("127.0.0.1")
                 .usingPort(port)
                 .withLogFile(new File("target/"+deviceUnderExecution+".log")));
     } 
     else if (osName.contains("Windows")) {
         service = AppiumDriverLocalService.buildService(new AppiumServiceBuilder()
                 .usingPort(port)
                 .withLogFile(new File("target/"+deviceUnderExecution+".log")));
     } 
     else {
         Assert.fail("Unspecified OS found, Appium can't run");
     }
    
     System.out.println("- - - - - - - - Starting Appium Server- - - - - - - - ");
     service.start();

In the code above, I have the port as parameter for parallel runs on device.

However for windows you can use npm or the appium desktop so as to launch the server.

To launch the appium server via code and we are generating an appium log file so as to capture the appium log which is again parameterized based on the device under execution.

service = AppiumDriverLocalService.buildService(new AppiumServiceBuilder()
                    .usingDriverExecutable(new File("/Applications/Appium.app/Contents/Resources/node/bin/node"))
                    .withAppiumJS(new File("/Applications/Appium.app/Contents/Resources/node_modules/appium/bin/appium.js"))
                    .withIPAddress("127.0.0.1")
                    .usingPort(port)
                    .withLogFile(new File("target/"+deviceUnderExecution+".log")));
PreviousCucumber-JVM-Appium - Gradle ProjectNextWrite your first Android Test

Last updated 5 years ago

Was this helpful?