量角器,失败:未知错误:无法将元素集中在自定义输入字段上

跳上测试车,现在正在实施一些网站的E2E测试,但我遇到了一个棘手的小问题。

我正在尝试选择一个字段,然后将密钥发送到该输入字段以更改值,唯一的问题是失败:未知错误:无法聚焦元素错误我似乎无法过去。 我让它在其他领域工作,而不是那些有这种设置的领域。

pageObject文件。

var profilePage = function() { this.firstName = element(by.id('firstname')); this.lastName = element(by.id('lastname')); this.saveBtn = element(by.css('ng-click="saveLocalAccount()"')); this.cancelBtn = element(by.css('ng-click="cancelChanges()"')); this.changeName = function(firstname, lastname) { this.firstName.click(); var input = firstName.element(by.css('input')); input.click(); this.input.sendKeys(firstname); this.lastName.click(); this.lastName.sendKeys(lastname); browser.waitForAngular(); } }; module.exports = new profilePage(); 

规范。

 var profilePage = require('TestProtractor/E2E/PageObjects/profile.pageObject.js'); describe('Testing the profile page functionality', function() { var firstNameTest = "firstTest"; var lastNameTest = "lastTest"; it('Navigate to profile page.' ,function() { browser.get('xxx'); expect(browser.getCurrentUrl()) .toContain('xxx'); }); it('Should change the firstname and lastname successfully', function() { profilePage.changeName(firstNameTest, lastNameTest); expect(element(by.id('firstname')).getText()).toContain(firstNameTest); expect(element(by.id('lastname')).getText()).toContain(firstNameTest); }); 

});

HTML

在此处输入图像描述

你会看到Failed: unknown error: cannot focus element error当你试图将sendKeys()发送到一个不是input的元素时, Failed: unknown error: cannot focus element error

在你可重用的方法changeName()你试图将sendKeys()lastName = element(by.id('lastname')) ,这不是一个input元素。 您必须以与输入名字文字相同的方式接近它

假设他们也是姓氏内的input

 this.changeName = function(firstname, lastname) { this.firstName.click(); var input = firstName.element(by.css('input')); input.click(); this.input.sendKeys(firstname); this.lastName.click(); var input2 = lastName.element(by.css('input')); input2.click(); this.input2.sendKeys(lastName); } }; 

想出这个:

 var input = firstName.element(by.css('input')); //declare the input browser.actions().click(input).sendKeys("owiejf").perform(); //sendkeys 

其中browser是在此声明的名称

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