javascript 如何使用 Robot Framework 处理提示框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23543666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to handle prompt box with Robot Framework?
提问by Illya
I am using Robot Framework with Selenium2Library for website tests automation. In one of the cases there is a prompt box (pop-up similar to alert, but with an input field in it, see example here) asking for some text. The problem is Robot Framework can only click OK or Cancel (Confirm Action and Choose Cancel On Next Confirmation keywords) on such pop-ups. So the question is: how can I input some text into the prompt box? Is it possible?
我正在使用带有 Selenium2Library 的 Robot Framework 进行网站测试自动化。在其中一种情况下,有一个提示框(类似于警报的弹出窗口,但其中有一个输入字段,请参见此处的示例)要求输入一些文本。问题是 Robot Framework 只能在此类弹出窗口中单击 OK 或 Cancel(Confirm Action 并选择 Cancel On Next Confirmation 关键字)。所以问题是:如何在提示框中输入一些文本?是否可以?
In SeleniumLibrary there was a Press Key Native keyword which could press keys without specifying the target element, but it is absent in Selenium2Library. If you know of any alternative - your answer will be much appreciated.
在 SeleniumLibrary 中有一个 Press Key Native 关键字,它可以在不指定目标元素的情况下按下按键,但它在 Selenium2Library 中不存在。如果您知道任何替代方案 - 您的回答将不胜感激。
Using AutoIT isn't an option as the tests could be run on different platforms (not only Win).
使用 AutoIT 不是一种选择,因为测试可以在不同的平台上运行(不仅是 Win)。
Am I missing something?
我错过了什么吗?
回答by Bryan Oakley
Selenium2Library doesn't currently have support for inserting text into a prompt. I've opened an issue in the issue tracker for this:
Selenium2Library 目前不支持在提示中插入文本。我为此在问题跟踪器中打开了一个问题:
https://github.com/rtomac/robotframework-selenium2library/issues/292
https://github.com/rtomac/robotframework-selenium2library/issues/292
Until it gets added, you can create your own selenium library by subclassing Selenium2Library, and you can add the function to your version.
在它被添加之前,您可以通过继承 Selenium2Library 来创建您自己的 selenium 库,并且您可以将该函数添加到您的版本中。
For example, create a file named "CustomSeleniumLibrary.py", and make it look like this:
例如,创建一个名为“CustomSeleniumLibrary.py”的文件,并使其看起来像这样:
# CustomSeleniumLibrary.py
from Selenium2Library import Selenium2Library
class CustomSeleniumLibrary(Selenium2Library):
def insert_into_prompt(self, text):
alert = None
try:
alert = self._current_browser().switch_to_alert()
alert.send_keys(text)
except WebDriverException:
raise RuntimeError('There were no alerts')
You can then write a test case which uses that library like this:
然后,您可以编写一个使用该库的测试用例,如下所示:
*** Settings ***
| Library | CustomSeleniumLibrary.py
| Suite Teardown | Close All Browsers
*** test cases ***
| Example of typing into a prompt
| | Open Browser | http://www.w3schools.com/js/tryit.asp?filename=tryjs_prompt
| | Select Frame | iframeResult
| | Click Button | Try it
| | Insert into prompt | my name is Inigo Montoya
| | Confirm action
回答by Phil Quarrell
Think it's worth pointing out that this keyword exists now (since SeleniumLibrary 3.0), so there's no longer any need to use a custom script/library. http://robotframework.org/Selenium2Library/Selenium2Library.html#Input%20Text%20Into%20Alert
认为值得指出的是,这个关键字现在存在(自 SeleniumLibrary 3.0 起),因此不再需要使用自定义脚本/库。 http://robotframework.org/Selenium2Library/Selenium2Library.html#Input%20Text%20Into%20Alert