This article has participated in the third “High Yield more text” track of the Denver Creators Training Camp, check out the details:Digg project | Creator Boot Camp phase 3 is underway, “write” to make a personal impact.

The introduction

I’ve been using and learning Selenium for a long time. I’ve heard a lot about PO design patterns (including FactoryPage) and THE BDD behavior driver framework. In terms of keyword driven framework design, I didn’t take the time to sort out the difficulties in understanding Java basic technology, so I ran aground. Now I pick it up again, just to remind myself: nothing is difficult in the world if you put your heart into it!

Framework thought

The keyword framework mainly uses the reflection mechanism, that is, to read the keyword < it is the specific method we define in the class > in the external data media, and then find the corresponding method in the running class through dynamic search and invoke execution. Then your use case execution is complete.

Layers of in-depth

  1. Java reflection mechanism, principle

This is a Java technical difficulty, how to understand the reflection mechanism and implementation?

  • What is reflection

Reflection is the mapping of components of a Java class to Java objects. That is, in the running state, all the attributes and methods of any class can be known; For any object, you can call any of its methods and properties. This ability to dynamically retrieve information and invoke object methods is called Java's reflection mechanism.

  • Classes that implement reflection mechanisms

The Java reflection mechanism is implemented primarily by the following classes (all of which are in the java.lang.Reflect package) :

  • Class: Represents a Class.

  • Field class: Represents the member variables of the class (also known as attributes of the class).

  • Method class: A Method that represents a class.

  • Constructor class: represents the Constructor of a class.

  • Array class: Provides static methods for dynamically creating arrays and accessing their elements.

  • The Class Class is the core Class in Reflection API. The main methods are as follows:

    • GetName () : Gets the full name of the class.
    • GetFields () : Gets a property of the class’s public type.
    • GetDeclaredFields () : Gets all attributes of the class.
    • GetMethods () : Methods to get the public type of a class.
    • GetDeclaredMethods () : Gets all methods of a class.
    • GetDeclaredMethods (String name,Class[] parameterTypes) : Gets a specific method for a Class. The name parameter specifies the name of the method and the parameterTypes parameter specifies the parameter type of the method.
    • GetMethod (String name, Class[] parameterTypes) : getMethod(String name, Class[] parameterTypes) : getMethod(String name, Class[] parameterTypes) : getMethod(String name, Class[] parameterTypes) : getMethod(String name, Class[] parameterTypes) : Gets a specific method for a Class.
    • Getconstrate () : Get the constructor of the class’s public type.
    • GetConstrutor (Class[] parameterTypes) : Obtain a particular constructor of a Class, and the parameterTypes parameter specifies the parameter type of the constructor.
    • NewInstance () : Creates an object of the class through its parameterless constructor.
  • Gets the Class object

There are three ways to get a Class object in reflection.

  • Reflection Demo Example

Start with a class that has methods, variables, and so on Write another test class that, as another class, calls the Reflect class through reflection

  1. Encapsulate the keyword operation object

Preset a test scenario: open the browser -- enter the address -- click the login entrance -- enter the account -- enter the password -- click the login button -- click exit; A simple process. How do you encapsulate the corresponding keyword methods? Same as using the PO design pattern, but this time reading data from Excel to identify keywords and execute test cases. First of all, the two operations of click login should be the same, both give you an element location to click the trigger event, so the input account/password should be the same method:

When designing a generic keyword method, shouldn't it be fixed in the lookup element method: by.xpath, when there is a better element, it has to be xpath? Obviously this is not wise, so we need to encapsulate a general method of finding elements, which requires a return to a rule where the convention is greater than the configuration, namely the convention rule, and those that do not conform to the rule are discarded or not dealt with:

The click and enter keyword operations are optimized again based on this general method of finding elements: And so on: encapsulate whichever keyword you need.

  1. Design keyword driven framework optimization scheme

Just for the record: there are some keyword-driven frameworks implemented in open source projects, but their design seems somewhat flawed to me:

  • There is nothing wrong with separating the data from the script: but don’t separate the steps from the elements, which means maintaining excel test cases and element object files; Is it possible for me to represent both the operation object and the input of elements and values in Excel?

// Input [@class='input_username']; //input[@class='input_username']; //input[@class='input_username'] It's obviously going to fail, although you can do splicing, but it's pretty low;

  • It is not recommended to rename the same operation: for example, clicking on the A label, button, or clicking on any jump is just a point on an element; So you don’t need click_login\click_logout\click_butto and so on; The similar operation is input_username\input_passwd.
  • There’s no need to duplicate ways of encapsulating the same functionality, and there’s no need to maintain more files, just one Excel: elements and text < use semicolons; Split, example: id; Kw >; Use cases that specifically require override values can be covered in other ways;
  • In the Excel use case above, there is one more thing that needs to be addressed: encapsulating a common method for finding elements, splitting the Element column so that finding elements in different ways is more flexible;
  • For the design of the framework itself, continue to test the framework using Testng, creating Testsuites and listening to generate HTML reports: ‘

conclusion

This article first throws the brick, gives the idea and the scheme, carries on the encapsulation to the part function code, also carried on the understanding and the code demonstration to the Java reflection mechanism. Implementation of Selenium keyword driven throughout will be covered later.