Android Studio

19
Jun

How to Debug Code with Android Studio

How to Debug Code with Android Studio



Being Tester we are used to reporting bugs and we know that developer debug the code to find out the line of code causing the bug. But now you are the one coding the test cases and you are responsible for solving any bug in your testing framework.


Android Studio provides some shortcut keys for debugging. In this post, we will use our HelloWord Espresso test Case Example and We will run our test Case In Debug mode and we will debug our code line by line.


It is always good practice to use Debug Mode for root tracing the bug as it allows you to run your code line by line by setting a debug point. Debug point is the nothing but marking the line of code from which you wish to start debugging. This speeds up your work and efficiency.

  • In your Android Studio project copy the below test case.
  •  
  • Now we will set a debug pointer. Suppose we are confident that our test of code is working fine except our test and we wish to debug the test case then we will place our debug pointer at the first line inside our test case. For this click on the left side space in front of the selected line and the pointer is set as shown the below screenshot(Red Dot represents the debug point).

  • Now we need to run our test case in Debug mode. Select the class name -> right click -> Debug as “MyActivityTest”. Make sure that the Android device is attached to your computer with USB debugging ON.

    Debug

  • As soon as your code runs till the debug point. You will see a debug console on your bottom left side and the code stops running at the debug point. Now the control is in your hands to run the code line by line.

  • The button highlighted in below screenshot allows you to run the rest of the code. When you click on it rest of the code runs automatically.

  • The button highlighted in below screenshot allows you to run the code step by step. Each time you click the button the line highlighted in the code gets executed.

  • You can also observe what is getting stored in your variables by opening checking variable window –

This will help you debug your test cases and write a efficient and bug free code.






17
Jun

How to Write and Run First Test Case in Android Studio?

How to Write and Run First Test Case in Android Studio?


 

So far we have learnt on creating our Android Studio project and overview of Junit and TestNG testing framework. In this Tutorial, we will write our first test case and run it. It sounds exciting but before that we need to decide a testing tool which we want to use for writing testing Framework.

We have many famous testing tools in market like Appium, Robotium , Espresso etc. Espresso is an open source framework provided by Android Studio so let use that to write our first test case.

The main purpose of this tutorial is to learn to write and run test case in Android Studio so we will focus on this and not on testing tools. We will learn about testing tools in future posts.

Perquisite –

  • Create an Android Studio Project
  • Configure TestNG Framework.
  • Configure Espresso Tool

 

Steps for Writing Test Cases –

      • Go to app -> src ->androidTest -> Java Android Studio

 

      • Right click on the package name

 

      • Go to New -> Java Class and click
      • Enter File Name and Click on Ok.
      • “Now your java class is ready.>

Now we will write a simple code as per given below

Test Case Explanation –

1. The class uses two annotations @LargeTest and @Runwith these two annotations are used to specify the behavior for our TestClass.
2. The class extends AcitivtyInstrumentationTestCase2 class with launch activity as MainActivity which we want to test in this example.
3. @Before and @After is annotations and used to specify which method should be executed before each test case and which method should be executed after completion of each test case.
[email protected] annotation is added for each test case method.
5. In the below test case the getAcitivity() launches our Main Activity.
6. onView() is used to select a view for testing and withId() is used to locate the UI element and rest is used to check whether the particular element is displayed or not.

Running Test Case –

        • For Android Studio Version Less than 2.1
          • Click on Build Variants
      • Select Android Instrumentation Tests
      • Right click on class name and click on Run
      • For Android Studio Versions More than 2.1
        1. Right click on class name and click on Run





16
Jun

How to Configure TestNG in Android Studio ?

How to Configure TestNG in 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 –

  • It allows you to use new features of Java programming language while writing test cases.
  • Like Junit it provides annotations to identify your test methods and flow of execution.
  • Separates compile time test code and run time configurations.
  • Provides powerful test case execution support.
  • Allows you to perform multithreading testing.
  • 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 –

    • Create and Android Project or open your existing project

 

    • Go to app -> build.gradle

 

  • Copy the below line of code in your dependencies section.
     
  • Click on Sync
  • Once you get build successful message then you are ready to go.

 






15
Jun

How to Configure Junit Testing Framework in Android Studio

How to Configure Junit Testing Framework in Android Studio?


In our previous post, we talked about the Android Studio and Java Installation. In this we will learn about very famous unit testing framework setup for android studio that is Junit. Before jumping on to the setup let us learn more about Junit Testing Framework.

What is Junit ?

Junit is a widely used testing framework used for Java Programming language. Before you learn, how to write automated test cases it is very important to learn Junit testing framework in detail. It helps you to define the flow of your testing framework.
Junit is built on idea of “first testing and then coding” which helps in increasing productivity of the test case developer and stability of the code.

 

