如何在两个相同模型中选择唯一元素?

时间:2018-08-08 06:45:35

标签: javascript protractor automated-tests e2e-testing

我正在尝试选择唯一元素,但问题是存在2个没有唯一模式的模型,并且id是动态的。 its the html 测试规范

it('Rental Car should work ', function() {

let rentalCar = new Hotel();
rentalCar.operator.sendKeys('Eurocar');
rentalCar.carClass.selectValue('M');
rentalCar.pickupTime.sendKeys('12');
rentalCar.DropOfTime.sendKeys('13');
//rentalCar.cancelable.check();
rentalCar.clientAmount.sendKeys('120');
rentalCar.supplierAmount.sendKeys('12');
rentalCar.pickupLocation.sendKeys('berlin');
rentalCar.selectpickOption.click();
browser.sleep(2000);
rentalCar.dropLocation.sendKeys('Han');
browser.sleep(2000);
rentalCar.selectDropOption.click();
browser.sleep(2000);
rentalCar.save();

})

我试图在页面对象的末尾添加first()和last(),但是它得到了错误“ TypeError:$(...)。first不是一个函数”

class Hotel {

constructor(){

this.operator = element(by.model('$ctrl.item.operator'));
this.carClass = new MdSelect('$ctrl.item.class');
this.pickupTime = element(by.model('$ctrl.item.pickup_time'));
this.DropOfTime = element(by.model('$ctrl.item.return_time'));
//this.cancelable = new MdCheckBox('$ctrl.item.isFlex');
this.clientAmount = element(by.model('$ctrl.item.traveler_item_data[0].line_items[0].gross_amount'));
this.supplierAmount =element(by.model('$ctrl.lineItem.supplier_gross_amount'));
this.pickupLocation = $('md-autocomplete md-autocomplete-wrap md-input-container [aria-label="Pickup location"]');
this.selectpickOption = $('md-virtual-repeat-container md-autocomplete-parent-scope');
this.dropLocation = $('md-autocomplete md-autocomplete-wrap md-input-container [aria-label="Drop-off location"]');
this.selectDropOption = $$('.md-virtual-repeat-container .md-autocomplete-parent-scope '); }

帮我解决这个问题。

2 个答案:

答案 0 :(得分:0)

first()last()ElementArrayFinder上可用。您的错误表明您正在尝试在first()上使用ElementFinder

答案 1 :(得分:0)

如果有两个相同的元素,则可以使用

element.all(by.model('model')).get(0);

根据索引,可以使用get(0),get(1)。