2016-12-06 120 views
0

我的問題:我有一個方法來填充字段,但問題是硒沒有發送完整的字符串到字段,所以我的斷言在驗證時失敗。硒 - send_keys()發送不完整的字符串

我的代碼:

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

    }); 
}; 

回答

2

看起來這是一個常見的問題。 [0]

在嘗試變通辦法之前,請確保在發送密鑰時輸入字段已準備好接收輸入。在調用SendKeys之前,您也可以嘗試清除該字段。我假設你看到你的字符串被截斷,而不是字符丟失或者前綴有一些工件(比如佔位符文本或來自先前測試的剩餘輸入)。

一些解決方法,如果沒有工作:

  1. 設置的,而不是調用SetKeys使用JavaScript輸入字段的值。在我這樣做的一些網站上,輸入值實際上不會被識別,除非我也觸發輸入更改的事件。

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

[0] https://sqa.stackexchange.com/questions/10450/selenium-sendkeys-not-completing-data-entry-before-moving-to-next-field
https://github.com/angular/protractor/issues/3196
https://github.com/angular/protractor/issues/2019
等等,等等更多的線程可以通過一個簡單的搜索中找到 「的webdriver的SendKeys不會等待所有鍵」,如果你想尋找其他可能的解決方案來解決您的問題。

+0

我試着用executeScript,但發生同樣的問題。 – RFtests

1

我在之前的版本中遇到過這個問題,並提交了一個錯誤報告。它已經被修復了,但也許它又被打破了?在任何情況下,當我們在protractor chat channel上討論這個時,都提出了以下建議:正常使用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)); 
       } 
      } 
     }); 
    } 
0

我對這個問題是每個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但我沒有解決。