There are many automated Web UI testing frameworks on the market, such as WatiN, Selinimu, WebDriver, and Coded UI in VS2010. these frameworks can operate Web controls, simulate user input, click, and other operations to achieve automated Web testing. In fact, these tools share the same principle. They call the ie com interface and html dom to operate IE browsers and WEB test objects.
This article describes how to break away from these automated testing frameworks. Directly use the shdocvm. dll library provided by. NET to operate IE browsers, and use the mshtml. dll library to operate HTML Objects in IE.
Reading directory
- Advantages
- Add reference
- Operation IE
- Use IE Develop Toolbar to view HTML DOM
- Operate HTML elements in IE
- Source code download
Advantages
Web automation is achieved through direct operations on ie com, allowing you to quickly establish a lightweight Automated Testing Program within minutes. This greatly improves the test efficiency. It also helps you understand the operating principles of WatiN automated testing frameworks.
Add reference
The COM component names of the shdocvm. dll and mshtml. dll libraries are different from their dll names. So it is hard to find.
The COM component of shdocvm. dll is named "Microsoft Internet Controls". Add reference to Add References-> Com Tab-> Microsoft Internet Controls
The COM component of mshtml. dll is named "Microsoft. mshtml". Add reference to Add References->. NET Tab-> Microsoft. mshtml
After adding a reference, you can reference the namespace.
using mshtml;
using SHDocVw;
Operation IE
Use the attributes and methods of the InternetExplorer object in shdocvm. dll, such as Height and Width. We can operate IE to simulate some user operations, such as adjusting the browser size and refreshing the page.
Static void Main (string [] args)
{
InternetExplorer IE = new InternetExplorer ();
IE. Visible = true;
Object nil = new object ();
String CnblogUrl = "http://www.cnblogs.com ";
// Open IE and open the homepage of the blog Garden
IE. Navigate (CnblogUrl, ref nil, ref nil );
Thread. Sleep (3000 );
// Set the position in the upper left corner of IE
IE. Top = 10;
IE. Left = 10;
// Set the height and width of IE
Internet Explorer. Height = 800;
IE. Width = 1000;
// Navigate to Baidu Homepage
Thread. Sleep (3000 );
String BaiduUrl = "http://www.baidu.com ";
IE. Navigate (BaiduUrl, ref nil, ref nil );
// Click the back button.
Thread. Sleep (3000 );
IE. GoBack ();
// Refresh IE
Thread. Sleep (3000 );
IE. Refresh ();
// Disable IE
Thread. Sleep (3000 );
IE. Quit ();
}
Use IE develop toolbar to view HTML DOM
In the next section, we want to operate the elements in HTML. First, we need to know the IDs of these controls. You can use IE Develop Toolbar or firebug to conveniently view the control IDs.
Press F12 to start IE Develop Toolbar, or click Tools> Develop Tools in the IE Toolbar.
You can see that the id of the search textbox in the blog garden is "q"
Operate HTML elements in IE
Simulate a search scenario on the blog homepage. Input data in the text box, and then click "find ".
We use the getElementById () method in the mshtml. dll library to obtain reference to the HTML element you want to operate on. Then you can operate on this object, such as entering a value and clicking it
Static void Main (string [] args)
{
InternetExplorer IE = new InternetExplorer ();
IE. Visible = true;
Object nil = new object ();
String CnblogUrl = "http://www.cnblogs.com ";
// Open IE and open the homepage of the blog Garden
IE. Navigate (CnblogUrl, ref nil, ref nil );
Thread. Sleep (3000 );
// Obtain the DOM object
HTMLDocument doc = (HTMLDocument) IE. Document;
// The id of the search Textbox on the blog homepage is "q"
HTMLInputElement SearchTextBox = (HTMLInputElement) doc. getElementById ("q ");
SearchTextBox. value = "tank ";
// The id of the "find" button on the blog homepage is "btnBloggerSearch"
HTMLInputElement SearchButton = (HTMLInputElement) doc. getElementById ("btnBloggerSearch ");
SearchButton. click ();
}
The running effect is as follows:
Source code download
Click here to open it with VS2008 or later
Reference books: <. NET Test Automation Recipes>