Java 使用 Selenium Webdriver 连接远程数据库并通过 Eclipse 从本地机器运行测试用例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18805538/
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-08-12 11:14:20  来源:igfitidea点击:

Connect Remote Database using Selenium Webdriver and run Test Cases from local machine through eclipse

java.htaccessseleniumselenium-webdriver

提问by John

I am using selenium web driver java built, the editor is eclipse. For testing one of our websites I am using Data-driven testing by fetching data from MySQL database.

我使用的是java 内置的 selenium web 驱动程序,编辑器是eclipse。为了测试我们的网站之一,我通过从 MySQL 数据库中获取数据来使用数据驱动测试。

I dumped the development server database to my local machine and installed that dumped data in my machine xampp and able to connect to the database and proceed through the testing process.

我将开发服务器数据库转储到我的本地机器,并将转储的数据安装在我的机器 xampp 中,并且能够连接到数据库并继续进行测试过程。

To connect to my local machine database I am using this connection string

要连接到我的本地机器数据库,我正在使用此连接字符串

String url1 ="jdbc:mysql://localhost:3306/databasename";           
String dbClass = "com.mysql.jdbc.Driver";
Class.forName(dbClass).newInstance();
Connection con = DriverManager.getConnection(url1, "root", "");
Statement stmt = (Statement) con.createStatement();

Now I need to connect to our original development server database which is in remote server.

现在我需要连接到远程服务器中的原始开发服务器数据库。

I have tried this as connection string to connect remote machine

我已经尝试将此作为连接字符串来连接远程机器

 String url1 ="jdbc:mysql://10.0.2.129:3306/test";
 String dbClass ="com.mysql.jdbc.Driver";
 Class.forName(dbClass).newInstance();
 Connection con = DriverManager.getConnection(url1, "root","root");

but not able to connect to the remote machine database.Following error is showing

但无法连接到远程机器数据库。显示以下错误

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
        at java.net.URLClassLoader.run(Unknown Source)
    at java.net.URLClassLoader.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)

Can anyone suggest me what changes are needed to be done in the connection string to connect the remote server database which is ht access protected? and how I can run my test cases from my local machine while connecting to the remote server database.

任何人都可以建议我需要在连接字符串中进行哪些更改才能连接受 ht 访问保护的远程服务器数据库?以及如何在连接到远程服务器数据库的同时从本地机器运行我的测试用例。

Please provide some suggestion.

请提供一些建议。

回答by Petr Mensik

You just need to specify machine's IP address, port and database name in the connection string. So try something like

您只需要在连接字符串中指定机器的 IP 地址、端口和数据库名称。所以尝试类似的东西

String dbUrl = "jdbc:mysql://192.168.1.53306/myDB";
DriverManager.getConnection(dbUrl, "username", "password");

And your tests should work the same way as before, you are just connectng to the different machine which can be more time consuming (e.g. queries can take more time) but that's the only difference.

并且您的测试应该以与以前相同的方式工作,您只是连接到可能更耗时的不同机器(例如查询可能需要更多时间),但这是唯一的区别。

回答by user861594

You can utilize data driven capability provided by one of the selenium testing-frameworks- ISFWwhere you can specify data base query to provide test data. For example:

您可以利用selenium 测试框架之一提供的数据驱动功能- ISFW,您可以在其中指定数据库查询以提供测试数据。例如:

@IsDataProvider(sqlQuery="select query")
@Test
public void testSomething(Map<String, String> testdata){
  //use query col as key testdata.get("colName")
  }

回答by Deepa Reddy

You need to add a jar file to connect to the database

需要添加一个jar文件来连接数据库

jar: mysql-connector-java