What isCoded UI
Coded UI test is a very powerful test tool that visual Studio 2010 provides for testing project (test Engineering), a framework for UI Automation testing, and an automated test that supports UI such as WIN32,WEB,WPF. Coded The UI supports automatic recording and manual authoring.
What Coded UI can do
- Open the Application
- Simulating user actions
- Get UI elements
- Uitestcontrol: base class for all UI class libraries
- Web test Class Library: HtmlControls (HTMLBUTTON,HTMLDIV, etc.)
- WinForm Test Class Library: Wincontrols (Winmenu,winwindow, etc.)
- WPF Test Class Library: Wpfcontrols (Wpfcheckbox,wpfpane, etc.)
Coded UI Main Class Library
- Uitestcontrol: base class for all UI class libraries
- Web test Class Library: HtmlControls (HTMLBUTTON,HTMLDIV, etc.)
- WinForm Test Class Library: Wincontrols (Winmenu,winwindow, etc.)
- WPF Test Class Library: Wpfcontrols (Wpfcheckbox,wpfpane, etc.)
Coded UI Results Validation class
Assert class: Microsoft.VisualStudio.QualityTools.UnitTesting
Common methods: IsTrue (), IsFalse (), IsNull (), AreEqual ()
Manually writing the coded UI Test
principle: manual writing is the same as the automatic recording principle, but more flexible. Automatic recording generated code is too Fanluan, not easy to modify maintenance, manual writing is easy to manage, can be used in accordance with the actual situation of some common programming methods, the code to do some customization, more readable, the extraction of public parts written general functions make the code easy to maintain.
Ideas:
Identify the program you want to test, and capture this control based on some of the properties of the program control
Perform some operations on the control based on test requirements
Extract control information According to requirements, control state, text information, control definition, etc.
Match the extracted information to the expected information
New coded UI Test project
- Ibid. create a new test project, add coded UI test
- Write the test method in the CodedUITest1.cs file. The method body of each test method must have [TestMethod] on it, in order to facilitate the VS2010 of the method execution tool to find the method.
- Manually get page controls to use IE Developer Tools
Coded UI some ways to do it
Call the HtmlControls class library (there is a sample program later)
- Open Browser: Call the Browserwindow class to instantiate a Browserwindow object
Browserwindow browser = browserwindow.launch (new Uri ("http://www.baidu.com"))
- Get div control: Call the Htmldiv class to instantiate a Htmldiv object
Htmldiv Header = new Htmldiv (browser);
Header. Searchproperties[htmldiv.propertynames.class] = "S_tab";
- Get text box: Call the Htmledit class to instantiate a Htmledit object
Htmledit Txtkeyword = new Htmledit (browser);
Txtkeyword.searchproperties[htmledit.propertynames.class] = "S_ipt";
- Get button: Invokes the HtmlInputButton class to instantiate a HtmlInputButton object
HtmlInputButton btnsubmit = new HtmlInputButton (browser);
Btnsubmit.searchproperties[htmlinputbutton.propertynames.class] = "S_BTN";
And so on, there are smart hints in the code, or you can query the MSDN Web site to find the appropriate method
- Mouse click: Mouse.click (HTMLControl);
Mouse.click (btnsubmit);
- Validation result: Various methods of invoking the Assert class
Assert.istrue (imglogo.exists, "Logo doesn ' t exist!");
- Wait for the browser to load complete: Browserwindow. Waitforcontrolready ();
- Let the program wait: System.Threading.Thread.Sleep (number of milliseconds);
Sample program: Verify Baidu Search success
Public classCodedUITest1 {[TestMethod] Public voidvarifyfeatures () {#regionOpen Browser Get control//Call Browserwindow Open Browser, open IE by defaultBrowserwindow browser = Browserwindow.launch (NewUri ("http://www.baidu.com")); //Open Other Browser methods//Process p = process.start (@ "C:\Users\XXX\AppData\Roaming\360se6\Application\360se.exe", "http://www.baidu.com"); //Browserwindow browser = browserwindow.fromprocess (p); //Find search text boxes based on propertiesHtmledit Txtkeyword =NewHtmledit (browser); Txtkeyword.searchproperties[htmledit.propertynames.class]="S_ipt"; //Find "Baidu button" based on attributesHtmlInputButton btnsubmit =NewHtmlInputButton (browser); Btnsubmit.searchproperties[htmlinputbutton.propertynames.class]="s_btn"; //Find header div based on attributesHtmldiv Header =NewHtmldiv (browser); Header. Searchproperties[htmldiv.propertynames.class]="S_tab"; #endregion #regionVarious validation//wait for the browser to load completeBrowser. Waitforcontrolready (); //Enter Test keywordsTxtkeyword.text ="Software Testing"; //Click on "Baidu button"Mouse.click (btnsubmit); //Verify that the keywords inside the search box are correct after the page jumpsAssert.istrue (TxtKeyWord.ValueAttribute.Contains ("Software Testing")); //Close BrowserBrowser. Close (); #endregion } }
View Code
What is the coded UI