Wednesday, January 14, 2015

Selenium in Android - Demo




We want to run our test scripts on a real Android device. The android driver allows us to execute our tests against an Android browser. This can be a simulator or a real device.
Before we can register our simulator we have to download the android SDK (Software Development Kit) from the following location:

Steps to Achieve.
  • Setup the device
  • Install the WebDriver APK 
  • Run the test.

Setup the device

Connect the android device with the computer using a USB cable.

Install the WebDriver APK

1) We need to retrieve the serial id with the following command:
1

adb devices
2) Download the Android server from Selenium Site and save it in the platform-tools directory. To install the application enter:
1
adb -e install -r android-server.apk
4) Now we need to setup the port forwarding in order to forward traffic from the host machine to the emulator. Enter the following in the terminal :
1
$./adb -s <serialId> forward tcp:8080 tcp:8080

Run the test

You will need to take a look in TestNG Framework

From this program.
  1. Open Android Browser
      driver = new AndroidDriver();

  1. Navigate to http://www.automationplace.blogspot.com
    driver.get("https://automationplace.blogspot.com");

  1. Close the browser.
       driver.quit();


Now we have our environment setup we can run our tests.Create new TestNG and paste the code below :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.selenium.test;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.android.AndroidDriver;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;

import com.selenium.pageObject.LoginPage;
import com.selenium.userAction.PostStatus;
import com.selenium.userAction.SignIn;

public class autoTestLoadingFacebook {
  
  /**
   * Create WebDriver as static variable
   */
  private static WebDriver driver;
  
  
  /**
 * Setup some variable to run your script test
 */
@BeforeTest
  public void beforeTest() {
    driver =  new AndroidDriver();
  }
  
  /**
 * your test script
 */
@Test
  public void f() {
  driver.get("https://automationplace.blogspot.com");
  //create more test script here
  }
  
  
  /**
 * after run your script test , use this code to close your browser
 */
@AfterTest
  public void afterTest() {
    driver.quit();
  }
}
5) Run your test.
Hope you like this post.



Monday, January 5, 2015

