Selenium sendKeys并且清除不适用于Safari 11中的Kendo自动填充字段

时间:2017-12-11 11:21:25

标签: selenium-webdriver autocomplete safari kendo-autocomplete

在Selenium-Ruby中编码自动化测试时,在Safari浏览器11.0.1中的自动填充文本字段(Kendo UI)上使用sendKeys / clear时,

测试代码:

actions.push(is_element_enabled?(PROF_IN_CHARGE,'PROF_IN_CHARGE'))
actions.push(is_element_displayed?(PROF_IN_CHARGE,'PROF_IN_CHARGE'))
actions.push(type(PROF_IN_CHARGE,first_name, 'PROF_IN_CHARGE'))

Selenium方法:

def is_element_enabled?(locator, name, raise_exception = true)
$logger.info "Verify whether #{name} is enabled."
highlight(web_driver.find_element(locator))
web_driver.find_element(locator).enabled?
$logger.info "Element enabled: #{name}"

def is_element_displayed?(locator, name, raise_exception = true, log_error = true)
$logger.info(__method__) { "Verifying element #{name}" }
$logger.debug(__method__) { locator }
web_driver.find_element(locator).displayed?
highlight(web_driver.find_element(locator), 0, "red")
return true

def type(locator, text, name, raise_exception = true)
$logger.info(__method__) { "Typing #{text} to #{name}" }
$logger.debug(__method__) { locator }
highlight(web_driver.find_element(locator), 0, "red")
#Clear existing text before typing
web_driver.find_element(locator).clear
web_driver.find_element(locator).send_keys(text)

显示以下错误:

 # --- Caused by: ---
 # Selenium::WebDriver::Error::ElementNotVisibleError:
 #   An element command could not be completed because the element is not visible on the page.
 #   /Users/admin/.rvm/gems/ruby-2.4.1/gems/selenium-webdriver-3.6.0/lib/selenium/webdriver/remote/response.rb:71:in `assert_ok'

此处的日志:

I, [2017-12-11T15:19:00.611929 #5324]  INFO -- : Verify whether PROF_IN_CHARGE is enabled.
I, [2017-12-11T15:19:00.635876 #5324]  INFO -- : Element enabled: PROF_IN_CHARGE
I, [2017-12-11T15:19:00.635940 #5324]  INFO -- is_element_displayed?: Verifying element PROF_IN_CHARGE
D, [2017-12-11T15:19:00.635964 #5324] DEBUG -- is_element_displayed?: {:id=>"fcCreateMainAssingee"}
I, [2017-12-11T15:19:00.647651 #5324]  INFO -- is_element_displayed?: true
I, [2017-12-11T15:19:00.660760 #5324]  INFO -- type: Typing Report to PROF_IN_CHARGE
D, [2017-12-11T15:19:00.660834 #5324] DEBUG -- type: {:id=>"fcCreateMainAssingee"}
E, [2017-12-11T15:19:00.685450 #5324] ERROR -- create_new_matter:  failed.
I, [2017-12-11T15:19:00.725482 #5324]  INFO -- screenshot: Screenshot saved in path: /Users/admin/PivotQA/screenshots/2017-12-11_1
5_18_15/MattersLib_create_new_matter_20171211_151900.png
E, [2017-12-11T15:19:00.725531 #5324] ERROR -- : An element command could not be completed because the element is not visible on t
he page.

即使成功检查该元素是否已启用且可见,使用selenium已启用,这种情况仍会发生?并显示?方法

但是,在Chrome,IE和Edge等其他浏览器上看不到这个问题。

我使用了javascript(" document.getElementById(' fcCreateMainAssingee' .value =' text')")来更改该文本字段的值它运行正常,但不显示动态选项下拉列表,这就是问题所在。

那么,是否有人遇到类似问题并解决了这个问题?很高兴得到你的帮助。

此外,如果有任何其他方式可以完成此建议。

以下是该自动填充字段的DOM:

<input formcontrolname="fcCreateMainAssingee" id="fcCreateMainAssingee" type="text" class="ng-untouched ng-pristine ng-valid k-input" data-role="autocomplete" placeholder="Username" autocomplete="off" role="textbox" aria-haspopup="true" aria-disabled="false" aria-readonly="false" aria-owns="fcCreateMainAssingee_listbox" aria-autocomplete="list" style="width: 100%;">

谢谢!

1 个答案:

答案 0 :(得分:0)

在我对Kendo自动填充字段的研究中,我偶然发现了有助于解决此问题的JavaScript代码。

以下是硒功能的可靠替代方案,并已在Safari,Chrome,IE和Edge浏览器上进行了全面测试,并且工作正常。

希望这能帮助过去几天与我在同一条船上的人!

这里有JS for send_keys的硒等价物 -

"document.getElementById('<ID>').value='<search_string>'"

(无需将文本清除为search_string中的js类型,删除现有的占位符字符串)

然后在键入后触发搜索,执行以下js:

"$( #{locator[:id]} ).data('kendoAutoComplete').search();"

以上js将触发搜索,然后动态下拉打开。现在,只需要使用selenium的find_element方法通过xpath找到该元素,然后使用js点击它(下面给出)

js_script = 'arguments[0].click();'
element   = web_driver.find_element(locator)
web_driver.execute_script(js_script, element)

这是完整的方法:

def kendo_auto_complete(locator, loc_name, full_name, raise_exception = true)
    $logger.info(__method__) { 'begin' }
    # Split full name ( Last_name, first_name) and get first_name
    first_name = full_name.split(",").last.strip
    # Type in the name using JS
    $logger.info(__method__) { "Typing #{first_name} into #{loc_name}" }
    web_driver.execute_script("document.getElementById('#{locator[:id]}').value='#{first_name}'")
    short_delay
    #Trigger search using JS
    $logger.info(__method__) { "Starting search.." }
    if BROWSER.upcase == 'IE'
      web_driver.execute_script("$( '##{locator[:id]}' ).data('kendoAutoComplete').search();")
    else
      web_driver.execute_script("$( #{locator[:id]} ).data('kendoAutoComplete').search();")
    end
    # Select the name from results in dynamic dropdown
    wait_for_element(create_dynamic_xpath(AUTO_CMP_OPTION[:xpath],full_name), full_name)
    js_click(create_dynamic_xpath(AUTO_CMP_OPTION[:xpath],full_name), full_name)
    $logger.info(__method__) { 'end' }
rescue StandardError => e
    $logger.error "Value of Kendo autocomplete - #{name} could not be set."
    raise e if raise_exception
    false
end

但是,如果某人有更好的做事方式,请在这里发表评论。谢谢!