Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS Protractor - Finding an Element on a Page Using Ng-Click Binding

I have a button on a page that looks like:

<button ng-click="myFunction()" ng-show="flag">
    Submit
</button>

The element has no ID.

Is there a way to find this element using the function bound to Ng-Click? Or do I have to assign an ID to this element to locate it using Jasmine / Protractor?

like image 855
Lloyd Banks Avatar asked Jul 17 '14 15:07

Lloyd Banks


3 Answers

Just tested this and it works:

element(by.css('[ng-click="myFunction()"]'))
like image 113
Ted Warner Avatar answered Nov 02 '22 20:11

Ted Warner


I went through the Protractor API and didn't find anything related to finding an element through ng-click. I did find

element(by.buttonText("Submit"));

Not quite the same, but does the job in my environment.

like image 35
Lloyd Banks Avatar answered Nov 02 '22 21:11

Lloyd Banks


If you want to use ng-show, you could try this:

element(by.Css("button[ng-show]")); // Get element with tag < button > and has ng-show attribute

or:

element(by.Css("button[ng-show*=flag]")); // Get element with tag < button > and has ng-show attribute which contains word flag
like image 1
Nguyen Vu Hoang Avatar answered Nov 02 '22 20:11

Nguyen Vu Hoang