简体   繁体   English

Selenium webdriver - 如何连接到由 Visual Studio 使用 C# 打开的同一个 firefox 实例

[英]Selenium webdriver - How to connect to the same firefox instance open by Visual Studio using C#

Hello friends.大家好。 I would like some help.我想要一些帮助。

I've been trying to use the same instance of Firefox window open by Visual Studio at first time.我一直在尝试使用第一次由 Visual Studio 打开的 Firefox window 的相同实例。

The question is, how to pickup the same window browser and continue to testing without it close the browser and open again every time i want to do a testing?问题是,如何获取相同的 window 浏览器并继续测试而不关闭浏览器并在每次我想进行测试时再次打开?

I know there is a lot of questions about this topic, but some that I've found is applicable for old versions of selenium and some codes didn't work due to be deprecated.我知道关于这个主题有很多问题,但我发现一些问题适用于旧版本的 selenium 并且一些代码由于被弃用而无法工作。

What i can do with success:我能做些什么成功:

I can work normally with traditional method to test a unit, opening Firefox at beginning and close after the end of test:我可以用传统方法正常工作来测试一个单元,在开始时打开 Firefox 并在测试结束后关闭:

[ClassInitialize]
public static void InitializeClass(TestContext testContext) {

    driver = new FirefoxDriver();
}

Then i commented the following lines to keep the window open when test is complete:然后我评论了以下几行,以在测试完成时保持 window 打开:

//driver.Quit();
//driver.Close();
//driver.Dispose();

What i can't manage to:我无法做到:

With searches I've been made, i read that we can connect to an existing session already open before.通过我进行的搜索,我了解到我们可以连接到之前已经打开的现有 session。 So i tried with the following:所以我尝试了以下方法:

[ClassInitialize]
public static void InitializeClass(TestContext testContext) {


    FirefoxOptions options = new FirefoxOptions();
    driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), options);

    //driver = new FirefoxDriver();

On the example above, I've commented driver = new FirefoxDriver();在上面的例子中,我注释了driver = new FirefoxDriver(); and used the method FirefoxOptions instead of the deprecated DesiredCapabilities.firefox() and when i try to connect to the open firefox window, i got an error and i can't go on.并使用FirefoxOptions方法而不是已弃用的DesiredCapabilities.firefox()方法,当我尝试连接到打开的 firefox window 时,我得到了一个错误,我不能在 Z34D1F91FB2E514B896BA8 上打开。 On log, there's a line saying:在日志中,有一行说:

Was'n possible to copy the file "..\packages\Selenium.Firefox.WebDriver.0.27.0\build\driver\geckodriver.exe" to "..\bin\debug\geckodriver.exe"

The file geckodriver.exe (61484) is being used by another process

With this, i tried another different ways to reach what i expect like:有了这个,我尝试了另一种不同的方法来达到我的期望:

[ClassInitialize]
public static void InitializeClass(TestContext testContext) {
    
    
     FirefoxOptions options = new FirefoxOptions();
     driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), options);
    
               
     options.AddArgument("--marionette-port " + "2828" + "--connect-existing");
     FirefoxDriverService ffDriverService = FirefoxDriverService.CreateDefaultService();           
     ffDriverService.BrowserCommunicationPort = 4444;
}

for last i tried to open a separated instance of Firefox to try to connect to like below:最后,我尝试打开 Firefox 的单独实例以尝试连接到如下所示:

firefox.exe --marionette -start-debugger-server 2828 -profile %temp%\FirefoxTEMP

I got success on open a instance of marionette,but i continuing unable to attach the test to this open window.我成功打开了一个木偶实例,但我仍然无法将测试附加到这个打开的 window。

I appreciate your attention and forgive me for some newbie techniques i made.感谢您的关注,并原谅我制作的一些新手技巧。

Questions I've read:我读过的问题:

How to connect Selenium to existing Firefox browser? 如何将 Selenium 连接到现有的 Firefox 浏览器? (Python) (Python)

How to connect Selenium Webdriver to existing Firefox/Chrome browser session? 如何将 Selenium Webdriver 连接到现有的 Firefox/Chrome 浏览器 session?

How to connect to an already open browser? 如何连接到已经打开的浏览器?

  1. You can use "detach" and change Firefox to Chrome(because I didn't see that option in Firefox).您可以使用“分离”并将 Firefox 更改为 Chrome(因为我在 Firefox 中没有看到该选项)。 Link to link .链接到链接 Shortly, it keeps your browser open depending on the option "true"/"false".很快,它会根据“true”/“false”选项让您的浏览器保持打开状态。

ChromeOptions options = new ChromeOptions(); ChromeOptions 选项 = 新 ChromeOptions();

options.setExperimentalOption("detach", true); options.setExperimentalOption("分离", true);

  1. You can use bad habits and simply use a global variable.您可以使用坏习惯并简单地使用全局变量。 global browser全局浏览器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Visual Studio 通过 C# 连接到 SQL 服务器实例 - How to connect to a SQL Server instance with C# using Visual Studio 使用C#与Selenium WebDriver和NUnit一起调试Visual Studio 2013 - Debug Visual Studio 2013 Using C# with Selenium WebDriver and NUnit 在Visual Studio中使用C#和ChromeDriver进行MS Test的Selenium WebDriver - Selenium WebDriver with MS Test using C# and ChromeDriver in Visual Studio Selenium Webdriver带有C#和Visual Studio的dependsOnMethod - Selenium Webdriver dependsOnMethod with C# and Visual Studio 使用Firefox便携式的C#硒webdriver - c# selenium webdriver using firefox portable 我无法使用 FireFox C# selenium webdriver 打开我的应用程序 - I am not able to open my application using FireFox C# selenium webdriver Selenium WebDriver C#Visual Studio N单元测试名称 - Selenium WebDriver C# Visual Studio N Unit Test Name 在 C# 中的 Selenium WebDriver 中使用特定的 Firefox 配置文件 - Using a specific Firefox profile in Selenium WebDriver in C# C# Selenium WebDriver FireFox Profile - 使用代理和身份验证 - C# Selenium WebDriver FireFox Profile - using proxy with Authentication 如何使用Visual Studio在C#中创建硒测试 - how to create a test for selenium in C# using visual studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM