简体   繁体   English

量角器:同一页面上的两个下拉菜单(angularJS),只能从一个下拉菜单中成功选择元素

[英]Protractor: Two drop down menus(angularJS) on the same page, can only successfully select element from one dropdown menu

import {
  browser,
  element,
  by,
  ElementFinder
} from 'protractor';

var path = require('path');


describe('TC:Create Units Component/Reset Button', () => {

  let select: ElementFinder;

  beforeEach(() => {
    browser.get('http://localhost:4200/content/createunits');
    select = element(by.tagName('ng-select'));
  });

  it('should be rendered', async () => {
    expect(select).toBeDefined();

    //Reset button
    select.element(by.css('.ng-select-container')).click();
    expect(select.getAttribute('class')).toMatch('ng-select-opened');
    //select.element(by.css('.ng-select-container')).click();
    //TO SELECT A PARTICULAR OPTION IN THE LIST
    element.all(by.css('.ng-option')).filter(async function(element, index) {
      const text = await element.getText();
      return text === 'compA1';
    }).first().click();
    browser.sleep(1000);
  })
  it('should be rendered', async () => {
    expect(select).toBeDefined();
    select.element(by.css('.ng-select-container')).click();
    expect(select.getAttribute('class')).toMatch('ng-select-opened');
   // select.element(by.css('.ng-select-container')).click();
    //TO SELECT A PARTICULAR OPTION IN THE LIST
    element.all(by.css('.ng-option')).filter(async function(element, index) {
      const text = await element.getText();
      return text === 'LF1';
    }).first().click();
    browser.sleep(1000);



    element(by.id('unitName')).sendKeys('dummy unit');
    browser.sleep(1000);
    element(by.id('description')).sendKeys('dc');
    browser.sleep(1000);
    var registerButton = element(by.buttonText('Reset'));
    var s = registerButton.click();
    browser.sleep(1000);

  })
})

I have tried this, but it doesn't handles the second dropdown. 我试过这个,但它没有处理第二个下拉列表。 How do I proceed to the next dropdown? 如何进入下一个下拉列表? The second 'it' block is for handling the second dropdown. 第二个'it'块用于处理第二个下拉列表。 Two drop down menus(angularJS) on the same page, can only successfully select element from one dropdown menu. 同一页面上的两个下拉菜单(angularJS)只能从一个下拉菜单中成功选择元素。

When protractor finds multiple matches for a locator, it just uses the first one. 当量角器找到定位器的多个匹配项时,它只使用第一个匹配项。 The ideal solution would be to add unique IDs to the dropdowns, so the test has something to latch on to. 理想的解决方案是在下拉列表中添加唯一ID,因此测试可以锁定。 The select could then be moved out of the beforeEach and into the it block as 然后可以将select移出beforeEach并进入it

select = element(by.id('myFirstDropdown'));
select = element(by.id('mySecondDropdown'));

Or if you prefer the $ alias: 或者如果您更喜欢$别名:

select = $('#myFirstDropdown');
select = $('#mySecondDropdown');

If you don't have the freedom to change the page, you could also get all dropdowns and index into the ElementArrayFinder, but be aware that this makes your test dependent on the order of the dropdowns in the DOM, which could be fragile. 如果您没有更改页面的自由,您还可以获取ElementArrayFinder的所有下拉列表和索引,但请注意,这会使您的测试依赖于DOM中下拉列表的顺序,这可能是脆弱的。

select = element.all(by.tagName('ng-select')).get(0);
select = element.all(by.tagName('ng-select')).get(1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM