1
I am working with automated tests and in my case I have several objects that contain the settings that will be inserted in the system fields.
There I have the methods Set filling the fields with the settings in the given object, for example:
dados = {
nome: "Diego",
sobrenome: "Henrique",
idade: "22"
}
preencheCliente(dados){
fillNome(dados.nome);
fillSobrenome(dados.sobrenome);
fillIdade(dados.idade);
}
I would like to have objects without some property and that continue to work with the method preencheCliente()
, however it will accuse a >>>error<<< when doing Age Set, example:
dados2 = {
nome: "Diego",
sobrenome: "Henrique"
}
preencheCliente(dados){
fillNome(dados.nome);
fillSobrenome(dados.sobrenome);
>>>fillIdade(dados.idade);<<<
}
I put in the title Does not resolve with hasOwnProperty(), on account that, imagine now that I have already implemented more than 100 fields, I would have to adjust for each method fills (preencheCliente
, preencheProduto
, preencheEndereco
, etc) and for each field do the test
if (hasOwnProperty("NOME_PROPRIEDADE_OBJ")):
preencheCliente(dados){
if (dados.hasOwnProperty('nome'))
fillNome(dados.nome);
if (dados.hasOwnProperty('sobrenome'))
fillSobrenome(dados.sobrenome);
if (dados.hasOwnProperty('idade'))
fillIdade(dados.idade);
}
Beyond the hasOwnProperty()
there are 2 other means that I discovered:
Operator IN and comparing with Undefined. See more in: https://dmitripavlutin.com/check-if-object-has-property-javascript/
However it always falls into the massive edition of pasting a test before the lines..
Is there a solution for this, without implementing all these conditionals? Or would you have to do some other kind of approach?
Where does this one come from
Sys.Field...
? You can also useObject.entries
to iterate on the keys of an object, but it is difficult to give a detailed answer without knowing what this isSys
– Leo Letto
I invented it for example, from the Testcomplete framework. Sys.Fieldname would be a Comobject, where I can access properties and methods from a system window for example. I will modify the example
– anijahzarri