C # _ automated testing 3_controll IE

Source: Internet
Author: User

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

  1. Advantages
  2. Add reference
  3. Operation IE
  4. Use ie develop toolbar to view HTML dom
  5. Operate HTML elements in IE
  6. 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 IE. navigate (cnblogurl, ref nil); thread. sleep (3000 );
// Set ie in the upper left corner. top = 10; IE. left = 10; // set the height and width of IE. height = 800; IE. width = 1000; // navigate to the Baidu homepage thread. sleep (3000); string baiduurl = "http://www.baidu.com"; IE. navigate (baiduurl, 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 IE. navigate (cnblogurl, 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>

 

Appendix: automated test series tutorials (coming soon)

Automated Testing (I) 12306 automated logon tool for train ticket websites

Automated Testing (ii) plug-ins for continuous view

Automated Testing (iii) Web automated testing principles

Automated Testing (4) Automatic Software Uninstall

Automated test (5) read and write the registry of a 64-bit Operating System

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.