Automate Functional Android app testing with Appium

Hello Folks,

Android app base is growing day by day and so it becomes vital to quickly test and release the apps to production. Appium is one among the promising android app testing frameworks. Let us get a grasp of it for testing Android applications.

Appium Architechture:

appium_architecture

Appium follows a server client architecture to execute test cases in real devices or emulator. Appium server is written in node.js and it runs in the developer machine. Once it is started, it will be ready to receive the requests via the test scripts written in any one the languages like Java, Python, Ruby, C#, node.js or Objective C++. Appium server will interpret the command from these languages and will send it to the device in a way that the device can understand. This is how It is able to support many languages.

But still it needs lot of support inside the android device to execute the test cases. So that for Android 4.2 and above, it utilizes UIAutomator along with its own libraries. For Android 2.3 and above , It uses selendroid along with Instrumenation class. To achieve this, Before installing the test apk, It will install all the dependencies to  the device.

Once this is done, test scripts will send the commands to the appium server and appium server will send it to android device. Support tools inside android device will interpret and execute the commands in the device.

Appium Support and Limitations:

Refer here for what can be done and what cannot be done with Appium as of now. Remind yourself to check this frequently as Appium is constantly getting better and better.

Appium Setup:

1. Download and Install node.js. Click here to download . As I told before, Appium server is written in node.js. So that we need to install node.js, before installing appium.

2. Download and install Appium server. Click here to download.

Open appium server and try out the different options provided in the GUI. Most of them are self explanatory. For Android,  Select the Android radio button on top and fill in details like SDK path, Device serial and your apk path, etc.  So with this you are all set with Appium server for android. Now launch appium server by clicking the Launch button on right top corner. It will launch the appium server and logs will get started to flow through in the appium server window.

Appium_server

Incase of any errors, Uncheck your proxy setup and try again.

Writing Appium Test case:

To write appium test cases, you can select any one of the supported languages and install the support libraries to start. In my case, I have chosen Python and installed the appium support libraries for python. Appium Client Libraries can be downloaded from here. Python “unitest” library is used to write Appium test cases.

Here is a simple Python based appium test script with two test cases:

# appium_testcase_android.py
from selenium.webdriver.firefox.webdriver import WebDriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver
import unittest
import time
import os

success = True
desired_caps = {}
desired_caps['appium-version'] = '1.0'
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '4.4'
desired_caps['deviceName'] = '32338239A8E400EC'
desired_caps['app'] = os.path.abspath('/Users/bala/Desktop/temp/MDusterDemo.apk')

class AppiumTests(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
        self.driver.implicitly_wait(60)

    def tearDown(self):
        self.driver.quit()

    def test_case_1(self):
        self.driver.find_element_by_xpath("//android.widget.ScrollView[1]/android.widget.FrameLayout[1]").click()

        tView = self.driver.find_element_by_xpath("//android.widget.ScrollView[1]/android.widget.TextView[1]")
        text = tView.get_attribute('text')

        self.assertEqual('ReceiveCalls', text);

    def test_case_2(self):
        self.driver.find_element_by_xpath("//android.widget.ScrollView[1]/android.widget.FrameLayout[1]").click()
        self.driver.find_element_by_xpath("//android.view.View[1]/android.widget.LinearLayout[1]").click()

if __name__ == "__main__":
    suite = unittest.TestLoader().loadTestsFromTestCase(AppiumTests)
    unittest.TextTestRunner(verbosity=2).run(suite)

If you look at the code, most of the part is boilerplate. You can define your test cases by adding more methods starting with ‘test_’ . So let us see, How to write the test commands to perform on our app.

Appium provides a tool called Inspector which provides a recorder interface where it creates the equivalent command for every user action. It supports all the languages that are supported by appium. So just do the test scenario in the recorder and copy the code from Inspector and place it happily in the respective methods of your script.

Appium Inspector:

This is how inspector looks like, I tap on the calendar app’s month spinner,
Inspector

Code is created by the Inspector at the bottom, Now I can copy and use it in my script.
Inspector_1

So in the above screenshot, I have opened the default calendar app, Selected the month spinner and clicked the tap button. So that you can the generated the line of code the next screenshot. So that I will do the sequence of operations this way and finally copy the code and use it my test script. Spend little time to check each and every part of the Inspector.

Running Appium Test case:
Once we write the script, Executing it is like any normal python script execution.
$python app_test_suite_1.py

So the output will look like this,
Appium_output

This test result for another app. So dont get confused about the calendar app example ;)

