【问题标题】:Protractor, Failed: unknown error: cannot focus element on custom input field量角器,失败:未知错误:无法将元素聚焦在自定义输入字段上
【发布时间】:2017-02-02 21:54:35
【问题描述】:

跳上测试车,现在正在对一个网站进行一些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

【问题讨论】:

    标签: javascript jquery angularjs protractor


    【解决方案1】:

    当您尝试 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);
    }
    };
    

    【讨论】:

      【解决方案2】:

      想出了这个:

      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';
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-02-03
        • 2016-03-30
        • 1970-01-01
        • 1970-01-01
        • 2017-10-27
        • 2016-07-12
        • 1970-01-01
        相关资源
        最近更新 更多