Where we learn technology

Author: Ragav (Page 2 of 2)

6. Connect to Appium using Java / Python

So far, we have seen how to connect the mobile device in Appium Desktop.

Now it’s time to see how we can connect the device and start testing the app using Java. Hence Java is our preferred language for this training, I will try to include code snippets for Python as well.

How to connect to Appium using Java code?

As we all know Appium will connect to any mobile device with the help of DesiredCapability. In this session we are going to discuss about how internally our Java code is communicating with Appium with the help of Appium libraries.

 Have you ever worked in Selenium grid? If yes, then it will be very easy for you guys to understand the Appium design. For example, in Selenium Grid we will use DesiredCapability to help Selenium grid library to identify the nodes right? Appium also uses the same methodology. Appium server acts as a Hub and the mobiles devices/ Simulators/Emulators connected will be treated as a Node. Please go through Appium architecture in our previous Blog in case of any doubts.

How to connect device using Java code via Appium?

To access Appium from our java code, we need to include the same “DesiredCapabilities” and its attributes that we used to connect mobile with Appium desktop.

In appium desktop we used GUI, but in our java program we need to include a class called “DesiredCapabilities” form the package “org.openqa.selenium.remote. DesiredCapabilities” to identify the device connected using Key/Value pair.

Have you noticed the package name? Yes we are importing it from Selenium grid support libraries. With the help of method setCapability we can get the mobile device attributes using the interface “MobileCapabilityType”. Syntax Below,

DesiredCapabilities cap = new DesiredCapabilities();

cap.setCapability(MobileCapabilityType.PLATFORM_NAME, “Android”);

here, DesiredCapabilities is a class and we are creating a strong reference called ‘cap’.

With the help of ‘cap’ we are calling the function/Method ‘setCapability’ and invoking the interface ‘MobileCapabilityType’ which extends its parent ‘CapabilityType’.

Remember the below important facts may be helpful at the time of Interview,

  • DesiredCapabilities is a class
  • Parent class for DesiredCapabilities is MutableCapabilities Class
  • setCapability is a method that is inherited from the class MutableCapabilities
  • MobileCapabilityType is an Interface which extends org.openqa.selenium.remote.CapabilityType

Using which capabilities Appium will find the Mobile Device?

Depends on the platformName, platform version or automationName given in the capabilities, the corresponding bootstrap loader will be triggered from Appium to find the device connected. Set of basic mobile capabilities are listed below.

Below diagram illustrates how Appium communicates with device.

How to open test App using Java code?

I don’t want to showcase any Native apps like calculator or Gallery in this session, We will see some real app in order to understand the functionality. To start our test execution we can follow two approaches,

  1. Using Appium desktop Server
  2. Using Appium command line

In this article, we will figure out using Method 1: Running our test with the help of “Appium Desktop Server”. In future blogs I will let you know how to install and use “Appium command line”

What is AppiumDriver?

AppiumDriver is a Class with a parent “RemoteWebDriver” having “AndroidDriver” and “iOSDriver” as its direct subclass. It means if we declare our driver as an AppiumDriver we can either initiate it as an AndroidDriver or iOSDriver based on our need.

For executing Android based devices we will use “AndroidDriver<?>”  <T extends WebElement> can be “AndroidElement”

For executing Android based devices we will use “AndroidDriver<?>”  <T extends WebElement> can be “iOSElement”

Before starting with the code, Make sure Appium Desktop is running in the background. It should have initiated its Localhost with the given proxy number. Like this,

Note: Once again before running the code, make sure Appium is running in the background else your code will return an exception “org.openqa.selenium.WebDriverException” “Connection refused: connect”.

Please find the sample code for launching an Android app in Java:

In this sample code, we launched the android app in real device with a basic DesiredCapabilities. Note: TestNG annotations are used only to maintain the test.

import java.net.URL;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.remote.AutomationName;
import io.appium.java_client.remote.MobileCapabilityType;

public class Testengine {
	
	public static AppiumDriver<?> driver;
	
