2011-06-24 49 views
1

當我嘗試運行連接到SQL數據庫的簡單Selenium測試時遇到問題。測試不會運行,它似乎在編譯時失敗,但不會提供有關遇到錯誤的位置的任何信息。Selenium SQL數據庫連接

我已經調查了這個http://automationtricks.blogspot.com/2010/05/how-to-pass-parameters-to-junit-or.html和Google羣組,但無法弄清楚。

這裏的代碼,希望有人能指出我在正確的方向。謝謝!

package com.XXX.Tests; 

import java.sql.*; 
import java.sql.Connection; 
import org.junit.Test; 
import org.testng.annotations.BeforeClass; 
import com.thoughtworks.selenium.*; 

import org.openqa.selenium.server.SeleniumServer; 


public class SeleniumandDB extends SeleneseTestBase { 

    @BeforeClass 
    public void setUp()throws Exception { 

     SeleniumServer seleniumServer=null; 
     try { 
       seleniumServer = new SeleniumServer(); 
       seleniumServer.start(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
        } 

        selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://wwww-test/"); 
        selenium.start(); 
        } 




    @Test public void testUntitled2() throws Exception { 
     String userID = null; 
     Connection conn=null; 
     Statement stmt=null; 
     ResultSet rs=null; 


     selenium.open("/"); 
     selenium.windowFocus(); 
     selenium.windowMaximize(); 

       Class.forName("net.sourceforge.jtds.jdbc.Driver"); 
       conn = DriverManager.getConnection("jdbc:jtds:sqlserver://XXXX:1433/XXX","XX","XXXX"); 
       stmt = conn.createStatement(); 
       rs = stmt.executeQuery("SELECT TOP 1 UserID FROM webuser ORDER BY 1 DESC"); 
        while(rs.next()){ 
          userID = rs.getString("UserID"); 
          conn.close(); 
          System.out.println(userID); 

     selenium.type("txtUserID", userID); 
     selenium.type("txtPassword", "password"); 
     selenium.click("btnLogin2"); 
     selenium.waitForPageToLoad("30000"); 
     selenium.stop(); 


    } 
    } 
} 

回答

1

試試這個,它應該工作。 1)去除 - 擴展SeleneseTestBase,然後使用JUnit (因爲從JUNIT @Test註解將扮演的角色)

OR

2)只是刪除@Test註釋和覆蓋設置()和一些其他方法運行like start() selenesetestbase類的方法並使用JUNIT容器運行該類。

3)或僅使用TESTNG。 (不擴展類並使用註釋)

我可以從你的解釋看到的問題和你的代碼, 要導入2個容器(TestNG的和Junit4),並要擴展測試類 隨着Junit3容器。

而定義的測試用例Junit4容器(通過使用@Test) 但使用junit3 container.so junit3需要這些默認的方法來了解哪一個 是安裝程序和方法運行測試類是testcase.but你是不supplieg這樣的信息 Junit3容器是簡單地運行你的類沒有任何測試用例。

我希望這將清除問題。但我可能會錯,因爲我使用TestNG並且不擴展任何類。

+0

輝煌。我實際上在另一個軟件包中重寫了腳本,並且它工作正常。我仍然感到困惑,因爲這個爲什麼不起作用。我沒有注意到我正在導入兩個容器。謝謝! – BPK

0

您確定您的classpath中有net.sourceforge.jtds.jdbc.Driver嗎?

+0

是的。我可以在同一個包中從另一個類執行查詢。僅查詢,它不包含TestNG框架。 – BPK

1
package DBCONN; 

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

public class DBCONN { 

    public static void main(String[] args) throws Exception { 
     DBCONN dbconn = new DBCONN(); 
     dbconn.open(); 
     dbconn.run(); 
     dbconn.close(); 
    } 

    // Connection object 
    static Connection con = null; 
    // Statement object 
    private static Statement stmt; 
    // Constant for Database URL 
    public static String DB_URL = "jdbc:oracle:thin:@hostname:Port#:SID"; 
    // Constant for Database Username 
    public static String DB_USER = "username"; 
    // Constant for Database Password 
    public static String DB_PASSWORD = "password"; 

    public static String query = "SELECT * FROM TABLE; 

    public void open() throws Exception { 
     try { 
      // Make the database connection 
      String dbClass = "oracle.jdbc.OracleDriver"; 
      System.out.println("Connecting to database"); 
      Class.forName(dbClass).newInstance(); 
      // Get connection to DB 
      con = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD); 
      // Statement object to send the SQL statement to the Database 
      System.out.println("Connected to the database"); 
      stmt = con.createStatement(); 
     } catch (Exception e) { 
      con.close(); 
      System.out.println("Closed connection to the database"); 
      e.printStackTrace(); 

     } 
    } 

    public void run() throws Exception { 
     try { 

      ResultSet res = stmt.executeQuery(query); 
      res.next(); 
      System.out.print(res.getString(1)); 

      System.out.print("\t" + res.getString(2)); 

      System.out.print("\t" + res.getString(3)); 

      System.out.println("\t" + res.getString(4)); 

     } catch (Exception e) { 
      con.close(); 
      System.out.println("Closed connection to the database"); 
      e.printStackTrace(); 

     } 
    } 

    public void close() throws Exception { 
     try { 

      con.close(); 
      System.out.println("Closed connection to the database"); 

     } catch (Exception e) { 
      System.out.println("Error closing connection to the database"); 
      e.printStackTrace(); 

     } 
    } 
}