How to use ShouldRejectIfExecutablePathIsInvalid method of Microsoft.Playwright.Tests.BrowserTypeLaunchTests class

Best Playwright-dotnet code snippet using Microsoft.Playwright.Tests.BrowserTypeLaunchTests.ShouldRejectIfExecutablePathIsInvalid

Run Playwright-dotnet automation tests on LambdaTest cloud grid

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

BrowserTypeLaunchTests.cs

Source: BrowserTypeLaunchTests.cs Github

copy
1/*
2 * MIT License
3 *
4 * Copyright (c) Microsoft Corporation.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25using System.Collections.Generic;
26using System.Threading.Tasks;
27using Microsoft.Playwright.NUnit;
28using NUnit.Framework;
29
30namespace Microsoft.Playwright.Tests
31{
32    ///<playwright-file>browsertype-launch.spec.ts</playwright-file>
33    public class BrowserTypeLaunchTests : PlaywrightTestEx
34    {
35        [PlaywrightTest("browsertype-launch.spec.ts", "should reject all promises when browser is closed")]
36        public async Task ShouldRejectAllPromisesWhenBrowserIsClosed()
37        {
38            await using var browser = await BrowserType.LaunchAsync();
39            var page = await (await browser.NewContextAsync()).NewPageAsync();
40            var neverResolves = page.EvaluateHandleAsync("() => new Promise(r => {})");
41            await browser.CloseAsync();
42            var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => neverResolves);
43            // WebKit under task-set -c 1 is giving browser, rest are giving target.
44            StringAssert.Contains(" closed", exception.Message);
45
46        }
47
48        [PlaywrightTest("defaultbrowsercontext-2.spec.ts", "should throw if page argument is passed")]
49        [Skip(SkipAttribute.Targets.Firefox)]
50        public Task ShouldThrowIfPageArgumentIsPassed()
51        {
52            var args = new[] { Server.EmptyPage };
53            return PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => BrowserType.LaunchAsync(new() { Args = args }));
54        }
55
56        [PlaywrightTest("browsertype-launch.spec.ts", "should reject if launched browser fails immediately")]
57        public async Task ShouldRejectIfLaunchedBrowserFailsImmediately()
58        {
59            await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => BrowserType.LaunchAsync(new() { ExecutablePath = TestUtils.GetAsset("dummy_bad_browser_executable.js") }));
60        }
61
62        [PlaywrightTest("browsertype-launch.spec.ts", "should reject if executable path is invalid")]
63        public async Task ShouldRejectIfExecutablePathIsInvalid()
64        {
65            var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => BrowserType.LaunchAsync(new() { ExecutablePath = "random-invalid-path" }));
66
67            StringAssert.Contains("Failed to launch", exception.Message);
68        }
69
70        [PlaywrightTest("browsertype-launch.spec.ts", "should fire close event for all contexts")]
71        public async Task ShouldFireCloseEventForAllContexts()
72        {
73            await using var browser = await BrowserType.LaunchAsync();
74            var context = await browser.NewContextAsync();
75            var closeTask = new TaskCompletionSource<bool>();
76
77            context.Close += (_, _) => closeTask.TrySetResult(true);
78
79            await TaskUtils.WhenAll(browser.CloseAsync(), closeTask.Task);
80        }
81
82        [PlaywrightTest("browsertype-launch.spec.ts", "should be callable twice")]
83        public async Task ShouldBeCallableTwice()
84        {
85            await using var browser = await BrowserType.LaunchAsync();
86            await TaskUtils.WhenAll(browser.CloseAsync(), browser.CloseAsync());
87            await browser.CloseAsync();
88        }
89
90        /// <summary>
91        /// PuppeteerSharp test. It's not in upstream
92        /// </summary>
93        public async Task ShouldWorkWithEnvironmentVariables()
94        {
95            var env = new Dictionary<string, string>
96            {
97                ["Foo"] = "Var"
98            };
99
100            await using var browser = await BrowserType.LaunchAsync(new() { Env = env });
101        }
102
103        /// <summary>
104        /// PuppeteerSharp test. It's not in upstream
105        /// </summary>
106        [Skip(SkipAttribute.Targets.Firefox, SkipAttribute.Targets.Webkit)]
107        public async Task ShouldWorkWithIgnoreDefaultArgs()
108        {
109            string[] args = new[]
110            {
111                "--remote-debugging-pipe",
112                "--headless",
113                "--hide-scrollbars",
114                "--mute-audio",
115                "--blink-settings=primaryHoverType=2,availableHoverTypes=2,primaryPointerType=4,availablePointerTypes=4"
116            };
117
118            await using var browser = await BrowserType.LaunchAsync(new() { IgnoreAllDefaultArgs = true, Args = args });
119        }
120    }
121}
122
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 ShouldRejectIfExecutablePathIsInvalid code on LambdaTest Cloud Grid

Execute automation tests with ShouldRejectIfExecutablePathIsInvalid 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)