【问题标题】:How can I externalise my selenium setup in order to make my selenium tests configurable如何将我的硒设置外部化以使我的硒测试可配置
【发布时间】:2013-11-18 12:59:18
【问题描述】:

我想将我的 selenium 测试设置外部化,以使其更具可配置性。 我想将我的 testURL 和我的节点 URL 外部化。

这是我的代码:

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)

我的属性文件在 src 文件夹下

【问题讨论】:

  • 很好地复制了项目源下的文件,感谢 Niall :)

标签: selenium-webdriver selenium-grid automated-tests


【解决方案1】:

您可以这样做的两种基本方法是:

  1. 传入JVM参数并使用System.getProperty(...)访问它
  2. 将您的配置外部化到属性文件中,例如here

我最近在我的 Selenium 测试中实现了第二个,如果您需要,可以扩展此答案以提供更多详细信息。

【讨论】:

  • 嗨 Niall,是的,如果您能扩展您的第二个解决方案,我将不胜感激。就我而言,我需要的配置键是经过测试的应用程序 URL、集线器 URL 和节点 URL。我访问了您发送的链接,并且确实了解了如何创建属性文件并阅读它。我的问题是如何在我的代码中添加 thess 属性。提前致谢
  • @Ziwdigforbugs 你还需要这个还是根据你对原始问题的评论解决了这个问题?
【解决方案2】:

在我的测试中,我解决了我创建名为 Environment 的 Java 类来存储有关给定环境的信息:

很少的sn-ps代码:

 public enum NameOfEnvironment {
    SYSTEMTEST, ACCEPTANCE
}

存储给定环境的名称:)

public String getBaseUrl() {
    switch (actualEnvironment) {
        case SYSTEMTEST: {
            baseUrl = getPrefix() + "172.23.32.251:9092/pages/index.html";
            break;
        }

会将环境的 URL 返回给我。在测试开始时,我有这样的事情:

 public static final Environment USED_ENVIRONMENT = new Environment(Environment.NameOfEnvironment.SYSTEMTEST);

稍后我只需致电USED_ENVIRONMENT.getBaseUrl(),它将返回当前运行的实际链接

顺便说一句,为了填补空白,这里是类的构造函数

 public Environment(NameOfEnvironment env) {
    this.actualEnvironment = env;
}

【讨论】:

  • 谢谢 Pavel,但如果可能的话,我想做的是将代码的设置完全外部化并将其移动到外部文件,这样执行代码的人就不会拥有进入代码以更改配置。
  • 好的。我知道可以做到,但是我的程序员经验真的很低。但你可以做到。尝试搜索“如何读取文件”等...
猜你喜欢
  • 2016-02-10
  • 2014-02-24
  • 1970-01-01
  • 2015-06-15
  • 1970-01-01
  • 1970-01-01
  • 2021-12-26
  • 2010-10-12
  • 2012-01-01
相关资源
最近更新 更多