	@BeforeMethod
	public void startAppium() {
		try {
			DesiredCapabilities cap = new DesiredCapabilities();
			cap.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
			cap.setCapability(MobileCapabilityType.PLATFORM_VERSION, "9");
			cap.setCapability(MobileCapabilityType.DEVICE_NAME, "5642c6b9");
			cap.setCapability("appActivity", "com.invoiceapp.InvoiceLaunchScreenAct");
			cap.setCapability("appPackage", "com.invoiceapp");
			cap.setCapability("autoLaunch", false);
			cap.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 500);
			cap.setCapability(MobileCapabilityType.AUTOMATION_NAME, AutomationName.ANDROID_UIAUTOMATOR2);
			driver = new AndroidDriver<AndroidElement>(new URL("http://127.0.0.1:4723/wd/hub"), cap);
		} catch (Exception e) {
			e.printStackTrace();
		}	
	}
	
	@Test
	public void test001() {
		driver.launchApp();
	}
	
	@AfterMethod
	public void tearDown() {
		driver.quit();
	}

}

In the above code I used TestNG just to maintain the hierarchy. In the @Test annotation I’m simply trying to launch the app. No testing has been performed here. Please find the screenshot below for Appium logs, and read the logs completely to understand how it works.

Please find the sample code for launching an iOS app in Java:

import java.net.URL;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.ios.IOSDriver;
import io.appium.java_client.ios.IOSElement;
import io.appium.java_client.remote.AutomationName;
import io.appium.java_client.remote.MobileCapabilityType;

public class Testengine {
	
	public static AppiumDriver<?> driver;
	
	@BeforeMethod
	public void startAppium() {
		try {
			DesiredCapabilities cap = new DesiredCapabilities();
			cap.setCapability(MobileCapabilityType.PLATFORM_NAME, "iOS");
			cap.setCapability(MobileCapabilityType.PLATFORM_VERSION, "13.3");
			cap.setCapability(MobileCapabilityType.DEVICE_NAME, "iOSDeviPad");
			cap.setCapability("bundleId", "com.invoice.manager");
			cap.setCapability("udid", "f209a1bc7c7eewf4543rweafdf96645be83fc615bfe3a");
			cap.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 100);
			cap.setCapability(MobileCapabilityType.AUTOMATION_NAME, "XCUITest");
			driver = new IOSDriver<IOSElement>(new URL("http://127.0.0.1:4723/wd/hub"), cap);
		} catch (Exception e) {
			e.printStackTrace();
		}	
	}
	
	@Test
	public void test001() {
		driver.launchApp();
	}
	
	@AfterMethod
	public void tearDown() {
		//driver.quit();
	}

}

In Python to Initiate Appium for an iOS application:

Please find the sample code below, to initiate Appium using Pytest framework. This will also work in UnitTest framework it’s up to you on which framework you are using. I prefer to use Pytest because it is having some advanced features when compared with UnitTest. Here also we are following the same desiredCapabilities.

In python, we use dictionary to send the DesiredCapabilities to Appium.


Points to Remember:

  • The capability “Automation_Name” is optional, but it is always a good practice to mention this capability.
  • UDID in iOS and Device_Name for Android are optional if we connect with only 1 device, but when it comes to multiple devices and emulators “Device_Name” is really mandatory else Appium bootloader will pick based on Platform Version.
  • For Android, if we are using Emulators, it is must to mention the “AVD” capability because Appium will prefer the Emulator even if real device is connected (this happens only if both devices and emulator android version are same)
  • Make sure to get the exact application activity for Android based application, else Appium will not initiate the connectivity
  • Sometimes WDA (WebDriverAgent) will uninstall automatically when launching app via Appium, This happens only if we use same device in different Mac machine to perform execution. So it is always good to have a dedicated iPhones for the Mac.

Please let me know in comments whether to include Python syntax also in our blog, else we will focus only on Java.


Cheers!!

Naveen AutomationLabs

Blog Contributors:

Author:  Ragavendran

Ragav, having 10+ years of testing experience on which 7 years of rich experience in automation using UFT, Selenium (Java/Python). Expertise in Web and Mobile automation testing using Appium.

https://www.linkedin.com/in/Ragavendran-Ragav

Reviewer: Naveen Khunteta 

https://www.linkedin.com/in/naveenkhunteta

Please follow and like us:

7. Creating a Framework[Maven project] for Appium – I


So far we have seen about Appium Inspector, also we created a simple test method to launch the app from our java code. Now it’s time to move to next level.

From this session we will cover few advanced topics, which would be helpful for beginners and intermittent test engineers. No worries we will cover step-by-step.

Guys, to understand Appium and its API’s to its root level, It is must to learn the basics of Selenium because at some point we will club both Selenium and Appium libraries. So to be honest, you are not expected to be a Geek in Selenium, but yes Selenium basics are must with any programming language.

