ChromeDriver exception reporting “target window already closed” in headless mode

前端 未结 1 1587
一向
一向 2021-02-11 08:33

I am using xUnit, SpecFlow, Selenium and headless Chrome to run automated tests, but every so often I will get a series of crashes when running on localhost when tr

相关标签:
1条回答
  • 2021-02-11 09:01

    As discussed the issue could be because of multiple reasons

    1. svc.Port = Randomiser.Next(29700, 29900);. You should never assign the port yourself, it can be conflicting with some other program port as well and even the same port number can be generated in random which will cause the issue

    2. You are launching scripts in parallel. In rare cases there could be a race condition in driver launch and the driver may fail to launch. So I would add Factory pattern to my driver initialization code like below

      public sealed class ChromeFactory 
      { 
          private static readonly object padlock = new object();
      
          ChromeFactory()
          {
          }
      
          public static WebDriver NewInstance
          {
              get
              {
                  lock (padlock)
                  {
                      return new ChromeDriver();
                  }
              }
          }
      }
      

    And initialize the driver like below

    Context.Driver = ChromeFactory.NewInstance
    
    1. Since you have parallel execution, I would recommend instead of using #2 suggestion, setup a selenium grid a browser limit count and use that. This will make life much more simpler for you
    0 讨论(0)
提交回复
热议问题