【问题标题】:Selenium SQL Database ConnectionSelenium SQL 数据库连接
【发布时间】:2011-06-24 02:32:15
【问题描述】:

当我尝试运行连接到 SQL 数据库的简单 Selenium 测试时遇到了问题。测试将不会运行,它似乎在编译时失败,但没有提供任何有关错误发生位置的信息。

我已经查看了这个 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();


    }
    }
}

【问题讨论】:

    标签: java sql selenium testng


    【解决方案1】:

    试试这个。它应该可以工作。 1) 删除 - 扩展 SeleneseTestBase 然后使用 junit 运行 (因为来自 JUNIT 的 @Test Annotation 将发挥作用)

    2) 只需删除 @Test 注释并覆盖 setUp() 和其他一些方法,如 start() selenesetestbase 类的方法并使用 JUNIT 容器运行该类。

    3) 或仅使用 TESTNG。 (不要扩展类并使用注释)

    从您的解释和您的代码中我可以看到的问题是, 您正在导入 2 个容器(TestNG 和 Junit4)并且您正在扩展 Test 类 使用 Junit3 容器。

    并用 Junit4 容器定义了测试用例(使用@Test) 但是使用 junit3 container.so 运行测试类 junit3 需要那些默认方法来理解哪一个 是 setUp 并且哪个方法是测试用例。但是您没有提供此类信息 Junit3 容器只是在没有任何测试用例的情况下运行您的类。

    我希望这能解决问题。但我可能错了,因为我使用 TestNG 并且不扩展任何类。

    【讨论】:

    • 太棒了。我实际上在另一个包中重写了脚本并且它起作用了。我仍然很困惑为什么这个不起作用。我没有注意到我正在导入两个容器。谢谢!
    【解决方案2】:
    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();
    
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      您需要在类路径中添加 net.sourceforge.jtds.jdbc.Driver。

      【讨论】:

      • 是的。我可以从同一个包中的另一个类执行查询。仅查询,不包含 TestNG 框架。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-27
      • 1970-01-01
      • 2013-07-08
      • 2016-11-03
      • 1970-01-01
      相关资源
      最近更新 更多