硒sendKeys不发送所有字符 [英] Selenium sendKeys are not sending all characters

查看:116
本文介绍了硒sendKeys不发送所有字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Java,Selenium和Chrome进行自动化测试.我们的开发人员最近将UI从AngularJS升级到Angular2(不确定是否重要).但是从那以后,sendKeys在文本字段中输入不完整的字符.这是一个示例:

I'm using Java, Selenium, and Chrome for test automation. Our developers recently upgraded our UI from AngularJS to Angular2 (not sure if that matters). But since then, sendKeys is inputting incomplete characters in to the text field. Here's an example:

    public void enterCustomerDetails()
    {   
        txtFirstName.sendKeys("Joh201605130947AM");
        txtSurname.sendKeys("Doe201605130947AM");
        txtEmail.sendKeys("johndoe@gmail.com");
    }

我也尝试过使用executeScript.没用它可以输入完整的字符,但表单认为该字段为空.

I also tried using executeScript. It didn't work. It can enter complete characters but the form thinks the field is null.

public void forceSendKeys(WebElement element, String text)
{
    if (element != null)
        ((JavascriptExecutor) this.webDriver).executeScript("arguments[0].value=arguments[1]", element, text);
}

public void enterCustomerDetails()
        {   
            forceSendKeys(txtFirstName, "Joh201605130947AM");
            forceSendKeys(txtSurname, "Doe201605130947AM");
            forceSendKeys(txtEmail, "johndoe@gmail.com");
        }

我还尝试在.sendKeys之前使用.click()并增加睡眠时间.他们也没有工作.

I also tried using .click() before .sendKeys and adding in sleep time. They didn't work too.

我有一个想法,可以从这篇文章中逐个输入字符:

I got an idea to enter the characters 1 by 1 from this post: How to enter characters one by one in to a text field in selenium webdriver?

它有效,但这意味着我必须将所有代码从sendKeys重写为新函数:

It worked but that means I have to rewrite all my codes from sendKeys to the new function:

    public void sendChar(WebElement element, String value)
{
    element.clear();

    for (int i = 0; i < value.length(); i++){
        char c = value.charAt(i);
        String s = new StringBuilder().append(c).toString();
        element.sendKeys(s);
    }       
}

public void enterCustomerDetails()
    {
        sendChar(txtFirstName, "Joh201605130947AM");
        sendChar(txtSurname, "Doe201605130947AM");      
        sendChar(txtEmail, "johndoe@gmail.com");
    }

如果你们知道更好的方法,请帮忙! :)

If you guys know a better way, please help! :)

推荐答案

我认为这是由Angular2问题引起的

I assume this is caused by this Angular2 issue https://github.com/angular/angular/issues/5808

Angular无法快速处理输入事件.

Angular can't process input events when they arrive too fast.

作为解决方法,您需要发送单个字符,每个字符之间的延迟很小.

As a workaround you would need to send single characters with a small delay between each.

这篇关于硒sendKeys不发送所有字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