Automated Tester

.science

This content shows Simple View

Visual Studio

Appium Mobile Emulation

Getting started with Appium requires a few installs and some configuration, so let’s get to it.

This article assumes you already have Java installed and set to the PATH environment variables.

What you need:

Android Studio

Once installed you need to look under Tools > Android > SDK. There are several components we need. Namely SDK Tools, SDK Build Tools and SDK Platform Tools.

Android SDK Install

Once setup you will need to add ANDROID_HOME to your PATH environment variable and point it to your main SDK folder. Note: A reboot may be required for Windows to pick up the change.

Android HOME

Visual Studio Emulator for Android

Now let’s look at Visual Studio Emulator for Android, once installed it should hopefully list a bunch of android API device profiles you can either run (if HyperV is enabled). Start one up and you should be able to interact with the virtual device. Note: If you look in Hyper-V Manager you can see the running emulator and tweak memory usage etc.

Device Profiles

HyperV Manager

If we navigate to our sdk tools we can also see the device running by running 

adb devices -L

 and this will list the device name for our Appium setup. In this instance the device is donatello.

Tip: If you happen to want to install an App you can do so with the command: adb install <path\to\yourapk.apk>

Appium Desktop

Next download Appium and click the Android icon to configure the server, input the relevant fields and device name then click the start button to run the server.

Appium Config

Installing an SSL Certificate on the device if required

As we are interacting with a website rather than an App for the purposes of this article, it can be useful to store SSL Certs on the devices when connecting. Using Visual Studio Emulator for Android we can setup and SD Card.

Emulator Options > SD Card

Pull from SD card first to a temp folder in order to create the directory structure first, then place the SSL Certificate in an easy to remember place and push to the SD card. Under Settings > Privacy in the Android OS we can then add a trusted certificate to the device.

When doing this, Android will force you to create a passcode for the device, do so and keep it easy to remember.

How do I find Android UI Elements for my tests?

Back over in Android SDK land…. in Sdk/tools/bin there is a batch file called uiautomatorviewer.bat. Open this in a text editor and find the line:

call "%java_exe%" "-Djava.ext.dirs=%javaextdirs%" "-Dcom.android.uiautomator.bindir=%prog_dir%" -jar %jarpath% %*

Then replace it with:

call "%java_exe%" "-Djava.ext.dirs=%javaextdirs%" "-Dcom.android.uiautomator.bindir=C:\DEV\androidSDK\tools" -jar %jarpath% %*

Making sure to set the binding directory to where your SDK tools are installed.

Then run uiautomatorviewer.bat whilst your emulator is running. Clicking the Android screenshot icon (second one from the left) will paint a view of what is currently on the emulator screen, you can then hover over buttons etc in order to find locators and anything else you might need.

uiautomatorviewer

Please note, your emulator API must be level 17 and upwards.

Time to Code

We can now write automation to run via this emulator with the usual webdriver goodies. Install the Appium Webdriver NuGet package and create the driver with the desired capabilities.

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.SetCapability("device", "Android");
capabilities.SetCapability(CapabilityType.Platform, "Windows");
capabilities.SetCapability("deviceName", "donatello");
capabilities.SetCapability("platformName", "Android");
capabilities.SetCapability("platformVersion", "6.0.0");
capabilities.SetCapability(MobileCapabilityType.BrowserName, "BROWSER");
capabilities.SetCapability(CapabilityType.AcceptSslCertificates, true);

driver = new AndroidDriver<AndroidElement>(new Uri("http://127.0.0.1:4723/wd/hub"), capabilities, TimeSpan.FromSeconds(180));

Note that browserName “BROWSER” will start the devices’ native Android browser.

We can now start the automation of the site, starting with some very crude Selenium here… :

driver.Navigate().GoToUrl("https://mySecureUrl.com");

Thread.Sleep(1000);
var element = driver.FindElement(By.Id("Email"));

