angularprotractore2e-testingangular-testangular-e2e

Protractor two buttons with the same text


Hey so I have this issue with Protractor e2e test:

I have menu with sub menu. Menu and sub menu has the same name text of buttons inside (1 in menu, 1 in sub menu) for example we can name it "menuItem". I know how to find and click first "menuItem" in menu but I can't find how to click sub menu "menuItem". I'm using locator by.buttonText to find first "menuItem" in menu. Both buttons has the same css classes. I'm using basic matMenu from angular.

Can somebody help me with this?


Solution

  • <button mat-button [matMenuTriggerFor]="menu">Menu</button>
    <mat-menu #menu="matMenu">
    <button mat-menu-item>Menu</button>
    </mat-menu>
    

    If your code is something like this then you can use element.all(by.buttonText('Menu')).last().click() You can give a browser.sleep(1000) before this line to make sure protractor is able to capture it.

    But if you multiple options with same name then you need to define something unique like 'id'or use XPath. I hope it helps.