使用SpecFlow,xUnit和Headless Chrome进行并行测试

时间:2018-03-26 12:25:37

标签: c# selenium specflow xunit google-chrome-headless

我想在无头Chrome中并行运行一些SpecFlow场景测试。我正在使用SpecFlow / xUnit并使用Selenium在我的BeforeScenario方法中启动无头Chrome,如下所示:

public class FeatureProperties : BaseSteps
{
    private static Object myLockObj = new Object();
    private static readonly Random Randomiser = new Random();

    private IObjectContainer Container { get; set; }

    public FeatureProperties(PageContext _context, IObjectContainer _container) : base(_context)
    {
        Container = _container;
    }

    [BeforeScenario]
    public void BeforeScenario()
    {
        var options = new ChromeOptions();
        options.AddArguments("--incognito", "--headless", "--window-size=1920,1080", "--disable-gpu", "--no-sandbox");

        options.AddArguments("--proxy-server='direct://'");
        options.AddArguments("--proxy-bypass-list=*");

        lock (myLockObj)
        {
            var rnd = Randomiser.Next(29700, 29900);
            Console.WriteLine("Randomised: " + rnd);
            var svc = ChromeDriverService.CreateDefaultService();
            svc.Port = rnd;
            svc.Start();
            Context.Driver = new ChromeDriver(svc, options);
        }
    }

当我执行xunit.console.exe

时,我在命令行中看到了这一点
Starting ChromeDriver 2.37.543627 (63642262d9fb93fb4ab52398be4286d844092a5e) on port 29780
Only local connections are allowed.
Starting ChromeDriver 2.37.543627 (63642262d9fb93fb4ab52398be4286d844092a5e) on port 29780
Only local connections are allowed.
[1522066314.720][SEVERE]: bind() returned an error: Only one usage of each socket address (protocol/network address/port) is normally permitted. (0x2740)

我理解(?)Chrome想要启动其他端口,但我不知道如何配置它。添加这个没有帮助:

svc.Port = new System.Random().Next(29700, 29900);

供参考,以下是我的packages.config:

的相关部分
<packages>
  <package id="Selenium.Support" version="3.11.0" targetFramework="net461" />
  <package id="Selenium.WebDriver" version="3.11.0" targetFramework="net461" />
  <package id="Selenium.WebDriver.ChromeDriver" version="2.37.0" targetFramework="net461" />
  <package id="SpecFlow" version="2.3.1" targetFramework="net461" />
  <package id="SpecFlow.xUnit" version="2.3.1" targetFramework="net461" />
  <package id="SpecFlow.xUnitAdapter" version="1.1.0" targetFramework="net461" />
  <package id="xunit" version="2.3.1" targetFramework="net461" />
  <package id="xunit.abstractions" version="2.0.1" targetFramework="net461" />
  <package id="xunit.analyzers" version="0.8.0" targetFramework="net461" />
  <package id="xunit.assert" version="2.3.1" targetFramework="net461" />
  <package id="xunit.core" version="2.3.1" targetFramework="net461" />
  <package id="xunit.extensibility.core" version="2.3.1" targetFramework="net461" />
  <package id="xunit.extensibility.execution" version="2.3.1" targetFramework="net461" />
  <package id="xunit.runner.visualstudio" version="2.3.1" targetFramework="net461" developmentDependency="true" />
</packages>

1 个答案:

答案 0 :(得分:0)

尝试使用一个共享的Random变量。 new System.Random()使用当前时间初始化伪随机数生成器。在并行执行的情况下,这很可能是同一时间,Next将导致所有呼叫的数字相同。