Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Protractor with angular formly

I have a project in angular using formly forms http://angular-formly.com/#/ and want to add some end to end testing using Protractor.

I can't select an element by the model since the form fields are defined in code not in template and when they are rendered they get different ng-model and I don't want to add specific classes since I have pretty big forms.

Is there a way to do it and test the forms rendered by Formly ?

like image 526
v0d1ch Avatar asked Jun 13 '26 21:06

v0d1ch


1 Answers

One option would be to find all formly input elements, evaluate the model value and filter:

var formlyInputs = $$("input");

var desiredModel = "modelName";
var desiredInput = formlyInputs.filter(function (input) {
    return input.evaluate("model[options.key]").then(function (model) {
        return model === desiredModel; 
    });
}).first();

desiredInput.sendKeys("some text");

You may need to extend that to support not only the input elements, but other form elements a form might have.


Another solution would be to rely on the preceding label of an input:

var label = "Text";

var desiredInput = element(by.xpath("//label[normalize-space(.) = '" + label + "']/following-sibling::input"));
desiredInput.sendKeys("some text");
like image 147
alecxe Avatar answered Jun 16 '26 17:06

alecxe