element.Click();
element.Clear();
element.SendKeys("username@mySecureUrl.com");

var element2 = driver.FindElement(By.Id("Password"));
element2.Click();
element2.Clear();
element2.SendKeys("mySecurePassword");
element2.SendKeys(Keys.Enter);

Hopefully this is a an easy enough to follow guide to get you up and running using Appium in your c# dev environment, these are the steps I took to being playing with Appium in more depth. Happy testing!



Recording Videos of your Selenium Tests

It is good practice to take a screen grab of test failures and review them later, particularly if you have some bizarre wait or timeout error that can be difficult to pin down but sometimes even these can lead you down the wrong path or not give enough context to what went wrong. This led me to explore being able to capture videos of running tests that can be scrutinised at a later time, much like you get when you run tests in SauceLabs or BrowserStack.

I created a helper class called RecordVideo and pulled down a NuGet package called Microsoft Expression Encoder. You will also need to install Microsoft Expression Encoder 4.

Having set up a start (and stop) recording method we can easily capture tests in and write them out to file with the help of our set up or specflow hooks.

Here’s how:

using System;
using Microsoft.Expression.Encoder.ScreenCapture;

public class RecordVideo
{
private static readonly ScreenCaptureJob vidRec = new ScreenCaptureJob();

public static void StartRecordingVideo(string scenarioTitle)
{
string timestamp = DateTime.Now.ToString("dd-MM-yyyy-hh-mm-ss");
vidRec.OutputScreenCaptureFileName = @"C:\Test Videos" + scenarioTitle + " " + timestamp + ".wmv";
vidRec.Start();
}

public static void EndRecording()
{
vidRec.Stop();
vidRec.Dispose();
}
}

We can now call these in our setup/ teardown (in my case I’m using Scenario Hooks) and use the test case name for the file title along with a timestamp.

[BeforeScenario]
public void BeforeScenario()
{
RecordVideo.StartRecordingVideo(ScenarioContext.Current.ScenarioInfo.Title);
objectContainer.RegisterInstanceAs(new PageContext(TestRunContext.Driver));
}
[AfterScenario]
public void AfterScenario()
{
if (ScenarioContext.Current.TestError != null)
{
TakeScreenShot.SaveScreenShot(TestRunContext.Driver, TestFailurePath + ScenarioContext.Current.ScenarioInfo.Title + ".jpeg");
}

RecordVideo.EndRecording();
}

Be sure to put something in place to clear the directory out of videos on each run or you might find yourself running out of disk space quite quickly.



API Testing with Specflow

I’m a big fan of Specflow/ Cucumber, it’s great for creating tests to run against a front end with webdriver but what if we want to keep that same plain English BDD approach and apply it against an API for automated API Testing? It’s certainly possible and this article will cover the basics to get you up and running.

I am going to assume you have a framework set up and in place, you can even use a webdriver based solution as described previously, although you may wish to run it with a headless driver.

I’m also going to add a SOAP endpoint to my solution and write a test in Gherkin, this endpoint is the publicly available Global Weather SOAP endpoint available here:

http://www.webservicex.com/globalweather.asmx?wsdl

This WSDL generates 2 service operations, GetCitiesByCountry (to get a list of all the cities for any country) and GetWeather (to get weather for any city/country combination).

Let’s add the WSDL somewhere we can use it in Visual Studio. If you right click your project and go to Add > New > Service Reference, you will be presented with a dialogue in which you can paste in the WSDL, click Go and give it an appropriate name.

Add Service Reference for API Testing

Click OK, you should see a new Service References folder in your solution. Lets begin formulating a test for our API Testing suite.

@API
Scenario Outline: Get Cities
Given I query the API
Then I can retrieve <City> from <Country>

Examples:
| City        | Country        |
| Reims       | France         |
| London      | United Kingdom |
| Agra        | India          |
| Porto Santo | Portugal       |
| Cancun      | Mexico         |
| Pretoria    | South Africa   |
| Melbourne   | Australia      |

