Showing posts with label Automation. Show all posts
Showing posts with label Automation. Show all posts

Sunday 2 October 2016

How to Configure TestNG with Android Studio

In our previous tutorial we have learnt about Junit Testing framework  and now in this we will get to know about one more powerful testing framework called TestNG. TestNG framework as some different functionalities which makes it more powerful testing framework.
What is TestNG Framework?
TestNG is a testing framework developed from inspiration of Junit and Nunit with additional features making is more effective and simple for use. It is a powerful automation testing framework where NG Stands for Next Generation. It gives developers power to write more flexible and powerful test cases.

Features of TestNG –
1.It allows you to use new features of Java programming language while writing test cases.
2.Like Junit it provides annotations to identify your test methods and flow of execution.
3.Separates compile time test code and run time configurations.
4.Provides powerful test case execution support.
5.Allows you to perform multithreading testing.
6.Initially It was designed for unit testing but now allows load testing , parallel testing etc.

There is so much more in this and  we will explore more and more when we start coding. But it is very important to learn basics about Testing frameworks before writing your test cases as selection of right framework is like building pillar for your automation framework.

How to Setup TestNG –

1. Create and Android Project or open your existing project
2. Go to app -> build.gradle
3. Copy the below line of code in your dependencies section.
       testCompile 'org.testng:testng:6.9.10'




4. Click on Sync
5. Once you get build successful message then you are ready to go.

Basics of Android Project Structure

Coding with android studio looks very promising because it is an IDE based on IntelliJ IDEA that is used for android application development. Even though we are not going to build android application, it is very important for us to understand the project structure so that we can code effective and understandable automation test framework.
There are five important components of Android Project Structure like main,gradle,.idea, app and External Libraries. Let us Discuss this in detail.

1. Main Project –This would be entire project context. Whatever you do in IntelliJ IDEA, you do that in the context of a project. A project is an organizational unit that represents a complete software solution. A project in Android Studio is like a workspace . In android Studio a project, can contain multiple modules. This means that, in theory it is possible to build multiple apps within the same project.

2. .idea –.idea is used for storing project specific metadata in Android Studio.

3. Project Module –This is the actual project folder where your application code resides. The application folder has following sub directories.
   a) build: This has all the complete output of the make process i.e. classes.dex, compiled classes and resources, etc.
  b) libs : The lib folder is used for storing project specific jar files.
  c) src: The src folder can have both application code and android unit test script. You will find two folders named “androidTest” and “main” correspond to src folder. The main folder contains two subfolders java and res. The java folder contains all the java codes and res contains drawables, layouts, etc.

4. Gradle – Gradle is used for configuration of build procress. We will see more detais about this in our nest post.

5. External Libraries -This is a place where Referenced Libraries and information on targeted platform SDK is shown.

Android Project Structure
Android Project Structure

How to Debug Code with Android Studio ?

Debugging your code to find out the flow of test case execution id very important for Mobile Automation Testing because if there is a test case execution error then we can find it easily and fix it quickly.

Android Studio  provides the shortcuts keys to debug your code and check for the root cause. Do try out below steps to debug your test case. Open any of your test case written in Android Studio.

1. Setting up debug pointer - Debug pointer means pin point a line of code from where you want to start debug mode and run your code one by one.left side of your code just perform a single click and then you will see a red dot which indicated the Debug Pointer.
 

2. Run Test Case in Debug Mode -  Right click on Test Class Name  -> Debug as ''Test Class Name'.


3. Debug Console -  As soon as you run the test code in Debug Mode, the code runs till the Debug Pointer and then at the bottom part of Android Studio a new window opens up which is called as Debug Console. It gives control in our hands now to run the code line by line.

4. Press the button highlighted in the below screenshot for recording the test code line by line. Each time you press the button the selected line of code will get executed.


5. If you got the error which you were looking for and now you want to run the entire set of code then press the button highlighted in the below window.


6. If you want to check out the variable value then open Variable Console and find out.

This is very simple and important thing to learn while writing Automation Code. Share with people who are working for Mobile Automation and spending more time on debugging test code. Happy Testing :)

Saturday 10 September 2016

How to Configure Junit with Android Studio

We have seen how to Install and create a project in Android Studio. Before starting automation testing we need to configure Junit Testing framework with Android Studio. Junit Testing Framework helps us define flow of execution. Let us learn more about Junit.

What is Junit?

Junit is widely used testing framework along with Java Programming Language. You can use this automation framework for both unit testing and UI testing.It helps us define the flow of execution of our code with different Annotations. Junit is built on idea of "first testing and then coding" which helps us to increase productivity of test cases and stability of the code.

Important Features of Junit Testing - 

1. It is open source testing framework allowing users to write and run test cases effectively.
2. Provides various types of annotations to identify test methods.
3. Provides different Types of Assertions  to verify the results of test case execution.
4. It also gives test runners for running tests effectively.
5. It is very simple and hence saves time.
6. It provides ways to organize your test cases in form of test suits.
7. It gives test case results in simple and elegant way.
8. You can integrate Jnuit with Eclipse ,Android Studio,Maven & Ant

