繁体   English   中英

Selenium-send_keys()发送不完整的字符串

[英]Selenium - send_keys() sending incomplete string

我的问题:我有一种填充字段的方法,但是问题是硒没有将完整的字符串发送到该字段,因此我的断言在验证时失败。

我的代码:

var webdriver = require('selenium-webdriver');
var casual = require('casual');
var expect = require('chai').expect;
var By = webdriver.By;

exports.addPropuesta = function (driver) {

var first_name = casual.first_name;

driver.findElement(By.xpath("//a[contains(text(),'Añadir Propuesta Test')]")).click();

name_field = driver.findElement(By.name('nombre'));
name_field.sendKeys(first_name);

driver.findElement(By.css("Input[type='submit']")).click();

driver.findElement(By.css('.table')).getText().then(function(table_content){

    expect(table_content).to.include(first_name);

    });
};

看来是一个常见问题。

在尝试变通办法之前,作为一个健全性检查,请确保在您发送键之前输入字段已准备好接收输入。 您也可以尝试在调用SendKeys之前清除该字段。 我假设您看到的字符串被截断了,而字符没有丢失或没有某种伪像(例如占位符文本或先前测试的剩余输入)。

如果不起作用,请采取以下解决方法:

  1. 使用JavaScript而不是调用SetKeys来设置输入字段的值。 在执行此操作的某些网站上,除非我也触发输入更改事件,否则实际上不会识别输入值。

    C#中的示例。 希望您唯一需要做的更改是使ExecuteScript改为executeScript。

     driver.ExecuteScript("var exampleInput = document.getElementById('exampleInput'); exampleInput.value = '" + testInputValue + "'; exampleInput.dispatchEvent(new Event('change'));"); 

    当然,您可以将其分为两行,一行设置值,第二行发送事件。

  2. 分别发送每个密钥。 这是一个变通办法,我已经从线程中多次看到有关此问题的信息。

     for (var i = 0; i < first_name.length; i++) { name_field.sendKeys(first_name.charAt(i)); } 

https://github.com/angular/protractor/issues/3196
https://github.com/angular/protractor/issues/2019
如果您想寻找其他可能的解决方案,则可以通过简单搜索“ webdriver sendkeys不等待所有键”找到更多线程。

我在以前的版本中遇到过此问题,并提交了错误报告。 此后已修复,但也许又被打破了? 无论如何,当我们在量角器聊天频道上对此进行讨论时,提出了以下建议:正常使用sendKeys,然后验证结果。 如果结果未通过健全性检查,则一次输入一个字符。

/**
 * A Typescript version that can be used as a mixin.
 * Make some minor modifications to use as a class.
 * @param data {string} The string to enter in the input element
 */
export class SendKeys {
    inputEl: ElementFinder;

    sendKeys(data: string) {
        var el = this.inputEl;
        // click on the input before sending data. This helps the focus and action situations.
        el.click();

        el.clear();
        el.sendKeys(data);

        // Verify whether or not hte whole data value was sent.
        // If not, send data one character at a time, which works.
        // See: https://github.com/angular/protractor/issues/3196
        el.getAttribute('value').then(function (insertedValue) {
            if (insertedValue !== data) {
                // Failed, must send characters one at a time
                el.clear();
                for (let i=0; i < data.lenght; i++) {
                    el.sendKeys(data.charAt(i));
               }
           }
       });
    }
}

-

/**
 * The Javascript version:
 * @param el {ElementFinder} The input element reference
 * @param data {string} The string to enter in the input element
 */
export function sendKeys(el, data) {
        var el = this.inputEl;
        // click on the input before sending data. This helps the focus and action situations.
        el.click();

        el.clear();
        el.sendKeys(data);

        // Verify whether or not hte whole data value was sent.
        // If not, send data one character at a time, which works.
        // See: https://github.com/angular/protractor/issues/3196
        el.getAttribute('value').then(function (insertedValue) {
            if (insertedValue !== data) {
                // Failed, must send characters one at a time
                el.clear();
                for (let i=0; i < data.lenght; i++) {
                    el.sendKeys(data.charAt(i));
               }
           }
       });
    }

我对此问题的解决方案是在每个send_keys之前添加driver.sleep(1)

例:

driver.sleep(1000);
driver.findElement(By.name('rut')).sendKeys(rut_text);

driver.findElement(By.name('dv')).sendKeys(dv);

driver.sleep(1000);
driver.findElement(By.name('nombre')).sendKeys(first_name);

driver.sleep(1000);
driver.findElement(By.name('apellido_paterno')).sendKeys(apellido_paterno_field);

driver.sleep(1000);
driver.findElement(By.name('apellido_materno')).sendKeys(apellido_materno);

driver.sleep(1000);
driver.findElement(By.name('celular')).sendKeys(phone_number);

driver.sleep(1000);
driver.findElement(By.name('email')).sendKeys(email);

我尝试解决添加execute_scriptclear但对我来说不解决的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM