简体   繁体   English

如何基于输入参数数组在函数内部执行函数?

[英]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? 谁能告诉我我输入参数的方式是否正确,如果可以,那么如何在函数内部遍历这些参数来调用带有参数的元素函数呢?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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