java Selenium SQL 数据库连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6462725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:58:45  来源:igfitidea点击:

Selenium SQL Database Connection

javasqlseleniumtestng

提问by BPK

I'm running into an issue when I try to run a simple Selenium test that connects to a SQL database. The test will not run, it seems to fail on compiling but does not provide any information on where the error was encountered.

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

I've looked into this http://automationtricks.blogspot.com/2010/05/how-to-pass-parameters-to-junit-or.htmland Google groups but can't figure it out.

我查看了这个http://automationtricks.blogspot.com/2010/05/how-to-pass-parameters-to-junit-or.html和 Google 组,但无法弄清楚。

Here's the code, hope someone can point me in the right direction. Thanks!

这是代码,希望有人能指出我正确的方向。谢谢!

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();


    }
    }
}

采纳答案by sudarsan

try this one.it should work. 1) remove - extends SeleneseTestBase and then run with junit (because @Test Annotation from JUNIT will play the role)

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

OR

或者

2)just remove @Test annotation and override setUp() and some other methods like start() Of selenesetestbase class methods and run the class using JUNIT container.

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

3)OR use TESTNG only. (do not extend class and make use of ANNOTATIONS)

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

The problem I could see from your explanation and your code is, You are importing 2 containers (TestNG and Junit4) and you are extending the Test class With Junit3 container.

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

And defined the test cases with Junit4 container( by using @Test) But running the test class using junit3 container.so junit3 needs those default methods to understand which one Is setUp and which method is a testcase.but you are not supplieg such info Junit3 container is simply running your class without any testcases.

并使用 Junit4 容器定义了测试用例(通过使用@Test)但是使用 junit3 容器运行测试类。所以 junit3 需要那些默认方法来了解哪个是 setUp 哪个方法是测试用例。但是你没有提供这样的信息 Junit3容器只是在没有任何测试用例的情况下运行您的类。

I hope this will clear the issue.but I might be wrong as I use TestNG and dont extend any classes.

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

回答by Preet

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();

        }
    }
}

回答by Felipe Knorr Kuhn

Are you sure you have net.sourceforge.jtds.jdbc.Driver in your classpath?

你确定你的类路径中有 net.sourceforge.jtds.jdbc.Driver 吗?