sam2013
sam2013

Reputation: 21

Java Cucumber Using Keys From Property File In Examples Table

Its been a long time since I have worked with Scenario Outlines. What I wanted to achieve was to reference keys from my config_data.properties file in the Examples table, so that I have once source of truth for content/data for my tests.

I have been getting an error on the steps that need to get the data from the properties file and the value that gets entered into the first name text box when I run my test is firstName1 not the value in the properties file. The error in question:

[31morg.openqa.selenium.WebDriverException: unknown error: keys should be a 
string

Here is what I have:

Feature File:

@new_test
Scenario Outline: User fills out the Personal Info Form With Valid Data
And I enter a first name as "<first_name>"
And I enter a middle name as "<middle_name>"
And I enter a last name as "<last_name>"



Examples:

|first_name | middle_name | last_name | 
|firstName1 | middlename1 | lastname1 |

Step Definitions: (I think this is where the problem is)

public class PersonalInfoFormSteps {

    private PersonalInfoFormPage personalInfo;

    private DataReader data;

    @When("^I enter a first name as \"([^\"]*)\"$")
    public void i_enter_a_first_name_as(String first_name) throws Throwable {
    personalInfo.getFirstNameField().click();
    data.loadData().getProperty(first_name);
    personalInfo.getFirstNameField().sendKeys(first_name);

    }
}

DataReader.class (This works fine for getting data that does not involve scenario outline)

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.WebDriverWait;

import net.serenitybdd.core.pages.PageObject;

public class DataReader extends PageObject {

WebDriverWait wait = null;
private WebDriver driver;

String result = "";
InputStream inputStream;
File file = new File(
        "file path to properties file goes here");

public DataReader(WebDriver driver) {
    super();
}

public Properties loadData() throws IOException {
    Properties prop = new Properties();
    FileInputStream fileInput = null;
    try {
        fileInput = new FileInputStream(file);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    // load properties file
    try {
        prop.load(fileInput);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return prop;
}

}

Properties File Data:

firstName1: Mickey
middleName1: M
lastName1: Mouse

Upvotes: 1

Views: 4937

Answers (2)

sam2013
sam2013

Reputation: 21

Haha figured it out. I changed the code in the step definition:

@And("^I enter a first name as \"([^\"]*)\"$")
public void i_enter_a_first_name_as(String first_name) throws Throwable {
    personalInfo.getFirstNameField().click();

    personalInfo.getFirstNameField()
            .sendKeys(data.loadData().getProperty(first_name));

}

Upvotes: 1

Hamza Torjmen
Hamza Torjmen

Reputation: 270

Your Step Definition is wrong, replace this line :

@When("^I enter a first name as \"([^\"]*)\"$")

by this line

@And("^I enter a first name as \"([^\"]*)\"$")

Or replace this line :

And I enter a first name as "<first_name>"

By this line

When I enter a first name as "<first_name>"

Upvotes: 0

Related Questions