Wednesday, July 15, 2015

Run your Coded UI tests against different browsers using the Selenium components


Run your coded ui tests against the different browsers using the
selenium components.

If we have a dedicated agent machines with browser to run the 
coded ui test with out modifying the code.

For this i am using the system variables. In each machine i will 
create a system variable name:browser and value is like "IE" 
or "Chrome" or "Firefox" ect 

Ex: Agent-1 should run with IE browser and Agent-2 should run
 with Chrome browser...

Download the Selenium components from below link

After installing the cross-browser testing installer, verify that
Microsoft.VisualStudio.TestTools.UITest.Extension.CrossBrowserProxy.dll
 is found in the following location:

"%ProgramFiles%\Common Files\microsoft shared\VSTT\Cross 
Browser Selenium Components" (for 32 bit machines)

"%ProgramFiles(x86)%\Common Files\microsoft shared\VSTT\Cross
 Browser Selenium Components" (for 64 bit machines)

Download chrome driver from below link

Download selenium dot net bindings from:

Right click on the downloaded zip files.

Select "properties".

Under "General" tab, click on the "Unblock" button.

Now unzip both the files and copy the contents to the following path
(for the selenium-dotnet 2.43.1 binaries, copy the contents of net40                  folder):

"%ProgramFiles%\Common Files\microsoft shared\VSTT\Cross 
Browser Selenium Components" (for 32 bit machines)

"%ProgramFiles(x86)%\Common Files\microsoft shared\VSTT\Cross             Browser Selenium Components" (for 64 bit machines)

Supported versions
This installer works with VS 2012 Update 4, VS 2013 VS 2013 update 3
 and VS 2013 Update 4.

Supported Browser versions
The following versions of browsers have been verified and are 
supported: Firefox 33.
Latest version of Chrome (version 38.0.2125.111 m as of 11/14/2014)

Setting Environment Variables for Coded UI
Create environment variable on each agent machine and set its value
 as the desired browser name (IE8/IE9/Chrome/Firefox) against which
 you want to run your automated tests.
1.Go to “My Computer” Right click and select “Properties


2.Click on “Advance System Settings


3.Go to “Advance Tab” and the click on “Environment Variables” 


4.Under “system variables” section click on “New


5.Give the “Variable Name” and “Variable Value


6.Click on “ok” and again click on “ok” and again click on “ok”.

7.Create a Test Initialize method and read the value of the                               environment variable before test starts on each agent machine.
   This will return the name of the browser that needs to be used 
   for playing back automation.

String browser;
        [TestInitialize]
        public void Init()
        {
            browser                            = Environment.GetEnvironmentVariable("LaunchBrowser");
        }

8.Inside your test method, set “BrowserWindow.CurrentBrowser
   value as “browser” which holds the browser name stored in 
   environment variable

Sample code…
[TestMethod]
public void CodedUITestMethod1()
{
    BrowserWindow.CurrentBrowser = browser;
    this.UIMap.RecordedMethod1();
    this.UIMap.AssertMethod1();
}

Depending on the value stored in the environment variable, Coded UI launches the browser and runs the test against launched browser.


Tuesday, July 14, 2015

Creating a Data Driven Coded UI Testing with different data sources

After creating a coded UI test, you might want to run your coded UI test multiple times with different sets of data to test different scenarios. This lab will show you how to do this by adding parameter values from a data source to create a data-driven coded UI test. Each row in the data source is an iteration of the coded UI test. The overall result of the test will be based on the outcome for all the iterations All changes to your test to enable it to use a data source for the parameters will be made to your test's source code file (i.e. CodedUITest1.cs). You cannot modify the code in the UIMap.Designer.cs file.

//This is a recorded method for addition and validating the sum

[TestMethod] 
 public void CodedUITestMethod1() 
       {           
           this.UIMap.Addition();   
         this.UIMap.validation(); 
       }

We will only add data for the calculator test method that adds two numbers and verifies that they are added together correctly for this test to pass.The data source can be a csv, XML, Excel, Data Base and Test Case that contains the following data
 Num1   Num2  Sum    

   1          2        3    
   2          4        6

