SELENIUM+JDBC Implementing parameter Automation testing

Source: Internet
Author: User
Tags stmt xpath

Test the simulation environment: perform two user login and exit operations in the Www.1905.com website

The required documents are:

1, the User instance class:

 Public classUser {PrivateString username; PrivateString password;  PublicString GetUserName () {returnusername; }     Public voidSetusername (String username) { This. Username =username; }     PublicString GetPassword () {returnpassword; }     Public voidSetPassword (String password) { This. Password =password; }    }

2, Dbutil

 Public classMysqlutil { Public StaticConnection getconnection () {Connection conn=NULL; Try{class.forname ("Com.mysql.jdbc.Driver"); Conn= Drivermanager.getconnection ("Jdbc:mysql://localhost:3306/sy", "Root", "1905"); } Catch(Exception e) {e.printstacktrace (); }        returnConn; }     Public Static voidClose (Statement stmt,connection conn) {if(conn!=NULL){            Try {                if(stmt!=NULL) {stmt.close ();            } conn.close (); } Catch(Exception e) {e.printstacktrace (); }        }    }}
View Code

3, Userdao

 Public classUserdao { PublicList<string> usernamelist ()throwsexception{String username; List<String> usernamelist=NewArraylist<string>(); Connection Conn=mysqlutil.getconnection (); Statement Stat=conn.createstatement (); ResultSet rst= Stat.executequery ("SELECT * from User");        SYSTEM.OUT.PRINTLN (RST);  while(Rst.next ()) {username= rst.getstring ("name");        Usernamelist.add (username);        } System.out.println (Usernamelist); returnusernamelist; }     PublicList<string> passwordlist ()throwsexception{String password; List<String> passwordlist=NewArraylist<string>(); Connection Conn=mysqlutil.getconnection (); Statement Stat=conn.createstatement (); ResultSet rst= Stat.executequery ("SELECT * from User");  while(Rst.next ()) {Password= rst.getstring ("Password");        Passwordlist.add (password);        } System.out.println (Passwordlist); returnpasswordlist; }    }
View Code

4. JUnit Test class

 Public classLoginjdbctest {Private StaticWebdriver Driver; Private StaticNavigation Navigate; Private StaticString url= "Http://www.1905.com"; Private StaticString filesrc = "Userandpassword.xls"; @BeforeClass Public Static voidSetupbeforeclass ()throwsException {//Load BrowserDriver =NewFirefoxdriver (); Navigate=driver.navigate ();        Navigate.to (URL);    Driver.manage (). window (). Maximize (); } @AfterClass Public Static voidTeardownafterclass ()throwsException {if(driver!=NULL) {driver.close ();        Driver.quit (); }} @Test Public voidTest ()throwsException {Userdao user=NewUserdao (); List<String> usernamelist =user.usernamelist (); List<String> passwordlist =user.passwordlist (); intUsersize =usernamelist.size ();  for(inti=0;i<usersize;i++){            NewWebdriverwait (driver,15). Until (Expectedconditions.presenceofelementlocated (By.xpath (".//*[@id = ' SITE_NAV_MD ']/ ul/li[2]/a "))); Try{Thread.Sleep (8000); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } webelement Logandreg= Driver.findelement (By.xpath (".//*[@id = ' site_nav_md ']/ul/li[2]/a"));            Logandreg.click (); Webelement username= Driver.findelement (By.xpath (".//*[@id = ' inputusername ']")); Webelement Password= Driver.findelement (By.xpath (".//*[@id = ' Inputpassword ']")); Webelement Login= Driver.findelement (By.xpath (".//*[@id = ' Loginreg ']/div/div[1]/form/p/button"));            Username.clear (); String name=Usernamelist.get (i);            Username.sendkeys (name); //enter the corresponding password value             for(intJ=0;j<passwordlist.size (); j + +) {password.clear (); String Pass=Passwordlist.get (j);            Password.sendkeys (pass);            } login.click (); webdriverwait wait=NewWebdriverwait (driver,10); Webelement Logoutbutton= Wait.until (Expectedconditions.elementtobeclickable (By.xpath (".//*[@id = ' site_nav_md ']/ul/li[3]/a[2]")));        Logoutbutton.click (); }    }}
View Code

The above code is to give yourself a record, some of which can continue to improve the package, the rest of the perfect after the ~ ~

SELENIUM+JDBC Implementing parameter Automation testing

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.