Actually when running Selenium test case. I takes the live (Main Database) database which is configured in Glassfish. If we add records for testing purpose then it will replicate in the Main database. This is not good know. So is there any way to have separate DataBase for Selenium test case.
实际上,当运行Selenium测试用例时。我获取在Glassfish中配置的实时(主数据库)数据库。如果我们为测试目的添加记录,那么它将在主数据库中复制。这是不太清楚的。所以有没有办法为Selenium测试用例建立单独的数据库。
2 个解决方案
#1
2
For Glassfish:
Glassfish:
-
Define your JDBC Connection Pool resource to refer to different databases for your production server vs your development server.
定义JDBC连接池资源,以引用生产服务器与开发服务器的不同数据库。
-
If you're not running two different servers, then your first step is to fix that, and be running different servers. And different database servers. Never point your development machine/server at production data, or even the production database server.
如果您没有运行两个不同的服务器,那么您的第一步是修复它,并运行不同的服务器。和不同的数据库服务器。不要在生产数据、甚至生产数据库服务器上指出开发机器/服务器。
#2
1
If you are trying to do a unit test that should run on deployment, then you must create a second connection in your unit test program.
如果您正在尝试进行应该在部署中运行的单元测试,那么您必须在单元测试程序中创建第二个连接。
You might want to use an in memory database for this, maybe HSQL which comes bundled with the java sdk so you don't need to install any drivers and also you won't need to cleanup anything after the tests are run.
您可能想要在内存数据库中使用这个,可能是与java sdk绑定的HSQL,这样您就不需要安装任何驱动程序,而且在运行测试之后,您也不需要清理任何东西。
How you create the second connection depends on what you are using, hopefully you will have a central class or method to get the database connections, something like this:
如何创建第二个连接取决于您使用的是什么,希望您有一个中心类或方法来获取数据库连接,类似如下内容:
Connection c = MyConnectionClass.getConnection();
so you will have it easy modifying the getConnection method so you can point out to the HSQL direct connection for your tests, with something like this:
因此,你可以很容易地修改getConnection方法,这样你就可以指出你的测试的HSQL直接连接,类似这样:
public Connection getConnection(){
if(testing){
Connection c = DriverManager.getConnection("jdbc:hsqldb:mem:mymemdb", "SA", "");
}else{
//get your connection from your pool or whatever you are doing right now
}
}
or also you can be more correct and create a mock for this class. But I'm not sure if selenium supports this out of he box.
或者您可以更正确地为这个类创建一个mock。但是我不确定硒是否支持这个。
And of course, you will need to create your database schema into the in memory database before beginning the tests. If you use hibernate or JPA for example then that should be simple (be sure that you add a second persistence unit and use that in this case), if not then you should have the scripts for your database and run them with JDBC as you would run them in any database.
当然,在开始测试之前,您需要在内存数据库中创建数据库模式。如果你使用hibernate或JPA例如那应该简单(确保你添加第二个持久性单元和使用,在这种情况下),如果没有,那么你应该有脚本与JDBC数据库和运行它们你会在任何数据库运行它们。
Also if you don't want to run the scripts every time the tests are run use hsql but in file mode (change the jdbc:hsqldb:mem for jdbc:hsqldb:file it will use a file to store the database).
另外,如果您不希望在每次运行测试时都运行这些脚本,那么请使用hsql而不是文件模式(更改jdbc:hsqldb:mem for jdbc:hsqldb:file,它将使用一个文件来存储数据库)。
here's some info about the hsql database if you want to know more: link
如果您想了解更多,这里有一些关于hsql数据库的信息:链接
Also if you dont like hsql you can try sqlite (only for file mode) which I think has more tools for accessing it
如果你不喜欢hsql,你可以试试sqlite(只适用于文件模式),我认为它有更多的工具来访问它
#1
2
For Glassfish:
Glassfish:
-
Define your JDBC Connection Pool resource to refer to different databases for your production server vs your development server.
定义JDBC连接池资源,以引用生产服务器与开发服务器的不同数据库。
-
If you're not running two different servers, then your first step is to fix that, and be running different servers. And different database servers. Never point your development machine/server at production data, or even the production database server.
如果您没有运行两个不同的服务器,那么您的第一步是修复它,并运行不同的服务器。和不同的数据库服务器。不要在生产数据、甚至生产数据库服务器上指出开发机器/服务器。
#2
1
If you are trying to do a unit test that should run on deployment, then you must create a second connection in your unit test program.
如果您正在尝试进行应该在部署中运行的单元测试,那么您必须在单元测试程序中创建第二个连接。
You might want to use an in memory database for this, maybe HSQL which comes bundled with the java sdk so you don't need to install any drivers and also you won't need to cleanup anything after the tests are run.
您可能想要在内存数据库中使用这个,可能是与java sdk绑定的HSQL,这样您就不需要安装任何驱动程序,而且在运行测试之后,您也不需要清理任何东西。
How you create the second connection depends on what you are using, hopefully you will have a central class or method to get the database connections, something like this:
如何创建第二个连接取决于您使用的是什么,希望您有一个中心类或方法来获取数据库连接,类似如下内容:
Connection c = MyConnectionClass.getConnection();
so you will have it easy modifying the getConnection method so you can point out to the HSQL direct connection for your tests, with something like this:
因此,你可以很容易地修改getConnection方法,这样你就可以指出你的测试的HSQL直接连接,类似这样:
public Connection getConnection(){
if(testing){
Connection c = DriverManager.getConnection("jdbc:hsqldb:mem:mymemdb", "SA", "");
}else{
//get your connection from your pool or whatever you are doing right now
}
}
or also you can be more correct and create a mock for this class. But I'm not sure if selenium supports this out of he box.
或者您可以更正确地为这个类创建一个mock。但是我不确定硒是否支持这个。
And of course, you will need to create your database schema into the in memory database before beginning the tests. If you use hibernate or JPA for example then that should be simple (be sure that you add a second persistence unit and use that in this case), if not then you should have the scripts for your database and run them with JDBC as you would run them in any database.
当然,在开始测试之前,您需要在内存数据库中创建数据库模式。如果你使用hibernate或JPA例如那应该简单(确保你添加第二个持久性单元和使用,在这种情况下),如果没有,那么你应该有脚本与JDBC数据库和运行它们你会在任何数据库运行它们。
Also if you don't want to run the scripts every time the tests are run use hsql but in file mode (change the jdbc:hsqldb:mem for jdbc:hsqldb:file it will use a file to store the database).
另外,如果您不希望在每次运行测试时都运行这些脚本,那么请使用hsql而不是文件模式(更改jdbc:hsqldb:mem for jdbc:hsqldb:file,它将使用一个文件来存储数据库)。
here's some info about the hsql database if you want to know more: link
如果您想了解更多,这里有一些关于hsql数据库的信息:链接
Also if you dont like hsql you can try sqlite (only for file mode) which I think has more tools for accessing it
如果你不喜欢hsql,你可以试试sqlite(只适用于文件模式),我认为它有更多的工具来访问它