Automated testing for web application with Visual Studio CodedIUTest without recordings

Author: Ferran Sánchez

If you want to do testing on a web application you can have an alternative with CodedIUTest tool of Visual Studio but without recordings. In this methodology you can use objects such as HtmlHyperlink, HtmlInputButton and HtmlEdit to access to their characteristics directly (see https://automatedtestingtools.wordpress.com/2016/04/19/strategy-to-get-automated-testing-without-recording-actions).

It is known that the recording tool of Visual Studio provides internet explorer as default browser. The key point to work with a different browser is to initialize a static variable BrowserWindow to select the browser and launch web page. For example, if you want to work with Chrome you must write: BrowserWindow.CurrentBrowser = “chrome”;

After that, you can begin the automated testing web. The following example shows this possibility providing data source like a .csv file for data input parameters.

Also, to work with a different browser you must take into account the next preconditions:

1 – Configuration of “Selenium Components for Coded IU Cross”: https://visualstudiogallery.msdn.microsoft.com/11cfc881-f8c9-4f96-b303-a2780156628d/

2 – Troubleshoot: If you have some error when you run tests on Chrome browser, you can download another version on http://selenium-release.storage.googleapis.com/index.html

[CodedUITest]
public class CodedUITest1
{
	// Static var for launch the browser window
	BrowserWindow browser;
	public CodedUITest1()
	{
	}
	// Initialize browser and web page
	[TestInitialize()]
	public void MyTestInitialize()
	{
		BrowserWindow.CurrentBrowser = "chrome";
		browser = BrowserWindow.Launch(new Uri("http://www.amazon.es"));
		browser.Maximized = true;
		browser.CloseOnPlaybackCleanup = false;
	}
	[DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\dataAmazon.csv", "dataAmazon#csv", DataAccessMethod.Sequential), DeploymentItem("dataAmazon.csv"), TestMethod]
	public void CodedUITesAmazonHandcoding()
	{
		// Assert Vender tab string
		HtmlHyperlink VenderTabString = new HtmlHyperlink(browser);
		VenderTabString.SearchProperties.Add(HtmlHyperlink.PropertyNames.InnerText, "Vender");
		Assert.AreEqual(
"Vender", VenderTabString.InnerText.Trim());

		HtmlHyperlink VenderTab = new HtmlHyperlink(browser);
		VenderTab.SearchProperties.Add(HtmlHyperlink.PropertyNames.InnerText, "Vender");
		Mouse.Click(VenderTab);
		Playback.Wait(1000);
		// Assert "Vender tus productos" string
		HtmlSpan labelVender = new HtmlSpan(browser);
		labelVender.SearchProperties[HtmlSpan.PropertyNames.InnerText] = "Vende tus productos en Amazon";
		Assert.AreEqual("Vende tus productos en Amazon", labelVender.InnerText.Trim());
		// Assert Ofertas tab string
		HtmlHyperlink OfertasTabString = new HtmlHyperlink(browser);
		OfertasTabString.SearchProperties.Add(HtmlHyperlink.PropertyNames.InnerText, "Ofertas");
		Assert.AreEqual("Ofertas", OfertasTabString.InnerText.Trim());
		Playback.Wait(1000);

		// Assert Ayuda tab string
		HtmlHyperlink AyudaTabString = new HtmlHyperlink(browser);
		AyudaTabString.SearchProperties.Add(HtmlHyperlink.PropertyNames.InnerText, "Ayuda");
		Assert.AreEqual("Ayuda", AyudaTabString.InnerText.Trim());
		HtmlHyperlink AyudaTab = new HtmlHyperlink(browser);
		AyudaTab.SearchProperties.Add(HtmlHyperlink.PropertyNames.InnerText, "Ayuda");
		Mouse.Click(AyudaTab);
		Playback.Wait(1000);
		// Verify "contraseña" wrong
		HtmlHyperlink IdLink = new HtmlHyperlink(browser);
		IdLink.SearchProperties.Add(HtmlHyperlink.PropertyNames.Id, "nav-link-yourAccount");
		Mouse.Click(IdLink);
		//Assert "Contraseña" string
		HtmlLabel labelCont = new HtmlLabel(browser);
		labelCont.SearchProperties[HtmlLabel.PropertyNames.InnerText] = "Contraseña";
		Assert.AreEqual("Contraseña", labelCont.InnerText.Trim());

		HtmlEdit MailInput = new HtmlEdit(browser);
		MailInput.SearchProperties.Add(HtmlEdit.PropertyNames.Id, "ap_email");
		MailInput.Text = TestContext.DataRow["user"].ToString().Trim();
		HtmlEdit PasswordInput = new HtmlEdit(browser);
		PasswordInput.SearchProperties.Add(HtmlEdit.PropertyNames.Id, "ap_password");
		PasswordInput.Text = TestContext.DataRow["password"].ToString().Trim();

		HtmlInputButton IniciarButton = new HtmlInputButton(browser);
		IniciarButton.SearchProperties.Add(HtmlInputButton.PropertyNames.Id, "signInSubmit");
		Mouse.Click(IniciarButton);
		Playback.Wait(10000);
	}

HOW TO CLICK IN HYPERLINKS WITHOUT RECORDING ACTIONS

Author: Alfonso Alarcón

In Coded UI Test solutions of visual Studio (Enterprise, Ultimate or Premium versions) is recommendable to optimize efforts avoiding recording actions when a large numbers of elements must be recorded. See the general strategy from https://automatedtestingtools.wordpress.com/2016/04/19/strategy-to-get-automated-testing-without-recording-actions

In the next method is shown how to click on a hyperlink:

private static Dictionary objDict = UiTestHtmlHyperlinkDict.UiTestHtmlHyperlink();

public static void MouseClickInHyperlink(string dictKey)
{
	Mouse.Click(objDict[dictKey]);
}

As it is shown in the previous code the UI Control of the grid parent is stored in a dictionary. Please see the next post for more information of how to store UI Controls in dictionaries: https://automatedtestingtools.wordpress.com/2016/03/28/how-to-create-dictionaries-to-add-ui-controls

HOW TO CLICK IN BUTTONS WITHOUT RECORDING ACTIONS

Author: Alfonso Alarcón

In Coded UI Test solutions of visual Studio (Enterprise, Ultimate or Premium versions) is recommendable to optimize efforts avoiding recording actions when a large numbers of elements must be recorded. See the general strategy from https://automatedtestingtools.wordpress.com/2016/04/19/strategy-to-get-automated-testing-without-recording-actions

In the next class is shown a method to click in buttons such as ok, cancel, confirm, exit….

public static WinButton WinButton;

private static Dictionary<string, WinButton> objDict = UiTestWinButtonDict.UiTestWinButton();

public static void DoubleClickInButton(string tableKey)
{
	WinButton winButton = objDict[tableKey];
	if (winButton.WaitForControlReady(2000))
	{
		Mouse.DoubleClick(winButton);
	}
	else
	{
		Assert.Fail("The button is not ready or does not exist.");
	}
}

As it is shown in the previous code the UI Control of the grid parent is stored in a dictionary. Please see the next post for more information of how to store UI Controls in dictionaries: https://automatedtestingtools.wordpress.com/2016/03/28/how-to-create-dictionaries-to-add-ui-controls

HOW TO SET VALUES IN WINEDIT BOXES

Author: Alfonso Alarcón

In Coded UI Test solutions of visual Studio (Enterprise, Ultimate or Premium versions) is recommendable to optimize efforts avoiding recording actions when a large numbers of elements must be recorded. See the general strategy from https://automatedtestingtools.wordpress.com/2016/04/19/strategy-to-get-automated-testing-without-recording-actions

In the next class is shown a method to set data in a box using SetProperty():

private static Dictionary<string, WinEdit> objDict = UiTestWinEditDict.UiTestWinEdit();

public static void SetValueInWinEdit(string textComments, string tableKey)
{
	WinEdit winEdit = objDict[tableKey];
	if (winEdit.WaitForControlEnabled(2000))
	{
		winEdit.SetProperty("Text", textComments);
	}
	else
	{
		Assert.Fail("The next value cannot be written in the box: " + textComments);
	}
}

As it is shown in the previous code the UI Control of the grid parent is stored in a dictionary. Please see the next post for more information of how to store UI Controls in dictionaries: https://automatedtestingtools.wordpress.com/2016/03/28/how-to-create-dictionaries-to-add-ui-controls

HOW TO CLICK IN CELL ELEMENTS FROM A PARENT GRID

Author: Alfonso Alarcón

In Coded UI Test solutions of visual Studio (Enterprise, Ultimate or Premium versions) is recommendable to optimize efforts avoiding recording actions when a large numbers of elements must be recorded. See the general strategy from https://automatedtestingtools.wordpress.com/2016/04/19/strategy-to-get-automated-testing-without-recording-actions

Therefore, the best option is to get Coded UI controls from the parent control and to get from it all the children items using the Add a new control to the UIMap tool (see https://automatedtestingtools.wordpress.com/2016/04/11/how-to-capture-ui-control-with-coded-ui-test-builder-without-recording-actions).

In the next example a table grid is presented with two columns.  The strategy to follow is to get only the squared UI Control and from it get both all the cells of the grid.

tablegrid

A method to click in any cell of the grid from the parent element is shown in the next class. The key point is to use first GetCell():

private static Dictionary<string, WinTable> objDict = UiTestTableGridDict.UiTestGridTables();

public static void ClickInCellElement(int row, int colum, string dictKey)
{
	WinTable table = objDict[dictKey];
	UITestControl children = table.GetCell(row, colum);
	if (!children.Enabled)
	{
		Assert.Fail("The cell element is not enabled or does not exist.");
	}
	else
	{
		Mouse.Click(children);
	}
}

As it is shown in the previous code the UI Control of the grid parent is stored in a dictionary. Please see the next post for more information of how to store UI Controls in dictionaries: https://automatedtestingtools.wordpress.com/2016/03/28/how-to-create-dictionaries-to-add-ui-controls

How to get combo box items from the parent element

Author: Alfonso Alarcón

In Coded UI Test solutions of visual Studio (Enterprise, Ultimate or Premium versions) is recommendable to optimize efforts avoiding recording actions when a large numbers of elements must be recorded. See the general strategy from https://automatedtestingtools.wordpress.com/2016/04/19/strategy-to-get-automated-testing-without-recording-actions

Therefore, the best option is to get Coded UI controls from the parent control and to get from it all the children items using  the Add a new control to the UIMap tool (see https://automatedtestingtools.wordpress.com/2016/04/11/how-to-capture-ui-control-with-coded-ui-test-builder-without-recording-actions).

In the next example a combo box is presented with a list of countries.  The strategy to follow is to get only the red squared UI Control and from it get both all the countries items of the list.

combobox

A method to click in any list item of a combo box (for example Germany) from the parent element is shown in the next class. The key point is to use first Items.ToList():

public static WinComboBox ComboBox;

public static List GetElementsFromComboBox;

private static Dictionary objDict = UiTestComboBoxDict.UiTestComboBox();

public static void MouseClickInComboBoxElement(string nameComboBoxElement, string dictKey)
{
	ComboBox = objDict[dictKey];
	UITestControl elemeUiTestControl =
		ComboBox.Items.ToList().FirstOrDefault(element => element.Name == nameComboBoxElement);

	if (elemeUiTestControl != null)
	{
		Mouse.Click(ComboBox);
		Mouse.Click(elemeUiTestControl);
	}
	else
	{
		Assert.Fail("The next combobox element does  not exist: " + nameComboBoxElement);
	}
}

As it is shown in the previous code the UI Control of the combo box parent is stored in a dictionary. Please see the next post for more information of how to store UI Controls in dictionaries: https://automatedtestingtools.wordpress.com/2016/03/28/how-to-create-dictionaries-to-add-ui-controls

How to get menu elements from the parent element

Author: Alfonso Alarcón

In Coded UI Test solutions of visual Studio (Enterprise, Ultimate or Premium versions) is recommendable to optimize efforts avoiding recording actions when a large numbers of elements must be recorded. Therefore, the best option is to get the Coded UI controls from the parent control and to get from it all the children items using  Add a new control to the UIMap tool (see https://automatedtestingtools.wordpress.com/2016/04/11/how-to-capture-ui-control-with-coded-ui-test-builder-without-recording-actions).

For example in a typical calculator the menu bar has a set of menus (View, Edit and Help) the strategy to follow is to get only the squared UI Control and from it get both all the menus and related items of each menu.

calculator

In the next class is provided how to click in an element of a menu. Therefore, if you want to click for example in the Programmer item of the menu bar the key point is to use GetChildren():

        public static WinMenuBar MenuBar;
        private static Dictionary<string, WinMenuBar> objDict = UiTestMenuBarDict.UiTestWinMenuBar();
        public static void MouseClickSubMenuItems(string menu, string subMenu, string dictKey)
        {
            MenuBar = objDict[dictKey];

            foreach (WinMenuItem item in MenuBar.Items)
            {
                if (item.FriendlyName == menu)
                {
                    Mouse.Click(item);
                    foreach (WinMenuItem subMenuItem in item.Items)
                    {
                        if (subMenuItem.FriendlyName == subMenu)
                        {
                           Mouse.Click(subMenuItem);
                        }
                    }
                }                
            }
        }