有没有办法将已经运行的浏览器附加到 java 中的 selenium webdriver?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11969893/
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
Is there any way to attach an already running browser to selenium webdriver in java?
提问by Satish Pandey
I am developing a application in which we need to check if the web application is running and the url giving the expected results.
我正在开发一个应用程序,我们需要在其中检查 Web 应用程序是否正在运行以及提供预期结果的 url。
Our Scheduler run this application 12 times a day. In every launch the firefox driver gets executed and a new browser window opens to perform the operations.
我们的调度程序每天运行此应用程序 12 次。在每次启动时,firefox 驱动程序都会被执行,并会打开一个新的浏览器窗口来执行操作。
I want a technique where we open the firefox browser 1 time and reuse it in every call by selenium-driver.
我想要一种技术,我们打开 Firefox 浏览器 1 次并在 selenium-driver 的每次调用中重用它。
采纳答案by some_other_guy
I am actually not entirely sure you can switch to a window not spawned by the driver. I think the people working on the selenium 2 project have worked a little bit on switching to a window spawned by a different driver. When you do:
我实际上并不完全确定您可以切换到不是由驱动程序生成的窗口。我认为从事 selenium 2 项目的人员在切换到由不同驱动程序生成的窗口方面做了一些工作。当你这样做时:
driver.getWindowHandles()
All you get are the windows spawned by the driver object it is called on.
你得到的只是由它被调用的驱动程序对象产生的窗口。
This is a pretty old feature request: Allow webdriver to attach to a running browser. So it's not possible right now.
这是一个相当古老的功能请求:Allow webdriver to attach to a running browser。所以现在是不可能的。
回答by djangofan
You can see an example I wrotethat proves that re-using browsers works just fine in WebDriver. Basically, as long as a browser window is instantiated from WebDriver, then you can use the getWindowHandles() to always grab onto it. If you have multiple windows open, just keep track of them using a List . You can identify windows that appear a certain way or contain certain information in them by using various WebDriver methods.
你可以看到我写的一个例子,它证明在 WebDriver 中重用浏览器工作得很好。基本上,只要浏览器窗口是从 WebDriver 实例化的,那么您就可以使用 getWindowHandles() 来始终抓住它。如果您打开了多个窗口,只需使用 List 跟踪它们。您可以使用各种 WebDriver 方法识别以某种方式出现或其中包含某些信息的窗口。
In other words, if a browser window was not opened by WebDriver, then WebDriver has no ability to hook onto it.
换句话说,如果浏览器窗口不是由 WebDriver 打开的,那么 WebDriver 就无法钩住它。
A rough example:
一个粗略的例子:
public static boolean selectWindow(WebDriver driver, String windowTitle){
//Search ALL currently available windows
for (String handle : driver.getWindowHandles()) {
String newWindowTitle = driver.switchTo().window(handle).getTitle();
if(newWindowTitle.equalsIgnoreCase(windowTitle))
//if it was found break out of the wait
return true;
}
return false;
}
In one project I did, I created a method that returns certain int status codes, depending on my arrangement of windows. If the status code is what I am expecting , then I know the next test can proceed without logging in again or without opening a new window.
在我做过的一个项目中,我创建了一个方法,该方法返回某些 int 状态代码,具体取决于我对窗口的排列。如果状态代码是我所期望的,那么我知道下一个测试可以继续进行而无需再次登录或无需打开新窗口。
Of course, if your test framework, such as Surefire or TestNG, forks threads by class, then you need one webdriver instance per class. If your test framework forks by method, then you'll need to pass the webdriver instance as an argument to the test method so the thread has access to it.
当然,如果您的测试框架,例如 Surefire 或 TestNG,按类分叉线程,那么每个类需要一个 webdriver 实例。如果您的测试框架按方法派生,那么您需要将 webdriver 实例作为参数传递给测试方法,以便线程可以访问它。