Sunday 19 June 2016

Appium Error: Failed to start an Appium session, err was: Error: Screen did not unlock

The below error occured in Appium during the execution

error: Failed to start an Appium session, err was: Error: Screen did not unlock


https://github.com/appium/appium/issues/5255


While we  checked. issue is with nexus 4(Android 4.4.2) and nexus 5(Android 5.0.1). It is working fine with nexus 7(Android 4.3)

This error occured while taking screenshot automatically and make unlock.

Appium Error: Activity used to start app doesn't exist or cannot be launched! Make sure it exists and is a launchable activity

The below error occured in appium during execution

Error: Activity used to start app doesn't exist or cannot be launched! Make sure it exists and is a launchable activity


Solution : If the we provide incorrect activity name then we are getting this error.

Android Emulator Issue: The home button, Menu button, back button, Zoom Button is not woking in Android Emulator


The home button, Menu button, back button, Zoom Button is not wotking in Android Emulator, So please use below steps for resolve this issue

-> Goto below path in machine


C:\Users\<User_ID>\.android\avd\<AVD_Name>.avd\config.ini


-> Open the config.ini file

-> change the below settings

from
hw.mainKeys=no   change to  
hw.mainKeys=yes

-> Now restart the AVD then we got home button, Menu button, back button, Zoom Button is woking in Android Emulator


hw.mainKeys=yes

Saturday 18 June 2016

Appium - Code for Automate the Calculator App in Android Mobile


Please follow below steps to Automate the mobile application in Appium


Emulator Setup for Android Mobile -> Setup




The First Appium code for Automate the calculator App



import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL; 
import java.util.concurrent.TimeUnit; 

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By; 
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.remote.CapabilityType; 
import org.openqa.selenium.remote.DesiredCapabilities; 
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterTest; 
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;


public class Mobile 



WebDriver driver;

@BeforeTest 

public void setUp() throws MalformedURLException

{

// Created object of DesiredCapabilities class.
DesiredCapabilities capabilities = new DesiredCapabilities();

// Set android deviceName desired capability. Set your device name. 
capabilities.setCapability("deviceName", "sdk"); 
// 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, "4.4.4"); 
// 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); 
}

@Test
public void Sum() throws IOException 


System.out.println("Appium");


driver.findElements(By.xpath("//android.widget.Button")).get(0).click();
// driver.findElement(By.name("CLR")).click(); 
// Click on number 2 button. 
driver.findElement(By.name("6")).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);

File srcFile=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(srcFile, new File(System.getProperty("user.dir")+"\\ScreenShots.png"));


}

@AfterTest public void End() { driver.quit(); } 

}

How to find app Launcher activity name in Appium



We have two options for find app activity name

1) By using Command Prompt
2) Manifest Info
3) Hierarchy Viewer


1) By using Command Prompt

Please find below steps to find app activity name using command prompt.


-> app APK package name. EX : com.android.calculator2

-> Launch the Android Emulator and wait till starts properly.

-> Goto command prompt and goto E:\android-sdks\platform-tools and verify the device connected properly (not offline). by using below command

E:\SDK\platform-tools>adb devices

List of devices attached
emulator-5554   device

-> Open the reuired app and keep on active in Emulator

-> GOTO and command prompt and use below commands

E:\android-sdks\platform-tools>adb logcat >log.txt

-> Now its generated log text file. Here you can search by app Package name (com.android.calculator2) then we will have log like

I/ActivityManager(  878): Displayed com.android.calculator2/.Calculator: +7s332ms


Here com.android.calculator2.Calculator is app activity name.



2) By using AndroidManifest.xml Info



http://software-testing-tutorials-automation.blogspot.in/2015/10/find-launcher-activity-and-package-name.html


-> Right click on .apk file of APK Info app(or any other .apk file for which you wants to know package and activity name).

-> Select Open with -> WinRAR archiver option from right click context menu.

-> It will open .apk file in WinRAR window and you can see AndroidManifest.xml.

-> Open AndroidManifest.xml file in notepad. Now find keyword "p a c k a g e" In notepad.

-> Package name for my APK Info app is : com.intelloware.apkinfo Find keyword "a c t i v i t y" In notepad. It will show you activity name of your app.


-> Activity name for my APK Info app is display: .MainActivity. Here you need to append package name ahead of activity name so full activity name is : com.intelloware.apkinfo.MainActivity