We’re going to need a helper class which I will call ‘GeoHelper’ where we can reference the endpoint and then make some methods we can call from the step definition. Below I am passing a string value to the ‘GetCitiesByCountry’ extension on the endpoint and returning the result. Then I have created another helper method which takes that result and asserts the city is present in the response.

public class GeoHelper
{
     private readonly GlobalWeatherSoap globalWeather = new GlobalWeatherSoapClient();

     public string GetCitiesByCountry(string country)
     {
          var x = globalWeather.GetCitiesByCountry(country);
          return x;
     }

     public void CheckCityNameIsPresent(string countryName, string cityName)
     {
          Thread.Sleep(200);
          Assert.IsTrue(GetCitiesByCountry(countryName).Contains(cityName));
     }
}

So our step definition calls this like so:

private readonly GeoHelper geo = new GeoHelper();

[Then(@"I can retrieve (.*) from (.*)")]
public void ThenICanRetrieveFrom(string cityName, string countryName)
{
     geo.CheckCityNameIsPresent(countryName, cityName);
}

And that in brief is how we can leverage the use of Specflow for API Testing.



Gallio Test Runner

Gallio (sometimes known as MB Unit or Gallio Icarus) is a test runner which to me has two distinct advantages over MsTest and NUnit.

Parallel test execution support and built in test reports, although NUnit does have parallel support with PNUnit.

How to set it up

  • Download and install Gallio from here.
  • In your Visual Studio Project navigate to Project > [project name] Properties from the drop down menu.
  • Click Debug
  • Set startup program to Gallio, by default it should be somewhere like: C:\Program Files\Gallio\bin\Gallio.Icarus.exe
  • In Start Options > Command Line Arguments paste in the path of your projects DLL housing the tests you want to execute
  • Save
  • Hit Start when you want to run or debug your tests

Gallio_Setup

Gallio GUI

This looks very similar to NUnit but with some extra buttons and menus, the only ones you will really care about are start/ stop/ debug and test reports.

Gallio_GUI

The reports aren’t anywhere near as nice as that you would find in Allure, but they are easily generated and easy to view.

Gallio_Report

Bootnote: In Gallio Icarus select menu Tools -> Options, select page “Preferences”, set “Test Runner Factory” to IsolatedAppDomain or Local to get the debugger to work.



Browser Profiles with Selenium

I decided to write this article after someone asked about browser profiles with Selenium in the IRC channel.

With Selenium we can specify a particular browser profile when instantiating our driver so if you happen to have specific browser setup requirements such as Firefox with certain plugins installed we can tell the driver to use this.

Setup

To begin with we will want to setup up our profile to be used. In this example we will use Firefox and use the Firefox profile manager.

To access this run “firefox -p” from Run.

profiles1

Which will load up the profile manager, here we will create a new profile and call it ‘QAToolsProfile’ – for this example we will choose the save folder as ‘Browser Profile’ on the desktop.

profiles2

profiles3

Next, start up firefox and add in the tools required for your new profile, I am going to add a tool called REST Client.

profiles4

profiles5

Right then! We now have a customised browser profile to call in our code.

Calling the Profile

As you might expect this is relatively easy as we just need to pass in the location of the profile which we dumped on the desktop:

private static void SetupFirefoxDriver()
{
string profileDir;

profileDir = @"C:\Users\user.name\Desktop\BrowserProfile";

FirefoxProfile profile = new FirefoxProfile(profileDir);
Driver = new FirefoxDriver(profile);
}

If we now say to a test to navigate to “chrome://restclient/content/restclient.html” we can automate the browser plugin. In this instance we can perform some API checks with RESTClient’s user interface. If we did not specify the profile then Firefox would load up with no extra frills.

profiles6

You can find more information on Browser Profiles with Firefox here.




top