Samantha J T Star
Samantha J T Star

Reputation: 32778

How can I execute functions inside a function based on an array of input parameters?

I have the following code:

element(by.model('home.modal.data.version')).sendKeys('Version1');
element(by.model('home.modal.data.product')).sendKeys('Product1');
element(by.model('home.modal.data.audiences')).sendKeys('Audience1');

What I would like to do is to simplify this so that I have a function to do everything. I was thinking about a function that could take an input parameter like:

var inputData =  {
            [ 'home.modal.data.version' : 'Version1' ],
            [ 'home.modal.data.product' : 'Product1' ],
            [ 'home.modal.data.audiences' : 'Audience1' ]
            };

I am not sure if this is a good way to specify parameters.

and a function that I could call such as:

enterData(inputData);

Can anyone tell me how if the way I am doing the input parameters is correct and if so then how could I loop through these inside a function to call the element function with the parameters?

Upvotes: 0

Views: 52

Answers (1)

James Thorpe
James Thorpe

Reputation: 32202

I would use a slightly more descriptive way of passing the parameters:

var inputData = [
    {model: 'home.modal.data.version', keys: 'Version1'},
    {model: 'home.modal.data.product', keys: 'Product1'},
    {model: 'home.modal.data.audiences', keys: 'Audience1'}
];

Your function can then use these within itself:

function enterData(data) {
  for (var x = 0; x < data.length; x++) {
    element(by.model(data[x].model)).sendKeys(data[x].keys);
  }
}

Upvotes: 1

Related Questions