2017-05-27 46 views
0

我需要從下面的方法調用一些字符串(如用戶名)。包名稱是login,數據庫方法是SQLConnector,測試用例是Firstlogin。我需要調用數據庫的Firstlogin連接使用,從它串在Firstlogin和Firstlogin過於執行查詢:在我需要使用數據庫連接在第二種方法從Java方法調用數據庫oracle連接到另一個方法

package login; 

import org.junit.After; 
import org.junit.Before; 
import org.junit.Test; 
import static org.junit.Assert.assertEquals; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.Statement; 

public class SQLConnector { 
static Connection con = null; 
private static Statement stmt; 
public static String DB_URL = "jdbc:oracle:thin:@10.96.0.65:1521:orcl"; 
public static String DB_USER = "POS_SOF"; 
public static String DB_PASSWORD = "POS_SOF"; 
    static String username; 

@Before 
public void setUp() throws Exception { 
     try{ 
       String dbClass = "oracle.jdbc.driver.OracleDriver"; 
       Class.forName(dbClass).newInstance(); 
       Connection con = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD); 
       stmt = con.createStatement(); 
       } 
       catch (Exception e) 
       { 
        e.printStackTrace(); 
       } 

} 
@Test 
public void test() { 
     try{ 
     String query = "select * from users where id = '45450'"; 

     String expectedEmpName = "test1234"; 
     ResultSet res = stmt.executeQuery(query); 
    while (res.next()) 
     { 
     username = res.getString("user_name"); 
     System.out.print(username); 
     assertEquals(expectedEmpName, username); 

     } 

     } 

     catch(Exception e) 

     { 

       e.printStackTrace(); 

     }  

} 
@After 
public void tearDown() throws Exception { 
     if (con != null) { 
     con.close(); 

     } 

} 

} :

package login; 

import static org.junit.Assert.assertEquals; 
import static org.junit.Assert.fail; 
import java.util.concurrent.TimeUnit; 
import org.junit.After; 
import org.junit.Before; 
import org.junit.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.interactions.Actions; 
import org.openqa.selenium.support.ui.Select; 
import login.SQLConnector; 


public class FirstLogin { 

private String baseUrl; 
private boolean acceptNextAlert = true; 
private StringBuffer verificationErrors = new StringBuffer(); 

@Before 
public void setUp() throws Exception { 
    System.setProperty("webdriver.chrome.driver", "D://DownLoads/chromedriver_win32/chromedriver.exe"); 
    driver = new ChromeDriver(); 
    baseUrl = "https://100.96.0.650:9443"; 
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
} 

@Test 
public void testAddAccount() throws Exception { 
    driver.get(baseUrl + "/POSAdminTool/AdminTool/SearchPOS.faces"); 
    driver.manage().window().maximize(); 
    driver.findElement(By.id("form1:usernameLabel")).clear(); 
    driver.findElement(By.id("form1:usernameLabel")).sendKeys(SQLConnector.username); 
    driver.findElement(By.id("form1:passwordLabel")).clear(); 
    driver.findElement(By.id("form1:passwordLabel")).sendKeys("1234"); 
    driver.findElement(By.id("form1:btn_login")).click(); 
    Thread.sleep(1000); 
    Actions action = new Actions(driver); 
    WebElement element = driver.findElement(By.cssSelector("html body table.mainTable tbody tr td p.menuItem a")); 
    action.moveToElement(element); 
    action.click(); 
    action.perform(); 
    driver.findElement(By.linkText("Add Account")).click(); 
    driver.findElement(By.id("addPOS:locationID")).clear(); 
    driver.findElement(By.id("addPOS:locationID")).sendKeys("9999"); 
    driver.findElement(By.id("addPOS:menu1")).clear(); 
    driver.findElement(By.id("addPOS:menu1")).sendKeys("10000"); 
    driver.findElement(By.id("addPOS:menuStatus1")).click(); 
    new Select(driver.findElement(By.id("addPOS:usageList"))).selectByVisibleText("Pharmacy"); 
    driver.findElement(By.id("addPOS:textareaDescription1")).clear(); 
    driver.findElement(By.id("addPOS:textareaDescription1")).sendKeys("New"); 
    driver.findElement(By.id("addPOS:addAcctTerminal")).click(); 
    new Select(driver.findElement(By.id("AddAcctTerminalData:statusList"))).selectByVisibleText("Active"); 
    new Select(driver.findElement(By.id("AddAcctTerminalData:TermList"))).selectByVisibleText("Point of Sale"); 
    driver.findElement(By.id("AddAcctTerminalData:textSN1")).clear(); 
    driver.findElement(By.id("AddAcctTerminalData:textSN1")).sendKeys("22-55-88"); 
    driver.findElement(By.id("AddAcctTerminalData:textPin1")).clear(); 
    driver.findElement(By.id("AddAcctTerminalData:textPin1")).sendKeys("1234"); 
    driver.findElement(By.id("AddAcctTerminalData:add")).click(); 
    driver.findElement(By.id("addPOS:textDailyLimit1")).clear(); 
    driver.findElement(By.id("addPOS:textDailyLimit1")).sendKeys("10000"); 
    driver.findElement(By.id("addPOS:textCreditLimit1")).clear(); 
    driver.findElement(By.id("addPOS:textCreditLimit1")).sendKeys("10000"); 
    driver.findElement(By.id("addPOS:button1")).click(); 
    assertEquals("Account Added Successfully", driver.findElement(By.id("AddAccountSuccess:CorrectMessage")).getText()); 
    String AddedAccount = "SELECT CODE FROM ACCOUNTS WEHER ID IN (SELECT MAX(ID) FROM ACCOUNTS)"; 
    String AccountCode = driver.findElement(By.id("AddAccountSuccess:AccountCode")).getText(); 
    System.out.print(AccountCode); 
    System.out.print(AddedAccount); 
    assertEquals(AddedAccount, AccountCode); 

} 

@After 
public void tearDown() throws Exception { 
    driver.quit(); 
    String verificationErrorString = verificationErrors.toString(); 
    if (!"".equals(verificationErrorString)) { 
     fail(verificationErrorString); 
    } 
} 

}

