[Small North de programming notes]: Lesson 04-selenium for C # API

Source: Internet
Author: User

This part, I am ready to introduce to you selenium Webdriver Common API, learning this part of the content needs to have some simple HTML related knowledge, this article mainly involves the following content:

    • Selenium API: element checking
    • Selenium API: Event handling
    • Selenium API: Other operations
    • Selenium API: Framework Extensions

Look back at one of the concepts I've talked about: the so-called automated tests can be simply attributed to the process of identifying or locating elements from the program being tested, and performing operations and validating elements . From a framework design perspective, a driver for an automated test framework should provide the user with the ability to "element positioning, performing operations, and element Validation". On the excellent "element positioning" capabilities offered by Selenium, the previous article, "Lesson 03-selenium for C # element positioning", has been described. This blog will describe how selenium simulates a user's actions and how to get the elements that need validation.

(i) Selenium API: element check

When it comes to automated testing based on B/S, the main object we need to check is the DOM element of the Web page, and selenium provides us with a number of standard APIs to get what we want:

    • Gets the text of the element
    • Get the attributes of an element
    • Gets the CSS style of the element
@. get element Text information

Speaking of which, let me briefly introduce the relationship between these three parts and the DOM elements (take care of the students who do not understand HTML). The so-called element of the text, can be considered as a manual test process is visible on the page text information (that is, display content), such as the text in an input box, a read-only text field or a page title .... These all refer to the text of the element. Iwebelement defines how to get the text of the element (read-only Text property):

