Introduction
This chapter focuses on the automated testing framework – the object Library layer. This layer is a key layer in UI Automation, design automation framework, is inevitably the object library, has a good object library, can make the whole test framework maintainability higher, greatly enhance the reusability of code.
Before speaking, we should popularize a concept: PO mode PO mode
That's what PO mode is, why use PO mode. Quote the following passage, and you will suddenly realize
PO mode, full Page object mode, is a test design pattern in selenium, which is to design each page as a class that contains the elements (buttons, input boxes, headings, etc.) that need to be tested in the page so that when writing the test script, You can get page elements by calling the page class. When a page element ID or location changes, then do not change the test script, just change the corresponding page class on the line.
The above paragraph, summed up is: PO is a design pattern, the code is organized in pages, for all the information on this page, related operations are put into a class, so that the specific test cases into a simple call and validation operations.
If you understand the benefits of the Po object, of course, it is only to explain its maintainability, but its reusability is better understood, there is not much to explain. introduction of Object library
For the above-mentioned PO mode, we will not have a doubt. So an app has a lot of pages, take the DJI go as an example of 50 or so, it is not to design 50 page classes, and then each page class to write the corresponding elements. In this way, only the page class to write so much, feel the project is too large, and the code reusability is not high.
is there any way to improve it?
1. First define a BasePage class, after all, all the pages have a common thing, each page has elements, each page element has a corresponding method. The class consists of a member variable pageName, and a encapsulated method. Overrides its constructor, which is used to initialize the member variable. Each time a page is called, the corresponding page is obtained by using the new BasePage object and passing in the corresponding parameter pagename.
Package com.dji.object;
Import java.io.IOException;
Import Java.util.HashMap;
Import Com.dji.utils.AppiumExecutorImpl;
Import Com.dji.utils.Log;
Import Com.dji.utils.XmlUtils; Import Io.appium.java_client.
Appiumdriver; Import Io.appium.java_client.
Mobileelement; /** * Encapsulates a basepage class, after all, all the pages have a common thing, each page has elements, each page element has a corresponding method * * @author charlie.chen * * */public class BasePage Exte
NDS Appiumexecutorimpl {protected appiumdriver<?> driver; protected String pageName; Page name protected String Xmlpath;
page element path protected hashmap<string, locator> Locatormap;
Public log log = new log (This.getclass ());
Public BasePage (appiumdriver<?> driver,string pageName) throws Exception {super (driver);
This.driver = driver;
This.pagename=pagename; Gets the path to the resource file Page.xml//xmlpath=system.getproperty ("User.dir") + "\\src\\main\\java\\com\\dji\\pageObject\\Page.xml
"; Xmlpath=basepage.class.getclassloader ().GetResource ("Page.xml"). GetPath ();
Locatormap = Xmlutils.readxmldocument (Xmlpath, This.getclass (). Getsimplename ());
Locatormap = Xmlutils.readxmldocument (Xmlpath, pageName);
} public void type (string locatorname, String values) {Super.type (GetLocator (locatorname), values);
Log.info ("Type value is:" + values);
} public void Click (String locatorname) {Super.click (GetLocator (locatorname));
Log.info ("click:" +locatorname); } public string GetText (string locatorname) {//TODO auto-generated method stub return Super.gettex
T (GetLocator (locatorname)); } public mobileelement findelement (String locatorname) {//TODO auto-generated method stub return S
Uper.findelement (GetLocator (locatorname)); } public boolean iselementdisplayed (String locatorname) {//TODO auto-generated method stub return S
Uper.iselementdisplayed (GetLocator (locatorname)); /** * Locatorname Locator * * * @author charlie.chen * @param locatorname * @return
* @throws IOException */Public Locator getlocator (String locatorname) {Locator Locator = null;
if (locatormap!=null) {locator = Locatormap.get (Locatorname);
} return locator;
}
}
The code above has a collection of Locatormap, the main storage of the corresponding PAGENAME and locator content
2. Next package element, each element has a corresponding location address (XPath path or CSS or ID), wait time, positioning method
Package com.dji.object; /** * Encapsulates page elements, each with a corresponding location address (XPath path or CSS or ID), wait time, positioning method * * @author charlie.chen * */public class Locator {Priv Ate String address; Location address private int waitsec; Wait time private bytype bytype; Positioning mode/** * Anchor Type Enumeration * @author Charlie.chen * */public enum bytype{by, XPath, LinkText, ID, name, className} public Locator () {}/** * Locator constructor, default anchor type By.xpath * *
@author Charlie.chen * @param element */Public Locator (String address) {this.address = address;
This.waitsec = 3;
This.bytype = Bytype.xpath;
} public Locator (String address, int waitsec) {this.waitsec = waitsec;
this.address = address;
This.bytype = Bytype.xpath;
} public Locator (String address, int waitsec, Bytype bytype) {this.waitsec = waitsec;
this.address = address;
This.bytype = Bytype; } pUblic String getaddress () {return address;
} public int getwaitsec () {return waitsec;
} public Bytype Getby () {return bytype;
} public void Setby (Bytype bytype) {this.bytype = Bytype;
} public Bytype Getbytype () {return bytype;
}
}
management of object libraries
For the above two classes BasePage and locator, in fact, is to represent the Page object library and the element object library respectively. The management of object library is to separate the data in the object library, such as PAGENAME and element attribute id,xpth, and save it in the Page.xml file, so as to achieve the effect of data isolation and higher maintainability.
Page.xml as follows
<?xml version= "1.0" encoding= "UTF-8"?> <map> <!--locator of page map info--<page Pagenam E= "Menupage" > <!--Locator lists-to <locator type= "Xpth" timeout= "3" value= "//android.widget.t Extview[contains (@text, ' devices ')] "> Devices </locator> <locator type=" xpth "timeout=" 3 "value="//android.widget.t Extview[contains (@text, ' editor ')] > Editor </locator> <locator type= "xpth" timeout= "3" value= "//android.widget . Textview[contains (@text, ' Sky City ') ' > Sky City </locator> <locator type= "xpth" timeout= "3" value= "//android.wid Get. Textview[contains (@text, ' me ')] "> Me </locator> </page <page pagename=" Minepage "> <!--Loc
Ator lists-<locator type= "id" timeout= "3" value= "Dji.pilot:id/icon_user" > User images </locator> <locator type= "id" timeout= "3" value= "Dji.pilot:id/mine_user_name" > User name </locator> <locator type= "id "Timeout=" 3 "value= "Dji.pilot:id/v2_mine_store" >dji Mall </locator> <locator type= "id" timeout= "3" value= "Dji.pilot:id/v2_mi Ne_academy ">dji Forum </locator> <locator type=" xpth "timeout=" 3 "value="//android.widget.textview[contains (@text, ' gift card ')] " > Gift card </locator> <locator type= "xpth" timeout= "3" value= "//android.widget.textview[contains (@text, ' upload queue ' )] "> Upload list </locator> <locator type=" xpth "timeout=" 3 "value="//android.widget.textview[contains (@text, ' "> Messages </locator> <locator type=" xpth "timeout=" 3 "value="//android.widget.textview[contains (@text, ' I Collection ')] "> My favorites </locator> <locator type=" xpth "timeout=" 3 "value="//android.widget.textview[contains (@tex T, ' more ')] "> More </locator> <locator type=" xpth "timeout=" 3 "value="//android.widget.textview[contains (@tex T, ' Set ')] "> Settings </locator> </page> <page pagename=" LoginPage "> & lt;! --locAtor lists-<locator type= "id" timeout= "3" value= "Dji.pilot:id/edt_email" > Login Enter Account box </locator> <locator type= "id" timeout= "3" value= "Dji.pilot:id/edt_password" > Login Enter Password box </locator> <locator typ E= "id" timeout= "3" value= "DJI.PILOT:ID/BTN_SIGN_OK" > Login </locator> </page> <page pagename= "Setti Ngpage "> <!--Locator lists--<locator type=" id "timeout=" 3 "value=" dji.pilot:id/mine_setting S_back_button "> Return </locator> <locator type=" id "timeout=" 3 "value=" Dji.pilot:id/mine_settings_cellular _switch "> Upload files using mobile data </locator> <locator type=" id "timeout=" 3 "value=" Dji.pilot:id/mine_settings_develop _switch "> Turn on USB debugging </locator> <locator type=" id "timeout=" 3 "value=" Dji.pilot:id/mine_settings_reset_guid E_button "> Reset Novice Guide </locator> <locator type=" id "timeout=" 3 "value=" Dji.pilot:id/mine_settings_clean_cach E_button "> Clear data Cache </Locator> <locator type= "id" timeout= "3" value= "Dji.pilot:id/mine_language_change" > Multi-language & Lt;/locator> <locator type= "id" timeout= "3" value= "Dji.pilot:id/mine_settings_privacy_button" > Privacy settings </lo Cator> <locator type= "id" timeout= "3" value= "Dji.pilot:id/mine_settings_rate_app_button" > Give us a rating </locato R> <locator type= "id" timeout= "3" value= "Dji.pilot:id/mine_settings_sign_out_button" > Exit DJI Account &L T;/locator> <locator type= "id" timeout= "3" value= "Android:id/button2" > Cancel </locator> <loc Ator type= "id" timeout= "3" value= "Android:id/button1" > OK </locator> <locator type= "id" timeout= "3" Val
Ue= "Dji.pilot:id/mine_user_protocol" >djigo User Agreement </locator> </page> </map>
Analysis of Page.xml login page, corresponding to the Pagename= "LoginPage", the corresponding element name has "Login Input Account Box", "Login Input Password box", "Login button" and corresponding positioning method and wait time. This is not clear, and later if the page element property changes, only with the above configuration file can be changed. As for the reading of the XML file, it is mentioned in the third chapter that the read information is saved in the HashMap collection Locatormap by Xmlutils. Summary
The bottom line is that the page and element are encapsulated in two classes, and then the corresponding data is extracted and placed in an XML file for management.
Object Library greatly improve the testing framework of reusability and maintainability, in the test framework plays a central role, here is just my approach, I believe there is a more concise and easier way, waiting for everyone to dig.
The next chapter focuses on the automated testing Framework (V): Business layer and use case layer, please look forward to.