How to use KillProcess method of PuppeteerSharp.Tests.FixturesTests.FixturesTests class

Best Puppeteer-sharp code snippet using PuppeteerSharp.Tests.FixturesTests.FixturesTests.KillProcess

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

FixturesTests.cs

Source: FixturesTests.cs Github

copy
1using System;
2using System.Diagnostics;
3using System.IO;
4using System.Linq;
5using System.Net;
6using System.Runtime.InteropServices;
7using System.Threading.Tasks;
8using PuppeteerSharp.Helpers;
9using PuppeteerSharp.Transport;
10using Xunit;
11using Xunit.Abstractions;
12
13namespace PuppeteerSharp.Tests.PuppeteerTests
14{
15    [Collection("PuppeteerLoaderFixture collection")]
16    public class FixturesTests : PuppeteerBaseTest
17    {
18        public FixturesTests(ITestOutputHelper output) : base(output) { }
19
20        [Fact]
21        public void ShouldDumpBrowserProcessStderr()
22        {
23            var success = false;
24            var process = GetTestAppProcess(
25                "PuppeteerSharp.Tests.DumpIO",
26                $"\"{new BrowserFetcher().RevisionInfo(BrowserFetcher.DefaultRevision).ExecutablePath}\"");
27
28            process.ErrorDataReceived += (sender, e) =>
29            {
30                success |= e.Data != null && e.Data.Contains("DevTools listening on ws://");
31            };
32
33            process.Start();
34            process.BeginErrorReadLine();
35            process.WaitForExit();
36            Assert.True(success);
37        }
38
39        [Fact]
40        public async Task ShouldCloseTheBrowserWhenTheConnectedProcessCloses()
41        {
42            var browserClosedTaskWrapper = new TaskCompletionSource<bool>();
43            var chromiumProcess = new ChromiumProcess(
44                new BrowserFetcher().RevisionInfo(BrowserFetcher.DefaultRevision).ExecutablePath,
45                new LaunchOptions { Headless = true },
46                TestConstants.LoggerFactory);
47
48            await chromiumProcess.StartAsync().ConfigureAwait(false);
49
50            var browser = await Puppeteer.ConnectAsync(new ConnectOptions
51            {
52                BrowserWSEndpoint = chromiumProcess.EndPoint
53            });
54
55            browser.Disconnected += (sender, e) =>
56            {
57                browserClosedTaskWrapper.SetResult(true);
58            };
59
60            KillProcess(chromiumProcess.Process.Id);
61
62            await browserClosedTaskWrapper.Task;
63            Assert.True(browser.IsClosed);
64        }
65
66        [Fact]
67        public async Task ShouldCloseTheBrowserWhenTheLaunchedProcessCloses()
68        {
69            var browserClosedTaskWrapper = new TaskCompletionSource<bool>();
70            var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true }, TestConstants.LoggerFactory);
71
72            browser.Disconnected += (sender, e) =>
73            {
74                browserClosedTaskWrapper.SetResult(true);
75            };
76
77            KillProcess(browser.ChromiumProcess.Process.Id);
78
79            await browserClosedTaskWrapper.Task;
80            Assert.True(browser.IsClosed);
81        }
82
83        private void KillProcess(int pid)
84        {
85            var process = new Process();
86
87            //We need to kill the process tree manually
88            //See: https://github.com/dotnet/corefx/issues/26234
89            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
90            {
91                process.StartInfo.FileName = "taskkill";
92                process.StartInfo.Arguments = $"-pid {pid} -t -f";
93            }
94            else
95            {
96                process.StartInfo.FileName = "/bin/bash";
97                process.StartInfo.Arguments = $"-c \"kill -s 9 {pid}\"";
98            }
99
100            process.Start();
101            process.WaitForExit();
102        }
103
104        private Process GetTestAppProcess(string appName, string arguments)
105        {
106            var process = new Process();
107
108#if NETCOREAPP
109            process.StartInfo.WorkingDirectory = GetSubprocessWorkingDir(appName);
110            process.StartInfo.FileName = "dotnet";
111            process.StartInfo.Arguments = $"{appName}.dll {arguments}";
112#else
113            process.StartInfo.FileName = Path.Combine(GetSubprocessWorkingDir(appName), $"{appName}.exe");
114            process.StartInfo.Arguments = arguments;
115#endif
116            process.StartInfo.UseShellExecute = false;
117            process.StartInfo.RedirectStandardError = true;
118            return process;
119        }
120
121        private string GetSubprocessWorkingDir(string dir)
122        {
123#if DEBUG
124            var build = "Debug";
125#else
126            
127            var build = "Release";
128#endif
129#if NETCOREAPP
130            return Path.Combine(
131                TestUtils.FindParentDirectory("lib"),
132                dir,
133                "bin",
134                build,
135                "netcoreapp2.2");
136#else
137            return Path.Combine(
138                TestUtils.FindParentDirectory("lib"),
139                dir,
140                "bin",
141                build,
142                "net471");
143#endif
144        }
145    }
146}
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Trigger KillProcess code on LambdaTest Cloud Grid

Execute automation tests with KillProcess on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)