Encapsulation of the Java Webdriver API

Source: Internet
Author: User

Let's take a look at the code written by the official website, which is the original writing:

Driver.findelement (By.id ("kw")). Click ()

This writing is not any problem, but this does not put the element object, data, business logic, not to achieve the effect of loading force, in order to make these three aspects of separation, we first analyze This code: Here is related to three objects: Driver object, by object, Webelement object. Since most people now use the so-called Page-object model, please refer to my other blog:

Http://www.cnblogs.com/zhangfei/p/3456159.html

This achieves the effect of separating the element object from the script, and finally the above code can be changed to this:

Page.getelement ("Baidu button"). Click ();

After this, it seems to be very good, but some people want to click on the automatic output log, but the click Is Webelement method, and no interface, and do not allow inheritance, can not be done, it is possible to use the AOP framework to inject, or byte code injection way, both ways, I've just heard it, it's not going to work at all. Let's take a look at the methods most people use:

        public void Click (String key) {webelement element = this.getelement ("key"); System.out.println ("Click the:" +element.tostring ()); Element.click ();}        Page.click ("Baidu button");    

This also seems to have achieved the effect of log auto output, but is this good for you? Isn't it disgusting? Do you have an object-oriented feeling? The click is an action, originally is to find the object to click, you write, is to send a click action, and then go to look for objects, too disgusting, can not accept AH. After a woman who had read my document, I processed it, and it was this way of packing:

1. First write a Extendwebelement class: The API for storing all the Webdriver

Package Com.test.base;import Org.openqa.selenium.webelement;public class Extendwebelement {private webelement element ;p ublic void SetElement (webelement element) {this.element = element;} public void Click () {System.out.println ("click the:" +element.tostring ()),/** * do something */element.click ();/** * does so mething */}}

2. In the locator class (that is, the Deml class in the Element management chapter), add

Private Extendwebelement extwebelement;public Locator (webdriver driver) {                this.driver = driver;        Extwebelement = new Extendwebelement ();    }

3. Change the GetLocator method:

Private Extendwebelement GetLocator (String key, string[] Replace, Boolean wait) {webelement element = null;            if (Ml.containskey (key)) {map<string, string> m = Ml.get (key);            String type = M.get ("type");            String value = M.get ("value");            if (replace! = null) value = this.getlocatorstring (value, replace);            by by = This.getby (type, value);                if (wait) {element = This.watiforelement (by);                                 Boolean flag = this.waitelementtobedisplayed (element);            if (!flag) element = null;                } else {try {element = Driver.findelement (by);                } catch (Exception e) {element = null;  }}} else Log.loginfo ("Locator" + key + "is not exist in" + Yamlfile +        ". Yaml"); Extwebelement.setelement (element);    return extwebelement; }

4. The final invocation method is also:

Page.getelement ("Baidu button"). Click ();

And you can add in the code you want to add, the final script, no need to change anything ... It's a fucking blast!

Of course, this also has a weakness, is to use the common Webdriver API is encapsulated in this extendwebelement class ...

Encapsulation of the Java Webdriver 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.