Please don’t get confused between Selenium and Appium. As we know both are BINDINGS/Libraries which provided an API for us to perform Automation testing. Bindings means a collections of packages supporting any programming languages. In Java term we can say collections of “Jar” files which can be used with Java.

Appium bindings/libraries with any programming languages are used to automate a mobile application

In other hand, Selenium bindings/libraries with any programming languages are used to automate Web applications. Means both are having its own libraries to perform an operation.

For better understanding, I will take an example, In Selenium library we have an exclusive class called “Actions” using this class we have perform important web page operations like mouseHover(moveToElement), doubleClick, contextClick, dragAndDrop, keyUp, keyDown etc.. But do we need all these actions for Mobile? Do we really need to rightclick and mousehover’s in mobile app? The answer is NO. That’s why Appium is having its own list of libraries, from Appium library we have a class called “TouchActions” where we can perform mobile based operations like Swipe, Scroll, tap, point etc.

It’s all about from which library we are importing into our code

Selenium: “import org.openqa.selenium.interactions.Actions;”

Appium: “import io.appium.java_client.TouchAction;”

I hope we are clear with Appium and Selenium API’s. Now we are in a shape to proceed even further.

Starting now we will focus on creating a Maven project with TestNG and Excel utility to test the App. So that it will be interesting for you guys to follow-up and you will also get the satisfaction of framework design and much more.


How to create a Maven Project?

Before creating a Maven project, we should know about what Maven is and why we need that?

Maven is basically a build tool. Unlike Java project, Maven is having its own folder structure and Dependency mechanism where one can easily maintain the code and update the dependencies in a structured and proper way.

We do have our own video tutorial on what is Maven and how to create a Maven project by Naveen. Please refer the link below for tutorial:

In our tutorial, we are giving preference to Maven framework, because of the some reasons like,

  1. Will save our time and effort from downloading the Appium jars and other must have Jars and link it with our project
  2. We can use the existing Maven folder structure to maintain our class/java files.
  3. With the help of POM file, we can easily maintain our libraries up-to-date
  4. Now a days Maven projects are getting famous in QA fraternity. So it is in must learn list 😊

Creating a Maven project in Eclipse:

Step 1: Open eclipse

Step 2: From the eclipse home page to to Menu bar and click on  

Step 3: Search for the text “Maven”

Step 4: Install the Maven plugin, just by clicking on Install button

Step 5: Once you got the below screen, select all checkboxes and click on “Confirm” button

Installation should be successful. Restart the eclipse.

Step 6: To confirm whether maven plugins are available in Eclipse, that just go to Menu bar and click on New -> Projects

Look in Project wizard for the Name “Maven”. You should get a screen like below,

Step 7: Select “Maven Project” and click on Next

 Step 8: In this screen, you can ignore any pre-defined or existing archtype just by selecting the checkbox “Create a simple project (skip archtype selection)”. We are selecting this checkbox because, there are lot of developers and Maven community guys created pre-defined set of architecture which will be useful for developers with selected POM dependencies. Since we need Maven framework for our Testing, the existing archtypes won’t help so we can simple skip the existing ones and create our own.

Step 9: Click on Next button. Here comes our Major part.

  1. Group id: This is an important field an internal Identity to identify the project.
    1. Naming practice: Reversed domain name of the app under test.
    1. Example: com.invoiceapp (or) org.invoiceapp  [usually lowercases are preferred]
  2. Artifact id: Artifacts means a document, a name of the jar we are going to create. Also the name given in this field is going to be our framework name.
    1. Lower cases are preferred. For our project I’m giving the name as “invoiceapp-regression”
  3. Version: Skip this by selecting the default value 0.0.1-SNAPSHOT. The usage of this field is for version controlling includes giving increments for Major, Minor and low level changes. You can understand the importance and usage of this field when we discuss about the POM file in future sessions.
  4. Packaging: Let it be jar only
  5. You can enter any valid values in “Name” and “Description” fields.
  6. Ignore the Parent Project. This will simply help to inherit the arch from other Maven project.

Now our Maven project and framework skeleton is ready. This is how the initial structure look like.

In any Maven based projects, the below 3 fields are really Mandatory. Without these 3 fields we cannot create or use a Maven project

  1. GroupID
  2. ArtifactID
  3. Version

There is one more field called “Scope”. We will see all these fields in detail in POM Dependency session.

In our next session, we will see

What is a framework? Why we need a Framework

