Tuesday, 3 January 2017

Procedure to start and stop Appium server programmatically using Java code



Hi Guys 

Here I am explaining the procedure to start and stop the appium server programmatically

I hope all the software required for mobile automation using appium are installed and they are running.

Step 1 : Find the path of node.exe where it is installed 

Its good practice to install appium server in separate drive like "D://" or "E://" drive instead of "C://" drive

In my system, node.exe is installed in the following path

 node_Exe = "D:\\Appium\\node.exe" 

Step 2 : Find the path of Appium.js where it is installed 

In my system, Appium.js is installed in the following path

 appium_js = "D:\\Appium\\node_modules\\appium\\bin\\appium.js" 


Step 3: Now we will write bunch of code to start appium server programmatically


Note : 

## Start_Server()  is method to start appium server


public void Start_Server() throws IOException, InterruptedException
    { 



## Here we combined the path of both node.exe and appium.js server and stored in String variable.


        String Start_server="D:\\Appium\\node.exe  D:\\Appium\\node_modules\\appium\\bin\\appium.js";
        
        process = Runtime.getRuntime().exec(Start_server);
        
        if(process!= null)
        {
            System.out.println("Started Appium Server");
        }
        else
        {
            System.out.println("NOT Started the Appium Server");
        }
        
        Thread.sleep(12000);    
        
        
    } 


Step 4 : Now we will write the bunch of code to stop the appium server programmatically.



## Stop_Server()  is method to stop appium server


public void Stop_Server() throws InterruptedException
    {

        if(process!=null)
        {
            process.destroy();
            Thread.sleep(6000);
            System.out.println("Stopped the appium Server");
        }
    }


Step 5: Now we will reuse above method to start and stop appium server programmatically using an example  


1. now We will create one base class which consist of 3 methods 

   a. Start_Server()
   b. Stop_Server()
   c. Init_App()          ## which initialise app


After combining all those our base class looks like this.


package Day4;

import io.appium.java_client.android.AndroidDriver;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.remote.DesiredCapabilities;

public class Base_Class_drag_drop {
   
    static Process process;
    public static AndroidDriver driver;
   
    //start the server
    public static void Start_Server() throws IOException, InterruptedException
    {
        String Start_server="D:\\Appium\\node.exe  D:\\Appium\\node_modules\\appium\\bin\\appium.js";
       
        process = Runtime.getRuntime().exec(Start_server);
       
        if(process!= null)
        {
            System.out.println("Started Appium Server");
        }
        else
        {
            System.out.println("NOT Started the Appium Server");
        }
       
        Thread.sleep(12000);   
       
       
    }
   
    //Init app
    public static void Init_app() throws MalformedURLException
    {
        DesiredCapabilities capabilities= new DesiredCapabilities();
       
        capabilities.setCapability("deviceName","GT-I9300I");
        capabilities.setCapability("platformName","Android");
        capabilities.setCapability("platformVersion","4.4.4");
       
        capabilities.setCapability("appPackage","com.mobeta.android.demodslv");
        capabilities.setCapability("appActivity","com.mobeta.android.demodslv.Launcher");
       
       
        driver= new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
       
    }
   
   
    //Stop server
   
    public static void Stop_Server() throws InterruptedException
    {

        if(process!=null)
        {
            process.destroy();
            Thread.sleep(6000);
            System.out.println("Stopped the appium Server");
        }
    }
   
   





Step 6 : With the help of Base class we will start and stop the appium server programmatically and also perform drag and drop operation on APK

Entire code will look like this.


package Day4;

import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;

import java.io.IOException;
import java.net.URL;
import java.util.List;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;

public class driver_drag_drop extends Base_Class_drag_drop {

    public static void main(String[] args) throws IOException, InterruptedException {
       

## This call is calling Start_Server() method which will start appium server programmatically
      
    Start_Server(); 



## This call is calling Init_app() method which will initialise the app i.e app will open on mentioned device.


   Init_app();   
       
 
## Here we are performing actual action  

        WebElement ele = driver.findElementById("com.mobeta.android.demodslv:id/activity_title");
        ele.click();
       
       
        List<WebElement> drag_ele = driver.findElementsById("com.mobeta.android.demodslv:id/drag_handle");
        System.out.println(drag_ele.size());
       
       
       
       
        //drag and drop use Touchaction
       
        TouchAction action= new TouchAction(driver);
       
        action.longPress(drag_ele.get(0)).moveTo(drag_ele.get(5)).release().perform();
       
        Thread.sleep(8000);
       
        action.longPress(drag_ele.get(6)).moveTo(drag_ele.get(1)).release().perform();
        Thread.sleep(4000); 






## This call is calling Stop_Server() method which will stop appium server programmatically
     
        
   
        Stop_Server();
       
       
         }



PS: I hope you people enjoyed the blog.

Wednesday, 13 May 2015

Using CSV file with jmeter to perform performance testing for multiple login and logout



Follow the below steps to perform

1.Open Jmeter (After opening jmeter follow the below steps)
2.Click on Test plan and add thread group
3.Add configuration elements(1.Http request default , 2.Http cookie manager , 3.Http Header manager)

4.After adding click on Http request default (Here you need to give URL name here)
5.Now add Http proxy server to workbench
6.Add timer(Constant timer)
7.Now we need to set browser proxy
8.Now start the proxy
9.Perform operation what you want to perform.
10.Stop the proxy.
11.Now keep the request which are required for you omit remaining.
12.Now you want perform login operation so add CSV data set configuration file to request
13.Use those variable in http request default
14.Add the listener which you want to use then run the test
15.Then  run the test