一、讀取配置文件的腳本以下:html
package TEST; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class GetProperties { public static Properties getproperties() { Properties pro=new Properties(); /*pro.setProperty("url", "https://soasadmin-stg.paic.com.cn/admin/admin/index.html"); pro.setProperty("username", "YANLI498"); pro.setProperty("userpwd", "iH6B8i9z");*/ // String Str1="hello 123"; // InputStream in=new FileInputStream // ("D:\\myselenium\\config.properties"); InputStream in=null; try { //in = new BufferedInputStream(GetProperties.class.getResourceAsStream("config.properties")); in = new BufferedInputStream(new FileInputStream("D:\\config.properties")); pro.load(in); }catch(Exception e){ e.getStackTrace(); }finally{ if(in!=null) try { in.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return pro; } }
二、瀏覽器驅動初始化腳本封裝以下:java
package TEST; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.ie.InternetExplorerDriver; import org.openqa.selenium.remote.DesiredCapabilities; public class OpenBrowser { public static WebDriver openBrowser() { // TODO Auto-generated method stub //System.setProperty("webdriver.gecko.driver","D:/myselenium/geckodriver.exe"); //WebDriver driver = new FirefoxDriver(); /*System.setProperty("webdriver.ie.driver","D:/myselenium/iedriver/IEDriverServer.exe"); DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer(); ieCapabilities.setCapability (InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true); WebDriver driver = new InternetExplorerDriver(ieCapabilities); */ System.setProperty("webdriver.chrome.driver","D:/myselenium/selenium-kuanjia2016/chromedriver_win32/chromedriver.exe"); WebDriver driver = new ChromeDriver(); //WebElement d=driver.findElement(By.name("百度 一下")); //d.click(); return driver; } }
三、鏈接數據庫,查詢SQL操做,及登陸操做等封裝腳本以下(這裏幾個方法都放一塊兒,也能夠另建一個COMMON公共的JAVA文件存放,再到其餘地方調用這些方法就行,實例只是方便才這樣寫):web
/** * */ /** * @author YANLI797 * */ package TEST; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; public class TestGetProperties{ public static void main(String[] args) { executeQuery("10511125"); //login(); } public static void login(){ Properties pro=GetProperties.getproperties(); String url=pro.getProperty("url"); String username=pro.getProperty("username"); String userpwd=pro.getProperty("userpwd"); WebDriver driver=OpenBrowser.openBrowser(); driver.get(url); driver.manage().window().maximize(); driver.findElement(By.className("loginName")).sendKeys(username); WebElement a=driver.findElement(By.className("loginPwd")); a.sendKeys(userpwd); driver.findElement(By.className("login")).click(); try { Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static Connection getConn(String url,String name,String pwd,String oracledriver){ Connection conn=null; try { Class.forName(oracledriver); conn=DriverManager.getConnection(url, name, pwd); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } public static void executeQuery(String userid){ Connection conn=null; Statement statment=null; PreparedStatement statmentPre=null; ResultSet result=null; Properties pro=GetProperties.getproperties(); /*String url=pro.getProperty("url"); String username=pro.getProperty("username"); String userpwd=pro.getProperty("userpwd"); */ String urlDB=pro.getProperty("PA18dburl"); String PA18dbuserDB=pro.getProperty("PA18dbuser"); String PA18sdbpswDB=pro.getProperty("PA18sdbpsw"); try{ //Class.forName(pro.getProperty("orcal.driverName"));//加載數據庫驅動 //conn=DriverManager.getConnection(urlDB, PA18dbuserDB, PA18sdbpswDB); conn=getConn(urlDB,PA18dbuserDB,PA18sdbpswDB,pro.getProperty("orcal.driverName")); //String sql="select * from t_accepted_customer_info i where i.userid = '10511125'"; String sql="select * from t_accepted_customer_info i where i.userid = '"+userid+"'"; System.out.println(sql); statment=conn.createStatement(); result=statment.executeQuery(sql); //statmentPre=conn.prepareStatement(sql); //System.out.println(result); while(result.next()){ System.out.println("id:"+result.getInt("id")+"\t"+"name:"+result.getString("custname")); } }catch(Exception e){ e.getStackTrace(); }finally{ try { if (conn!=null){ conn.close(); } if (statment!=null){ statment.close(); } if (result!=null) result.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }