Website testing with Coded UI
First Create a new Coded UI Test Project
After creating new project a dialog will appear to record a test, click on Cancel button
Now remove extra comment line on code and after removing it will looks like –
using System; using System.Collections.Generic; using System.Text.RegularExpressions; 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 SampleTestProject { [CodedUITest] public class CodedUITest1 { public CodedUITest1() { } [TestMethod] public void CodedUITestMethod1() { } [TestInitialize()] public void MyTestInitialize() { } [TestCleanup()] public void MyTestCleanup() { } public TestContext TestContext { get { return testContextInstance; } set { testContextInstance = value; } } private TestContext testContextInstance; } }
Now our objective is to
- Open IE browser with a link
- Put an information on textbox
- Close IE browser.
To open a browser with url , we need to specify it on Test Initialize section –
[TestInitialize()] public void MyTestInitialize() { BrowserWindow.Launch("http://demo.tanmaysarkar.com/sample_01.html"); } And our test method will looks like – [TestMethod] public void CodedUITestMethod1() { BrowserWindow Browser = new BrowserWindow(); Browser.SearchProperties[UITestControl.PropertyNames.Name] = "Automation Sample 1:: Tanmay Sarkar"; HtmlEdit FirstName = new HtmlEdit(Browser); FirstName.SearchProperties[UITestControl.PropertyNames.Name] = "ts_first_name"; Keyboard.SendKeys(FirstName, "tanmay"); }
Now build the solution
and open Test Explorer
Now we can see our test methods are listed on it’s window
Right click on Test and select “Run Selected Test”
And it will open IE browser with specific URL and fill the Textbox with specified data and finally close the browser and the output will looks like –