Search code examples
angularjsprotractorangularjs-e2e

Angular Protractor e2e testing Multiple Dropdowns


I have Multiple dropdowns in the header part with the same class name. How to write protractor e2e test cases for multiple dropdowns.

This is my HTML Code Snippet:

<ul class="nav navbar-nav">
    <li class="dropdown">
          <a class="dropdown-toggle" data-toggle="dropdown">Patient Flow <span class="caret"></span></a>
          <ul class="dropdown-menu" role="menu">
            <li><a href="#/patientDashboard/appointments">Patient Portal</a></li>
            <li><a href="#/dashboard2/stations/1/patients">Patient Queue</a></li>
            <li><a href="#/dashboard4/appointments">Visitor Dashboard</a></li>
            <!--  <li><a href="#/patientDashboard/appointments">Patient List View</a></li> -->
          </ul>
    </li>
    <li><a href="#/dashboard3/encounters">OR Flow</a></li>
    <li class="dropdown">
          <a class="dropdown-toggle" data-toggle="dropdown">Track Assets <span class="caret"></span></a>
          <ul class="dropdown-menu" role="menu">
            <li><a href="#/listViewTable/assets">Asset List View</a></li>
            <li><a href="#/dashboard5/assets">Asset Map View</a></li>
          </ul>
    </li>
    <li class="dropdown">
          <a class="dropdown-toggle" data-toggle="dropdown">System Setup <span class="caret"></span></a>
          <ul class="dropdown-menu" role="menu">
            <li><a href="#/listViewTable/patients">Patients</a></li>
            <li><a href="#/listViewTable/appointments">Appointments</a></li>
          </ul>
    </li>

    <li><a href="#/dashboard10/encounters/1">Reports</a></li>
  </ul>

Thanks in advance for your help.


Solution

  • You can use:

    var dropdown = element.all(by.css('.dropdown')).get(N);
    var toggle = dropdown.element(by.css('.dropdown-toggle'));
    toggle.click();
    var list = dropdown.all(by.css('.dropdown-menu li'));
    expect(list.first().getText()).toEqual('Patient Portal');
    

    to get the Nth dropdown (Doc), and test the first element of it...

    Then for this kind of quite complex page, I suggest to use Page objects.

    [Edit: Also, I don't know if it's a good practice, but if you don't want to be stuck to a specific order, I would add a unique class on each dropdown (or an ID)]