Junit With Android Studio

1. Create a new Android Studio Project or Open your existing project.
2. Go to App -> Build.gradle.



3. In the file add the line of code as shown in below screenshot.


4. click on sync.


5. Once you get Buid Successful Message then your Junit Setup is ready for use.

Tuesday 8 March 2016

How to log test cases in Android Studio


Logging test case results is as important as writing test cases. There are many ways to log the results provided by JUnit but I am going to provide a simple and effective solutions which I adopted to Log Test case results on Android Studio.This method is independent of selection of automation tool. 

It is also a good practice to take a screenshot from the device when test case fails but that code is tool dependent so I will share that too in future posts.

The way you log your test case results will simplify tracking of bugs and generate a proper report as output of your Test Suit Run.Hence I suggest you to start Logging your test case results starting from your very first test case.

Let us see with simple example.

1. Create a Java Class names TestCases in the Android Studio.
2. Declare a Tag in the class as String LoginTestCase.
3. Then write the log on sucess or failure as per you like as shown in below test case. I have added Appium test case as sample but this you can use in any of your Test Cases.


public void TestCase
{
AppiumDriver driver;
String Tag_name="ScrollTestCase";
@Before
    public void testCaseSetup()throws  Exception
    {

        DesiredCapabilities cap=new DesiredCapabilities();
        cap.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
        cap.setCapability(MobileCapabilityType.DEVICE_NAME, "Android device");
        cap.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, "4000");
        cap.setCapability(MobileCapabilityType.APP, "c://apks//seekbarsample.apk");
        cap.setCapability("noReset", true);
        driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);

    }

@Test
    public void testVerticalScroll()
    {
        //Scroll till the 25th row
        driver.scrollTo("List item:25");
        //Click on the 25th row text
        driver.findElement(By.name("List item:25")).click();
        try {
            Log.e(Tag_name,"Test case passes");
            Assert.assertEquals("Clicked on row no: 25", driver.findElement(By.id("row_select")).getText());
        }catch (AssertionError e)
        {
            Log.e(Tag_name,"Test case failed")
        }
    }
@After
    public void testCaseTearDown()
    {
        driver.quit();
    }
}

4. Once you are done adding adding log in each test case ten run the test suite.
5. Then click on Android Monitor at the  and it will open the Monitor as shown in below screenshot
Log test case in Andorid Studio
Log Appium Test Execution Report

6. Then click on Edit Filter Configuration as shown below.

Log Appium Test Case in Android Studio
Check logs in Android Studio

7.Enter Filer name and our Tag name hit OK.

Log Appium Test Case in Android Studio
Filter Logs in Android Studio

8. Then you can read your test case logs.

I hope you find this tutorial useful. Do share your feedback and questions in comments section below.Please follow QA Automated on social media to get latest post updates. Happy Testing :-)






Thursday 4 February 2016

How to locate Android App UI elements ?

Before you commence with writing test cases in any of your choice automation tool, you need to know how will you find the UI element within you android app. It is important to understand why you need to learn this. Automation is nothing but performing actions on different UI element via test cases so before you perform these actions , it is very important that you know on which UI element you want to perform the action. So finding that unique identifier to locate the UI element within the app is becomes the first priority.

Currently I am using UI Automator Viewer for Locating android app elements which is pretty good and meets our requirement. I use chrome inspector to  locate elements for web views in hybrid apps. 

UI Automator is tool provided by android SDK which offers an interface to view and analyze UI component's properties in hierarchical manner and we can create our own xpath using these properties. (here properties are id, class name, content description, text etc)

Steps to inspect UI elements using UI Automator Viewer- 

1. Connect your device to PC with USB and turn on USB debugging as explained in HERE.
2. Go to command Line and give path till android-sdk/platform-tools/
2. run command uiautomatorviewer
3. Wait till UI Automator Viewer  window is launched.
4. click on the green icon at top left corner shown below when the app to be tested is open in your android device.

5. Mouse over the on UI element which you want to locate and then click on it to fix it.
6. Then on the right side you will find the UI properties associated with the element which you can use in your test case.


Steps to inspect Web View UI elements with Chrome-
1. Go to chrome browser
2. Type this url -chrome://inspect/devices#devices
3. click on any url listed on web page.
4. Then click on any UI element and you will get associated html source code which you can use to write your own xpath.




How to Run the Automation Test Case on Real Android Device?

It is more accurate if you run your test cases with real device than with emulators. I will post later how to run the test case with emulators but now we will learn how to run test cases on the android devices. This tutorial is generic irrespective of which tool you to write automation. 

The process is simple you need to connect your device with USB cable to your PC  and then you need to enable the USB debugging option.

