Sunday 27 December 2015

Twin Automation Tool - Automate windows calculator application using java client library.

Watch Demo
 

Prerequisites:
- .NET framework installed (version 3.0 or above).

Check Which Version of Microsoft .NET Framework is Installed in your Windows OS?
.Net Framework version can be verified by running below command in command prompt.
wmic /namespace:\\root\cimv2 path win32_product where "name like '%%.NET%%'" get version
 
Reference: http://www.askvg.com/how-to-check-which-version-of-microsoft-net-framework-is-installed-in-windows/ 

Download twin-rc server and twin-java client library from
https://code.google.com/archive/p/twin/downloads 
 

unzip the twin-1.0.zip
 

click 'SharpClaws.exe' to launch twin-rc.
 

Verify twin-rc is running by typing the url http://<<IP Address of machine>>:4444

 By default only 'notepad' application is configured with 'twin-rc'.
 
copy the 'twin-client-standalone-1.0.jar' to the lib folder of the project and set the java build path.
 

twin-ide can be accessed from http://<<IP Address of machine>>:4444/ide


If you are unable to access the twin-ide from browser, then click on link 'standalone app' which downloads 'ide.jar'

For working with windows applications other than 'notepad', add the target application details in 'sharpclaws.xml'.


adding the calculator application details in 'sharpclaws.xml' file.



stop the twin-rc server, if it is already running.
re-launch the twin-rc server.
 

click 'SharpClaws.exe' to launch twin-rc.
 

Launch 'ide.jar', which will launch twin-ide.


twin-ide is launched.
select 'calculator' application and click on 'Connect' button.
 

Expand the tree to identify the elements.


Sample Program:
import static org.ebayopensource.twin.Criteria.className;
import static org.ebayopensource.twin.Criteria.id;
import static org.ebayopensource.twin.Criteria.type;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.ebayopensource.twin.Application;
import org.ebayopensource.twin.Criteria;
import org.ebayopensource.twin.Element;
import org.ebayopensource.twin.TwinException;
import org.ebayopensource.twin.element.Pane;
import org.ebayopensource.twin.element.Window;

public class CalculatorTest {
      static final String RC_URL = "http://127.0.0.1:4444";
      static final String APPLICATION = "calculator";
   
      private Map<Integer,String> calcNumPad_ObjectRepository;
      private Map<String,String> calcOperPad_ObjectRepository;
      {
       //Object Repository for Calculator Numbers
       calcNumPad_ObjectRepository = new HashMap<Integer,String>();
       calcNumPad_ObjectRepository.put(0, "130");
       calcNumPad_ObjectRepository.put(1, "131");
       calcNumPad_ObjectRepository.put(2, "132");
       calcNumPad_ObjectRepository.put(3, "133");
       calcNumPad_ObjectRepository.put(4, "134");
       calcNumPad_ObjectRepository.put(5, "135");
       calcNumPad_ObjectRepository.put(6, "136");
       calcNumPad_ObjectRepository.put(7, "137");
       calcNumPad_ObjectRepository.put(8, "138");
       calcNumPad_ObjectRepository.put(9, "139");

       //Object Repository for Calculator Operators
       calcOperPad_ObjectRepository = new HashMap<String,String>();
       calcOperPad_ObjectRepository.put("+", "93");
       calcOperPad_ObjectRepository.put("-", "94");
       calcOperPad_ObjectRepository.put("*", "92");
       calcOperPad_ObjectRepository.put("/", "91");
       calcOperPad_ObjectRepository.put("=", "121");
       calcOperPad_ObjectRepository.put("clear", "81");
       }
   
      private static Pane pane = null;
      private static Criteria criteria = null;
      private static Window window = null;
     
      //Keypad Panel
      private static Element keyPadPanel = null;
      private static String SCREENSHOT_DIR = System.getProperty("user.dir")+"/Screenshots/";
   
      public static void main(String[] args) throws TwinException, IOException, InterruptedException{
        Application app = null;

        try{
            app = launchApplication();
            CalculatorTest ct = new CalculatorTest();
          
            //Clear Screenshots folder
            clearScreenshotsFolder();
          
            //Perform Addition
            System.out.println("Addition of 1,3 - Actual Results: "+ct.add(1,3)+",  Expected Results: 4");
            window.getScreenshot().save(new File(SCREENSHOT_DIR + "Add_1_and_3.png"));
          
            System.out.println("Addition of 17,3 - Actual Results: "+ct.add(17,3)+",  Expected Results: 20");
            window.getScreenshot().save(new File(SCREENSHOT_DIR + "Add_17_and_3.png"));
                      
            //Perform Subtraction
            System.out.println("Subtraction of 5,1 - Actual Results: "+ct.subtraction(5,1)+",  Expected Results: 4");
            window.getScreenshot().save(new File(SCREENSHOT_DIR + "Subtraction_5_and_1.png"));
          
            System.out.println("Subtraction of 90,7 - Actual Results: "+ct.subtraction(90,7)+",  Expected Results: 83");
            window.getScreenshot().save(new File(SCREENSHOT_DIR + "Subtraction_90_and_7.png"));
            
            //Perform Multiplication
            System.out.println("Multiplication of 8,2 - Actual Results: "+ct.multiplication(8,2)+",  Expected Results: 16");
            window.getScreenshot().save(new File(SCREENSHOT_DIR + "Multiplication_8_and_2.png"));

            System.out.println("Multiplication of 15,4 - Actual Results: "+ct.multiplication(15,4)+",  Expected Results: 60");
            window.getScreenshot().save(new File(SCREENSHOT_DIR + "Multiplication_15_and_4.png"));
            
            //Perform Division
            System.out.println("Division of 9,3 - Actual Results: "+ct.division(9,3)+",  Expected Results: 3");
            window.getScreenshot().save(new File(SCREENSHOT_DIR + "Division_9_and_3.png"));

            System.out.println("Division of 100,2 - Actual Results: "+ct.division(100,2)+",  Expected Results: 50");          
            window.getScreenshot().save(new File(SCREENSHOT_DIR + "Division_100_and_2.png"));
          
        }finally{
            //Close the application
            app.close();
        }
    }
     
    private static void clearScreenshotsFolder(){
        for (File f : new File(SCREENSHOT_DIR).listFiles())
        {
            if (f.isFile() && f.exists())
            { f.delete(); }
        }           
    }
   
    private static Application launchApplication() throws MalformedURLException{
        //URL of the remote control, connect to rc
        Application app = new Application(new URL(RC_URL));
          
        //launch application 'calculator' on remote control.      
        app.open(APPLICATION, null);

        window = app.getWindow();
        pane = window.getChild(type(Pane.class));
        criteria = type(Pane.class).and(className("#32770"));

        //Keypad Panel
        keyPadPanel = pane.getChildren(criteria).get(1);
       
        return app;              
    }
   
      //Perform 'Addition'.
     public int add(int a, int b) throws InterruptedException{
      performOperation("clear");
     
      clickNumber(a);
      performOperation("+");
      clickNumber(b);
      performOperation("=");
     
      return Integer.parseInt(getResults().trim());
     }  
   
      //Perform 'Subtraction'.
     public int subtraction(int a, int b) throws InterruptedException{
      performOperation("clear");
     
      clickNumber(a);
      performOperation("-");
      clickNumber(b);
      performOperation("=");

      return Integer.parseInt(getResults().trim());
     }
   
      //Perform 'Multiplication'.
     public int multiplication(int a, int b) throws InterruptedException{
      performOperation("clear");
     
      clickNumber(a);
      performOperation("*");
      clickNumber(b);
      performOperation("=");

      return Integer.parseInt(getResults().trim());
     }

     //Perform 'Division'.
     public int division(int a,int b) throws NumberFormatException, InterruptedException{
      performOperation("clear");
      clickNumber(a);
      performOperation("/");
      clickNumber(b);
      performOperation("=");
     
      return Integer.parseInt(getResults().trim());
     }
   
     //Click the Number in the calculator application.
     private void clickNumber(int number) throws InterruptedException{
        String sNumber = String.valueOf(number);
        for(int i = 0; i < sNumber.length(); i++) {
            String id_ = calcNumPad_ObjectRepository.get(Character.digit(sNumber.charAt(i), 10));
            keyPadPanel.getChildren(id(id_)).get(0).click();
            Thread.sleep(1000);
        }
     }
   