回答

0

你需要做出兩個改變:

  • 向位於@Before方法Connection對象非靜態
  • 刪除局部變量con的聲明,即改變

    Connection con = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);

    con = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);

聲明在一個局部變量方法(與實例變量名稱相同)sh允許實例變量,因此實例變量永遠不會被初始化。通過上述更改,con將在任何測試運行之前得到初始化,您將能夠以任何方法使用它。

+0

這裏是我做過什麼: 公共連接CON = NULL; con = DriverManager.getConnection(DB_URL,DB_USER,DB_PASSWORD); 但仍然是同樣的問題 –

0

試試這個:

import java.sql.SQLException; 

import oracle.jdbc.OraclePreparedStatement; 

    public class SQLConnector { 
    static final String DB_URL = "jdbc:oracle:thin:@10.96.0.65:1521:orcl"; 
    static final String DB_USER = "POS_SOF"; 
    static final String DB_PASSWORD = "POS_SOF"; 
     static String username; 

    @Before 
    public Connection setUp() throws Exception { 
    Connection connection = null; 

      try { 
       try { 

        Class.forName(JDBC_DRIVER); 

       } catch (ClassNotFoundException e) { 
        e.printStackTrace(); 
        return; 
       } 

       System.out.println("Oracle JDBC Driver Registered!"); 

        System.out.println("Status: \n - Connecting to database..."); 
        connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD); 
        System.out.println(" - You are connected to the " + DB_URL); 
        } 
        catch (Exception e) 
        { 
         e.printStackTrace(); 
        } 
     return connection; 
    } 

    @Test 
    public void test() { 
     Connection conn = setUp(); 
      try { 
       /*Oracle thin connection require OraclePreparedStatement class*/ 
       String query = "select * from users where id = '45450'"; 
       OraclePreparedStatement stmt = (OraclePreparedStatement) conn.prepareStatement(query); // is not necessary needed to pass query variable 

       String expectedEmpName = "test1234"; 
       ResultSet res = stmt.executeQuery(query); 
       while (res.next()) { 
        username = res.getString("user_name"); 
        System.out.print(username); 
        assertEquals(expectedEmpName, username); 
       } 

      } 
      catch (SQLException se) { 
       se.printStackTrace(); 
      } 
      catch (Exception e) { 
       e.printStackTrace(); 
      } finally { 
       try { 
        if (conn != null) 
         conn.close(); 
       } catch (SQLException se) { 
        se.printStackTrace(); 
       } 
      } 
    } 
相關問題