Selenium keyword driver for UI Automation test framework

Source: Internet
Author: User

First, principle and characteristics 1. Keyword-driven testing is an improved type of data-driven testing 2. The main keywords include the three classes: the Manipulated object (Item), the Action (operation), and the value, which can be represented as item.operation (value) 3 in Object-oriented form. The test logic is decomposed by these keywords to form a data file. 4. The test logic is encapsulated in a data file in the form of a keyword, and the test tool can be used to automate it as long as it interprets these keywords. Prepare to use the tool: third-party jar package for Eclipse: Poi.jar (Operation Excel); Selenium.jar Understanding the difficulty: the Java reflection mechanism; gradual layering iii. framework conception 1. Write a script first we write a script that logs on to open source China public class Login_script {public static webdriver driver =null; public static void Main (String []agrs) throws interruptedexception{//start Firefox driver= new Firefoxdriver ();//Maximize Driver.ma Nage (). window (). Maximize ();//Open Open source China URL driver.get ("http://www.oschina.net/");//Click Login driver.findelement (By.xpath (" *[@id = ' Osc_userbar ']/a[1]). Click ();//Enter the user name Driver.findelement (by.xpath ("//*[@id = ' f_email ']"). SendKeys (" Xxxxxxb "),//Enter the password driver.findelement (By.xpath ("//*[@id = ' f_pwd ') "). SendKeys (" Xxxxxxxa ");//Click the login button// Driver.findelement (By.xpath ("//*[@id = ' Login_osc ']/table/tbody/tr[7]/td/input")). Click ();//Thread.Sleep (30);// Click the Exit button Driver.findelement (By.xpath ("//*[@id = ' Osc_userbar ']/a[3]"). Click ();//Close Browser DrivEr.quit (); }}2, script Analysis This is the landing scene operation Step one step: Start the browser The second step: Enter the URL Fourth step: Click Login Fifth Step: Enter the user name Sixth step: Enter the password Seventh step: Click the Login button eighth step: Click to exit the Nineth step: Close the browser 3, Using Excel to create an Excel class in Java that operates in Excel, the main implementation is read and write to Excel, the main code is as follows: public class Excelutils {public static Hssfsheet ExcelSheet; public static Hssfworkbook Excelbook; public static Hssfrow Row; public static Hssfcell Cell; public static void Setexcelfile (String path,string sheetname) throws exception{FileInputStream excelfile=new FileInputStream (Path); Excelbook=new Hssfworkbook (Excelfile); Excelsheet=excelbook.getsheet (SheetName); public static void Setcelldata (String Result, int RowNum, int colnum,string Path) throws exception{Row = Excelsheet.get Row (RowNum); Cell = Row.getcell (Colnum, row.return_blank_as_null); if (cell = = null) {cell = Row.createcell (Colnum); Cell.setcellvalue (Result); } else {cell.setcellvalue (Result);} FileOutputStream fileout = new FileOutputStream (Path); Excelbook.write (fileout); Fileout.flush (); Fileout.close (); } public static String getcelldate (int rownum,int Clonum) {Cell=excelsheet.getrow (RowNum). Getcell (Clonum); String Celldata=cell.getstringcellvalue (); return celldata; }}4, create a new Actionkeywords class public class Actionkeywords {public static webdriver driver=null;//start the browser and maximize public static void O Penbrowser () {driver= new Firefoxdriver (); Driver.manage (). window (). Maximize (); Open Open Source China Web site public static void Navigate () {driver.get ("http://www.oschina.net/");} Click Login public static void Login_click () {driver.findelement (By.xpath ("//*[@id = ' Osc_userbar ']/a[1]"). Click (); Enter the user name public static void Input_name () {driver.findelement (By.xpath ("//*[@id = ' f_email ']"). SendKeys ("Xxxxxxa");} Enter the password public static void Input_password () {driver.findelement (By.xpath ("//*[@id = ' f_pwd ']"). SendKeys ("Xxxxxxb");} Click the login button public static void Login_button () {driver.findelement (By.xpath ("//*[@id = ' Login_osc ']/table/tbody/tr[7]/td/ Input "). Click (); }//Click the exit button public static void Logout_click () {driver.findelement (By.xpath ("//*[@id = ' Osc_userbar ']/a[3]"). Click(); }//close the browser public static void CloseBrowser () {driver.quit ();}} 5. Modify the Login_script script. public class Login_script {public static void main (String []agrs) throws exception{Excelutils.setexc Elfile ("D:\\data\\testdata.xls", "Steps"); Actionkeywords actionkeywords= new Actionkeywords (); String Keywords=null; for (int rownum=1; Rownum<=excelutils.getlastrownums (); rownum++) {keywords=excelutils.getcelldate (RowNum, 3), if (Keywords.trim (). Equals ("Openbrowser")) { Actionkeywords.openbrowser (); }else if (Keywords.trim (). Equals ("Navigate")) {actionkeywords.navigate ();} else if (Keywords.trim (). Equals ("Login_click")) {Actionkeywords.login_click ();} else if (Keywords.trim (). Equals ("Input_name")) {Actionkeywords.input_name ();} else if (Keywords.trim (). Equals ("Input_password")) {Actionkeywords.input_password ();} else if (Keywords.trim (). Equals ("Login_button")) {Actionkeywords.login_button ();} else if (Keywords.trim (). Equals ("Logout_click")) {Actionkeywords.logout_click ();} else if (Keywords.trim (). Equals ("CLosebrowser ")) {Actionkeywords.closebrowser ();}} The framework of this code is basically already set up, the code structure is as follows: four, structure optimization 1, optimize the code in the Login_script class Note: Here is the reflection mechanism public class Login_script {public static Actionkeywords actionkeywords; public static String Keywords=null; public static method[] Method; Public Login_script () {actionkeywords= new actionkeywords (); Method=actionkeywords.getclass (). GetMethods (); static void Main (String []agrs) throws exception{excelutils.setexcelfile ("D:\\data\\testdata.xls", "Steps"); New Login _script (); for (int rownum=1; Rownum<=excelutils.getlastrownums (); rownum++) {keywords=excelutils.getcelldate (RowNum, 3); Login_action ();}} public static void Login_action () {for (int i=0;i Selenium keyword driver for UI Automation test framework

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.