Exemplo n.º 1
0
        public async Task CloseAsync()
        {
            if (_runContext.IsBeingDebugged)
            {
                Log("CloseAsync(): Debug mode detected, leaving browser open");
                return;
            }
            else
            {
                Log("CloseAsync(): Attempting to close browser...");
            }

            // note you can't use any of the following, since the process to start Chrome
            // opens the addtional window on the main chrome process and then exits immediately
            //process.CloseMainWindow();
            //process.Close();
            //process.Kill();

            // this won't work either because Chrome consists of 7 different processes, this will only kill the windowed process
            //foreach (Process process in Process.GetProcessesByName("chrome"))
            //{
            //	if (process.MainWindowHandle == IntPtr.Zero)
            //		continue;
            //	if (process.MainWindowTitle.StartsWith("Courgette Test Runner"))
            //		process.CloseMainWindow();
            //}

            // use CDP to close Chrome (should also work for Chromium, Firefox, Edge 76+ and Opera, but NOT Safari)
            Log($"Attempting to connect to browser using CDP on port {_chromeDebuggingPort}");
            CdpClient cdp = new CdpClient(_chromeDebuggingPort);

            // wait for browser to close
            string result = await cdp.CloseBrowserAsync().ConfigureAwait(false);
        }
Exemplo n.º 2
0
        public async Task LaunchAsync()
        {
            //Debugger.Break();

            // ensure http listener has started (HttpServer is started synchronously, so not currently required, but listener code might change to async startup)
            while (VsHttpServer.Port == 0)
            {
                await Task.Delay(100).ConfigureAwait(false);
            }

            ushort vsListeningPort = VsHttpServer.Port;

            // Note that Chrome only supports single debugging port per user profile, if you want
            // multiple concurrent instances you need to configure the --user-data-dir startup param
            //	https://stackoverflow.com/questions/52797350/multithreading-chromedriver-does-not-open-url-in-second-window/52799116#52799116

            // for list of some of the Chrome startup params, look here:
            // https://github.com/GoogleChrome/chrome-launcher/blob/master/docs/chrome-flags-for-tools.md

            // --remote-debugging-port option keeps chrome open until we close it
            //string args = $"--user-data-dir={Logger.LogPath}{_chromeDebuggingPort} --enable-automation --disable-extensions --no-sandbox --disable-gpu --remote-debugging-port={_chromeDebuggingPort}";
            string args;

            if (_runContext.IsBeingDebugged)
            {
                args = $"--user-data-dir={Logger.LogPath}{_chromeDebuggingPort} --remote-debugging-port={_chromeDebuggingPort}";
            }
            else
            {
                args = $"--user-data-dir={Logger.LogPath}{_chromeDebuggingPort} --enable-automation --disable-extensions --no-sandbox --headless --disable-gpu --remote-debugging-port={_chromeDebuggingPort}";
            }
            Log($"Chrome cmd line args are : {args}");

            // start chrome with no initial URL specified
            const string chromeExe = @"c:\Program Files (x86)\Google\Chrome\Application\chrome.exe";
            var          process   = Process.Start(chromeExe, args);

            // next get websocket endpoint uri using http via the debugging port
            // note that trying to get this from within the browser is problematic because you will be hitting CORS violations, CORS applies to http only, not web sockets
            CdpClient cdp          = new CdpClient(_chromeDebuggingPort);
            string    wsBrowserUri = await cdp.CdpConfig.LoadConfigAsync();

            // run specified test file and also pass in VS listening port and WS endpoint uri
            string url = $"http://localhost:10202/TestRunner.html?wwwroot={_wwwroot}&testFileUrl={_testFileUrl}&vsPort={vsListeningPort}&wsUri={wsBrowserUri}";
            string response;

            response = await cdp.EnablePageEventsAsync();

            response = await cdp.EnableNetworkEventsAsync();

            response = await cdp.NavigatePageAsync(url);
        }