3) Hierarchy Viewer 


-> Launch the Android Emulator and wait till starts properly.

-> Launch the appium and start the server.

-> Open the Hierarchy viewer from below path. Android Emulator should be starts properly, its should not be closed.

C:\android-sdks\tools\hierarchyviewer.bat


-> Then goto page in emulator which activity needs to be record.

-> Then automatically we can see the package name and activity name in Hierarchy viewer

we can see in bold color

com.android.calculator2/com.android.calculator2.calculator

Here,

package Name = com.android.calculator2
Activity Name = com.android.calculator2.calculator

How to find APK Package using UI Automator Viewer


We can identify the Package name by using below two methods


1) UI Automator viewer
2) Hierarchy Viewer


1) UI Automator viewer

Please find below steps to find APK package using UI Automator viewer


-> Open the reuired app and keep on active in Emulator

-> Launch the uiautomatorviewer.bat file from below mentioned path

E:\android-sdks\tools\uiautomatorviewer.bat

-> Click "Device Screenshot ("Uiautomater Dump") in uiautomatorviewer then its taking screenshot and paste into uiautomatorviewer.

-> Now we can inspect the element.

-> Right hand side we can see the option like "package" for APK package name.




2) Hierarchy Viewer


-> Launch the Android Emulator and wait till starts properly.

-> Launch the appium and start the server.

-> Open the Hierarchy viewer from below path. Android Emulator should be starts properly, its should not be closed.

C:\android-sdks\tools\hierarchyviewer.bat


-> Then goto page in emulator which activity needs to be record.

-> Then automatically we can see the package name and activity name in Hierarchy viewer

we can see in bold color

com.android.calculator2/com.android.calculator2.calculator

Here,

package Name = com.android.calculator2
Activity Name = com.android.calculator2.calculator



How to inspect element in mobile app for appium mobile automation testing (By using Emulator)



We can inspect the element in mobile app by using uiautomatorviewer.bat file from Android SDK folder.


-> Open the required app and keep on active in Emulator

-> Launch the uiautomatorviewer.bat file from below mentioned path

E:\android-sdks\tools\uiautomatorviewer.bat

-> Click "Device Screenshot ("Uiautomater Dump") in uiautomatorviewer then its automatically taking screenshot and paste into uiautomatorviewer.

-> Now we can inspect the element from Uiautomationviewer.

UnInstall .apk file into emulator for mobile automation testing in Appium





we have two ways to Uninstall .apk file into emulator

1) By using setting in emulator
2) By using Command Prompt




1) By using setting in emulator (Like Android Phone)

-> Goto setting in emulator
-> Goto App
-> Select required app and uninstall




2) By using Command Prompt

-> Download required .pdk file

-> Launch the Android Emulator and wait till starts properly.

-> Goto command prompt and goto E:\android-sdks\platform-tools and verify the device connected properly (not offline). by using below command

E:\SDK\platform-tools>adb devices

List of devices attached
emulator-5554   device

-> Run below command for install required .apk file in command prompt.

E:\SDK\platform-tools>adb uninstall Calender.apk

Success

-> We will get "Success" message if apk uninstalled in emulator.

-> Goto emulator and verify the calender app is uninstalled or not.

Install .apk file into emulator for mobile automation testing in Appium




Please follow below steps for install .apk file into emulator


-> Download required .pdk file

-> Launch the Android Emulator and wait till starts properly.

-> Goto command prompt and goto E:\android-sdks\platform-tools and verify the device connected properly (not offline). by using below command

E:\android-sdks\platform-tools>adb devices

List of devices attached
emulator-5554   device

-> copy and paste required .apk file into below mentioned path
E:\android-sdks\platform-tools

-> Run below command for install required .apk file in command prompt.


E:\android-sdks\platform-tools>adb install Calender.apk

1910 KB/s (93651283 bytes in 431.338s)
        pkg: /data/local/tmp/Calender.apk
Success


-> We will get "Success" message if apk installed in emulator.

-> Goto emulator and verify the calender app is installed or not.

Appium Setup for Mobile Automation Testing





The below software needs to be install in our machine


1) Download ADT Plug in (Eclipse)
2) Download and Set SDK location
3) Microsoft .Net Framework
4) Appium
5) Node JS



1) ADT Plugin (in Eclipse)


Use following steps to install ADT Plugin

