The principle and implementation of automatic test keyword driving

Source: Internet
Author: User

Reprint Address: http://www.cnblogs.com/zhangfei/p/5330994.html


Automated testing is now becoming more and more platform-oriented, platform is committed to work together to improve efficiency, so that more people participate in the process of automation, in my opinion, the platform, there is a more critical point, is the keyword driver, only the automated testing of the code into a more easy to understand the natural language, In order to let more people do not understand the code to join in order to achieve the purpose of platform. Today we're going to talk about the principle and implementation of keyword-driven in automated testing.

First look at an example, the teacher said to the students: go to the table from a location to the B location. When the teacher issued this order, found that no one moved, because there is no clear object, do not know who the order is issued to whom, so the teacher said again: Zhang San, to the table from a location to the B location. At this time, with a definite object-Zhang San, the subject of the command-move the table from a to B. So the phrase "Zhang San, to move the table from location A to place B," There are two keywords: Zhang San, the table from a location to the B location. When these two keywords are missing any one, it is impossible to complete this thing. So, we can think of this scenario as a keyword driver, if we convert the above sentence into code:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 public class Movedesk {           private person Zhangsan;    &N bsp;   public void Setzhangsan (person Zhangsan) {         this. Zhangsan = Zhangsan;     }        public void movedeskfroma2b () {          Zhangsan.getthecommandfromteacher ();          Zhangsan.findthedeskata ();          Zhangsan.movedesktob ();          zhangsan.notifyteacherhadfinished ();     }        public static void Main (string[] args) {   & nbsp;     Movedesk MD = new Movedesk ();          Md.setzhangsan ("Zhang San");          md.movedeskfroma2b ();     }  }

The above code, of course, is pseudo-code, but it can be seen that in order to achieve a purpose, the process is the same, the two keywords in the main method is reflected as follows: Zhang San corresponds to Md.setzhangsan ("Zhang San"), "the table from a location to the B location" Corresponds to MD.MOVEDESKFROMA2B (). Thus we can understand this: Each keyword corresponds to a specific method. So, summarize the implementation features of the keyword driver:

1. Each keyword corresponding to the method (hereinafter referred to as the key method) must be able to be called at any time, as the teacher's command to Zhang San Zhang San must be executed. In Java in order to achieve this key method can be called at any time, its class object must be new good, so there is a pool of objects, this object pool like a class, there are many students can be issued by the teacher command and to execute.

2. Keywords must be associated with key methods, just like the teacher let Zhang San move the table, and Zhang San ran to buy a pack of cigarettes, which is not right, so, must have a keyword and the key method of the mapping table.

3. There must be a keyword parsing method, such as the above command, we want to be able to draw Zhang San, the table from a location to the B location of the two keywords, if not to derive the keyword, the key method will be less executed, as in the above pseudo-code, if the Md.setzhangsan ("Zhang San") This sentence, obviously will be reported null pointer abnormal.

4. The above three has been implemented, the teacher issued an order, Zhang three to be able to carry out, of course, if Zhang three is a thorn head said otherwise. Java code can be handed to the JVM to execute, but after converting to a keyword, the JVM does not recognize these keywords, so we also have to have a robust execution engine, so that "Zhang San, to move the table from a location to the B location," This sentence can be executed successfully.

To sum up, as long as we put the above four through the code to achieve, a simple keyword framework of the prototype came out, the next one of the implementation of the strip.

1. Object pool:

1 2 3 4 5 6 7 8 public class Registercenter {public static map<string, object> obj_pools = new hashmap<string, Object            > ();      static {Obj_pools.put (Movedesk. class. GetName (), New Movedesk ()); } }

Initialize all classes that contain the critical methods, and then place them in the Obj_pools object, and call the key methods directly behind them. (Please try to use annotations to achieve, will be more dazzling)

2. Mapping Table:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public class Keywordreflect {           public static map<string, map& Lt String, string>> keyword_pools = new hashmap<string, map<string, string>> ();            Static {         Keyword_pools.put ("Zhang San", Keywordreflect.methodinfo (Movedesk. Class GetName (), "Setzhangsan"));          Keyword_pools.put ("Move the table from a to B location", Keywordreflect.methodinfo ( Movedesk. Class. GetName (), "movedeskfroma2b"));     }            public static map<string, String> methodInfo (String className, String methodName) {         map< String, string> methodInfo = new hashmap<string, string> ();          methodinfo.put ("Class", className);          methodinfo.put ("method", MethodName);          return methodInfo;     }      }

Description: The data structure of the Keyword_pools object above is a map nested in the map, this is because to clarify the class object, because different classes may have the same name method, so in order to avoid confusion, must be labeled good one method class and method name, so as to establish a good one by one mapping table. (also can be implemented with annotations)

3. Parsing keywords

In order to be able to parse out the keywords, we have to "Zhang San, go to the table from a location to the B location," The keyword to mark out, to change the "${Zhang San}, to ${the table from a location to the B place}", so that the keyword to clear the label out, although from the intuitive feeling of a little bit worse, But it's a very important part of the keyword drive. The next question turns into a parsing of ${} in a string:

Regular class:

1 2 3 4 5 6 7 8 9 ten-in-one public class RegExp {    & nbsp      public boolean match (String Reg, String str) {          Return pattern.matches (Reg, str);     }         public list<string> Find (string reg, String str) {         Matcher Matcher = Pattern.compile (reg). Matcher (str);           list<string> List = new arraylist<string> ();          while (Matcher.find ()) {              List.add (Matcher.group ());         }          return list;     }       }

Get the keyword class:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 public class Parsekeyword {           list<string> GetKeywords (String p) {         string reg = "(? <= (? <!\\\\) \\$\\{) (. *?) (? = (? <!\\\\) \ \}) ";               RegExp re = new RegExp ();          list<string> List = Re.find (Reg, p);          return list;     }            public static void main (string[] args) {         Parsekeyword p = new Parsekeyword ();           System.out.println (P.getkeywords ("A${a}a"));          System.out.println (P.getkeywords ("A\\${a}a"));          System.out.println (p.getkeywords ("A${a\\}a" ));          System.out.println (P.getkeywords ("A${a\\}a}a"));          System.out.println (P.getkeywords ("a${a}a${"));          System.out.println (P.getkeywords ("A${ab}a${a}");     }}

Note: The use of the regular pre-check mode, the regular pre-check mode is a relatively high-level usage of the regular, after mastering this, your regular will be a step.

4. Execution Engine:

The execution engine first to find the need to execute the statement, so, the teacher should first send the command to read out, this command can be stored in any format file, as long as can be read out, here we save in Command.txt:

Read this command:

1 2 3 4 5 6 7 8 9

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.