最近我开始使用 Robot 和 Selenium2Library 来自动化一些 GUI 测试用例。我正在自动化的应用程序之一是 ReviewBoard。
到目前为止,我已经能够自动化一些东西,但在将文本输入文本区域时遇到很多问题。一个例子是评论板上的描述字段。
我最近的尝试是
:FOR ${URL} in @{URL_LIST}
\ Go To ${URL}
# Enter team reviewer name and press ok
\ Click Element xpath=//*[@id="fieldset_reviewers_body"]/tr[2]/td/a/div[@class="rb-icon rb-icon-edit"]
\ Input Text xpath=//*[@id="fieldset_reviewers_body"]/tr[2]/td/form/input rbtest_teamreviewer1
\ Press Key xpath=//*[@id="fieldset_reviewers_body"]/tr[2]/td/form/input \\9
\ Click Element xpath=//*[@id="fieldset_reviewers_body"]/tr[2]/td/form/span/input[@class="save"]
# Fill out Testing Done field
\ Click Element xpath=//*[@id="review_request_main"]/div[2]/label/a/div[@class="rb-icon rb-icon-edit"]
\ Press Key xpath=//*[@id='review_request_main']/div[2]/div/form/*//textarea Testing Done
\ Click Element xpath=//*[@id="review_request_main"]/div[2]/div/form/div[2]/input[@class="save"]
但是,我收到了异常
ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with
Stacktrace:
at fxdriver.preconditions.visible (file:///tmp/tmpW24ACY/webdriver-py-profilecopy/extensions/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e2849a86908b948790a2858d8d858e87818d8687cc818d8f" rel="noreferrer noopener nofollow">[email protected]</a>/components/command-processor.js:10092)
at DelayedCommand.prototype.checkPreconditions_ (file:///tmp/tmpW24ACY/webdriver-py-profilecopy/extensions/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="64021c00160d12011624030b0b030801070b00014a070b09" rel="noreferrer noopener nofollow">[email protected]</a>/components/command-processor.js:12644)
at DelayedCommand.prototype.executeInternal_/h (file:///tmp/tmpW24ACY/webdriver-py-profilecopy/extensions/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="04627c60766d72617644636b6b636861676b60612a676b69" rel="noreferrer noopener nofollow">[email protected]</a>/components/command-processor.js:12661)
at DelayedCommand.prototype.executeInternal_ (file:///tmp/tmpW24ACY/webdriver-py-profilecopy/extensions/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1e78667a6c77687b6c5e79717179727b7d717a7b307d7173" rel="noreferrer noopener nofollow">[email protected]</a>/components/command-processor.js:12666)
at DelayedCommand.prototype.execute/< (file:///tmp/tmpW24ACY/webdriver-py-profilecopy/extensions/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d7b1afb3a5bea1b2a597b0b8b8b0bbb2b4b8b3b2f9b4b8ba" rel="noreferrer noopener nofollow">[email protected]</a>/components/command-processor.js:12608)
我尝试了不同的方法,例如使用输入文本而不是按键,但遇到类似的问题...输入类型时没有任何问题。
有人知道我该如何解决这个问题吗?
如果您有兴趣,可以查看演示评论板:http://demo.reviewboard.org/r/1502/用户名:guest6317 密码:demo
最佳答案
CodeMirror 用它自己的对象替换文本区域。该对象具有与小部件交互的方法。但是,在评论板的情况下,只有单击文本区域后,该对象才会初始化。因此,解决方案将如下所示:
- 找到并单击文本区域
- 查找对 CodeMirror 编辑器对象的引用
- 使用 CodeMirror 方法与编辑器小部件交互
第 1 步:点击文本区域
第一步是单击文本区域以初始化小部件。这可以通过“Click Element”关键字轻松完成:
click element id=field_description
第 2 步:获取编辑器对象的引用
评审板开发人员可能拥有该对象的引用,因此您可以向他们询问变量的名称。但是,我们可以创建自己的变量用于测试目的。 CodeMirror 在包含编辑器的 div 上添加了一个 CodeMirror
属性,因此可以使用此信息来保存对临时 JavaScript 变量的引用:
Execute javascript
... _editor = document.querySelectorAll("div.CodeMirror")[0].CodeMirror;
第 3 步:与编辑器交互
CodeMirror 编辑器有 many methods用于与编辑器的内容进行交互。例如,如果您想用自己的字符串替换内容,可以调用 setValue
方法。
例如,将所有数据替换为“Hello world!”你可以这样做:
execute javascript _editor.setValue("Hello world!");
第 4 步:将所有内容放在一起
这是一个完整的测试脚本,它将编辑器的内容替换为“Hello world”,然后暂停,以便您可以验证它是否有效。我在带有 chrome 和 firefox 的 Linux 系统上对此进行了测试。
*** Variables ***
${ROOT} http://demo.reviewboard.org
${BROWSER} chrome
${USERNAME} guest6317
${PASSWORD} demo
*** Settings ***
Library Selenium2Library
Library Dialogs
Suite Setup open browser ${ROOT} ${BROWSER}
Suite Teardown close all browsers
*** Test Cases ***
Example
[Setup] run keywords
... Log in
... AND go to ${ROOT}/r/1502/
click element id=field_description
Execute javascript
... _editor = document.querySelectorAll("div.CodeMirror")[0].CodeMirror;
... _editor.setValue("Hello world!");
pause execution
*** Keywords ***
Log in
go to ${ROOT}/account/login
input text id=id_username ${USERNAME}
input text id=id_password ${PASSWORD}
submit form
关于selenium - Robot Selenium - 在 CodeMirror 文本区域中输入文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36805297/