Jenkins can’t find android device

Sometimes, we see the problem where the jenkins is no longer listing the android device when trying to run our automated tests.

We try adb devices and there is nothing coming back. We unplug and replug and nothing happens.

Well…I worked out what was causing so this so that I no longer see it…and I’ll tell you about that later, but in order to get things running again, here’s what you generally need to do.

You need to kill and restart the adb as root on the jenkins server and THEN you need to unplug and replug the device(s). Do the following (on an ubuntu server)

sudo su
enter password
cd Android/Sdk/platform-tools/
./adb kill-server 
./adb start-server 
./adb devices 
exit

So, what’s causing this? Generally it’s two calabash jobs running at the same time that causes adb to get confused as hell. To prevent this either build a rake task or install a build blocker plug-in.

Cucumber error – invalid byte sequence in UTF-8 (ArgumentError)

Ever get an invalid byte sequence in UTF-8 (ArgumentError) when trying to run your calabash-android project?
Here’s a thing to check – have you got 2 apks in your apk folder / prebuilt folder?
That’s usually the cause.

 

Hope that helps someone out there save 30 minutes trying to work out why your test isn’t running.

Searching cucumber feature files by tag

You know when you’re working on project and you end up with hundreds of cucumber feature files and your working environment doesn’t use JIRA very well and you just have all these feature files not linked to anything to help you determine the status of them…even though you’ve tagged them all nicely?…And you think to yourself, all I want to be able to do is to find all the feature files that I have tagged as @to_do so that I can work through them. Or someone wants a list of all the feature files where the scenarios are tagged @manual? Or you want to see what feature files you have tagged for @android_only or for @ios_only…and so on.

Well, since you’re in an environment which does’t want you messing up JIRA with all your test stuff!!??!!!???? …then you have to take control yourself…so what’s the answer here?

Update: The answer is to create the utility yourself!!
See this post..get files listed by tagname

 

Appium and Cucumber – Managing different iOS and device configurations

Usually you will want to run your cucumber appium test suite on a few different configurations.

For example, you may want to run it on an iPhone 6 9.0 simulator and you also may want to run it on an iPad Air 8.4 simulator. So how do you do this?
Well it’s easy. First you use your cucumber.yml file to list the different configurations. So if we use our example above
we could create two profiles, lets call them iphonesim and ipadsim.
They would look like this in the cucumber.yml file:

ipadsim: IDEVICENAME='ipad simulator'
iphonesim: IDEVICENAME='iphone simulator'

We then need to create two different appium.txt files. One for the ipadsim configuration and one for ipadsim configuration. For example, the iphonesim appium.txt one would look like this.

[caps]
platformName = "ios"
deviceName = "iPhone 6"
platformVersion = "9.0"
app = "../../../apps/TestApp/build/release-iphonesimulator/myApp.app"

[appium_lib]
sauce_username = false
sauce_access_key = false

Save theiphonesim appium.txt file in a folder under Features/Support called “iphonesim” and theipadsim appium.txt under in a folder under Features/Support called “ipadsim”.

Then in env.rb tell Appium where to go to pick up the correct appium.txt based on which profile you have started your tests with.
Do this by adding something like this to your env.rb file

if ENV['IDEVICENAME']=='ipad simulator'
caps = Appium.load_appium_txt file: File.expand_path("./../ipadsim/appium.txt", __FILE__), verbose: true
elsif ENV['IDEVICENAME']=='iphone simulator'
caps = Appium.load_appium_txt file: File.expand_path("./../iphonesim/appium.txt", __FILE__), verbose: true
else
caps = Appium.load_appium_txt file: File.expand_path('./', __FILE__), verbose: true
end

Lastly, to run with your selected profile, cd into your cucumber folder and do the following:
To run the iPhone configuration execute:

cucumber -p iphonesim

To run the iPad configuration execute:

cucumber -p ipadsim

What does this actually do?

Well, if we use “cucumber -p iphonesim” as an example, firstly, the -p flag indicates to cucumber that you want to use a specific profile, so it looks up the profile in the cucumber.yml and sees IDEVICENAME=’ipad simulator’.
Cucumber then looks at the env.rb file and sees that when IDEVICENAME=’ipad simulator’ it should use the appium.txt file in the phonesim folder.
Appium then launches the simulator with the capabilites defined in the Features/Support/iphonesim/appium.txt.