-> Goto Eclipse
-> Goto Help -> Install New Software
-> Here click "ADD" button
-> Then Name = ADT (Any Name)
-> URL = https://dl-ssl.google.com/android/eclipse/
-> Select all software and finish the installation

Refer this link for install ADT plugin -> http://software-testing-tutorials-automation.blogspot.in/2015/09/steps-to-install-eclipse-adt-plugin-to.html


2) Download and Set SDK Location in Eclipse

After install the ADT plugin in eclipse then we set the SDK location in Eclipse

-> once restart the Eclipse after install ADT plugin then its ask confirmation for install SDK.

-> select first option for "install SDK".

-> select first checkbox for install latest SDK

-> select second checkbox and give the path of SDK

-> click yes for send the statistics to the google and click proceed. its take some time to install SDK.



3) Microsoft .Net Framework


Download Microsoft .Net Framework by using this link -> https://www.microsoft.com/en-in/download/details.aspx?id=17851

Refer this link to download -> http://software-testing-tutorials-automation.blogspot.in/2015/09/install-other-required-software-for.html



4) Appium


Download Appium by using this link -> http://appium.io/

Refer this link to download -> http://software-testing-tutorials-automation.blogspot.in/2015/09/how-to-download-and-install-appium-in.html



5) Node JS

 Download Node JS by using this link -> https://nodejs.org/en/download/

Refer this link to download -> http://software-testing-tutorials-automation.blogspot.in/2015/09/install-other-required-software-for.html



6) Download below required jar files and configured with eclipse


gson-2.5
java-client-3.3.0



Launch Android Emulator


Pre-requiste

1) Installed SDK
2) Installed required pakages in Android SDK Manager



2) Installed required pakages in Android SDK Manager

