Selenium webdriver 如何将selenium设置外部化以使selenium测试可配置
我想将selenium测试设置外部化,以便使它们更具可配置性。 我想将testURL和节点url外部化 这是我的密码:Selenium webdriver 如何将selenium设置外部化以使selenium测试可配置,selenium-webdriver,selenium-grid,automated-tests,Selenium Webdriver,Selenium Grid,Automated Tests,我想将selenium测试设置外部化,以便使它们更具可配置性。 我想将testURL和节点url外部化 这是我的密码: public void setup () throws MalformedURLException { //the URL of the application to be tested TestURL = "http://frstmwarwebsrv2.orsyptst.com:9000"; //Hub URL B
public void setup () throws MalformedURLException
{ //the URL of the application to be tested
TestURL = "http://frstmwarwebsrv2.orsyptst.com:9000";
//Hub URL
BaseURL = "http://10.2.128.126";
//Node1 URL
winURL = "http://10.2.128.120:5556/wd/hub";
//Node2 URL
androidURL ="http://10.2.128.120:5555/wd/hub";
目前,我已经在每个测试中添加了这个设置函数,我想把它放在一个XML文件中作为一个示例,以使其可配置,有什么建议吗
谢谢
谢谢你的帮助
更新:
以下是我迄今为止所做的:
添加了包含以下内容的config.properties文件:
# This is my test.properties file
AppURL = http://************
HubURL= http://*****************
WinURL= http://*********/wd/hub
AndroidURL =
iOSURL
并创建一个类来读取属性:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
public class ReadPropertiesFile {
public static void main(String[] args) {
try {
File file = new File("config.properties");
FileInputStream fileInput = new FileInputStream(file);
Properties properties = new Properties();
properties.load(fileInput);
fileInput.close();
Enumeration enuKeys = properties.keys();
while (enuKeys.hasMoreElements()) {
String key = (String) enuKeys.nextElement();
String value = properties.getProperty(key);
System.out.println(key + ": " + value);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行此命令时,我收到以下错误:
java.io.FileNotFoundException: config.properties (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at ReadPropertiesFile.main(ReadPropertiesFile.java:15)
java.io.FileNotFoundException:config.properties(系统找不到指定的文件)
在java.io.FileInputStream.open(本机方法)
位于java.io.FileInputStream。(未知源)
位于ReadPropertiesFile.main(ReadPropertiesFile.java:15)
我的属性文件在src文件夹下在我的测试中,我解决了它,我创建了名为
环境
的Java类来存储关于给定环境的信息:
几段代码:
public enum NameOfEnvironment {
SYSTEMTEST, ACCEPTANCE
}
存储给定环境的名称:)
将返回环境的URL。在测试开始时,我有这样的想法:
public static final Environment USED_ENVIRONMENT = new Environment(Environment.NameOfEnvironment.SYSTEMTEST);
稍后我只需调用USED\u ENVIRONMENT.getBaseUrl()
,它将返回当前运行的实际链接
顺便说一句,为了填补空白,这里是类的构造函数
public Environment(NameOfEnvironment env) {
this.actualEnvironment = env;
}
有两种基本方法可以做到这一点:
System.getProperty(…)
我最近在Selenium测试中实现了第二个,如果您需要,可以扩展此答案以提供更多详细信息。谢谢Pavel,但如果可能的话,我想做的是将设置从代码中完全外部化,并将其移动到一个外部文件中,这样执行代码的人就不必进入代码来更改config.Ok。我知道这是可以做到的,但我的程序员经验真的很低。但你可以做到。尝试搜索“如何读取文件”之类的…嗨,尼尔,是的,如果你能扩展你的第二个解决方案,我将不胜感激。在我的例子中,我需要的配置键是经过测试的应用程序URL、中心URL和节点URL。我已经浏览了你发送的链接,我确实了解了如何创建属性文件并阅读它。我的问题是如何在代码中添加这些属性。谢谢你advance@Ziwdigforbugs你还需要这个吗?或者你已经根据你对原始问题的评论解决了这个问题吗?很好地复制了项目源下的文件,它成功了,谢谢尼尔:)
public Environment(NameOfEnvironment env) {
this.actualEnvironment = env;
}