Unlike few other functional testing frameworks like ‘MonkeyTalk’ Appium never touches the app apk. Also it installs and uninstalls the app for every test case. So that It will be highly effective for automating android app testing.

Happy Coding,
Balachandar KM

Join a WiFi network programmatically in Android

I recently came across a requirement where I need to connect the android device to specific Wifi Networks on request. Here is how i have coded it,

The request will be received through a broadcast

1. Create Wifi Configuration with WifiConfiguration class for the new wifi network.
2. Use wifi manager to disable all other networks except the network that I just added.
3. Now try to connect to an available enabled network. So that the device will connect to the only available enabled network which is the one we added.

Lets go through the code,

WifiConfiguration:

WifiConfiguration wifiConfiguration = new WifiConfiguration();
        wifiConfiguration.SSID = “\”” + networkSSID + “\””;  // This string should have double quotes included while adding.
    wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); // This is for a public network which dont have any authentication

//Add the created wifi configuration to device
        int netId = wifiManager.addNetwork(wifiConfiguration);  //Adds to the list of network and returns the network id which can be used to enable it later.

WifiManager:
WifiManager wifiManager  =(WifiManager)context.getSystemService(Context.WIFI_SERVICE);

       wifiManager.disconnect();
       wifiManager.enableNetwork(netId, true); // Seconds parm instructs to  disableOtherNetworks
       wifiManger.reconnect(); // Now we will connect to the only available enabled network.

Additional Notes:
      WifiManager can switch on your Wi-Fi too. But Incase If you want to switch wifi ON and then do the above, Try to provide few seconds delay (Use thread) for the wifi to switch on before you fire the methods to connect to your network.

To Switch On :  wifiManager.setWifiEnabled(true);
To Switch Off : wifiManager.setWifiEnabled(false);

Thanks to the stack over flow thread here.

Happy Coding, Balachandar KM

Android Studio – Time to Shift Gears with Android Development!

Hello Folks,
Among the number of announcements at Google IO, few of you may have missed the Android Studio Beta announcement.
It had a long span of early access preview and finally entered beta phase with a bang. Yeah, the only IDE that supports Android wear and Android TV development. During Android fire chat at Google IO, a Google developer told that they don’t have any plans to provide further support to eclipse adt as they chose Android Studio as their platform to do Android development.

image

Early previews of Android Studio were so buggy. I used to get frustrated in few hours. But I would say that I m quite impressed with beta. It has lot of nifty features tailored for Android development. I will write further on specific Android Studio features in upcoming blog posts. It’s time to shift your gears with Android Development guys ;)

Android Everywhere!

Android is going to be categorized in tech world as before Google I/O 2014 and after Google I/O 2014. Google has introduced three new platforms to develop Android apps namely Android Wear,  Android Auto and Android TV.
It is going to open the doors for developers to develop apps for so many different use cases and also to reach lot more users with lot more user engagement.
As an Android Developer,  I’m truly excited and started to grasp the enormous amount of information that was revealed in Google IO 2014.
A challenge I see here is to get the developer devices. Google released Android L developer preview images only for nexus 5 and nexus 7, Not for nexus 4. I don’t really have any pushing need as an user to buy Nexus5 as my nexus 4 does everything good without a lag. So either I need to live with emulators or spend 30k to quickly jump into Android L.
Android Wear is priced around 15k which I feel that it’s  a fair price point even though it can be priced less than 10k.  So I am looking for Moto360 as my first android Wear device.
As I read, Google Auto development can be done with tablet like Nexus 7. But it is really interesting to design UI for Auto users. Let’s wait and watch this segment of apps.
For Android TV,  Emulators will be the Dev testing place. Still I hope that Google will release the plug and play Android TV set top boxes soon which are going to make your existing TV’s smart.

So overall,  Android is moving in a direction to run all around you in an awesome way.