1      Public InterfaceIwebelement:isearchcontext2     {3         //omit other property methods ...4         //5         //Summary:6         //Gets The InnerText of this element, without any leading or trailing whitespace,7         //And with other whitespace collapsed.8         //9         //Exceptions:Ten         //OpenQA.Selenium.StaleElementReferenceException: One         //thrown when the target element was no longer valid in the document DOM. A         stringText {Get; } -}

The following code is to get the blog home navigation text and output, test output:

1         /// <summary>2         ///demo1: Check the homepage text3         /// </summary>4[Fact (DisplayName ="Cnblogs.SeleniumAPI.Demo1")]5          Public voidSeleniumapi_demo1 ()6         {7_output. WriteLine ("Step 01: Launch the browser and open the blog home page. ");8Iwebdriver Driver =Newfirefoxdriver ();9Driver. URL ="http://www.cnblogs.com";Ten  One_output. WriteLine ("Step 02: Look for page elements that need to be checked. "); A             varLnkhome = driver. Findelement (By.xpath (".//ul[@class = ' Post_nav_block ']/li[1]/a")); -  -_output. WriteLine (string. Format ("Navigation inside column contents: {0}", Lnkhome.text)); the  -_output. WriteLine ("Step 04: Close the browser. "); - driver. Close (); -}

@. Get element properties

Developers can control the behavior or style of DOM elements by configuring the attributes of various elements. Here you need to explain what the attributes of DOM elements are. or blog Home For example, with Firebug to see the navigation "home" corresponding elements:

The general DOM element format is as follows:

<tag_name attribute1= "value1" attribute2= "value2" ... >text content</tag_name>

Here tag_name is a, and class,href is the attribute corresponding to that element. We can use the GetAttribute method defined by Iwebelement to get the value of the property. To obtain the validation and judgment related to the DOM, you can get the class attribute of the element using the following code:

1 _output. WriteLine ("Step 02: Look for page elements that need to be checked. "); 2        var lnkhome = driver. Findelement (By.xpath (".//ul[@class = ' post_nav_block ']/li[1]/a"));   3        var value = Lnkhome.getattribute ("class");
@. get element CSS style

Simply put, the DOM element's tagname and attributes describe the type of DOM element (i.e. what this element is, the text box, the button, or the drop-down menu), while the element's presentation style is determined by the CSS (cascading style sheet). In this I default reader is a know some CSS knowledge of the classmate, after all, our theme is selenium. CSS defines the style of the element, such as width, height, color, etc..., we can use the Getcssvalue method defined by Iwebelement to get the style of the element, for example, we need to verify the display font, color, or display or not of an element. Can be obtained through this method. Also we can get and output blog Park is the font and color of the homepage text:

1         /// <summary>2         ///Demo2: Check the properties, fonts, and colors of the home page text3         /// </summary>4[Fact (DisplayName ="Cnblogs.SeleniumAPI.Demo2")]5          Public voidSeleniumapi_demo2 ()6         {7_output. WriteLine ("Step 01: Launch the browser and open the blog home page. ");8Iwebdriver Driver =Newfirefoxdriver ();9Driver. URL ="http://www.cnblogs.com";Ten  One_output. WriteLine ("Step 02: Look for page elements that need to be checked. "); A             varLnkhome = driver. Findelement (By.xpath (".//ul[@class = ' Post_nav_block ']/li[1]/a")); -              -Lnkhome.getcssvalue ("Color"); the  -_output. WriteLine (string. Format ("Navigation inside column contents: {0}", Lnkhome.text)); -_output. WriteLine (string. Format ("Classs Property Contents: {0}", Lnkhome.getattribute ("class"))); -_output. WriteLine (string. Format ("font: {0}", Lnkhome.getcssvalue ("font-family"))); +_output. WriteLine (string. Format ("color: {0}", Lnkhome.getcssvalue ("Color"))); -  +  A_output. WriteLine ("Step 04: Close the browser. "); at driver. Close (); -}

Output Result:

(ii) Selenium API: Event handling

The event handling of selenium can be divided into two parts: simple event processing and compound operation.

    • Simple event: Refers to a button that is similar to clicking, entering text, and so on.
    • Composite event: Refers to the execution of a chain of events (for example: Ctrl + mouse click)
@. Simple Event Handling

In this section I mentioned in the previous article "Lesson 02-selenium for C # Core objects", Iwebelement defines a common event handler function:

    • Clear: Empties the contents of the current DOM element (for text input elements).
    • Click: Clicking the current DOM element.
    • Sendkey: Enter the specified text into the current DOM element.
    • Submit: Submits the current element to the Web server.

The following example implements the following functions: (Navigate to the Blog home > enter "Small North de programming notes" in the Search box > click "Look for" and wait five seconds > close the browser)

1         /// <summary>2         ///Demo3: Home Query3         /// </summary>4[Fact (DisplayName ="Cnblogs.SeleniumAPI.Demo3")]5          Public voidSeleniumapi_demo3 ()6         {7_output. WriteLine ("Step 01: Launch the browser and open the blog home page. ");8Iwebdriver Driver =Newfirefoxdriver ();9Driver. URL ="http://www.cnblogs.com";Ten  One_output. WriteLine ("Step 02: Find the page elements that need to be manipulated. "); A             varTxtsearch = driver. Findelement (By.id ("zzk_q")); -             varBtnsearch = driver. Findelement (By.xpath (".//input[@type = ' button ' and @value = ' look for ']")); -  the_output. WriteLine ("Step 03: Enter query text"); -             Txtsearch.sendkeys (" small North de programming notes ");  -  -_output. WriteLine ("Step 04: Click to Inquire"); + Btnsearch.click (); -  +System.Threading.Thread.Sleep ( the); A  at_output. WriteLine ("Step 05: Close the browser. "); - driver. Close (); -}

Run the demo as described in the code as a step-by-step operation ...

The space is limited, today first write here (actually is sleepy ~ ~ ~ ha). Next I'll explain selenium implementation of complex interactions, element dragging, special DOM element handling (dropdown menu, Radio check box)

"Selenium for C #" related article: Click here.

    • [Small North de programming notes]: Lesson 01-selenium for C # Environment construction
    • [Small North de programming notes]: Lesson 02-selenium for C # core objects
    • [Small North de programming notes]: Lesson 03-selenium for C # element positioning
    • [Small North de programming notes]: Lesson 04-selenium for C # API
    • [Small North de programming notes]: Lesson 05-selenium for C # API
    • [Small North de programming notes]: Lesson 06-selenium for C # Process Control
    • [Small North de programming notes]: Lesson 07-selenium for C # pagefactory
    • [Small North de programming notes]: Lesson 08-selenium for C # Summary

Description

When I finish publishing a post with no hyperlinks, I'll add the appropriate links to list the directories first.

Demo Address: Https://github.com/DemoCnblogs/Selenium

If you think this article is good or something, you can click on the bottom right corner of the "Recommended" button, because your support is I continue to write, share the most power! Small North @north Source: Http://www.cnblogs.com/NorthAlan Statement: The original text of this blog only represents my work in a certain time to summarize the views or conclusions, and I do not have a direct interest in the unit. Non-commercial, unauthorized, post please keep the status quo, reprint must retain this paragraph statement, and in the article page obvious location to the original connection.

[Small North de programming notes]: Lesson 04-selenium for C # API

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.