Features of Junit Testing –

  • It is open source testing framework allowing users to write and run test cases effectively.
  • Provides various types of annotations to identify test methods.
  • Provides different types of Assertions to verify the results of test case execution.
  • It also gives test runners for running tests effectively.
  • It is very simple and hence saves time.
  • It provides ways to organize your test cases in form of test suits
  •  You can integrate Junit with Eclipse, Android Studio, Intellij etc.

How to Setup JUnit–

    • Create a new Project in Android Studio – (Refer our previous post)
      • Go to app -> build.gradle
      • In the file add below line of code in dependencies section:
        • testCompile group: ‘junit’, name: ‘junit’, version: ‘4.12’

     

      • Click on SyncJunit
      • Once you get Build is Successful message then your Junit Framework is Set and you can use it while writing your test cases
      • Note – We will see how to use Junit in your test cases in our future posts

     






14
Jun

How to install Android Studio?

How to install Android Studio?


 

A Manual tester want to start and learn automation, there are many curiosity come to their mind about what tool to use?, which IDE(Integrated Development Environment) to select?, what testing framework to select? This tutorial will help you to know about an IDE called Android Studio. IDE is nothing but development environment for writing and executing code.
Android Studio is the official IDE for coding android app, but it is also very good alternative for writing and executing test cases. Android Studio is very easy to install and faster to write, deploy and run.

 

Let us see why Android Studio is best suitable for testing

  • Android Studio supports both JUnit and TestNG testing framework which are most commonly used for building automation framework.
  • We can use various automation tool with Android Studio like Appium, Espresso and Robotium etc.
  • Android Studio provides support for Unit Testing (White box testing) and Instrumentation Testing (Black box testing).
  • After Test case execution, Android Studio generates html report as an overview of passed and failed test cases.
  • Most interesting feature provided by Android Studio is we can calculate code coverage on the basis of test cases written. This helps us measure our testing efforts.

After knowing, how we can effectively use Android Studio for our testing effort. Now, we will see how to install Android Studio. Before installing Android Studio, it is required to install Java.

      • First of all Download latest JDK version from This Link
      • Click on “Accept License Agreement” and download latest code.
      • Click on .exe file and then java installation window will open.
      • Click on next and finish installation.
      • Installation doesn’t complete unless you set JAVA_HOME.
      • So go to my computer properties.
      • Click on Advanced System Settings -> Advanced > Environment Variables
      • Click on New under System Variable. Then in Variable Name enter “JAVA_HOME” and Variable Value as “Java Installed Path” (It mostly lies in : C:\Program Files\Java\jdk1.8***)
      • Now, edit path variable and add “;%JAVA_HOME%\bin” at the end.
      • To verify that java is installed successfully, go to command prompt and enter “java – version”. You will see “Java Version 1.8.***
    • Now Download Android Studio from THIS LINK
    • Click on .exe and install the Android SDK into your system.
    • Installation doesn’t complete unless you set ANDROID_HOME. So go to my computer properties.
    • Click on Advanced System Settings -> Advanced -> Environment Variables -> New Variable and Variable Name – “ANDROID_HOME” and variable value-” C:\Users A\Downloads\android-sdk . The same way as we did for Java Home
    • Then, Select Path Variable -> Edit and then add following path after semicolon (;) %ANDROID_HOME%/tools;%ANDROID_HOME%/platform-tools

 






13
Jun

Difference between unit tests and instrumentation tests

Difference Between Unit Tests and Instrumentation Tests


 Android Studio allows you to write unit test as well as instrumentation tests. It is very important for us to learn more about unit tests and instrumentation tests. We have defined it in terms of Android Application.

In layman terms, Unit tests are the test cases written for testing code written by developers and Instrumentation test are the test cases written for testing UI & functionality of the application. In short, Unit tests is associated with white box testing and instrumentation tests is associated with black box testing.

Unit Tests –

  • Unit tests runs in local JVM and minimizes the execution time.
  • Unit Tests cannot test UI of the app without mocking activity objects.
  • Unit test are used for white box testing for testing code.
  • Most of the time Unit Tests are written by Developers.
  • You don’t need a device connected for execution of Unit Tests.

Instrumentation Tests –

  • Instrumentation tests are used for black box testing.
  • It is used to test GUI of the application along with its functionality in real environment.
  • For execution of Instrumentation tests you need a device /emulator on which the application is first installed and then tests are executed.
  • Automation Testers are involved in writing instrumentation framework.





11
Jun

Basics of Android Project Structure

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.

  • 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.
  • .idea –.idea is used for storing project specific metadata in Android Studio.
  • Project Module –This is the actual project folder where your application code resides. The application folder has following sub directories
    • build: This has all the complete output of the make process i.e. classes.dex, compiled classes and resources, etc.
    • libs : The lib folder is used for storing project specific jar files..
    • 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.
  • Gradle – Gradle is used for configuration of build procress. We will see more detais about this in our nest post.
  • External Libraries -This is a place where Referenced Libraries and information on targeted platform SDK is shown.