-> Open the Android SDK Manager (Eclipse-> Window-> Android SDK Manager

-> Update Proxy settings in Android SDK Manager (Tools-> Options)

-> Refresh (Android SDK Manager-> Pakages->Reload)

-> Its list out the available pakages. Select required pakage and install

Appium - Introduction




-> Appium is mobile application software testing tools.

-> we can automate native app, Web app and Hybrid app by using Appium.

-> Its open source software automation tool which is useful to automate android and IOS platform apps.

-> Appium is cross-platform automation tool so we can write automation tests and run against IOS and Android.


Multiple Platforms Support

Appium support bellow given different platforms.

Android
IOS
FirefoxOS



Multiple Languages Support

Appium supports bellow given languages with the Selenium WebDriver API and language-specific client libraries.

Java
Objective-C
JavaScript with Node.js
PHP
Python
Ruby
C#
Clojure
Perl



Limitations Of Appium

There are few limitation in appium right now.

For Android, No Support for Android API level < 17.
Script execution is very slow on IOS platform.
Gestures support is limited.
No support for Toast messages.


Appuium supports only Android 17+ API level version only.


****************************************

What is Android SDK


-> Androd SDK is software development kit which enables to create application for android platform.

For Example, If you want to create any android software games then we need Android SDK.

-> Also its provides ability to create emulators to test new created games or any other android apps.

-> By using appium we can create and run automation tests for native apps and web apps in android device or emulators. But we need Android SDK is pre-requisite to run tests on android platform using Appium.


*************************************************


Difference between simulator and emulator


The basic difference is Emulator runs for Android OS (Samsung galaxy,htc,motorola,etc,.) but simulator runs for Mac (iOS) iPhone 3g,4,4s,iPod.


Device is Emulator
Software is simulator

Saturday 4 June 2016

How to create excel sheet using apache poi





By using below code we can create excel sheet using apache poi



code:


public static void main(String agrs[])
{

Workbook book2=verifyExcelVersion(ToFilePath);
Sheet sheet2=null;


createExcelSheet(wb,"Sheet2",Download_FilePath);

}



// create new Excel sheet in workbook

public static void createExcelSheet(Workbook wb, String SheetName, String ExcelPath) throws IOException
{
wb.createSheet(SheetName);
FileOutputStream output_file =new FileOutputStream(new File(ExcelPath));  
wb.write(output_file); 
output_file.flush();
output_file.close(); 

}


// verify Excel version

public static Workbook verifyExcelVersion(String FilePath) throws FileNotFoundException, IOException
{
Workbook book1=null;
String fileExtn=FilenameUtils.getExtension(FilePath);

if(fileExtn.equals("xls"))
book1=new HSSFWorkbook(new FileInputStream(FilePath));
else if(fileExtn.equals("xlsx"))
book1=new XSSFWorkbook(new FileInputStream(FilePath));
else if(fileExtn.equals("xlsm"))
book1=new XSSFWorkbook(new FileInputStream(FilePath));
return book1;
}


How to verify excel sheet present or not in apache poi



By using below code we can verify the perticular excel sheet present or not. If not available we can create by using apache POI command.



code:

public static void main(String agrs[])
{

Workbook book2=verifyExcelVersion(ToFilePath);
Sheet sheet2=null;


if(!isExcelSheetExist(wb,"Sheet2"))
createExcelSheet(wb,"Sheet2",Download_FilePath);

}


// verify Excel Sheet exist or not

public static boolean isExcelSheetExist(Workbook wb, String SheetName)
{
if(wb.getSheetIndex(SheetName)>=0)
return true;
else
return false;

}


// create new Excel sheet in workbook

public static void createExcelSheet(Workbook wb, String SheetName, String ExcelPath) throws IOException
{
wb.createSheet(SheetName);
FileOutputStream output_file =new FileOutputStream(new File(ExcelPath));  
wb.write(output_file); 
output_file.flush();
output_file.close(); 

}


// verify Excel version

public static Workbook verifyExcelVersion(String FilePath) throws FileNotFoundException, IOException
{
Workbook book1=null;
String fileExtn=FilenameUtils.getExtension(FilePath);

if(fileExtn.equals("xls"))
book1=new HSSFWorkbook(new FileInputStream(FilePath));
else if(fileExtn.equals("xlsx"))
book1=new XSSFWorkbook(new FileInputStream(FilePath));
else if(fileExtn.equals("xlsm"))
book1=new XSSFWorkbook(new FileInputStream(FilePath));
return book1;
}




How to create common functions for working with different format of Excel Sheet by using Apache POI



By using below verifyExcelVersion() we can create common function for handle with different Excel format by using Apache POI



code:

public static void main(String agrs[])
{

Workbook book2=verifyExcelVersion(ToFilePath);
Sheet sheet2=null;


if(!isExcelSheetExist(wb,"Sheet2"))
createExcelSheet(wb,"Sheet2",Download_FilePath);

}




// verify Excel version

public static Workbook verifyExcelVersion(String FilePath) throws FileNotFoundException, IOException
{
Workbook book1=null;
String fileExtn=FilenameUtils.getExtension(FilePath);

if(fileExtn.equals("xls"))
book1=new HSSFWorkbook(new FileInputStream(FilePath));
else if(fileExtn.equals("xlsx"))
book1=new XSSFWorkbook(new FileInputStream(FilePath));
else if(fileExtn.equals("xlsm"))
book1=new XSSFWorkbook(new FileInputStream(FilePath));
return book1;
}

how to break out of nested loops java



break;

By using break we can come out from single loop.

But if we want to come out from all nested loop then we shoule use lables for acheiving this concept.



Example:

int points = 0;
int goal = 100;
someLabel:
while (goal <= 100) {
   for (int i = 0; i < goal; i++) {
      if (points > 50) {
         break someLabel;
      }
   points += i;
   }
}
// you are going here after break someLabel;

How to set auto detect proxy settings by using selenium in browser



Normally selenium referred the system proxy settings. If we want to use auto detect proxy setting from selenium code then we have to use different code like below



       org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
       proxy.setAutodetect(true); 
       DesiredCapabilities cap = new DesiredCapabilities();
       cap.setCapability(CapabilityType.PROXY, proxy);
       WebDriver driver5 = new FirefoxDriver(cap);
       
       driver5.manage().window().maximize();

How to Generate XSLT reports by using Ant+TestNG



We have to follow below steps to generate XSLT reports

1) ANT Installation
2) Configure XSLT and build.xml
3) Genrate XSLT report by using ANT

1) ANT Installation

-> Download Latest Version of apache-ant zip
Download latest version of apache-ant from

http://ant.apache.org/bindownload.cgi.

Extract zip folder and save into our local path.

-> Set JAVA_HOME Environment variables

* Right click on My Computer -> Properties and go to -> Advanced tab.
* Clicking on Environment variables button will open Environment variable dialog.
* Click on New button from System Variables box and add Variable Name = JAVA_HOME and Variable Value = Path of your jdk folder.
* Here my JDK folder path is 'C:\Program Files\Java\jdk1.8.0'

