2011-06-24 16 views
1

SQLデータベースに接続する単純なSeleniumテストを実行しようとすると、問題が発生しています。テストは実行されません。コンパイル時に失敗するようですが、エラーが発生した場所に関する情報は提供されません。Selenium SQL Database Connection

私はこの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)削除 - (ちょうど@Test注釈を削除し、セットアップを上書き)

OR

2)SeleneseTestBaseを拡張して、JUNITから@Testアノテーションが役割を果たしますので、JUnitの (と実行)といくつかの他の方法start() のようなselenesetestbaseクラスのメソッドとJUNITコンテナを使用してクラスを実行します。

3)またはTESTNGのみを使用してください。私はあなたに説明します。コードから見ることができた問題は、あなたが2個のコンテナ(TestNGのとJunit4)をインポートしていて、Junit3コンテナでテストクラス を拡張している 、ある

を(クラスを拡張し、アノテーションを使用しません) 。

そして しかし、1 を設置しているかを理解するために、これらのデフォルトの方法を必要とし、方法junit3 junit3のcontainer.soを使用してテストクラスを実行している(@Testを使用して)Junit4コンテナでテストケースを定義したがtestcase.butあなたですそのような情報を提供していません Junit3コンテナはテストケースなしでクラスを実行しています。

私はこれが問題をクリアすることを願っています。しかし、私はTestNGを使用しているので、間違いかもしれません。

+0

ブリリアント。私は実際に別のパッケージにスクリプトを書き直しました。私はまだこの1つがうまくいかない理由として困惑していました。私は2つのコンテナを輸入していることに気付かなかった。ありがとう! – BPK

0

クラスパスに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(); 

     } 
    } 
}