1. Check if the developer option is enabled for the device or not. To do so open Settings -> check for text "developer options if you find it then. GO TO step 3.


2. Enable Developer Options - Go to Settings -> click on About Phone -> Tap on Build Number continuously for seven times -> You will see the toast saying " you are a developer". You can now find Developer options in Settings.

3.Last step is to enable USB debugging for that first connect device to PC and them click on Developers options and check USB debugging. It will ask you for with pop up the hit on "OK".


Now your device is ready for test case execution.

You can also cross check whether you have done it correctly or not -

1. open command prompt
2. Run command adb devices
3. You can then see the list of connected devices.


Now you and your device is ready for running test cases. in case of any doubt post comments. 

Tuesday 19 January 2016

How and Where to add Test cases in Android Studio?

There are some specific rules for writing test folder in android studio related to project structure and hierarchy. In case of Espresso, Robotium and UIautomator we need to write instrumentation test cases which will be written in androidTest folder of the project. Let us see how to create that folder.

1. In android studio it is important to write instrumented functional test cases in src/androdTest/java folder and unit tests in src/Test/java.

2.Now we need to create androidTest folder. right click on the src -> New ->Folder ->Java folder


3. Click on Change Folder Location and add location src/androidTest/Java

4.  Right click on java -> New -> Package and the enter package name


5. Once the package is created then right click on the package name -> New -> Java Class give Class Name TestClass  and click on OK.


After creating the TestClass you are ready to start writing your test cases.

How to Create Android Project?

This is very simple post about how to create Android project you which you need to do before writing test cases.

Open android studio and create a new project-
  1. click on start new Android ProjectScreenshot (8)
  2. Enter App Name and Package Name and click NextScreenshot (9)
  3. Click Next                                             Screenshot (10).png
  4. Select Blank Activity and Click on NextScreenshot (11).png
  5. You can edit the Activity Name here and then click on Finish. Screenshot (12).png 
  6. you have successfully created your project now.

Getting Started with Automation

Hi all in this post is about moving your manual testing efforts into automation testing for your native and hybrid android application. Once you decided to for automation to save time for release cycle and to release quality App , then next step is to decide on which tool is suitable for your application, organization and team so that you can benefit form your automates test suits.

I am going to share with some top most automation testing tools for native and hybrid android application with their pros and cons so that you can make a better choice and take first step towards automation.

1. Appium

Appium is open source tool available for native and hybrid mobile applications testing for android as well as iOS. Appium uses vendor provided automation frameworks under the hood such as Apple's UI automator for iOS apps, for android apps 4.2 and above UIAutomator and 2.3 to 4.1 Selendroid.
Appium's architecture is client server based architecture. Appium server exposes REST API which recieves connections form client and executes those commands on mobile devices.Automation is performed in form of sessions and Desired Capabilities and the key to send information to server regarding the requires session.

Pros-
1. Appium client libraries supports multiple languages hence you can write test cases in different languages like Java, Ruby, Python, PHP, JavaScript and C#.
2. Appium supports cross platfrom script development.
3. Appium has large community support where you ask and share.
4. It supportd test running on physical devices as well as Emulators.
5. Supprt for native and hybrid mobile application for android as well as iOS.
6. Record and play back is available for Mac OS 

Cons- 
1. Record and play back is not available on Windows OS. 
Documentation is not user friendly.
3. Does not support image comparison.
4. Setting up takes some time especially in case of Linux system.
5. Limites Support for Gestures.
6. Limited support for android version < 4.1
7. No Support for Toast Messages.

 

2. Robotium

Robotium is also an open source automation testing framework for android. With robotium is very effective to write black box test cases and if you have sorce code you can also go for grey box testing.Its API is very simple and you can quickly start writing the UI automated test cases.


Pros-
1. Simple APIs provides to write test cases so that you can concentrate more on your test scenarios.
2. Supports back box as well as grey box testing.
3. Easy integration with Gradle, ANT and Maven.
4. Can be used with Jacoco to measure code coverage.

Cons-
1. To write test cases using Robotium one must have knowledge of Java 
3. Espresso

Espresso is an open source framework for native android applications developed by Google.Espresso is build on top of Android Instrumentation Framework.Espresso supports white box testing too.

Pros-
1. Supported for all android versions.
2. Simpler and Quicker to setup.
3. Supports Jacoco to measure code coverage.
4. Extensive testing is possible as it covers many UI actions and gestures.

Cons-
1. The test cases are written inside the android application project code and hence building project to run test case takes lot of time.

4. UIAutomator-

UIAutomator is test framework by Google fr native android apps and games.It comes with APIs to create UI test cases and execution engine.It supports devices with API level 16 and above. It is very good for native apps but lacks support for web views and hybrid apps.

Pros-
1.Simple and Easy to learn information is available.
2. Support is strongly maintained by Google.
3. Possible to to do cloud bases test management.

Cons-
1. Supports android version greater than 4.1.
2. Web view is not supported.