How to add Appium and other utility jar’s in Maven POM file


Cheers!

Naveen AutomationLabs

Blog Contributors:

Author:  Ragavendran

Ragav, having 10+ years of testing experience on which 7 years of rich experience in automation using UFT, Selenium (Java/Python). Expertise in Web and Mobile automation testing using Appium.

www.linkedin.com/in/Ragavendran-Ragav

Reviewer: Naveen Khunteta 

https://www.linkedin.com/in/naveenkhunteta

Please follow and like us:

1. How to Download and Install Appium Desktop on Win and Mac


What is Appium ?

Appium is an open source a cross platform mobile automation tool for use with native, hybrid and mobile web apps. Appium is basically a tool for mobile automation testing and this is not a testing  FRAMEWORK.

Below listed are few reasons to use Appium in our testing framework:

  • Appium will never demand a source code or recompiling of the app on test. 
  • Appium allows to use your preferred programming language, frameworks, and tools.
  • Appium supports programming languages that have Selenium client libraries like Java, Objective-C, JavaScript with node.js, PHP, Ruby, Python, C#, etc
  • Appium also supports cross platform testing frameworks. Single framework to support for iOS and Android, can be achieved using Desired Capabilities and POM.
  • Appium is an open source project and it has a good contributing community.
  • Appium library is having a common class “AppiumDriver” which will support both Androiddriver and IOSDriver. fyi.. It also supports WindowsDriver.
  • Appium is haing a Wrapper program combined with WebDriver Interface that supports vendor provided frameworks like XCUITest, UIAutomator2, Expresso ect.
  • Appium not only supports Real device it also extends its support to Emulators and Simulators
  • Appium is now supporting lot of advanced features like TV automation, Media streaming, Artificial Intelligence, and only emulator supported features like face reading, fingerprint ect..

How to Download and Install Appium Desktop on Win and Mac

Appium Desktop is an application for Windows, Macintosh and linux OS. Appium Desktop is a UI based app with a lot of connectivity and configuration options. It will allows the user to view some advanced options in UI like Inspector for identifying elements, Relaxed security, exporting appium logs and so on..

  • Using a graphical interface for the Appium Server. You can set options, start/stop the server, see logs, etc… Appium Desktop comes with Node runtime files, so no need to separately download and install Node.
  • One of the powerful and main feature on Appium desktop is INSPECTOR. Inspector is the object spy in Applium (like QTP you can spy an element by clicking). Inspector tool will identify the elements along with ID, XPATH and all applicable attributes.

http://appium.io/ is the official website for Appium desktop. One can easily download the latest desktop build of Appium form Downloads sections. Based on preferred the OS we can easily download the build. Hence it is an open source we can download and use it free of cost.

For Windows:

If you are using Windows based OS, then you have to download the .exe file. (ex: Appium-windows-1.15.1.exe). Once downloaded you can install the software by double clicking on the exe file.

On double click on exe file, the below installation screen will popup

Select the 1st radio button if you want appium to be avaialbe for all user who logs in this computer. Else click on 2nd option.  On clicking on “Install” button, installation will begin

Click on “Finish” button to launch the Appium desktop and also to complete the installation process and to quit the installation wizard.

For Mac,

If you are using Mac OS, then you have to download .dmg file so that you can simply move the file under Application to get started. (ex: Appium-mac-1.15.1.dmg). Once downloaded you just need to extract the file and move the build to Applications.

Just click and drag the appium logo to Applications.

Appium also available as an AppImage for Linux. App image is like an executable file for windows. Here in linux no need to install the build, just we need to open the appImage so that your Appium server UI will be prompted.

The good thing in Appium desktop is no UI change for any operation system. UI is built to be stable across platform.

Now we are ready with our Appium desktop to launch and starting the server.

Launching Appium Desktop after Installation for the first time:

Appium desktop UI is very simple and easy to access. Below image represents the home page of Appium Desktop.

In Simple Mode, one can start the Appium server by providing the “Host” and “Port” values. By default Appium REST http interface listener will use the Host 0.0.0.0 and Port “4723”. If any other programs like Jenkins/tomcat are running on a default local server/port then we can change the Port in Appium.

Clicking on “Start Server” button will initiate a local host server with the given port #. You can start seeing logs in Appium desktop like below.

You can also verify the status in any browser by entering the command/URL http://localhost:4723/wd/hub/sessions. Which will give you the current status (same like selenium grid)

By any chance if your firewall is blocking Appium or if you get the below screen, select any or both the checkboxes and click on “Allow access” button. So that Appium can initiate a local host.

How to connect to an Emulator or a real Device with Appium:

Connecting a real device with Appium is very simple. Appium Desired Capabilities will take care of all the connectivity protocols. But before trying to connect we need to make sure all prerequisites are available in the computer. It’s all about configuration and SDK’s

If you want to connect an Android device on a Windows/Mac based OS, then you have to download and install the Android Studio/ Android SDK. Without Android SDK Appium cannot initiate a connectivity to any android based devices. The reason behind this is, Appium will connect to android device in developer debugging mode. Hence to enable the debugging mode “SHELL” commands are inevitable, Commands like ADB, Shell are available only on Android SDK. So to make a connectivity to a real android device “Android SDK” is must.

You can downlad Android SDK on its official website https://developer.android.com/studio. Based on the OS you can download and set the preferences. Once Android studio is installed it is also necessary to install the android SDK based on the device Android version. Ex. My real device is running Android version 9, then I need to install the SDK for Android 9. This can be achieved by AVD in android studio.

“Please use the below steps for creating an Android Emulator”

Step 1: Open android studio

In the home page, you can find “AVD Manager” icon on the top left corner

Step 2: Click on “AVD Manager” icon to get the below screen

Step 3: Create a Virtual Device (Emulator) by clicking the button “ + Create Virtual Device..” then you will be navigated to the below screen

Select the option “Phone” and you can select any device model based on your real device screen size (if needed) and click on “Next”

The below screen will list out all the applicable Android version for the selected device. Then you can click on download as per the android version you need.

Here I have selected “Android Pie” and click on Download.

Accept the license Agreement and click on Next button

Now the selected Android OS will start to download

Click on finish once download is completed.

So we are done with downloading SDK and successfully created Android Emulator.

Click on “Next” button to create an Emulator

Provide any name to the Emulator and click on “Finish”.

On the next time when you click on “AVD Manager” you will get the below screen where you can see the list of AVD’s created

Click on “Launch this AVD” button (Play button) under Actions section to start the Emulator.

Emulator is launched successfully 😊

Android Environmental variables

Once Android SDK is downloaded successfully we can start using ADB and Shell executables from Android SDK. But the problem is windows OS don’t know from where it need to take the executable files. Imaging its like searching a small tool on a big warehouse. So to make the search easier we need to instruct the OS from where we need to utilize the commands, here comes the Environment variables. We need to inform Windows OS to take the executable files from this location and execute my command. So how to configure this ? let’s see

There are many ways to navigate to “Environment Variables” screen, A common way is to click on start menu and type “My Computer” or “My PC”,  then right click on that and select the “properties” option form the menu. Select “Advanced System Settings” to get system properties screen

Click on “Environment Variables” and Select “New” button under “User Variables” section

Now, its time to create “Android_Home” path. Android_Home is nothing but the parent folder wherewe have our SDK and Platform tools available. By default the Android_Home path should be

“C:\Users\current user\AppData\Local\Android\Sdk”

Enter the “Variable Name” as “Android_Home” and “Variable value” with the directory value and click on “OK” button to add the record.

Now “%android_home%” command refers to the directory where our SDK files are located.

It’s time to add “Platform-tools” and “tools” link to our Sytem variables, under “Path” record. For that click on “Path” record and select “Edit” button under System Variables

As I already mentioned, “%android_home%” refers to our SDK directory, now it’s time to add below 3 directories

%ANDROID_HOME%\platform-tools

%ANDROID_HOME%\tools

%ANDROID_HOME%\tools\bin

Just copy and paste it one by one under “Path” record. Like below,

Now we are done with setting up Environment variables for Android. Click on OK button and close all the open windows.

Now open “Command Prompt” and simply type “ADB” and Enter

If you get the text as mentioned in the image, then you successfully configured the Android SDK variable path in our OS. Now we are good to go with Appium to connect to any real device/ Emulators.

Blog Contributors:

Author:  Ragavendran

Ragav, having 10+ years of testing experience on which 7 years of rich experience in automation using UFT, Selenium (Java/Python). Expertise in Web and Mobile automation testing using Appium.

https://www.linkedin.com/in/ragavendran-ragav

Reviewer: Naveen Khunteta 

https://www.linkedin.com/in/naveenkhunteta

Please follow and like us:
Newer posts »