Home » Articles posted by code2test.com (Page 6)
Author Archives: code2test.com
How to Use UISelector in Appium
Previous Topic, In this tutorial we are going to learn How to Use UISelector in Appium.
UISelector is a class which implements the object class and provides different public method to locate or identify elements with different attribute or property values.
Note: In this tutorial we will discuss few of the most frequently used method in UISelector with syntex and for the rest of method will share their name(implementation is same for all) with its decription
How to Use MobileBy class with UISelector in Appium?
Below are the most used method:
1 ) className (String className): This method sets the criteria of matching the class name with in the code heirarcy with the name provided as a parameter.
//Syntex
driver.findElement(MobileBy.AndroidUIAutomator("new UiSelector().className(\"Enter the Class Name here\")"));
2 ) resourceId(String id): This method search for the locator or inspect element from the code hierarchy on the basis of resourceId present first on the code.
//Syntex
//driver.findElement(MobileBy.AndroidUIAutomator("new UiSelector().resourceId(\"Enter the resource Id here\")"));
3) text(String text): Search on the basis of text visible on the widget of the page.
//Syntex
//driver.findElement(MobileBy.AndroidUIAutomator("new UiSelector().text(\"Enter the text here\")"));
4 ) packageName(String name): Search on the basis of package Name on the widget for the mobile application.
//Syntex
driver.findElement(MobileBy.AndroidUIAutomator("new UiSelector().packageName(\"Enter the package name here\")"));
5) decription(String desc): By providing the attribute value of content-description of the widget. We are good to locate or inspect the element.
//Syntex
driver.findElement(MobileBy.AndroidUIAutomator("new UiSelector().description(\"Enter the content-description here\")"));
6) instance(int instance): when we have element with similar property in that case we can identify or locate the element with the occurance of it instance no.The instance of an element is given by identifying element with common attribute followed by instance method.
//Syntex
driver.findElement(MobileBy.AndroidUIAutomator("new UiSelector().description(\"Enter the content-description here\")").instance(2));
7) index(int index): Search the widget on the basis of node index from layout.
//Syntex
driver.findElement(MobileBy.AndroidUIAutomator("new UiSelector().resourceId(\"Enter the content-description here\")").index(2));
As we See above how UISelector works and defined with syntex, now we are going to list down few more methods below, their systex are same as we discussed above.
8) checkable(boolean value): It matches as search criteria with the current mobile window code hierarchy and looks if the element is checkable.
9) checked(boolean value): This method provides the search criteria in the widget to check that is checked.
10) clikable(boolean Value): This method checks whether the element is clikable.
FindElement & FindElements Command in Appium
Previou Topic, In this tutorial we will learn how to FindElement & FindElements Command in Appium.
If we wish to interact the elements with in mobile device, the first step is to interact with elements, Android driver instance provides the method “FindElement” and “FindElements” to locate the elements in mobile application.
How to implement FindElement And FindElements Command with Appium
The major difference between FindElement And FindElements is that FindElement returns a WebElement and FindElement returns a list of WebElements.
The different types of locators are below:
1 ) By ID: By using this ID attribute the Appium inspector find the first element with the same id value and return the object. This is the most adopted way of locating the element, as most of the time the ID is unique. If the locator is not unique it displays an error message as “NoSuchElementException”.
Syntex for ID Attribute is below:
//driver.findElementById("com.flipkart.android:id/search_widget_textbox");
OR
//driver.findElement(By.id("com.flipkart.android:id/search_widget_textbox");
2 )By Name: The other way of locating the element is by Name of the element. We define the text of the locator and it return the object of the element but problem arises is that the text is not always unique.
Syntex are below:
//driver.findElementByName("Search for Products, Brands and More");
OR
//driver.findElement(By.name("Search for Products, Brands and More"));
How to locate Element by Content-desc?
Note : If the attribute type is “content-desc” it will be handled in the same way as we handle the attribute text, by By name method.
Handling attribute “content-desc” are as below:
Syntex of "content-desc" are below:
//
//driver.findElementByName("Open Drawer");
OR
//driver.findElement(By.name("Open Drawer"));
3 ) By Class: We can also locate the element using class attribute but the problem again arises regarding uniqueness, As there can be multiple elements be found with same class name.
Syntex of "ClassName" are below:
//driver.findElementByClassName("android.widget.ImageView");
OR /driver.findElement(By.className("android.widget.ImageView"));
4) By AccessibilityId: You can also find element by accessibilityID, the accessibility id provided by appium inspector for the element in mobile application can be given by below syntex.
Syntex"
//driver.findElementByAccessibilityId("Shop");
5) By Xpath: This method is the most preferred way of object identification in all area for, WebApplication along with mobile application.As we come up with many elements where unique elements or the id is not available.
So to handle such elements we define locator or identify locator by xpath.
To understand the concept of xpath, consider a scenario, where we are identifying the locator , by different attribute name.
So to find the locator we will use by class : android.widget.ImageButton
but the class is not unique and we do not find other attribute that is unique within this page. So the solution for such elements is using xpath.
We generally created Xpath by the combination of different attribute and also by traversing from parent to child element. Here will locate xpath by the combination of different attibute value shown below.
//Syntex
driver.findElement(By.xpath("//android.widget.ImageButton[@content-desc="Open Drawer"]"));
OR
//driver.findElementByXPath("//android.widget.ImageButton[@content-desc="Open Drawer"]");
So here we made combination of class attribute and content-desc attribute to create a xpath as: //android.widget.ImageButton[@content-desc=”Open Drawer”]
Inspect Element Using Appium Inspector
Previous Topic, In this tutorial session we are going to learn how to Inspect Element Using Appium Inspector.
How to Inspect Element Using Appium Inspector?
To inspect element by Appium Inspector, we need to first connect our mobile device with our local system and follow the below steps:
Step1) Open the Appium desktop client application.
Step 2) Now Click in to Start Server, to get appium your server started.
Step 3) Now define the desired capabilities as below:
- udid
- deviceName
- platformName (define as android)
- platformVersion (define as per per mobile device)
- noReset (define as true)
- fullReset (define as false)
Refer the screenshot below:
Step 4) Now save the desired capabilities for future reference, so that we need not to define these capabilities again. Click to Save As and give any name of your preference and click to save button.
Step 5) Now we see that the capabilities we have save is getting displayed under “Saved Capabilities Sets1” menu.
Step 6) Now select the capability and click to Start Session button and wait for few seconds.
We see that the Flipkart application opened in mobile device is getting displayed in appium inspecter, the screenshot is displayed on left and its properties are displaying on the right side of window.
Step 7) Now lets check the properties of element in the application, click to Hemburger button on the top left of the element and check its properties and source on the right pane.
Note: We can also click or perform any action to Hemburger menu from the appium inspector window and view all the listing under the menu option without touching the mobile device.
For this click to Tap button on the right side of the pane below Selected Element heading (refer the screenshot below).
Conclusion:
So we from the above tutorial we have learned inpecting of locating the elements of mobile device using Appium Inspector and perform any task for the selected element from appium inspector itself.
Appium Mobile Device Commands
Previous Topic, In this tutorial we will learn Appium Mobile Device Commands and its syntex under Android driver.
What are Appium Mobile Device Commands?
Those commands which are written under the script for handling the android device features like keyboard handling, orientation etc are referred to as Appium Device Commands.
Lets discuss the commands in details:
1 ) pressKeyCode(key): As every key in the mobile device is associated with a code in its back-end, and to hit the key we need to call the keycode to perform operation.
If we want to hit the key in mobile device through appium script we call pressKeyCode(Key) method and pass keycode as a parameter. For calling keycode we use method “AndroidKeyCode“.
//Syntex
//This syntex will hit the home button of your mobile device
adriver.pressKeyCode(AndroidKeyCode.HOME);
2 ) pressKeyCode(AndroidKeyCode.KEYCODE_APP_SWITCH): There are multiple scenarios where we need to switch from on application to another application, so to handle such scenario we can use this method.
This method minimizes the current window and all the application gets displayed on single screen.
//Syntex
adriver.pressKeyCode(AndroidKeyCode.KEYCODE_APP_SWITCH);
How to change the orientation of screen in mobile device?
3 ) rotate(orientation): If we want to change the orientation of the screen of device, it can be performed using the command, this command requires screen orientation as parameter and returns nothing or void.
The orientation is defined by using the ScreenOrientation class followed by its type (ex Landscape, portrait ) etc
//Syntex
//Change orientation to landscape
driver.rotate(ScreenOrientation.LANDSCAPE);
4 ) getKeyboard(): We can perform any task on the mobile key board only when its enabled or displayed on screen.
To get the keyboard on mobile device we call getKeyboard() method, it takes nothing as parameter or void and return keyboard type object.
//Syntex
//Enable the keyboard on mobile device
driver.getKeyboard();
5 ) hideKeyboard(): To hide or disable the keyboard from the mobile screen we call the hideKeyboard() method, it take nothing and return nothing or void.
//Syntex
driver.hideKeyboard();
6 ) lockDevice(): Using the lockDevice() method, we can lock mobile device through appium script , it takes no parameter and returns void.
//Syntex
driver.lockDevice();
7 ) unlockDevice(): As we can lock mobile device, similarly we have the capability to unlock device through appium script. It takes no parameter and returns void.
//Syntex
driver.unlockDevice();
8 ) isLocked(): If we want to check whether the mobile device is locked in current session or not, we can get this information using the command. It takes no parameter and return boolean (true or false).
As per the return boolean to the condition we can perform multiple operations.
//Syntex
//It check wheter mobile device is locked or not and returns boolean
driver.isLocked();
9 ) toggleLocationServices(): Appium provides the capability to toggle the location service through command, it takes no parameter and returns void.
//Syntex
driver.toggleLocationServices();
10 ) getSessionId(): Session Id is something which active thoughout the session and provided the connectivity of appium server with mobile application.
To get the session id we use getSessionId().
//Syntex
//Will give the session id of the instance
driver.getSessionId().toString();
Conclusion:
So below are the few basic device command to work on mobile application, in next topic we will learn few more advance topic on Appium.
Please comment us for any query or suggession.
Mobile Application Command
In previous topic we have discussed Identify Element Using UIAutomatorViewer, in this topic we will discuss Appium Mobile Application command.
What are Mobile Application Commands in Appium?
Those commands which helps in riding and controls the mobile application in terms of installation, launch, perform multiple activity etc with in application is refereed to as mobile commands.
Lets discuss Appium Mobile Application command in details:
1 ) launchApp(): This commands re-launches the current application once its been closed. The basic syntex of launchApp()
driver.launchApp();
2) closeApp(): This commands closes the current running application from the mobile window and the script stops execution.
//Syntex:
driver.closeApp();
3) resetApp(): As we run this commands, all the cache and the cookie gets cleaned for the application and removes the previous backlogs.
//syntex:
driver.resetApp();
How to Run Application on background?
4) runAppInBackground(): This command help to run the application in background mode or in other words the application running in mobile device is not visible on screen.
This commands takes int as a parameter and returns nothing or Void, we pass time in seconds as a parameter.
Note: If we want to run our application permanently on background, we can pass time or parameter as -1;
//Syntex
driver.runAppInBackground(10); //where 10 represents 10 seconds
driver.runAppInBackground(-1); //Runs application permanently on background
5) startActivity(): If we want to open another application in mobile device or execute any automation script which requires to run two different application to open in single run.
The startActivity() commands requires app Package and app activity as a paramater and returns nothing or Void.
For example, if we want to open calculator application, we hit the commands as below.
//Syntex:
driver.startActivity("com.miui.calculator","com.miui.calculator.cal.CalculatorActivity"); // Parameter are app package and app activity of calculator
6) currentActivity(): In Mobile application every page or every window of mobile application has its app activity, so if we want to get the app activity of any page or window, we can call this method, this command takes no parameter and returns a String.
//Systex:
adriver.currentActivity();
7) isAppInstalled(): This command gives the confirmation of installation of an application. This commands requires the bundleId as a parameter and returns boolean.
//Syntex:
adriver.isAppInstalled(bundleId);
8) installApp(appPath): We can use this command to direct install an application to the mobile device. This command requires appPath or apk file and returns nothing or Void.
//Syntex:
driver.installApp(appPath); // appPath is the APK file of android application
9) removeApp(appPath): This command will remove the application from the mobile device . This command requires bundleId or app Package and returns nothing or Void.
//Syntex:
driver.removeApp(appPath); // appPath is the APK file of android application
Conclusion:
So in the above tutorial we have discussed the basic commands for mobile application, further we will discuss commands to Touch gesture, and identification of object with different attributes.
Identify Element Using UIAutomatorViewer
Previous topic, In this tutorial we will learn locate or Identify Element Using UIAutomatorViewer. Let discuss in depth.
Why and how to check device connectivity?
In initial step, connect your mobile device with local system or computer and check your device connectivity. This can be done by two ways:
First way is go to open perspective->select DDMS and click to ok button and the connected device will be displayed under the devices list as shown blow.
Second way is hit command-> adb devices under cmd prompt or command prompt as below.
How to Identify Element Using UIAutomatorViewer?
Follow the below steps for locating the elements:
Step 1) Open the application on your mobile device (as we are working on Flipkart, will identify its element.
Step 2) Browse the android folder till the uiautomatorviewer.bat file, or follow the below path->C:\Users\xxxxxx\AppData\Local\Android\android-sdk\tools\bin and double click to uiautomatorviewer .bat file.
Step 3) As we hit to uiautomatorviewer.bat file, a UI Automatoe viewer will get opened.
Step 4) Click in to device screenshot button available as second menu on the top. The screenshot for the flipkart application will get opened. (Note: keep flipkart application opened on mobile device).
Step 5) Now click to Main Menu on the top of the screenshot and all the properties of the element will get displayed on the right.
Step 6) Now click to search field on the flipkart application screenshot, similarly all the attribute along its values will be displayed on the bottom right.
Now lets understand how to save the screenshot and its related files.
How to save screenshot and UiX File?
It is very important to save your screenshot and its related UiX file because, one need to take screenshot again and again for the same page and check for device connectivity.
With the save feature inside the UI Automation viewer, once can save the screenshot of the application and UiX file and can use later even when the devices is not connected with computer.
Steps to save the screeshot along with Uix File are below:
Step 1) Take the screenshot of the flipkart application and click to save button on the top.
Step 2) Now give the path and click to OK button.
Step 3) now go the that path and you will see that 2 files are created on the location.
The two files are created in the same location one is the screenshot and other is the properties of all the elements displayed in the screenshot.
How can we open the saved UI files?
Below are the steps to open the saved UI files:
Step 1) Click on the open menu at the top.
Step 2) click on to browser and give the path of dump file , on the saved location.
Step 3) Screenshot of flipkart application alon with properties values gets displayed under UI Automator viewer now.
So following the above steps we are now good with locating or identifying the elements of any Android application for Appium automation.
Youtube->https://www.youtube.com/channel/UCJmuzLmF7txuObVRHXYTGZQ?view_as=subscriber
First Appium Test to Launch Flipkart
Previous Topic, In this tutorial we will learn to write our First Appium Test to Launch Flipkart.
How to write First Appium Test to Launch Flipkart?
Before writing script , we need to launch Appium server first, below are the steps to launch server.
Step 1) Open the Appium server.
Let keep the host=”0.0.0.0″ and port “4723” as it is( default).
Step 2) Click to Start Server button and the server will get start working with default host number and port number.
Step 3) Now copy the below code and paste in a class inside the eclipse ide.
package code2test.MavenAppiumProject;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.Assert;
public class Harness {
public AndroidDriver<AndroidElement> adriver;
public void openapp(){
System.out.println("Opening App");
File app =new File(System.getProperty("user.dir")+"\\apks\\Flipkart-7.3.apk");
DesiredCapabilities capabilities =new DesiredCapabilities();
capabilities.setCapability("deviceName","Redmi");
capabilities.setCapability(CapabilityType.BROWSER_NAME,"");
capabilities.setCapability("platformVersion","9 PKQ1.181203.001");
capabilities.setCapability("platformName","Android");
capabilities.setCapability("noReset","true");
capabilities.setCapability("fullReset","false");
capabilities.setCapability("appPackage","com.flipkart.android");
capabilities.setCapability("appActivity","com.flipkart.android.activity.HomeFragmentHolderActivity");
capabilities.setCapability("app", app.getAbsolutePath());
try {
adriver = new AndroidDriver<AndroidElement>(new URL("http://0.0.0.0:4723/wd/hub"), capabilities);
} catch (MalformedURLException e) {
e.printStackTrace();
Assert.fail("Driver failed to start" + e.getMessage());
}
adriver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
}
public static void main(String[] args) {
Harness flipDriver=new Harness();
flipDriver.openapp();
}
}
Code Inside Eclipse will look as below:
Step 4) Now run the code by right click to code Run->Run As->Java Application.
Step 5) In few minutes of time we see that the flipkart application in mobile gets opened.
Code explanation step by step:
//Code to provide the path of Flipkart APK in system
File app =new File(System.getProperty(“user.dir”)+”\apks\Flipkart-7.3.apk”);
Desired Capabilities:
DesiredCapabilities capabilities =new DesiredCapabilities(); // Creating object for desired capabilities.
// Defining the name of your device
capabilities.setCapability(“deviceName”,”Redmi”);
//Defining the name of mobile browser as empty
capabilities.setCapability(CapabilityType.BROWSER_NAME,””);
// Defining the platform version of the device
capabilities.setCapability(“platformVersion”,”9 PKQ1.181203.001″);
//Defining Name of the platform, as Android
capabilities.setCapability(“platformName”,”Android”);
//defining the mobile to open in noReset
capabilities.setCapability(“noReset”,”true”);
//Defining the capability to not re-install application
capabilities.setCapability(“fullReset”,”false”);
capabilities.setCapability(“appPackage”,”com.flipkart.android”); // defining the application packackage of Flipkart
//Defining application activity of flipkart
capabilities.setCapability(“appActivity”,”com.flipkart.android.activity.HomeFragmentHolderActivity”);
//Defining the file path in one statement and retuning a String
capabilities.setCapability(“app”, app.getAbsolutePath());
try {
// Creating the object of android driver and passing host and port number and capabilities to it
adriver = new AndroidDriver<AndroidElement>(new URL("http://0.0.0.0:4723/wd/hub"), capabilities);
} catch (MalformedURLException e) {
e.printStackTrace();
Assert.fail("Driver failed to start" + e.getMessage());
}
So following the below steps, we now able to create and execute the appium script to launch flipkart application and other application too.
Download Appium client library
After Installing PdaNet for Android device, now next step is to Download Appium client library for java.
What is Appium client library?
Appium client library binds selenium library and it provides all the selenium commands along with additional libraries that have commands to control mobile devices.
The commands which are capable to handle multi gesture,actions , orientation along with multiple behaviour like pinch, swipe etc.
How to Download Appium client library for java?
Below are the steps by step way to download library
Step 1) Click on the Appium Client Library and select java to install Appium libray for java
Step 2) Now click to downward arrow under Download column.
Step 3) Select Jar menu.
Step 4) A warning pop window will appear, click to Keep button
Step 5) Save the java-client jar to the preferred folder, this jar will further be imported in eclipse for automation.
So following the above steps sequentially we will download the appium java-client and further be import in java project.
Download and Install PdaNet for Android device
After enabling the developer option in Android device now next step is to Download and Install PdaNet for Android device.
About PdaNet
Pdanet helps your connect your phone internet with your computer.
I works same as modem and helps to provide net connection to your system.
Connection between system and android device can be provided by two ways by bluetooth and USB.
How to Download and Install PdaNet for Android device
Below are the steps by step ways for installation:
Step 1) Click to PdaNet to re direct to PdaNet official website and click to “Download window click app” for window installation on window
Step 2) Now select folder on your local to save the setup file.
Step 3) Double click to PdaNet installer setup and start the installation.
Step 4) Accept the license agreement and click to Next button.
Step 5) Click to Next button under “Information” window.
Step 6) Click to Next button under ‘Select Start Menu Folder’ window
Step 7) Click to OK button to Enable USB mode
Step 8) click to Install button start installation
Step 9) Once the installation gets succeeded click to Finish button
Step 10) Connect your mobile device, now in this step you will get a pop up window on your mobile device to allow USB debugging, click to OK button.
Now following above steps sequentially you have installed the setup on you local system.
Step 10) To check the installation hit the command adb devices on command line, it will show you the device name as below screenshot.
Enable developer mode Android Device
Once Downloading and installing Appium desktop client, now we have to turn on the USB Debugging option under Enable developer mode on Android Device.
How to Enable developer mode on Android Device?
Below are the step by step ways to setup.
Step 1) Open your mobile home screen and search for setting icon and tap to it.
Step 2) Under settings window, tap to About Phone icon
Step 3) Tap on MIUI version 7 times on the icon(we are using mi phone so we have MIUI version) or If you find “Build number” icon on your mobile screen then you tap 7 times on the icon
Step 4) Now your developers options gets enabled on your settings window, open the developers options window.
Step 5) Scroll down till you see USB debugging option, turn it on (or right swip)
Step 6) A pop up window will appear, simply select OK.
so following the above steps we have now enabled the USB debugging on your device.
Follow Us on Facebook: https://www.facebook.com/code2test/?modal=admin_todo_tour