简体   繁体   中英

How can I externalise my selenium setup in order to make my selenium tests configurable

I would like to externalise my selenium tests setting in order to make them more configurable. I would like to externalise my testURL and my node URLS.

Here is my code :

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";

At the moment I have added this setup function in every test I would like to have it in an XML file for an example in order to make it configurable, any suggestions?

Thanks

Thanks for your help

Update :

Here is what i did so far :

Added a config.properties file with this content :

# This is my test.properties file
AppURL = http://************
HubURL= http://*****************
WinURL= http://*********/wd/hub
AndroidURL =
iOSURL 

And created a classe to read properties :

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();
        }
    }

}

when running this i get this error :

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)

my properties file is under src folder

Two basic ways you could do this are:

  1. Pass in JVM argument and access it using System.getProperty(...)
  2. Externalize your configuration in to properties files, like here

I recently implemented the second one in my Selenium tests and can expand this answer to give more details if you need them.

In my tests, I resolved it that I created Java class called Environment to store information about given Environment:

Few snippets of code:

 public enum NameOfEnvironment {
    SYSTEMTEST, ACCEPTANCE
}

stores the Name of given Environment :)

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

will return me the URL to the environment. And on beginning of the test I have something like this:

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

And later on I just call USED_ENVIRONMENT.getBaseUrl() which will return me the link which is being actual for current run

Btw, to fill in the blanks, here is the constructor f the class

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM