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

时间:2017-02-02 21:54:35

标签: javascript jquery angularjs protractor

跳上测试车,现在正在实施一些网站的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

enter image description here

2 个答案:

答案 0 :(得分:7)

当您尝试Failed: unknown error: cannot focus element error某个不属于sendKeys()

的元素时,您会看到input

在您可重复使用的方法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); } };

from __future__ import with_statement
import atexit
from tools import cli
from pyVim import connect
from pyVmomi import vim, vmodl


def get_args():
    *Boring args parsing works*
    return args

def main():
    args = get_args()
    try:
        service_instance = connect.SmartConnectNoSSL(host=args.host,
                                                user=args.user,
                                                pwd=args.password,
                                                port=int(args.port))

        atexit.register(connect.Disconnect, service_instance)
        content = service_instance.RetrieveContent()

        vm = content.searchIndex.FindByUuid(None, args.vm_uuid, True)

        creds = vim.vm.guest.NamePasswordAuthentication(
            username=args.vm_user, password=args.vm_pwd
        )

        try:
            pm = content.guestOperationsManager.processManager
            ps = vim.vm.guest.ProcessManager.ProgramSpec(
                programPath=args.path_to_program,
                arguments=args.program_arguments
            )
            res = pm.StartProgramInGuest(vm, creds, ps)
            if res > 0:
                print "Program executed, PID is %d" % res

        except IOError, e:
            print e
    except vmodl.MethodFault as error:
        print "Caught vmodl fault : " + error.msg
        return -1

    return 0

# Start program
if __name__ == "__main__":
    main()

答案 1 :(得分:0)

想出了一个:

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

其中浏览器是在此处声明的名称

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