Different Data sources

 //Data source for Test Case "Test Case from Team Foundation Server"       [DataSource("Microsoft.VisualStudio.TestTools.DataSource.TestCase", "http://vsalm:8080/tfs/fabrikamfibercollection;Calculator", "248", DataAccessMethod.Sequential)]


 //Data source for XML 

[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", "|DataDirectory|\\CalcData.xml", "DataContextRow", DataAccessMethod.Sequential), DeploymentItem("CodedUITestProject2\\CalcData.xml")]
  
//Data source for CSV 
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\data.csv", "data#csv", DataAccessMethod.Sequential), DeploymentItem("CodedUITestProject2\\data.csv")]

//Data source for Excel

 [DataSource("System.Data.Odbc", "Dsn=Excel Files;dbq=|DataDirectory|\\data.xlsx;defaultdir=C:\\TestData;driverid=1046;maxbuffersize=2048;pagetimeout=5", "Sheet1$", DataAccessMethod.Sequential), DeploymentItem("CodedUITestProject2\\data.xlsx")]

//Data source for SQL

[DataSource("System.Data.SqlClient", "Data Source=.\\sqlexpress;Initial Catalog=Tfs_FabrikamFiberCollection;Integrated Security=True", "CodedUI_Test", DataAccessMethod.Sequential)]

Here i am using csv file, must be copied to the output directory, or the test can’t run. Use the Properties window to copy it.
Deploy the .CSV file
To find the search property open the UIMap.uitest file for solution explorer.
Open the Coded UI Test Editor
Click on UI Action "Click '1' button " then it will highlight in UI  Control Map  
"UIItem1Button"

Use the Coded UI Test Editor to assist with code
right click on control "UIItem1Button" choose properties there you will see the Search Properties  click on it Name properties which equals to 1.

For every run we will replace this value.

by seeing this UIMap.uitest we can wright the code to over right the values like

  this.UIMap.UICalculatorWindow.UIItemWindow.UIItem1Button.SearchProperties[WinButton.PropertyNames.Name] = TestContext.DataRow["Num1"].ToString();

Use the search properties to assist in coding
Add using Microsoft.VisualStudio.TestTools.UITesting.WinControls to the top of the CodedUITest.cs file

 [TestMethod]

[DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\data.csv", "data#csv", DataAccessMethod.Sequential), DeploymentItem("CodedUITestProject1\\data.csv")]

public void CodedUITestMethod1() 
       {
           this.UIMap.UICalculatorWindow.UIItemWindow.UIItem1Button.SearchProperties[WinButton.PropertyNames.Name] = TestContext.DataRow["Num1"].ToString(); 
           this.UIMap.UICalculatorWindow.UIItemWindow2.UIItem2Button.SearchProperties[WinButton.PropertyNames.Name] = TestContext.DataRow["Num2"].ToString();

  this.UIMap.Addition(); 

 this.UIMap.validationExpectedValues.UIItem12TextDisplayText = TestContext.DataRow["Sum"].ToString(); 


this.UIMap.validation();

        }


Monday, July 13, 2015

Coded UI Hand Code with out recording for Web Application



  •  Create a Coded UI Test Project in Visual Studio.
  •  Window will pop up for recording the Coded UI or generate for MTM close it.
  •  Now go to the test method and start writing the code. 

In my scenario i am writing a coded ui hand code for adding items to the cart and check out for "Tailspin Toys Application"


        [TestMethod]
        public void CodedUITestMethod1()
        {
            // Select the browser type Launch
            BrowserWindow.CurrentBrowser = "IE";

            //launch the browser and browse the application URL

            BrowserWindow Scenario1 = BrowserWindow.Launch("http://localhost:8001");

            //Maximise the window

            Scenario1.Maximized = true;

            //Define the action for Hyper Link control 

            UITestControl ModelAirplanes = new UITestControl(Scenario1);
            ModelAirplanes.TechnologyName = "Web";
            ModelAirplanes.SearchProperties[HtmlHyperlink.PropertyNames.InnerText] = "Model Airplanes";
            ModelAirplanes.SearchProperties[HtmlHyperlink.PropertyNames.Href] = "http://localhost:8001/?slug=model";
            ModelAirplanes.DrawHighlight();
            Mouse.Click(ModelAirplanes);
           
          //Define the action for Hyper Link control 
            UITestControl Viewplane = new UITestControl(Scenario1);
            Viewplane.TechnologyName = "Web";
            Viewplane.SearchProperties[HtmlHyperlink.PropertyNames.InnerText] = "View Plane";
            Viewplane.SearchProperties[HtmlHyperlink.PropertyNames.Href] = "http://localhost:8001/home/show?sku=modtrr";
            Viewplane.DrawHighlight();
            Mouse.Click(Viewplane);

            //Define the action for Button control 
            UITestControl addtocart = new UITestControl(Scenario1);
            addtocart.TechnologyName = "web";
            addtocart.SearchProperties[HtmlButton.PropertyNames.Class] = "add-cart";
            Mouse.Click(addtocart);

            //Define the action for Button control 
            UITestControl checkout = new UITestControl(Scenario1);
            checkout.TechnologyName = "web";
            checkout.SearchProperties[HtmlInputButton.PropertyNames.Class] = "checkout";
            checkout.SearchProperties[HtmlButton.PropertyNames.Type] = "button";
            Mouse.Click(checkout);
            
            //Define the action for Enter the text control 
            UITestControl first = new UITestControl(Scenario1);
            first.TechnologyName = "web";
            first.SearchProperties[HtmlEdit.PropertyNames.Id] = "FirstName";
            first.SetProperty("Text","Manu");

            //Define the action for Enter the text control
            UITestControl last = new UITestControl(Scenario1);
            last.TechnologyName = "web";
            last.SearchProperties[HtmlEdit.PropertyNames.Id] = "LastName";
            last.SetProperty("Text", "Anu");

            //Define the action for Enter the text control
            UITestControl Email = new UITestControl(Scenario1);
            Email.TechnologyName = "web";
            Email.SearchProperties[HtmlEdit.PropertyNames.Id] = "Email";
            Email.SetProperty("Text", "manuanu@nuvu.com");
            //Email.DrawHighlight();
            //Playback.Wait(1000);

            //Define the action for Enter the text control

            UITestControl Street1 = new UITestControl(Scenario1);
            Street1.TechnologyName = "web";
            Street1.SearchProperties[HtmlEdit.PropertyNames.Id] = "Street1";
            Street1.SetProperty("Text", "Amalapuram");

            //Define the action for Enter the text control
            UITestControl City = new UITestControl(Scenario1);
            City.TechnologyName = "web";
            City.SearchProperties[HtmlEdit.PropertyNames.Id] = "City";
            City.SetProperty("Text", "Chilkalurupeta");

            UITestControl country = new UITestControl(Scenario1);
            country.TechnologyName = "web";
            country.SearchProperties[HtmlComboBox.PropertyNames.Id] = "countrySelect";
            Mouse.Click(country);
            country.SetProperty("SelectedItem", "USA");

            //Define the action for Enter the text control
            UITestControl stateSelect = new UITestControl(Scenario1);
            stateSelect.TechnologyName = "web";
            stateSelect.SearchProperties[HtmlComboBox.PropertyNames.Id] = "stateSelect";
            Mouse.Click(stateSelect);
            stateSelect.SetProperty("SelectedItem", "Texas");

            //Define the action for Enter the text control
            UITestControl Zip = new UITestControl(Scenario1);
            Zip.TechnologyName = "web";
            Zip.SearchProperties[HtmlEdit.PropertyNames.Id] = "Zip";
            Zip.SetProperty("Text", "12345");

            //Define the action for Button control
            UITestControl revorder = new UITestControl(Scenario1);
            revorder.TechnologyName = "web";
            revorder.SearchProperties[HtmlButton.PropertyNames.Class] = "textbutton";
            revorder.FilterProperties[HtmlButton.PropertyNames.Type] = "submit";
            Mouse.Click(revorder);

            //Define the action for Drop Down control
            UITestControl CardType = new UITestControl(Scenario1);
            CardType.TechnologyName = "web";
            CardType.SearchProperties[HtmlComboBox.PropertyNames.Name] = "CardType";
            Mouse.Click(CardType);
            CardType.SetProperty("SelectedItem", "MasterCard");

            //Define the action for Enter the text control

            UITestControl cardno = new UITestControl(Scenario1);
            cardno.TechnologyName = "web";
            cardno.SearchProperties[HtmlEdit.PropertyNames.Id] = "AccountNumber";
            cardno.SetProperty("Text", "0400 8110 8000 1095");

            //Define the action for Enter the text control

            UITestControl VerificationCode = new UITestControl(Scenario1);
            VerificationCode.TechnologyName = "web";
            VerificationCode.SearchProperties[HtmlEdit.PropertyNames.Id] = "VerificationCode";
            VerificationCode.SetProperty("Text", "040");

            //Define the action for Drop Down control

            UITestControl ExpirationMonth = new UITestControl(Scenario1);
            ExpirationMonth.TechnologyName = "web";
            ExpirationMonth.SearchProperties[HtmlComboBox.PropertyNames.Name] = "ExpirationMonth";
            Mouse.Click(ExpirationMonth);
            ExpirationMonth.SetProperty("SelectedItem", "3");

            //Define the action for Drop Down control

            UITestControl ExpirationYear = new UITestControl(Scenario1);
            ExpirationYear.TechnologyName = "web";
            ExpirationYear.SearchProperties[HtmlComboBox.PropertyNames.Name] = "ExpirationYear";
            Mouse.Click(ExpirationYear);
            ExpirationYear.SetProperty("SelectedItem", "2016");

            //Define the action for Button control

            UITestControl PlaceOrder = new UITestControl(Scenario1);
            PlaceOrder.TechnologyName = "web";
            PlaceOrder.SearchProperties[HtmlButton.PropertyNames.Class] = "textbutton";
            PlaceOrder.FilterProperties[HtmlButton.PropertyNames.Type] = "submit";
            Mouse.Click(PlaceOrder);
            Playback.Wait(1000);

        }

TFS Java Projects with MS Build and Release Management (TFS End To End ALM for Java Project)




Setup Java environment for TFS Build
Download the Dump software’s which we can un zip and make use of it no need of installation.

1.       Download Apache Ant from below link
2.       Download Java JDK from below link
3.       Download Apache tomcat from below link
4.       Download TFS build Extension from below link
5.       Download eclipse from below link
http://eclipse.org/downloads/
6.   Download Horizons java project from below link
http://templated.co/horizons/

Steps for Setup
1.       Un zip the apache ant folder and copy to C drive


2.       Un zip the apache tomcat folder and copy to C drive or if it is exe file install it



3.       Un zip the java folder and copy to C drive or if it is exe install it



4.      Go to “My Computer” Right click and select “Properties
5.      Click on “Advance System Settings






6.      Go to “Advance Tab” and the click on “Environment Variables” 

7.      Under “system variables” section click on “New

8.      Set the Environment variables for Ant, Java, Apache Tomcat
For ant give the variables and click on ok

Again click on new For java give the variables and click on ok

Again click on new For Tomcat give the variables and click on ok

9.       Now go to “path variable” and click on edit

Append the variables at the end of the “path variable” like
Note: give the “; “ properly in variable values.
%JAVA_HOME%\bin;JAVA_HOME%\lib;%ANT_HOME%\bin;%ANT_HOME%\lib;%CATALINA_HOME%\bin;%CATALINA_HOME%\lib;




10.   For apache tomcat change the port number
Open server.xml file “C:\apache-tomcat-7.0.55\conf\server.xml”

Now I am changing the port 8080 to 6767 and save it

11.   Now enable the users
Open tomcat-users.xml “C:\apache-tomcat-7.0.55\conf\tomcat-users.xml”

Now remove the comment surrounded the roles and user tag(<!-- -->)
And add two lines highlighted and save it

12.   Now start the tomcat and check it working
Got to the path “C:\apache-tomcat-7.0.55\bin”
Right click on statup.bat and click on “Run as Administrator”

Then tomcat will start you will see the CMD screen like this


13.   Now go and launch browser with this URL http://localhost:6767
             



14.   Click on Manage App it will prompt the windows security window then give the credentials (the credentials which you given in this file “tomcat-users.xml” where you add two line in it).





After click on ok you will see the projects list screen which are in tomcat server.
 




15.   Now install the TFS build Extension on build server




Click on next





Select “I Agree” and click on next

Click on next




Click on next

Installation is completed click on close.
 




16.   Setup project in eclipse
Note:(If it is existing eclipse project goto file->import->select “existing project to work space” Give the path of the project and click on finish)
Open eclipse
     


It will prompt for work space give any path and click on ok
Now go to file->new->dynamic web project

Give the project name and click on next



Click on next

Check the option Generate web.xml and click on finish


You will see the project

Open the “Horizons project” which you downloaded from above link copy all the content in it

Past it in the eclipse project which you created horizons->webcontent (under web content folder past the files)


Generate the build.xml file using export option
Right click on  project click on export

Select ant and click on next

Select the project and click on finish

You will see the build.xml file in project

17.   Install Team explore every where
1.       Open Eclipse.
2.       On the Help menu, choose Install New Software.
The Install dialog box appears.

The install wizard looks slightly different for different versions of Eclipse.
3.       Choose Add.
The Add Repository dialog box appears.

4.       In Name, enter TFS Plugin for Eclipse.
5.       In Location, enter http://dl.microsoft.com/eclipse/tfs, and then choose OK.
6.       In the list of features in the Install dialog box, select the check box that corresponds to Team Explorer Everywhere, and then choose Next two times.
Note: uncheck Contact all update sites during install to find required software 

7.     Accept the Microsoft Software License Terms, and then choose Finish.

8.     Restart Eclipse when prompted click on restart.


18.   Open team explore everywhere.
       Window ->show view ->other


And select Team explorer click on ok

After team explorer window will open add the TFS server
     



19.   Share the project which is created in the eclipse

Select the team foundation server and click next

Select the team project to share the web project and click next

Click on finish



After finishing the sharing go to team explore -> pending changes
We can see the pending changes of the project shared click on check-in

It will ask for conformation to check in click on yes

We can see the changeset created



20.   Go to build and create the new build definition

Give the name of the build



In trigger select manual build 
In source settings give the TFS path for the project

In build defaults



In project File click on create(here we can see the warning)

Select the ant click on next

We generated build.xml in previous steps
Select the build.xml and click on next

Here in drop down select default which will take the version of the java and ant we configured
Note: (if we want to run this project on different versions in drop down select the source control refer the link "http://msdnsolutions.blogspot.in/2015/07/java-to-build-in-different-versions.html”)
Click on finish

After finishing warning will be gone it will create a .TFSproj file in version control you can check it.
Click on ok

21.   Now trigger the build

 Click on queue

You can see the build has been passed

22.   For release management use the template we provided
Add the ReleaseUpgradeTemplate.xaml to that team project and check in it.
(download build template form here ReleaseUpgradeTemplate.xml)

Goto team explore in visual studio -> builds
Select the horizonss build which we created
Right click and select edit build definition

Goto process click on new

Select the template which we checked-in

Now select the template which we add from drop down
In Release Build option select “True”
In configurations to Release select “Any CPU and Release” And save the build

Now goto release management client create a release path.

Create components

In deployment from drop down select xcopy Deployer and then click on save

Now go to release templates double click on template created now

On components right click and add the component we created

Drag and drop the server where the tomcat is there in my case VSALM server

Drag and drop the copy file or folders for back up the existing content in tomcat server
Note create a folder in tomcat install path -> webapps -> create a folders horizons and horizons backup (source=horizons, destination= horizons backup)

Drag and drop component we created
And give the path of Horizons which is created in above step
Now go to the Property and linked the build

Drag and drop roll back activity and then inside this activity drag and drop the copy file or folders (source= horizon backup, destination = horizons)



Now go to eclipse modify the index.html page and save the file and go to pending changes and check in.

Now trigger the build
Go and check in RM client the project has been released

Now go to browser URL http://localhost:6767/
And click on manage app and then you can see the projects list
Here we can see the projects horizons and horizons backup
Click on horizons

Here we can see the change which we modified in index.html file