簡體   English   中英

Selenium webdriver - 如何連接到由 Visual Studio 使用 C# 打開的同一個 firefox 實例

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

大家好。 我想要一些幫助。

我一直在嘗試使用第一次由 Visual Studio 打開的 Firefox window 的相同實例。

問題是,如何獲取相同的 window 瀏覽器並繼續測試而不關閉瀏覽器並在每次我想進行測試時再次打開?

我知道關於這個主題有很多問題,但我發現一些問題適用於舊版本的 selenium 並且一些代碼由於被棄用而無法工作。

我能做些什么成功:

我可以用傳統方法正常工作來測試一個單元,在開始時打開 Firefox 並在測試結束后關閉:

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

    driver = new FirefoxDriver();
}

然后我評論了以下幾行,以在測試完成時保持 window 打開:

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

我無法做到:

通過我進行的搜索,我了解到我們可以連接到之前已經打開的現有 session。 所以我嘗試了以下方法:

[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();

在上面的例子中,我注釋了driver = new FirefoxDriver(); 並使用FirefoxOptions方法而不是已棄用的DesiredCapabilities.firefox()方法,當我嘗試連接到打開的 firefox window 時,我得到了一個錯誤,我不能在 Z34D1F91FB2E514B896BA8 上打開。 在日志中,有一行說:

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

有了這個,我嘗試了另一種不同的方法來達到我的期望:

[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;
}

最后,我嘗試打開 Firefox 的單獨實例以嘗試連接到如下所示:

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

我成功打開了一個木偶實例,但我仍然無法將測試附加到這個打開的 window。

感謝您的關注,並原諒我制作的一些新手技巧。

我讀過的問題:

如何將 Selenium 連接到現有的 Firefox 瀏覽器? (Python)

如何將 Selenium Webdriver 連接到現有的 Firefox/Chrome 瀏覽器 session?

如何連接到已經打開的瀏覽器?

  1. 您可以使用“分離”並將 Firefox 更改為 Chrome(因為我在 Firefox 中沒有看到該選項)。 鏈接到鏈接 很快,它會根據“true”/“false”選項讓您的瀏覽器保持打開狀態。

ChromeOptions 選項 = 新 ChromeOptions();

options.setExperimentalOption("分離", true);

  1. 您可以使用壞習慣並簡單地使用全局變量。 全局瀏覽器

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM