Thursday 16 March 2017

Appium - Caliculator Automation - windows os



This is the first programme,


Setup

Install Android  SDK s and Appium ( no need of node.js / npm)

How to install SDK :
download Android studio it will automatically download SDK.
you can find sdk downloaded here C:\Users\PK\AppData\Local\Android\sdk

How to install Appium:
go to official appium site and download the latest appium and just start appium ( nothing required to configure )










Configure like above, when you Launch your appium, it should show console message as above.

Connect your mobile device via USB cable and choose FileTransfer option in USB options.

to check the device properly connected or not.



Path settings:

SET : ANDROID_HOME to C:\Users\PK\AppData\Local\Android\sdk

SET path : %ANDROID_HOME%\tools ;%ANDROID_HOME%\platform-tools ;

Android_home






Add below dependencies


     only one jar required, it internally contains selenium-java libs


<!-- https://mvnrepository.com/artifact/io.appium/java-client -->
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>5.0.3</version>
</dependency>


import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

public class NewTest {

public static void main(String[] ards) throws MalformedURLException {
WebDriver driver;
// Created object of DesiredCapabilities class.
DesiredCapabilities capabilities = new DesiredCapabilities();

// Set android deviceName desired capability. Set your device name.
capabilities.setCapability("deviceName", "ZY222XRC5L");

// Set BROWSER_NAME desired capability. It's Android in our case here.
capabilities.setCapability(CapabilityType.BROWSER_NAME, "Android");

// Set android VERSION desired capability. Set your mobile device's OS
// version.
capabilities.setCapability(CapabilityType.VERSION, "6.0.1");

// Set android platformName desired capability. It's Android in our case
// here.
capabilities.setCapability("platformName", "Android");

// Set android appPackage desired capability. It is
// com.android.calculator2 for calculator application.
// Set your application's appPackage if you are using any other app.
capabilities.setCapability("appPackage", "com.android.calculator2");

// Set android appActivity desired capability. It is
// com.android.calculator2.Calculator for calculator application.
// Set your application's appPackage if you are using any other app.
capabilities.setCapability("appActivity",
"com.android.calculator2.Calculator");

// Created object of RemoteWebDriver will all set capabilities.
// Set appium server address and port number in URL string.
// It will launch calculator app in android device.
driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"),
capabilities);
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

// Click on DELETE/CLR button to clear result text box before running
// test.
driver.findElements(By.xpath("//android.widget.Button")).get(0).click();

// Click on number 2 button.
driver.findElement(By.name("2")).click();

// Click on + button.
driver.findElement(By.name("+")).click();

// Click on number 5 button.
driver.findElement(By.name("5")).click();

// Click on = button.
driver.findElement(By.name("=")).click();

// Get result from result text box.
String result = driver.findElement(
By.className("android.widget.EditText")).getText();
System.out.println("Number sum result is : " + result);

driver.quit();
}
}



we can locate elements using

C:\Users\PK\AppData\Local\Android\sdk\tools\uiautomatorviewer.bat  tool


Wednesday 15 March 2017

Java reflection

Reflection in Java is a very powerful concept and it’s of little use in normal programming but it’s the backbone for most of the Java, J2EE frameworks. Some of the frameworks that use java reflection are:
JUnit – uses reflection to parse @Test annotation to get the test methods and then invoke it.
Spring – dependency injection, read more at Spring Dependency Injection
Tomcat web container to forward the request to correct module by parsing their web.xml files and request URI.
Eclipse auto completion of method names
Struts
Hibernate
The list is endless and they all use java reflection because all these frameworks have no knowledge and access of user defined classes, interfaces, their methods etc.


http://www.journaldev.com/1789/java-reflection-example-tutorial#reflection-in-java

Tuesday 14 March 2017

json schema validators

http://wilddiary.com/validate-json-against-schema-in-java/

https://jsonschema.net/#/

http://jsonviewer.stack.hu/


sample json schema:
{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "mytitle",
  "description": "my description",
  "type": "object",
  "required": [
    "data",
    "status"
  ],
  "properties": {
    "status": {
      "type": "number"
    },
    "data": {
      "type": "array",
      "minItems": 1,
      "additionalItems": true,
      "items": {
        "required": [
          "myStatus1",
          "myMediaPlanType",
          "myBudget",
          "myAccountID",
          "myAccountName",
          "myNotes",
          "myMediaPlans",
          "myAuditData",
          "myUsage1",
          "myMediaPlansCount",
          "myIsParentCrossBrandCampaign",
          "myCrossBrandIDs",
          "myCrossBrandChildCampaign",
          "myID",
          "myName"
        ],
        "myStatus1": {
          "type": "number"
        },
        "myMediaPlanType": {
          "type": "number"
        },
        "myBudget": {
          "type": "number"
        },
        "myAccountID": {
          "type": "number"
        },
        "myNotes": {
          "type": "string"
        },
        "myMediaPlans": {
          "type": "array"
        },
        "myAuditData": {
          "type": "object",
          "required": [
            "myCreatedByID",
            "myLastUpdatedByID",
            "myCreatedDate",
            "myLastUpdatedDate"
          ],
          "properties": {
            "myCreatedByID": {
              "type": "number"
            },
            "myLastUpdatedByID": {
              "type": "number"
            },
            "myCreatedDate": {
              "type": "string"
            },
            "myLastUpdatedDate": {
              "type": "string"
            }
          }
        },
        "myUsage1": {
          "type": "number"
        },
        "myMediaPlansCount": {
          "type": "number"
        },
        "myIsParentCrossBrandCampaign": {
          "type": "string"
        },
        "myCrossBrandIDs": {
          "type": "array"
        },
        "myCrossBrandChildCampaign": {
          "type": "array"
        },
        "myID": {
          "type": "number"
        },
        "myName": {
          "type": "string"
        }
      }
    }
  }
}




Tuesday 7 March 2017

Write into file in append mode / file writer

package com.google.email;

import java.io.*;
import java.io.IOException;

public class WriteTextUtil {
public void writeInAppendMode(String path, String strFinal) throws IOException {
BufferedWriter bw = null;
FileWriter fw = null;
try {
fw = new FileWriter(path, true);
bw = new BufferedWriter(fw);
PrintWriter out = new PrintWriter(bw);
out.println(strFinal);
out.println("\n");

} finally {
if (bw != null)
bw.close();

if (fw != null)
fw.close();
}
}
}

Saturday 4 March 2017

Appium on OSX

Appium-OSX Tutorial

Required software:

Java: JDK


Any java IDE: Eclipse / Intellij idea

Appium java client : https://mvnrepository.com/artifact/io.appium/java-client/4.1.2

Selenium stand alone server : https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-server-standalone/2.53.0

Xcode : developer.apple.com/xcode/downloads : version 5.1 or higher (i.e 6.1 )
( for opening Iphone simulator)
-          Command line tools (OS X 10.9).pkg
-          Xcode_6.1.1.dmg

Device Configuration : Enable UI automation in mobile (1.Connect your mobile to laptop.  2. Go to settings 3. Developper 4. Enable UI automation

Iphone Simulator verification:

Open Xcode

Click on Create a new Xcode project – Choose single view application – product name : test, 

language : Objective c, Device: Universal
Next
Choose folder name
Create
Click on Run

If it opens a simulator, you are done and then you can close everything.

Installation:

Install Appium  and choose IOS

Click on Apple symbol to add or modify settings

Basic  Tab:

Provide App path : App which you want to automate ( download app in your local  .ipa extension)

Force Device : check the check box and choose your connected device

Platform version: UDID- Unique Device Id: (open xcode -> Devices-> select the device you want to 
test , you can find Identifier , this is the UDID ) (This is basically alias name , give Name: KranthiIphone6)

Full Rest : check the checkbox – every time we run test we have a clean start

Advanced tab:

Xcode path : make sure xcode path pointed to actual installation
Click on launch button
Once you see 200 in the console, you can start inspecting elements
Click on Inspector UI ( magnifier symbol )
This will install the app on your mobile and shows screen in inspector
We can choose any element and find xpath for the same

Appium setup Check:
Click on Appium Doctor button

Code sample:
Open Eclipse
Create a maven project
Add appium-client and selenium server standalone jar to build path

Public class FirstTest{
Public static void main(String[] args){
DesiredCapabilities cap=new DesiredCapabilities();
Cap.setCapabilities(“platformName”,”ios”);
Cap.setCapabilities(“platformVersion”,”8.1”);
Cap.setCapabilities(“deviceName”,” KranthiIphone6”);// this we configured earlier in appium server
IOSDriver driver=new IOSDriver(new URL(http://127.0.0.1:4723/we/hub”),cap);
Driver.manage.timeouts().implicitwiat(60,TimeUnits.SECONDS);
Driver.findElement(By.xpath(“xpath”)).click();
//we can build our own xpaths by copying xpath of the device screen and then we can write xpaths
//xpathtester.com/xpath
Driver.quite();
}