[Small North de programming notes]: Lesson 08-selenium for C # pagefactory & Team Building

Source: Internet
Author: User
Tags appium

What this article wants to share with you is selenium's support for PageObject mode and the construction of the automated Test team. The article in the Selenium for C # series has come to an end here, and if you read and practice the previous article, I believe you should be able to simulate 80% common manual test cases in your daily work. Note: My term is a mock use case, not a writing automated test case. The building of an enterprise-level automated test is not a technology that can hold selenium alone. The so-called simulation refers to a case that can only be automated, but not the use of engineering. In the practice of automating tests that I have approached several companies, there are many automated testing frameworks that are not or are not reasonably layered and packaged. Test case in the operation of the page-driven code everywhere, can not be based on the personnel's technical ability to division, unable to switch the test environment, can only use a single browser test, the use of the high threshold for QA ... And so on.

This article will cover the following topics:

    • PageObject Design Pattern Introduction.
    • Selenium support for PageObject mode.
    • A simple test structure
    • Automated Test team member Division
    • Demo: Blog Park Personal Home page implementation (this is to deceive you ...) )
(a) Introduction of PageObject design pattern

Page object is the industry's most popular automated test design pattern, which can effectively improve the maintainability of automated testing and code reuse rate. Almost all automated test-driven frameworks provide support for this pattern (Appium, QTP, white, etc.). Of course, even if the driver you're using doesn't support it, we can write code to implement it. After all, the so-called design patterns but the organization and collaboration of code.

What is Page object mode? Simply put, the page of the application being tested is encapsulated into an object that exposes the relevant properties and methods to the user (the writer of the test case). is the package for the page:

As you can see, the page class provides a TitleText property that returns the title text of the page to the user, and also provides basic page manipulation. For the user, they don't have to worry about how the elements on this page are positioned or what technology implementations are used internally (Selenium, or QTP). Another benefit is that if development makes a certain adjustment to the elements of the page, we only need to deal with pageobject internal code implementations without modifying the actual test case code. The benefits of this model are many, this article will not repeat. I will describe it later in the framework practice related articles.

(ii) Selenium support for the PageObject model

Let's take a look here, Selenium support for PageObject mode. The following code is a simple encapsulation of a login page:

1      Public classSigninpage2     {3          PublicIwebdriver Driver {Get;Set; }4          Publicdashboardsigninpage (iwebdriver driver)5         {6              This. Driver =driver;7Pagefactory.initelements (Driver, This);8         }9         #regionPage elementsTen[Findsby (how = how.id, Using ="username")] One         protectediwebelement txtUserName; A  -[Findsby (how = how.id, Using ="Password")] -         protectediwebelement Txtpassword; the  -[Findsby (how = how.xpath, Using =".//button[text () = ' sign in ']")] -         protectediwebelement Btnsignin; -         #endregionPage elements +  -         #regionAction for test case +         /// <summary> A         ///Sign on for Dashboard at         /// </summary> -         /// <param name= "UserName" >User name</param> -         /// <param name= "password" >Password</param> -          Public voidSignIn (stringUserName,stringpassword) -         { -              This. Txtusername.clear (); in              This. Txtpassword.clear (); -  to              This. Txtusername.sendkeys (userName); +              This. Txtpassword.sendkeys (password); -  the              This. Btnsignin.click (); *         } $ Panax Notoginseng         //Other action ... -         #endregion the}

The Findsby and pagefactory that appear in the above code are exactly what selenium Webdriver provides for PageObject mode support.

@PageFactory class

Fagefactory provides the ability to pageobject instances, and we can see that it itself provides a number of ways to build instances of specific page classes. Its main function is to map the fields we tagged with the Findsby property and the specified DOM elements. We can then manipulate DOM elements directly using specific properties without using the Findelement method to locate them.

1      Public Sealed classpagefactory2     {3          Public StaticT initelements<t>(Ielementlocator locator);4          Public StaticT initelements<t>(Iwebdriver driver);5          Public Static voidInitelements (Isearchcontext driver,Objectpage);6          Public Static voidInitelements (Objectpage, ielementlocator locator);7          Public Static voidInitelements (Isearchcontext driver,Objectpage, ipageobjectmemberdecorator decorator);8          Public Static voidInitelements (Objectpage, Ielementlocator Locator, ipageobjectmemberdecorator decorator);9}
@FindsBy Properties

The Findsby property is used to mark how an element in a program is positioned. We can use any of the positioning methods described earlier in "Lesson 03-selenium for C # element positioning" to mark how elements are positioned. Use the How parameter to specify the positioning method, which is the specific value identified by the using parameter. If you are not sure how to locate the element, you can view the previous article, and you will not be repeating it here.

1     //Summary:2     //provides the lookup methods for the Findsby attribute (for using in pageobjects)3      Public enum How4     {5         //Summary:6         //Finds by OpenQA.Selenium.By.Id (System.String)7Id =0,8         //9         //Summary:Ten         //Finds by OpenQA.Selenium.By.Name (System.String) OneName =1, A         // -         //Summary: -         //Finds by OpenQA.Selenium.By.TagName (System.String) theTagName =2, -         // -         //Summary: -         //Finds by OpenQA.Selenium.By.ClassName (System.String) +ClassName =3, -         // +         //Summary: A         //Finds by OpenQA.Selenium.By.CssSelector (System.String) atCssselector =4, -         // -         //Summary: -         //Finds by OpenQA.Selenium.By.LinkText (System.String) -LinkText =5, -         // in         //Summary: -         //Finds by OpenQA.Selenium.By.PartialLinkText (System.String) toPartiallinktext =6, +         // -         //Summary: the         //Finds by OpenQA.Selenium.By.XPath (System.String) *XPath =7, $         //Panax Notoginseng         //Summary: -         //Finds by a custom OpenQA.Selenium.By implementation. theCustom =8, +}

So finally, let's take a look at how the consumer (the upper-level test case writer) will use this class, which is just an example of using signinpage, and we see that the code for creating driver and navigation pages is written in the test case. In accordance with the previous structure is also a problem, here is no longer open, look forward to my follow-up on the automation framework design Article ~ ~ ~:

1         Private Const stringCst_displayname ="Basecheck.signin";2[Fact (DisplayName = Cst_displayname +". Success")]3          Public voidsignin_success ()4         {5             varDriver =Newfriefoxdriver ();6Driver. URL ="Www.xxx.com/signin";7 8             varSigninpage =Newsigninpage (driver);9Signinpage.signin ("Your name","Password");Ten  One             //Omit code ... A driver. Close (); -}
(c) Simple test structure

Read here, even if you are a beginner. must also understand some pageobject design mode for what, here I still describe pageobject intention: PageObject mode is to encapsulate page element positioning, page wait, jump and other operations page related logic. Allows the user to write related test cases without having to care about the logic. Let's review the structure of the general Enterprise Test framework I mentioned earlier in the core object of Lesson 02-selenium for C #:

The red line in the circle is the PageObject consumer (writing the tester of the test case). As you can see, the next layer of developers can encapsulate the page into PageObject, the upper level of the user does not have to care about the specific implementation technology is which kind (Selenium, Appium ... )。 PageObject consumers can focus on the logical implementation of test case.

(iv) Automated Testing team member Division

A lot of companies and friends have talked to me about how to build an automated Test team. There is no exception to these questions: the need for QA team to have strong coding skills, they have to learn various drivers (Selenium, QTP, Appium ...). ), learn some JavaScript, HTML, CSS-related knowledge, have oop thinking ... And so on. The final conclusion is that building automated testing has made a high demand for QA personnel. In my opinion, if a QA can have the above ability I think he is already a developer (this requirement is impractical). Would it be possible for the development team to do this thing? This certainly does not, the reason has more than N. I only mention two here. First, the development team's perspective is often not at the user's point of view, so it will greatly improve project risk. Second, is an automated test build successful or not? A reasonable test plan is often the deciding factor, but the test plan is not developed by developers. (There is no discrimination to develop the meaning.) Hehe ~ ~ I also develop ~ ~ ~ Do not like to spray). So, how to build it? This is actually the reason we want to build the test framework, layering, using the PageObject model, for the simple structure I drew in the previous section, the team can have several roles as follows:

