Wednesday, November 26, 2014

Running selenium scripts on android mobile web browser

Pre requisites:
On your mobile device navigate to Settings -> Applications, and check "Unknown Sources".
Android SDK should install and add to environment variables.
Server Computer and the mobile device should be in the same Wi-Fi network
This will help to run selenium scripts inside an android mobile web browser

Step-by-step guide

Set-up Android device
  1. Download the latest android webdriver apk file from HERE (im playing with C#).
  2. Install downloaded apk file on the android device.(adb install <file name.apk>).
  3. From the android device locate the the Webdriver application with the selenium logo.
  4. Tap on the icon to start the selenium server.
  5. From the host computer/ server open a command prompt and run type adb forward tcp:8080 tcp:8080 .
  6. Tap on enter key.
If you get any error on above command try to kill all the processors / task listening and started using port 8080.Re run the command.
We use above command to forward the ports and create a link between android device and the host computer/ server 
     7.  To check whether the server woks fine , navigate to web browser and go to below URL.(Using host computer/ server )
If the server works fine (newly created link between android device and the host computer are working file) ,User should see something like below image on the web browser.
     8.  Click on "Create Session" button and select android as on the below image.

     9.  Click on OK button.
   10.  Session should create successfully as on the bellow image.


Set-up Host computer / Server
  1. Download the latest selenium .net drivers from  HERE.
  2. Open Visual studio and create a new CodedUI project.
  3. Add all the dll files which are inside the downloaded selenium .net driver archive to the reference folder of newly created CodedUI project as on the below image.


  4. You may need add OpenQA.Selenium.Android namespace to test class file.
  5. On the test class create new AndroidDriver instance to initialise the webDriver and start writing test scripts according to the standard way.
  eg:
[TestMethod]
        public void CodedUITestMethod1()
        {
            IWebDriver driver = new AndroidDriver();
            driver.Navigate().GoToUrl("https:www.google.com");
            Assert.AreEqual(driver.Title.ToString(),"Google");
        }

Thursday, November 6, 2014

Getting started with Appium, The hybrid application automation

You have to have Java,android installed in your PC before continuing below steps.
And an android device or emulator should be connect to your PC.
try adb device on a command prompt and if any thing is there you are good to go.


Download the latest appium appication from there web site.
Unzip and run the exe.



As you can there are nothing on this screen.
I dint check any of the text boxes.
Below is my general settings.
I just the keeping the default settings as it is.


Click on the play button to start the server.

As on the image you will only ;

> Checking if an update is available
> Update not available
> Starting Node Server

For now stop the server. :) 

I will be starting the test as a CodedUI test and passing all the parameters using c# code.
Open a visual studio and create a new CodedUI project.
Add references as on the below image.



below is my setup method ofr appium.
From the code user have to pass parameters to appium interface.

         

Important :

Switching to webview_0. (Use  consol.writelin(driver.WindowHandles) to identify what the the windows currently application is having).
If hybrid like phonegap you have to switch to webview.

To get the app package name : aapt dump badging droid.apk command to get the package name.
(Nacigate to sdk\build-tools\XXXXX copy your apk file and run the above command on  command line)

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
using Microsoft.VisualStudio.TestTools.UITesting;
using System.Windows.Forms;
using System.Threading;
using NUnit.Framework;

namespace AppiumTest
{

    class DriverSetup
    {
        private static  RemoteWebDriver driver;

        
        public void AppiumSetupMethod()
        {


            string appPAth = @"**************.apk";
            DesiredCapabilities capabilities = new DesiredCapabilities();

            capabilities.SetCapability(CapabilityType.BrowserName, "");
            capabilities.SetCapability(CapabilityType.Version, "4.4.2");
            capabilities.SetCapability(CapabilityType.IsJavaScriptEnabled, "true");
            // capabilities.SetCapability("automationName", "selendroid");
            capabilities.SetCapability("deviceName", "Xperia Z");
            capabilities.SetCapability("platformName", "Android");
            capabilities.SetCapability("autoWebview", "true");
            capabilities.SetCapability("sessionOverride", "true");

            capabilities.SetCapability("app", appPAth);

            capabilities.SetCapability("appPackage", "xx.xx.xx");
            //  capabilities.SetCapability("appActivity", "com.dtz.sg.DTZMobility");

            try
            {
                driver = new RemoteWebDriver(new Uri("http://127.0.0.1:4723/wd/hub"), capabilities);
            }
            catch (Exception e )
            {

                MessageBox.Show(e.Message);
            }
            
            Thread.Sleep(4000);
            
            driver.SwitchTo().Window("WEBVIEW_0");

        }
        public static RemoteWebDriver _driver
            {
                get { return driver; }
                set { driver = value; }

            }

    }
   
    
}

Start the appium server by clicking the play button.

Now run your codedUI project.

Check what is happening on your appium interface.

Our connected android device should install the application which you mention in the code. :D

Trouble shooooting.

 Failed to start an Appium session, err was: Error: Command failed: error: cannot bind to socket
OR
forward tcp:8080 tcp:8080

> info: [debug] Cleaning up appium session
OR
> error: Couldn't start Appium REST http interface listener. Requested port is already in use. Please make sure there's no other instance of Appium running already.

That means you have to kill everything which are runing on port 8080.


netstat -a -n -o | find "<port ID>"
taskkill /PID 2052 /F  - 2052 means the process ID

Now server is ready start writing test scripts like you testing a web page... Wish you all good luck




Monday, June 30, 2014

USB debugging over wifi

Move to wireless world :D

No need to connect the android phone via a USB cable to android development tools or any other applications to debug the code.

Just follow the below steps.

Connect the phone to WiFi network.(you PC need to be with the same network [wire or wireless])
Navigate to WiFi settings an get the mobile phone ip address as on the below image.

Connect the device to PC using a USB cable (just for the 1st time :) )
Open a command prompt and type "adb tcpip 5555"
Disconnect from the the USB cable.



It will take little bit of time.
The type adb connect <DEVICE_IP_ADDRESS>:5555

Replace the <DEVICE_IP_ADDRESS> with the IP address of your mobile phone.

Then you should get a message saying "connected to 192.168.133.19:5555"

Yooooou are done.........

Type "adb devices" to list your device.