Atata附加到浏览器实例

问题描述

我知道Selenium中有一种方法可以启动浏览器(至少在Chrome中),然后再附加到该实例。您可以通过Atata做同样的事情吗?

解决方法

以下是启动Chrome的示例,然后将Atata(ChromeDriver实例)附加到创建的Chrome。

// Set static or find available port number:
int chromePort = 9222;

// Run Chrome process:
Process chromeProcess = new Process
{
    StartInfo = new ProcessStartInfo
    {
        FileName = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe",Arguments = $"https://demo.atata.io/ --new-window --remote-debugging-port={chromePort} --user-data-dir=C:\\Temp"
    }
};

chromeProcess.Start();

// Create Atata context attached to the Chrome:
AtataContext.Configure()
    .UseChrome()
        .WithOptions(x => x.DebuggerAddress = $"127.0.0.1:{chromePort}")
    .Build();

// Do some actions using Atata:
Go.To<OrdinaryPage>(url: "https://demo.atata.io/products")
    .PageTitle.Should.Contain("Products");

// Clean up (just don't do it exactly like here. Use "using (...)",etc.):
AtataContext.Current.Dispose();
chromeProcess.CloseMainWindow();
chromeProcess.Dispose();

要附加到Chrome的主要内容是.UseChrome().WithOptions(x => x.DebuggerAddress = $"127.0.0.1:{chromePort}")