     //Perform operations.
     private void performOperation(String controlID) throws InterruptedException{
      String id_ = calcOperPad_ObjectRepository.get(controlID);
      keyPadPanel.getChildren(id(id_)).get(0).click();    
        
      Thread.sleep(1000);
     }    
   
     //Fetch the results after performing the operations
     private String getResults() throws InterruptedException{
      Thread.sleep(1000);
      Element resultPanel = pane.getChildren(criteria).get(0);
      String result = (resultPanel.getChildren().get(2)).getName();
     
      return result;
     }    
}




 

Related Links

Automating Calculator application using Java and Sikuli.
http://automation-home.blogspot.in/2014/09/AutomateCalcAppWithJavaAndSikuli.html

Java and AutoIt - Automating the calculator application using autoitx4java
http://automation-home.blogspot.in/2015/06/java-and-autoit-automating-calculator-application.html

Wednesday 23 December 2015

Appium - Differences in executing the web automation script in Mobile and PC.

Below video demo demonstrates differences for web automation in mobile and pc.

Below script can be executed in mobile and pc.
Sample Program:
import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import io.appium.java_client.remote.MobileCapabilityType;

public class SampleBrowserTest {
   
 public static void main(String[] args) throws MalformedURLException, InterruptedException {

  String sourceToTest = "mobile";
 
  WebDriver driver = sourceToTest.equals("mobile") ? getAppiumDriverForChrome() : getWebDriverForChrome();     
  driver.get("http://www.google.com");  

  driver.findElement(By.id("lst-ib")).sendKeys("java");
  driver.findElement(By.className("sbico")).click();
  //driver.findElement(By.xpath("//h3/a")).click();

  driver.quit();
 }

 private static WebDriver getWebDriverForChrome(){
     System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"//drivers//chromedriver.exe");
     return new ChromeDriver();
 }

 private static WebDriver getAppiumDriverForChrome() throws MalformedURLException{
    
     DesiredCapabilities capabilities = new DesiredCapabilities();
     capabilities.setCapability(MobileCapabilityType.DEVICE_NAME,"");
     //capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "4.4.4");
     capabilities.setCapability(MobileCapabilityType.BROWSER_NAME,"Chrome");
     capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
    
     return new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
 }

}
 

Above script can be re-written using 'AppiumDriver', but the script can be executed only in mobile web.
Sample Program:
import java.net.MalformedURLException;
import java.net.URL;

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

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;

public class SampleBrowserTest {
   
 public static void main(String[] args) throws MalformedURLException, InterruptedException {
      AppiumDriver driver = null;     
 
  try{
      driver =  getAppiumDriverForChrome();
      driver.get("http://www.google.com");  
      Thread.sleep(2000);
      driver.findElement(By.id("lst-ib")).sendKeys("java");
      driver.findElement(By.className("sbico")).click();
      //driver.findElement(By.xpath("//h3/a")).click();
     
  }finally{
      if(driver!=null){
          driver.quit(); 
      }
     
  }
 
 }

 private static AppiumDriver  getAppiumDriverForChrome() throws MalformedURLException{
   
     DesiredCapabilities capabilities = new DesiredCapabilities();
     capabilities.setCapability(MobileCapabilityType.DEVICE_NAME,"");
     //capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "4.4.4");
     capabilities.setCapability(MobileCapabilityType.BROWSER_NAME,"Chrome");
     capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
    
     return new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);   
 }

}

Saturday 19 December 2015

Execute protractor script on real android device using Appium


Prerequisite: 
1. Protractor is already installed.
    Protractor installation steps http://automation-home.blogspot.com/2015/11/protractor-installation.html

3. Java JDK can be downloaded and installed from       http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

4. Set the Environment variable for JAVA
    https://docs.oracle.com/javase/tutorial/essential/environment/paths.html 
    http://www.javatpoint.com/how-to-set-path-in-java

5. Install Android SDK

6. Set the Environment variable for Android SDK.

7. Configuring 'Protractor' in 'WebStorm IDE'.
     Running the 'Protractor' script in 'WebStorm'.
     http://automation-home.blogspot.com/2015/11/configuring-protractor-in-webstorm.html