Extract all web links Text from Website using Coded UI (C#)





Hi guys in this post we are to see "How to Print all the Links Text from Webpage" using Coded UI

In this example :


  1. Opening browser and navigating to http://automationplace.blogspot.in
     BrowserWindow bw = BrowserWindow.Launch("www.automationplace.blogspot.com");

   2.  Assigning the Webcontrol to UITestControl

     UITestControl links = new UITestControl(bw);
   links.TechnologyName = "web";
   links.SearchProperties.Add("Controltype", "Hyperlink");

   3. Loop all the links.

     foreach(UITestControl alllinks in links.FindMatchingControls())
   {
     Console.WriteLine("All links " + alllinks.GetProperty("Innertext"));
   }



Source Code :


using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Windows.Input;
using System.Windows.Forms;
using System.Drawing;
using Microsoft.VisualStudio.TestTools.UITesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UITest.Extension;
using Keyboard = Microsoft.VisualStudio.TestTools.UITesting.Keyboard;


namespace CodedUIDemo2
{
    /// <summary>
    /// Summary description for CodedUITest1
    /// </summary>
    [CodedUITest]
    public class CodedUITest1
    {
        public CodedUITest1()
        {
        }

        [TestMethod]
        public void Printlinks()
        {
            BrowserWindow bw = BrowserWindow.Launch("www.automationplace.blogspot.com");
            UITestControl links = new UITestControl(bw);
            links.TechnologyName = "web";
            links.SearchProperties.Add("Controltype", "Hyperlink");

            foreach(UITestControl alllinks in links.FindMatchingControls())
            {
                Console.WriteLine("All links " + alllinks.GetProperty("Innertext"));
            }
        }

    }
}


Run the above code.

After that, Click on Output from TextExplorer


You will see  all the hyperlink present in webpage.











Hope you Like this post.











Sunday, January 4, 2015

Hand Coded in Coded UI test using C# With Gmail Login Demo





Hi guys this time we are going see a demo on Coded UI test using C#(Visual Studio) With Example

This tutorial aims to help us become comfortable with CodedUI and will demonstrate how to go about creating a basic CodedUI test based on a simple application 

For the purpose of this exercise we will create the test using the CodedUI Test Builder.

During this tutorial we will cover the following areas:

1 . Creating test projects

2 . Adding CodedUI tests to that project

3 . Executing CodedUI test with Gmail login test.

Step 1 - Prepaing the test environment

We need to ensure that we have the following  pre-requisites deployed onto our test machine.

* Visual studio 2013 ultimate.

Step 2 - Creating a new Test Project

2.1. To create a test project do the following:

* Launch Visual Studio 2013 ultimate

* Click File -> New, and select Project from the menu.



*  Click on Installed Templates, choose the language of your   choice (C#.net for this exercise), and click on Test.



*  Select CodedUI Test Project from the results menu.



Thereafter you need to complete Name of the project which will automatically populate the solution name.

Click the OK button and you will have a brand spanking new Test Project.

After that your project look like below


All we have to do is .We need to write test script on  [TestMethod]



In this demo am going to write test on Gmail Login page.

My test is :

  • I Need to open Browser and Navigate to www.gmail.com
        For this am using the following code :
        BrowserWindow browser = BrowserWindow.Launch("www.gmail.com");
    
    This will open the browser and navigate to gmail
  • After that i need to fill detail the Username text box
        For this am using UITestControl
     //UITestControl is the class which has the ablity to locate controls on UI

     //Creating object for Usernametextbox
            //sending username from keyboard
            UITestControl username = new UITestControl(browser);
         username.TechnologyName = "web";
         username.SearchProperties.Add("Controltype", "Edit");
         username.SearchProperties.Add("Id", "Email");
         Keyboard.SendKeys(username, "kk.prashanth65@gmail.com");
     
      I need to locate elements in the webpage to do this 

         Rightclick - > Click on Generate code for codedUI test -> select Use Coded UI Test Builder

      See the below images in Step by Step :

    Right Click on VS and Select Generate code for codedUI test




     select Use Coded UI Test Builder


You will get UI Map- Code UI Test Builder on the bottom


Click on the Circle and drag and drop to Webelement


  You will get all the Property which we will use i our script like

TechnologyName = "web";
SearchProperties.Add("Controltype", "Edit");
SearchProperties.Add("Id", "Email");




  • After that i need to Fill details in Password textbox
       //Creating object for Passwordtextbox

            //sending password from keyboard
            UITestControl password = new UITestControl(browser);
          password.TechnologyName = "web";
          password.SearchProperties.Add("Controltype", "Edit");
          password.SearchProperties.Add("Id", "Passwd");
          Keyboard.SendKeys(password, "test123");

  • Click on Sign in button
           UITestControl click_signin = new UITestControl(browser);

          click_signin.TechnologyName = "web";
          click_signin.SearchProperties.Add("Controltype", "Button");
          click_signin.SearchProperties.Add("Id", "signIn");

            //Click button
           Mouse.Click(click_signin);
      



Source Code :

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Windows.Input;
using System.Windows.Forms;
using System.Drawing;
using Microsoft.VisualStudio.TestTools.UITesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UITest.Extension;
using Keyboard = Microsoft.VisualStudio.TestTools.UITesting.Keyboard;


namespace CodedUIDemo
{
    /// <summary>
    /// Summary description for CodedUITest1
    /// </summary>
    [CodedUITest]
    public class CodedUITest1
    {
        public CodedUITest1()
        {
        }

        [TestMethod]
        public void GmailLoginTest()
        {
            //open browser
            BrowserWindow browser = BrowserWindow.Launch("www.gmail.com");

            //UITestControl is the class which has the ablity to locate controls on UI

            //Creating object for Usernametextbox
            //sending username from keyboard
            UITestControl username = new UITestControl(browser);
            username.TechnologyName = "web";
            username.SearchProperties.Add("Controltype", "Edit");
            username.SearchProperties.Add("Id", "Email");
            Keyboard.SendKeys(username, "kk.prashanth65@gmail.com");

            //Creating object for Passwordtextbox
            //sending password from keyboard
            UITestControl password = new UITestControl(browser);
            password.TechnologyName = "web";
            password.SearchProperties.Add("Controltype", "Edit");
            password.SearchProperties.Add("Id", "Passwd");
            Keyboard.SendKeys(password, "test123");

            //Clicking Sign-in button on gmail

            UITestControl click_signin = new UITestControl(browser);
            click_signin.TechnologyName = "web";
            click_signin.SearchProperties.Add("Controltype", "Button");
            click_signin.SearchProperties.Add("Id", "signIn");

            //Click button
            Mouse.Click(click_signin);

            //Make test to wait 10 sec

            Playback.Wait(10000);


        }

     

    }
}

Run the above code your test will pass.



You also check in TestExplorer for Passed Test and time taken to complete test.




Hope you Like this post.