我需要使用Selenium Webdriver(JAVA)连接到Oracle SQL Developer。怎么做?

时间:2018-08-03 06:27:32

标签: oracle selenium testing selenium-webdriver oracle-sqldeveloper

我在网上寻找了一些资源,他们使用的是无法在Oracle SQL Developer中找到的“ dbURL”。谁能为此提供一些适当而明确的解决方案?

1 个答案:

答案 0 :(得分:0)

如果您使用oracle来查找您的URL,则可以执行以下操作。有一个 tnsnames.ora 文件,用于定义数据库地址。 该文件通常位于 $ ORACLE_HOME / network / admin 中,并且由oracle客户端使用。这是一个示例tns条目:

ORA11 =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = hostname)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = ORA11)
    )
  )

从此条目中可以得出您的jdbc连接字符串为:

  

jdbc:oracle:thin:@hostname:1521:ORA11

如果您想通过Java波纹管连接到数据库,则是一个示例。首先,您必须具有某种数据库服务器,在这种情况下,Oracle将连接到该数据库服务器。如果您遇到任何麻烦是由于驱动程序造成的,

我使用maven,这是一个依赖关系,您可能需要:

<!-- https://mvnrepository.com/artifact/com.oracle/ojdbc14 -->
<dependency>
    <groupId>com.oracle</groupId>
    <artifactId>ojdbc14</artifactId>
    <version>10.2.0.4.0</version>
</dependency>

这是一个示例类,您需要调整:

public class ConnectToDatabse {

Connection conn = null;
Statement stmt = null;
ResultSet resultSet = null;
WebDriver driver;

@BeforeTest
public void SetUpConnection() throws SQLException, ClassNotFoundException {

    // Register JDBC driver (JDBC driver name and Database URL)
    Class.forName("oracle.jdbc.driver.OracleDriver");

    // Open a connection
    conn = DriverManager.getConnection("jdbc:oracle:thin:@//HOSTNAME:PORT/SERVICENAME","user", "password");

    System.setProperty("webdriver.chrome.driver", "<Path of Driver>\\chromedriver.exe");
    ChromeOptions options = new ChromeOptions();

    // Code to disable the popup of saved password
    Map<String, Object> prefs = new HashMap<String, Object>();
    prefs.put("credentials_enable_service", false);
    prefs.put("password_manager_enabled", false);
    options.setExperimentalOption("prefs", prefs);
    driver = new ChromeDriver(options);
    driver.get("<URL>");
}

这是一种测试:

@Test
public void testTable() {
    try {
        // Execute a query
        stmt = conn.createStatement();
        resultSet = stmt.executeQuery("select * from sampletable");

        // Get the all row of UI Table
        List<WebElement> lstTr = driver.findElement(By.id("grdData")).findElements(By.tagName("tr"));

        // Index for Row
        int rowCount = 0;

        // Count of Matched Column
        int matchColumnCount = 0;

        // Count of Matched Row
        int matchRowCount = 0;

        System.out.println("Row Count => " + lstTr.size());

        // Extract the data from Table
        while (resultSet.next()) {


        List<WebElement> lstTd = lstTr.get(rowCount + 1).findElements(By.tagName("td"));
        System.out.println("Cloumn Count => " + lstTd.size());

        for (int j = 0; j < lstTd.size(); j++) {
            String uiCell = lstTd.get(j).getText();
            System.out.println("UI Cell Data => " + uiCell);

            /*
            * (j + 1) in the resultSet => because index is start from 1
            * and here loop is starting from 0
            */
            String dbCell = resultSet.getString(j + 1);
            System.out.println("DB Cell Data => " + dbCell);

            // Comparison between both string
            if (uiCell.trim().equalsIgnoreCase(dbCell.trim())) {
                matchColumnCount++;
            }
        }

        if (matchColumnCount == lstTd.size()) {
            matchRowCount++;
            System.out.println("========ROW MATCHED==========");
        }

        }
            assertEquals(matchRowCount, rowCount, "UI Table is the exact copy of Database Table");
    } catch (Exception e) {
            System.out.println(e);
    }
}

最后在完成测试后,关闭与数据库的连接。

@AfterTest
public void CloseTheConnection() throws SQLException {

// Code to close each and all Object related to Database connection
if (resultSet != null) {
    try {
        resultSet.close();
    } catch (Exception e) {
    }
}

if (stmt != null) {
    try {
        stmt.close();
    } catch (Exception e) {
    }
}

if (conn != null) {
    try {
        conn.close();
    } catch (Exception e) {
    }
}

driver.quit();
}

}

希望这会有所帮助,