Thursday, January 5, 2017

Visual Studio & C# Test Automation


****************************************************************************

DeploymentItem for Unit Testing
Create a visual studio unit testing project
Create a folder Resources and add a text file under it
Change ScriptFile.txt properties to Content and copy always

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
namespace DeploymentAttributeExample
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        [DeploymentItem("ResourceFiles","ResourceFiles")]
        public void TestMethod1()
        {
            string path = Path.Combine(Directory.GetCurrentDirectory(),"ResourceFiles\\ScriptFile.txt");
            string values = File.ReadAllText(path);
        }
    }
}

now the resource folder copied to out - output directory.


End
***************************************************************

Testing Tools:

The Accessible Explorer and UI Spy tools are obsolete and no longer available. Developers should use Inspect or AccScope instead.

https://msdn.microsoft.com/en-us/library/dd373661(v=vs.110).aspx



Data Driven Visual Studio Unit Testing vs Nunit

In NUnit, we can pass the data as test method attribute, but in Unit testing we should rely on DataSource from CSV, Excel, Database or XML



https://github.com/nunit/docs/wiki/Usage
https://stackoverflow.com/questions/14138907/set-up-test-method-with-different-inputs

*******************************************************************


Executing Automated tests in Build vNext using Test Plan, Test Suites:

https://www.visualstudio.com/en-us/docs/test/continuous-testing/index

--------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------
Using Net Use

net use * \\servername\sharefolder /user:username password
copy %1 c:\testfolder

--------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------

Triggering Build Definition using Windows Task Manger 

For Visual Studio 2012

"C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\TFSBuild.exe" start http://tfs.org:8080/tfs Caligo "Build-Definition-Name"

--------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------

Keyword Driven Testing:

        //[TestMethod]
        [DeploymentItem(@"KeywordDriven.xlsx")]
        [DataSource("System.Data.Odbc", "Dsn=Excel Files;Driver={Microsoft Excel Driver (*.xls)};dbq=|DataDirectory|\\KeywordDriven.xlsx;defaultdir=.;driverid=790;maxbuffersize=2048; pagetimeout=5;readonly=true", "Driver$", DataAccessMethod.Sequential)]
        public void GenericKeywordDrivenTestMethod()
        {
            var scenario2Run = TestContext.DataRow["Scenario 2 Run"].ToString();
            var URL = TestContext.DataRow["URL"].ToString();
         
            BrowserWindow bw = BrowserWindow.Launch(new Uri(URL));
            bw.Maximized = true;

            var cmd = TestContext.DataConnection.CreateCommand();
                        cmd.CommandText = "select * from ["+ scenario2Run +"$]"; //+ sheetName+"$"
            var reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                var control = reader.GetValue(0).ToString();
                var action = reader.GetValue(1).ToString();
                var value= reader.GetValue(2).ToString();

                Console.WriteLine("Control: {0}, action: {1}, Value {2}", control, action, value);
                ProcessScenarioStep(bw, control, action, value);
            }
        }

        private void ProcessScenarioStep(BrowserWindow browserWindow, string control, string action, string value)
        {
            var actionableControl = GetControlForAction(browserWindow, action, control);
            ExecuteAction(actionableControl, action, value);
        }

        private void ExecuteAction(HtmlControl actionableControl, string action, string value)
        {
            if (action == "Edit")
            {              
                HtmlEdit ctrl = actionableControl as HtmlEdit;
                ctrl.Text = value;
            }

            if (action == "Click")
            {
                // ensure we click in the middle of the control
                var top = actionableControl.Top;
                var left = actionableControl.Left;
                var width = actionableControl.Width;
                var clickPoint = actionableControl.GetClickablePoint();
                Console.Write("clickpoint : {0}, top:{1}, left:{2}, width:{3}", clickPoint, top, left, width);
                //actionableControl.DrawHighlight();
                Mouse.Click(actionableControl);
            }

        }

        private HtmlControl GetControlForAction(BrowserWindow browserWindow, string action, string controlName)
        {
            if (action == "Edit")
            {
                HtmlEdit element = new HtmlEdit(browserWindow);
                element.SearchProperties.Add(HtmlEdit.PropertyNames.Name, controlName);
                return element;
            }

            if(action == "Click")
            {
                HtmlControl element = new HtmlControl(browserWindow);
                element.SearchProperties.Add(HtmlInputButton.PropertyNames.FriendlyName, controlName, PropertyExpressionOperator.Contains);
                // now look if we found it, but ensure we have the
                // inner most control that we can find with this search
                if (element.TryFind())
                {
                    var child = new HtmlControl(element);
                    child.SearchProperties.Add(HtmlInputButton.PropertyNames.FriendlyName, controlName, PropertyExpressionOperator.Contains);
                    if (child.TryFind())
                    {
                        return child;
                    }
                    else
                        return element;
                }
                Console.WriteLine("Unable to locate control for action:{0} and controlname:{1}", action, controlName);
                return element;
            }
         
            return null;
        }
--------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------


Running MSTest using Windows Command Prompt:

Open Windows command Prompt:

cd C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE

  > MSTest.exe
  > mstest /testcontainer:"C:\Deploy\CaligoAutoWin7Main.dll" /test:InstallCaligo

--------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------

  5. Disable user control access

  http://www.howtogeek.com/howto/4820/how-to-really-completely-disable-uac-on-windows-7/
  https://www.raymond.cc/blog/task-scheduler-bypass-uac-prompt/

No comments:

Post a Comment

JMeter Simple Controller

  Simple Controller is just a  container  for user request.