8. Install Appium Server for windows os.

9. Install Phone Drivers

10. Enable Developer options in the mobile device.

11. Enable USB Debugging in Mobile Device.
    http://automation-home.blogspot.com/2015/12/enable-developer-options.html


Protractor script and configuration changes:
1. Copy 'todo-spec.js' and 'conf.js' from url http://www.protractortest.org/#/

2. Modify the 'conf.js' to include the appium configuration.
Conf.js:
exports.config = {
    seleniumAddress: 'http://localhost:4723/wd/hub',
    specs: ['todo-spec.js'],

    capabilities: {
        browserName: 'chrome',
        platformName: 'android',
        //platformVersion: '',
        deviceName: ''
    }
};

4. Connect real android device to your PC.

5. Run 'adb devices' and verify abd detects your android mobile device.

6. Launch Appium Server

7. Execute the protractor script.
        - Protractor script is executed on your real android device.

Thursday 17 December 2015

Appium - Run appium script on real android device remotely through Wi-Fi

1. Connect android device to your PC via USB.

2. Run the command 'adb devices' in command prompt.
       Verify adb is able to detect your android device.

3. Execute the command 'adb tcpip 5555'
        adb will be restarted to work over tcpip

4. Disconnect your android device from PC(i.e. remove USB connection).

5. Type below command in command prompt.
        adb connect <<IP Address of your mobile device>>

6. Type command 'adb devices', this will display the devices connected(i.e. remotely)

7. Execute the appium script now.

8. You can see the appium script is executed on real android device over Wi-Fi.

Note:
 Make sure your PC and android device is accessing same network.
 In the below video session, EriBank app is already installed in android device.

Watch Demo

Wednesday 16 December 2015

Set the Path for Android SDK

Prerequisite:
Install Android SDK

Copy the path of android sdk location in your PC.


 

Right click on 'Computer' icon and click 'Properties'.
 

Click 'Advanced System Settings'.
Click on 'Advanced' tab.
Click on 'Environment Variables' button.
 

Click ''New' button under 'System Variable'.
Variable name: 'ANDROID_HOME'
Variable value: <<Path of android sdk installation folder>>
                         ex: C:\Users\<<user>>\AppData\Local\Android\sdk
Click 'OK' button.
 

Edit path in system variable.




Append below text in the 'Path' variable.
%ANDROID_HOME%\tools;%ANDROID_HOME%\platform-tools;

Select 'Path' in 'System variables'.
Click on 'Edit' button.
Variable value: <<already existing path>>;%ANDROID_HOME%\tools;%ANDROID_HOME%\platform-tools;
Click 'OK' button.
 

Click 'Windows' + 'R'


Enter 'cmd' in command prompt.


Command prompt is opened.


Check android sdk path is set by typing the command 'adb version' in command prompt and press enterkey.


 

Ruby - Sample Program

Prerequisite:
Eclipse IDE is already installed.
Eclipse IDE can be downloaded from https://eclipse.org/downloads/

Ruby is already installed.
For ruby installation refer http://automation-home.blogspot.com/2015/12/ruby-installation.html

Ruby - Eclipse plugin is already installed.
For ruby eclipse plugin installation refer http://automation-home.blogspot.com/2015/12/ruby-eclipse-plugin.html

1. Launch 'Eclipse IDE', select 'File' > 'New' > 'Other' to create new ruby project.

Select 'Ruby' > 'Ruby Project'


Enter project name and click finish button.








 Create ruby script file.


Enter filename and click finish button.






Execute ruby script.


 

Ruby - Eclipse plugin

Prerequisite:
Eclipse IDE is already installed.
Eclipse IDE can be downloaded from https://eclipse.org/downloads/
 
Ruby is already installed.
For ruby installation refer http://automation-home.blogspot.com/2015/12/ruby-installation.html

Open the Eclipse IDE.
Select 'Help' menu, select 'Install New Software'


Select 'All Available Sites'


type 'Dyna'


Select 'Dynamic Languages Toolkit - Ruby Development  tools'.


Click 'Next' button.


Accept license agreement and click 'Finish' button.


 

 

Select 'Windows' > 'Preferences'.




Select 'Ruby' > 'Interpreters'.
Click on 'Search' button.