How can I call a function with an object and an array and add it to a property of that object?


Samantha JT Star

I have a Javascript test application using Selenium Web Driver. When setting up code ready to test, I always use code similar to the following code that sets properties before testing the page. In this case it's the theme page, so I have the following code to set the theme object and then add properties to it:

var topic = this;

topic.topicName = element(by.id('topicName'));
topic.topicVersion = element(by.id('topicVersion'));
.....
.....
topic.description = element(by.id('description'));
topic.topicHeader = element(by.id('topicHeader'));
topic.topicFooter = element(by.id('topicFooter'));

Is there a way to simplify this setup so I don't have to code the calls to it multiple times element(by.id()). Ideally I just want to pass an object and an array of different fields and execute some code and element(by.id(then add properties to that object? Note that the attribute name I want to have is the one that matches the element ID.

I'll give some tips on how to do this, any help would be greatly appreciated.

Denys Séguret

You can do this:

['topicName', 'topicVersion', ...].forEach(function(k){
    topic[k] = element(by.id(k));
});

Related