@ Functional Testers:

It is mainly composed of manual testers and testers who understand some simple programming grammars, who are the direct users (consumers) of the pageobject and testing frameworks. Use the completed PageObject and test framework for the preparation of test cases, and the development of test plans. In my personal experience, the general QA is a simple training that can be qualified for such work, and they have done a lot of work. Functional testers focus on testing with logic, rather than technical details.

@PageObject Writing staff:

This can be performed by a high level of technology (programming technology) in the Test team or by a developer, with the following requirements:

    • Be familiar with the use of related drivers (Selenium, QTP ...). ), unfamiliar or not, see my "Selenium for C #" series Ha ~ ~ ~
    • Familiar with web programming, JavaScript, HTML, CSS and other related knowledge. (based on B/S test construction, if the desktop or mobile phone to other knowledge)
    • Familiar with OOP ideas, can make a reasonable package for the page.

Therefore, this part of the students focus on providing easy-to-use page classes, without having to relate to complex business logic. This part of the person is also a professional development direction for testers (technical test).

@ Test Architect:

This requires some senior students to do, the architect's request is more. His main task in the team is to build a test infrastructure. such as the management of page navigation, provide ORM mechanism for testers to use, log output, test report generation, environment switch, browser switching, provide some cool and simple use features, there are some of the most document output. In this way, the test architect can also care about the technical challenges he needs to complete, not the implementation of the business and the specific page class.

To sum up, with these three kinds of division of labor actually work at different levels. Each of them completes the part that they are good at. It also provides the maintainability of the entire test. For example: The page has been modified, only need to pageobject personnel to modify their own package. The business logic is changed (the page does not change), but also the use case writers to modify their own test.

(v) written in the last ...

Originally wanted to do a demo for everyone. But...... The Chinese New Year ... Mom urged to go home for the New Year, pack up to go ah, have the opportunity to fill up again ... Today will be wayward a haha, you small partners, Happy New Year!

In front of the division of labor, now you are which level? I can see this article in a series of articles, most of which are still small partners who have no ability to build a test framework. So, if time permitting, I will write a series on the framework of automated testing, remember to pay attention to OH ~ ~ ~

"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 # window processing
    • [Small North de programming notes]: Lesson 08-selenium for C # pagefactory & Team Building

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 08-selenium for C # pagefactory & Team Building

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.