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