This allows us to switch around configurations without changing anything other than our initial cucumber -p command.

Neat eh?

Installing Appium with Ruby bindings – moving from Calabash – Part 3

Building an IOS app for Appium

For those coming across from calabash, you will be used to having to create a duplicate -cal scheme to embed the calabash.framework into your app. Well, thankfully with Appium there is no need to do this at all. Simply open your Xcode project and build the app. That’s it – nothing special required.

Benefit of Appium over Calabash

The major benefit of Appium here is that the app that you are testing is exactly the same app as you release into production – there is no linked automation framework. This is a definite advantage over calabash.

Running Appium

All you need to do now is open 2 tabs terminal tabs.

In the first one run :

node .

Once this has started up, in the second tab run:

 arc

You now have a fully functioning Appium environment.

Important note: I strongly advise that  you use bundler with gemfiles if you plan to have appium with ruby bindings installed on the same machine as calabash. If you don’t you can end up with horrible things happening.

If you are experiencing gem issues I whole heartedly recommend this post  Installing ruby the Correct Way by cbednarski. It also has a great Appendix on cleaning your environment before re-installing your gems.

Launching Selenium Webdriver from within a Calabash automation script

Many people ask if it’s possible to launch a browser from within a Calabash automation script. Well, since the selenium-webdriver is just another gem the answer is of course yes, it is entirely possible.

Before I explain how to do this, you might first want to consider whether you really should do this. If you are trying to test a mobile app and a website in one automation suite – then this isn’t a good idea  – they should probably have separate automation suites. If you are trying to inject data through something like a CMS for your app then you should probably be using the API. However, if you really feel the need to do this then the solution is simple.

  1. Add the selenium-webdriver gem to your bundle install
  2. Create a helper module – lets called it BrowserHelper
  3. In that helper module require the selenium webdriver
  4. Create some classes / methods in your BrowserHelper module as required
  5. Call the methods from your calabash script – i.e. BrowserHelper::ClassName.method_name()

You are now interacting the with browser using Selenium Webdriver.

Calabash run_loop fifo error – Swift app not launching in Simulator solution

For the past day or so I’ve been having a problem with calabash where I couldn’t get calabash to launch an ios app in the simulator without encountering a run_loop error saying :

RunLoop::Fifo::NoReaderConfiguredError

Turns out that this is was due to the push notification alert popping up before Instruments had taken control of the simulator. This issue is a known calabash issue – see here.

ios calabash fifo error

SOLUTION

The solution was a two step approach:

  1. Add a delay to the code to prevent the alert popping up too soon. The app was written in SWIFT. I added the delay code to AppDelegate.swift as follows:

//Existing code

        if application.respondsToSelector(“registerUserNotificationSettings:”) {

                    //Newly add code to delay the pop up.

            let time = dispatch_time(dispatch_time_t(DISPATCH_TIME_NOW), 4 * Int64(NSEC_PER_SEC))

            dispatch_after(time, dispatch_get_main_queue()) {

                //Code to be executed with delay

            let settings = UIUserNotificationSettings(forTypes: [UIUserNotificationType.Alert,UIUserNotificationType.Badge,

UIUserNotificationType.Sound], categories: nil)

            application.registerUserNotificationSettings(settings)

            application.registerForRemoteNotifications()

                          }

        }

  2. Add code to my calabash script to accept the alert:

if uia_query(:alert).count > 0
# dismiss by touching ‘OK’
uia_tap_mark ‘OK
end

uia_tap_mark ‘OK’

Sublime Text 2 with Cucumber and Gherkin formatting

I normally use RubyMine for my calabash automation code but it’s pretty memory intensive so I thought I’d try something a bit more lightweight (and free)..I choose Sublime Text 2 and installed the cucumber-sublime bundle from drewada on Github.

It works a treat – the only annoying this is that when you hit tab it always tries to autocomplete – this is particularly annoying when creating tables in your feature files.

The cucumber sublime bundle does everything that you’d expect to do i.e. it makes both feature files and step files easy to read; it prompts existing steps and autocompletes variables etc.

Note: it’s also a nifty ruby editor, bash editor and YAML editor. I find myself only opening up RubyMine now when I need to debug something or do something like find other usages of methods etc.

Here’s a little screen shot of how it formats a Gherkin in a Feature file:

cucumber_text_editor

Installing Appium with Ruby bindings – moving from Calabash – Part 2

In part 1 we looked at the set up of Appium when moving from Calabash. In the next articles we look at getting the ruby console (ARC) working to query your app, but first we have to generate an appium.txt file. This article will help you resolve the annoying App doesn’t exist error message relating to appium.txt when running the arc command.

Create the appium.txt file

The next step is to create the appium.txt file – simple right? Well when I first tried to this i hit a couple of problems.

The appium.txt tells appium what platform you are automating and what simulator you wish to use  and where your app is (amongst other things). Although it is nicely explained in the Appium tutuorial it fails to point out three things

  1.   You can create a sample empty appium.txt using the command
 arc setup ios

2.   Not everyone likes using nano to create files but if you use TextEdit to create / edit the appium.txt file you will end up with the wrong kind of double quotes and later on when you come to run the ARC (ruby console) you’ll get appium.txt errors. If this happens copy the correct double quoted from the file generated by the arc setup ios command.

3. When adding the path to you app, appium will automatically prefix it with the path to the folder where are running the arc command from i.e. it is a relative path, not an absolute path. if you use the absolute path to your app you will get an error something like this:

 App doesn't exist

My appium.txt looks like this – you can use it as an example:

[caps]
platformName = "ios"
platformVersion = "9.0"
deviceName ="iPhone 5s"
app = "./myAppName.app"
autoAcceptAlerts = "true"
waitForAppScript = "true"

[appium_lib]
sauce_username = ""
sauce_access_key = ""

I’ll explain the autoAcceptAlerts and waitForAppScript values in another article – running the Appium Ruby Console (arc).

In the next article i’ll explain the difference between building an app for calabash and building an app for Appium.

Installing Appium with Ruby bindings – moving from Calabash – Part 1

If you read my blog you will know that my test automation tool of choice for mobile applications is Calabash using Ruby. However there is definitely a shift in the market towards Appium as the preferred tool of choice for mobile app testing and towards Java as the preferred language of choice for a Software Developer In Test (SDET).

Until now I have avoided Appium as my initial results with it a couple of years ago showed it to be “buggy” and somewhat unreliable – for example a test would execute successfully 9 times and then fail on the 10th attempt for no known reason…but maybe it’s time to give it a go again.

This post shows what I needed to do to get Appium with Ruby Bindings set up and running on my mac

 To Install Appium with Ruby Bindings

  1. Install the appium console
gem install --no-rdoc --no-ri appium_console

2.  Install the flaky gem (f you don’t already have it)

gem install --no-rdoc --no-ri flaky

3.  Do  a brew update (I’m assuming here that you already have brew installed, if not take a look at the calabash set up post https://webdevjourney.wordpress.com/2013/09/30/calasba-sh-for-testing-ios-apps-part-1/ to see how to install homebrew

brew update

[Note – if you get a HOMEBREW_CORE_FORMULA_REGEX error here – just run brew update again (it’s a known brew issue)

4. Upgrade nodesj as follows

brew upgrade node

5. Install grunt

npm install -g grunt grunt-cli

6. I am assuming that you have ant and maven already installed – you may want to upgrade them.

7. Clone the appium git repo

git clone git://github.com/appium/appium.git

8. Ensure you have an Xcode version greater than 4.6.3 installed. See this post for a quick way to check your version of Xcode from the command line: https://webdevjourney.wordpress.com/2015/10/15/find-your-xcode-version-using-terminal/

9. cd into your appium folder (from the repo clone that you did in step 7 and execute

./reset.sh

Note – I encountered a problem here – i had the following output:

* Configuring Android bootstrap
---- FAILURE: reset.sh exited with status 1 ----

Please see the following post on how to resolve this issue:

10. Finally run:

 grunt authorize

11. You are then ready to launch Appium by running:

node .

You will then see a message like this:

info: Welcome to Appium v1.4.13 (REV 96cfaef4aafa2693b4f0630b9d44ca7f0f6ed369)

In the next article I look at running the ruby console to query your app.