Happy coding,
Balachandar KM

Google IO 2014 Extended

Last night I was watching Google IO 2014 extended. I felt so exited right from the start. I was clapping and whistling for every new milestone that they announced as if India won the cricket world cup. The way they designed the countdown timer for Google IO clearly shows the amount of dedication they have for Google IO.

They had not given any time to think at all.  Back to back,  awesome features were announced like a dream.

Android L 😍
Android One
Material Design
Android Wear
Android Auto – Bolt from the blue.  No one expected this.
Android TV
Chromecast Mirroring support
Google Cloud Tools
Appurify
Android for Enterprise
Android apps running on Chromebook
Material design for Web apps via Polymer
Project Volta
Battery Historian
App indexing open for all
Apps with contextual awareness
Google Fit
New features for Google Play Games
And 5000+ apis

I know there is a lot to digest for now. I am going to spend my weekend watching all Google IO 2014 sessions.

During the presentation,  Sundar showcased a video on how Android platform is used to build innovative solutions all around the world for the day to day problems.  I would say that this is clearly showing the true achievement of Android.

Finally he stated that Google paid $5 Billion dollars to developers who are developing content for Google Play. This totally shows the amount of importance that Google gives to Google Play.  They want it to be their one stop solution to transform a pure vanilla Android to Google Android. 

Happy coding,
Balachandarkm

Steve Jobs is Still Alive !!!

Steve Jobs is still alive – In my Heart  !!!
                    I got to know about Steve during my college days. I was so impressed when I touched his master piece Ipod. I was literally blown off. I had never seen a device which was so elegant and beautiful interms of software as well as hardware. It made me crazy and I enjoyed the music in a different way. Thanks to my brother, who bought an Ipod.

Steve Jobs ( 1955 - 2011 )
A Tech Legend

Steve earned so much of respect in my heart and I was so curious to know more about Stevy. Same time I was happened to watch the movie “Pirates of silicon valley” which portrayed two brilliant masterminds of the silicon valley namely Stevy and Bill. That movie really made a bad impression about stevy in my mind. Two things were pretty bad and made me dislike him little. It showed his arrogant and bossy nature over his employees and He didn’t accept Lisa as his daughter. So I thought like, he was one another american guy who was so arrogant but brilliant.
The new product line up from Apple like IMac, IPod, ITunes, IPhone and IPad were really changed the fortune of Apple and made it one of the top most company among the tech companies in the world. Remember when stevy rejoined Apple, It was sinking badly and everyone thought, Apple was going to rot soon. But his vision on the tech world really paid Apple so much. He made his own company stand up again.
All these things made me respect stevy so much and I started to watch him so close. I used to watch all his speeches, Interviews and introductions of new Apple products. His stanford commencement speech was a master piece from him for the tech world. I am sure that it will fuel the young minds all the time to acheive something INSANELY big.
Oct-5-2011. A Black day in my life. I woke up like every other day and checked regular news updates. I was taken back by the news that “Steve Jobs was Dead”. My heart felt some unbearable pain and I couldn’t do anything. I was simply thinking, “How the Hell it can happen to Steve?”. The real truth of life is so difficult to understand. I read his Biography written by Romain Moisescot. I learned lot from Steve. If you take risk then you will be awarded for sure. That is what Steve did all his life. He took so many risks to implement his visions and never tried to sit in a comfort zone.

Steve-Jobs-Death-Quote
Death is the True Inspiration

I don’t really believe after life things. But if it is true and Steve gets internet access in his IPad over there in Heaven, I have few words for him.
“Dear Steve, Though you left the earth, You are living in my heart for ever n ever till my heart beats. You are a true motivational factor to me. Long live Dear Steve with your thoughts”

Steve in Stanford : Death is the destination we all share, no one has ever escaped it. And that is as it should be because death is very likely the single best invention of life.

With Tears, :'( Balachandar KM

End of A Website A Day Series

Guys :

Hope you all enjoyed AWebsiteADay Series of posts. But it makes me boredom of posting one website a day and it filled my blog and kind of hiding all other posts. So I am stopping myself to post this and let me come up with a new series.

Thanks for your support.

Regards,

Balachandar.KM