-> Set ANT_HOME Environment Variables

* Right click on My Computer -> Properties and go to -> Advanced tab.
* Clicking on Environment variables button will open Environment variable dialog.
* Click on New button from System Variables box and add Variable Name = ANT_HOME and Variable Value = Path of your jdk folder.
* Here my ANT folder path is 'D:\Selenium\apache-ant-1.9.7'

-> Edit Path Variable

* Right click on My Computer -> Properties and go to -> Advanced tab.
* Clicking on Environment variables button will open Environment variable dialog.
* Edit existing Path variable by selecting Path variable from System variables list.
* And click Edit button and insert  %JAVA_HOME%\bin;%ANT_HOME%\bin  at the end of Path variable value string.
* Do not forget to put semicolon (;) before %JAVA_HOME%\bin

-> Copy tools.jar from jdk/lib and paste It In jre/lib
* There will be 2 folders In your C:Program Files\Java folder.
* In my system It Is jdk1.8.0 and jre8.
* Open C:\Program Files\Java\jdk1.8.0\lib folder and copy tools.jar file.
* Open C:\Program Files\Java\jre8\lib and paste tools.jar In to It.

Now restart your system to getting system variables changes effect.

Verify ant configured properly or not
To verify that ant Is configured properly or not, follow the bellow given steps after restarting system
* Open command prompt.
* Type command "ant" and press Enter button.
* It should show you message as bellow.


C:\Users\user>ant
Buildfile: build.xml does not exist!
Build failed





2) Configure XSLT and build.xml


-> The below jar files shoule be under lib folder

* saxon-8.7
* SaxonLiaison
* testng-xslt-maven-plugin-test-0.0
* Selenium-server-standalone 2.47

-> The below files should be under project path

* build.xml  -> Link to download
* testng-results.xsl -> Link to Download

-> Configure build.xml file

Verify the Value for "project.jars" property at line no 10 of build.xml file. It should be the path of "lib" folder of project.
Verify the path of testng-results.xsl file at line no 91 of build.xml file. It should be  path of testng-results.xsl.

Note : Don't change any thing else In build.xml file.




3) Genrate XSLT report by using ANT


Once your test execution get completed and testng results generated, We can go to generate XSLT report. You need to follow bellow given steps to generate XSLT report.


-> Open command prompt.

-> Goto your project

E:\Selenium_Project\Selenium_Testing>

-> Type "ant" to check we are in correct path to generate XSLT report.


E:\Selenium_Project\Selenium_Testing>ant
Buildfile: E:\Selenium_Project\Selenium_Testing\build.xml

usage:
     [echo]
     [echo]             ant run will execute the test
     [echo]

BUILD SUCCESSFUL
Total time: 0 seconds


-> Remove Previous Build : Type Command = "ant clear"

 It should give message "BUILD SUCCESSFUL"


E:\Selenium_Project\Selenium_Testing>ant clear
Buildfile: E:\Selenium_Project\Selenium_Testing\build.xml

clear:
   [delete] Deleting directory E:\Selenium_Project\Selenium_Testing\build

BUILD SUCCESSFUL
Total time: 0 seconds


-> Compile And Generate New Build Folder : Type Command = "ant compile".

 It should give message "BUILD SUCCESSFUL"

E:\Selenium_Project\Selenium_Testing>ant compile


-> Verify Build Folder Is Generated : Now Refresh your project In eclipse.
 ant compile command will created new folder with name of  build under your project

-> Execute Test Suites : Type Command = ant run In command prompt and press enter.
It will execute your all software automation test suites which are included In testng.xml file.

 So now you can execute your software test suites from command prompt Instead of testng.xml file.

-> Generate XSLT Reports : Type Command = ant reports In command prompt and press enter.
It should give message "BUILD SUCCESSFUL"

-> open XSLT Report : Once more refresh your project In eclipse.

 ant reports command will create new folder with name = XSLT_Reports under your project


->You will find index.html file In XSLT_Reports folder.
That Is XSLT Report of your webdriver test. Open that file In any web browser.
 It Is Interactive and easy to understand that how many test cases are Pass, Fail and Skip for specific test case.



Note : If pie chart is not available in your report then you will get error message in browser like below


Error Message : SVG Pie Charts are not available. Please install a SVG viewer for your browser


So please install SVG viewer in order to see pie chart in XSLT reports.