Pass parameters to test-run and dataprovider


#1

I am working heavily with the data driven approach in Java. Most of my test methods depend on parameters provided by dataprovider classes which read data from CSV files. That works pretty good so far.
What I am now trying to achieve is to pass some runtime parameters to test excution for reading specific property files (in test methods) and depending on values in such a specific prop-file set the CSV file path to load from dataprovider.
With this approach it would be possible to run datadriven tests with subsets of CSV files for parallelization. Another aspect is that I run the same tests for different environments. The property files contain settings for the different environments. Basic idea is to make my test methods generic using property files for settings (credentials, URLs etc.) and parameters (in chunks to run in parallel) for test data.


#2

Hello @HRA
Let’s take our data driven sample project as an example. If you have some additional CSV files, where different sets of data is stored and you need, depending on the environment, to use specific set of data from the CSV files:
You can set the environment variables and then make a check for a specific environment variable in the code, and based on that, you set the path to a specific data source needed, like in the example below:

public class CredentialsDataProvider {

    // Read data from csv file
    private static Object[][] readCsvData() {
        // Get your specific environment
        String testEnv = System.getenv("yourEnvironmentVariable");

        String csvFilePath = "src/test/java/uitest/data/data.csv";
        ArrayList<Object[]> dataList = new ArrayList<Object[]>();
        CSVReader reader = null;

        // Check for a specific environment variable
        if (testEnv != null) {
            if (testEnv.equals("foo")) {
                // set a different CSV file path depending on the env. variable
                csvFilePath = "somePath";
            } else if (testEnv.equals("bar")) {
                csvFilePath = "someOtherPath";
            }
        }

        try {
            reader = new CSVReader(new FileReader(csvFilePath));
            String[] line;
            while ((line = reader.readNext()) != null) {
                Object[] record = { line[0], line[1], Boolean.parseBoolean(line[2]) };
                dataList.add(record);
            }
            reader.close();

            // Convert ArrayList<Object[]> to Object[][]
            return dataList.toArray(new Object[dataList.size()][]);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

    }

    // Create the Data Provider
    @DataProvider(name = "Credentials")
    public static Object[][] credentials() {
        return readCsvData();
    }
}

With this, you can set different CSV data sources to be used based on the environment or some other variable. Hope this will help you achieve your goal :slight_smile:
Kind Regards!


#3

Hi @smatijas
Thanks again for the quick reply. Your solution is helpful but not suitable for what I am trying to achieve.
Basically I need a way to pass a variable value to a test method and dataprovider. So business logic can decide what to do (which data and/or parameter to load). The variable is not depending on the environment. I would like to start parallel test runs with one or more console parameters. Let me try to explain my use case:

  • I have a web application (my SUT)
  • Same code base is used for several installations
  • Each installation has its own customization (URL,language, credentials, settings etc.)
  • Test methods are implemented using data driven approach and “generic” logic. Means that there is one test method for testing all environments.
  • The customization values for the test target are stored in Java property files
  • The test data for specific test case/run is stored in CSV files
  • There is one machine with WebTestit installation running all tests from that instance
  • So when executing a test I need a mechanism to address which customization file(s) to load and which CSV file(s)
  • For parallelization I would like to chunk the CSV files for run the same tests with different files as datasource

#4

Hello,

we still have a question to make sure we can fully understand your use case:

If so, you could use the approach with the environment variable mentioned by @smatijas.
You would just need to set the variable locally within the same process:
For Windows you can pass a variable like this (I used the sample project https://github.com/rxse/java-data-driven-sample):

cmd /S /C "set csvFilePath=src/test/java/uitest/data/data.csv && "C:\Users\<username>\AppData\Local\Programs\webtestit\Ranorex Webtestit.exe" run <pathToSampleProject>"

and you can then get this variable with the following code:

String csvFilePath = System.getenv("csvFilePath");

and then use this variable to determine, which csv file and which .properties file you want to use. Of course, you can also set multiple environment variables.
You can then also pass this variable on to your test method from within the DataProvider. Just add it to the array, which is already passed to the method:

Object[] record = { line[0], line[1], Boolean.parseBoolean(line[2]), csvFilePath };

and don’t forget to add it to the signature of the test method:

@Test(dataProvider = "Credentials", dataProviderClass = CredentialsDataProvider.class)
public void checkCredentials(String sUsername, String sPassword, Boolean shouldPass, String csvFilePath) throws InterruptedException {

Does this help your use case? Or would it help you if there was a way to pass variables inside Webtestit?
We welcome any feedback and please feel free to correct me, if I misunderstood your use case.
Thanks and best regards!


#5

Hello @hmueller
Thanks for your reply.
Yes, I want to pass one or more runtime parameter for test execution. Depending on that parameter(s) I want to deal with settings and/or testdata in “business logic”.
Your suggestion would be a solution but seems to be an awkward way just for getting parameters to where they are required.
My prefered solution would indeed passing variables to WebTestit. So my tests could make some initialization on each specific test execution.


#6

Thanks for your feedback, @HRA!
Could you please elaborate on how passing variables to Ranorex Webtestit should look like?
We are still trying to understand what exactly your use case is in order to provide a suitable solution, so it would be very helpful if you could provide a sample (if you already have an idea how you would want to implement your use case with Maven) and/or explain where and at which point in your workflow you want to pass the variables.

We really appreciate your input and look forward to improve the app with your help.
Kindest regards!