Selenium - send_keys() sending incomplete string

后端 未结 3 677
说谎
说谎 2021-01-13 07:57

My problem: I have a method to fill a field, but the problem is that selenium is not sending the complete string to the field, so my assert fails at the tim

3条回答
  •  太阳男子
    2021-01-13 08:05

    I had run into this in a previous version and filed a bug report. It had since been fixed, but perhaps it is broken again? In any case, when we discussed this on the protractor chat channel, the following suggestion was made: Use sendKeys as normal, then verify the result. If the result fails the sanity check, then enter the characters one at a time.

    /**
     * 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));
                   }
               }
           });
        }
    

提交回复
热议问题