Tuesday, October 22, 2013

Cross Browser/Cross Platform Automated Testing with Ruby


As developers, we’ve typically developed cross browser applications but now, with the advent of mobile devices, there’s a new plethora of devices to develop against.  How do we test against these? 
Cucumber + Watir-WebDriver + Appium = Cross Browser, Cross Platform Testing
We’ll walk through the highlights of setting up each browser and platform and also to capture screenshots.
The full code is available from my Github:  https://github.com/matthewbussa/watir-webdriver-demo
In order to start with, you’ll need the following software:
  • Ruby 1.9.3
    • Cucumber gem
    • Watir-WebDriver gem
  • Appium – please note that support for Appium in Windows is in “beta”
  • XCode
    • Install the Command Line tools (XCode –> Preferences –> Downloads –> Command Line Tools
    • Install the 6.1 Simulator in the same place
  • GenyMotion – free account is required.  Ensure you have Oracle VirtualBox installed before installing Genymotion

Cucumber Scenario to test:

Scenario: "Capture screenshot for one device and platform"
    Given I am on the home page
    Then a screenshot is captured

Setting up the Browser

With Watir-WebDriver it’s easy to setup in instance of a Watir Browser and initiate it for different browsers or platforms.  Below are some examples on how to utilize this feature:

FireFox

setup_firefox()
    browser = Watir::Browser.new :firefox
    browser.driver.manage.window.maximize
    browser
end

Chrome

For Chrome, you’ll need to download ChromeDriver v2.2  and place it in your class path.  Be sure to start the chromedriver before running your tests.  At the time of this writing, ChromeDriver v2.3 was not compatible.  The following is the Ruby code to setup an automated Chrome browser: 
setup_chrome()
    browser = Watir::Browser.new :chrome
    browser
end

For iPhone, iPad, and Android, ensure that Appium is up and running on the default port of 4723.  Appium will automatically load the iPhone and iPad devices however, it doesn’t automatically shut them down.  This can easily be done using the “kill ‘iPhone Simulator’” or kill ‘iPad Simulator’ commands from the terminal.

iPhone

setup_iphone()
    capabilities = 
    {
        'device' => "iPhone Simulator",
        'browserName' => 'iOS',
        'platform' => 'Mac',
        'version' => '6.1',
        'app' => 'safari'
    }

    server_url = "http://localhost:4723/wd/hub/"
    driver = Selenium::WebDriver.for(:remote,
                 :desired_capabilities => capabilities,
                 :url => server_url)
    browser = Watir::Browser.new driver
    browser

end

iPad

The iPad version is the same as the iPhone setup with the exception that the ‘device” capability is different. 
setup_ipad()
    capabilities = 
    {
        'device' => "iPad Simulator",
        'browserName' => 'iOS',
        'platform' => 'Mac',
        'version' => '6.1',
        'app' => 'safari'
    }

    server_url = "http://localhost:4723/wd/hub/"
    driver = Selenium::WebDriver.for(:remote, 
                 :desired_capabilities => capabilities, 
                 :url => server_url)
    browser = Watir::Browser.new driver
    browser

end

Android

In order for Android to work, ensure you have both Appium running and that you have an Android Emulator up using Genymotion. Also ensure that the Android Emulator you are using has the Chrome browser installed. This can be installed through the emulator's play store. A Gmail account will be needed to log into the Play Store.
setup_android()
  capabilities =
  {
  'app' => 'chrome',
  'device' => 'Android'
  }

  server_url = "http://localhost:4723/wd/hub/"
  driver = Selenium::WebDriver.for(:remote, 
               :desired_capabilities => capabilities, 
               :url => server_url)
  browser = Watir::Browser.new driver
  browser.driver.manage.timeouts.implicit_wait = 30
  browser

end

Capturing the Screenshot

The logic to capture a screenshot is fairly straightforward for non-Android devices.  However, Android devices are a bit tricky.  We need to save the screenshot to the sdcard on the emulator, then move the file to our location and finally, do some cleanup and delete the image on the sdcard.

def capture_screenshot()
  time = Time.now.to_i.to_s
  filename = time + '.png'
  if (platform == 'android')
    %x(adb shell /system/bin/screencap -p /sdcard/screenshot.png)
    %x(adb pull /sdcard/screenshot.png ./screenshots/screenshot.png)
    FileUtils.mv('./screenshots/screenshot.png', filename)
    %x(adb shell rm /sdcard/screenshot.png)
  else
    browser.driver.save_screenshot filename
  end

  filename
end

If you’re already using Cucumber and Watir-WebDriver for your automated web testing, Appium makes it easy extend your coverage utilizing the existing tool set and begin testing on mobile devices and if you’re not, here’s a framework to get you started!

Happy Testing!


Update 11/11/2013:  With the XCode 5.0, Apple removed the ability to launch built in applications.  This means that if you use XCode 5.0 or above, Appium will not launch Mobile safari, which is what the tests are setup to do.

No fear, the solution is to ensure you have XCode 4.6.3 and iOS Simulator 6.1.  You can download the older version here:  https://developer.apple.com/downloads/index.action#

There's some documentation on how to switch between versions of XCode from Appium here:  https://github.com/appium/appium/blob/master/docs/running-on-osx.md

Saturday, August 17, 2013

My First Advocare 24 Day Challenge Experience

My wife and I started the 24 day challenge on July 8. Our motivation for going down this journey was primarily for our boy/girl twins. To sweeten the pot a bit, we both also signed up for healthywage.com for longer term motivation. We are both obese and wanted to form healthier habits in order to gain more energy but most of all to maintain these healthier habits so when the twins get to the point of eating solids, we can teach them good habits in hopes they won't have the same struggles as we have had. We didn't want a diet, we wanted a lifestyle! After hearing multiple friends having great success with Advocare, we decided to try it.


Before Advocare 


I was a frequent fast food/restaurant shopper. I'd often go to McDonalds for breakfast, go out for lunch, then have dinner at home and snack in between. I did more than my share of mindless eating and snacking. I would go out to lunch most days at work mostly because everyone else was doing it and not really eating healthy. Never really paid attention to what was going into my mouth. We don't have control over a lot of things in our lives but we do have control over what goes into our mouths!

A day before we started Advocare, we decided we would have somewhat of a "last meal". Mine was Skyline Chili. Yum!

Then we started...

Days 1-10 


First day of Advocare, we started the morning with the Spark (Grape and Fruit Punch are my favorites) and followed up with the Fiber Drink and necessary pills. The Fiber Drink was disgusting, tasted like pulpy cardboard! You need to drink this fast else it turns to a thick slush. The taste is somewhat improved if you mix it with the Mandarin Orange Spark, tastes more like orangey, pulpy cardboard.

The first few days I had very bad headaches, primarily in the evenings. For me, I think it was somewhat of sugar withdrawl but also I would skip eating snacks primarily because I was working and couldn't work it in. I ended up taking headache medicine the first few days to help calm them down. I was forced to work the snacks in at work. Over time, eating snacks did get somewhat better. I would at least eat one snack per day (you should eat 2) but not optimal.

We made meal changes like wrapping lettuce in turkey burgers instead of having hamburgers with cheese and a bun. It is a change but I didn't miss the bread or cheese nearly as much as I thought I would!

After the first couple days, the challenge became more routine. We really watched we ate and became more mindful of the food decisions we were making. I quit going out to eat cold turkey. It's amazing how much money you spend going out to eat and how much more money you have when you stop! I began taking salads and left overs from the previous nights supper for lunch.

The biggest health habit we took from the first 10 days was to fix stone cut oatmeal and eggs with salsa every morning. We would fix a crockpot recipe of oatmeal over the weekend and just heat it up every morning. This is a habit we've continued after the challenge. I put salsa on a ton of stuff nowadays!

After the first week, I lost 14 lbs!

The following week came around, headaches were gone and I learned that I was beginning to form healthier habits! We continue to follow the Advocare challenge and the second week was drastically reduced but still lost 2.6 lbs!

Days 11-24


We largely stayed on the same routine as days 1-10 with the exception of we swapped out eggs and oatmeal for breakfast for the meal replacement breakfast shakes. I had the Chocolate flavored. I was impressed by the flavor. I've had protein shakes before and am not a big fan of the taste, too chalky. This was like a chocolate milkshake. I'd just mix it with water but if you mix it with ice and put it in the blender, it's even better because you don't have clumps in it! These meal replacement shakes were definitely more convenient! 

The biggest challenge during this time was taking the larger MNS pills. They are about the size of the One-A-Day pills. I've always had trouble taking pills. This period of the challenge, you do take a lot of pills! I had trouble swallowing the larger ones. I ended up mixing the pills with natural, no sugar added applesauce. This coated the pills and eventually made it much easier to swallow but it was an adjustment for me!

In regards to diet, we continued eating lean meat, salads and developing healthier habits. One of my favorite meals now is taco salad! Don't think big taco shell, this taco salad has ground turkey with taco seasoning with lettuce, green pepper, and onion and top it off the salsa and maybe even some sunflower kernels! Yummy!

After day 21 I lost another 6 lbs and after 24 days, I lost a total of 24 lbs! This was strictly diet changes. I didn't change anything in regards to physical activity.

For inquiring minds, my wife ended up losing a total of 21.4 lbs!

After Advocare 


I'm writing this a few weeks after finishing the challenge. While not at the Advocare rate, I am continuing to lose weight. For the most part we have continued our healthy eating habits. We continue to fix eggs and salsa in the morning and have healthy dinners. I've only gone out to lunch once at work and continue to take left overs for lunch. Some of my co-workers have even started packing!

For a week or so after the challenge I felt hungry A LOT and even still do to some degree! The Advocare appetite suppressants really work! My wife and I have continued to encourage each other. I could not have gone down this journey without her!

We hope and pray that we continue going down this healthier way of living both for our health as well as our growing babies health. I'm grateful for all my friends and family in being encouraging during this time and even sometimes adjusting meals to be more "Advocare friendly".

We have decided we are going do another 24 day challenge towards the end of the year. You're supposed to wait 3 months before doing it again. I'm excited to see what the results from those will be! My goal is to incorporate exercise this time around!

I would highly recommend anyone try Advocare both to lose weight but also to begin forming healthier habits. We went into this not thinking this is a diet but a new lifestyle. Advocare is a springboard to changing your life!