-
Notifications
You must be signed in to change notification settings - Fork 295
Expand file tree
/
Copy pathPlaywrightSettingsProvider.cs
More file actions
128 lines (118 loc) · 4.64 KB
/
PlaywrightSettingsProvider.cs
File metadata and controls
128 lines (118 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* MIT License
*
* Copyright (c) Microsoft Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
using System;
using System.Text.Json;
using System.Xml;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter;
namespace Microsoft.Playwright.TestAdapter;
[ExtensionUri("settings://playwright")]
[SettingsName("Playwright")]
public class PlaywrightSettingsProvider : ISettingsProvider
{
private static PlaywrightSettingsXml? _settings = null!;
public static void LoadViaEnvIfNeeded()
{
if (_settings == null)
{
var settings = Environment.GetEnvironmentVariable("PW_INTERNAL_ADAPTER_SETTINGS");
if (!string.IsNullOrEmpty(settings))
{
_settings = JsonSerializer.Deserialize<PlaywrightSettingsXml>(settings);
}
else
{
_settings = new PlaywrightSettingsXml();
}
}
}
public static string BrowserName
{
get
{
var browserFromEnv = Environment.GetEnvironmentVariable("BROWSER")?.ToLowerInvariant();
// GitHub Codespaces and DevContainers sets the BROWSER environment variable, ignore it if its bogus.
if (!string.IsNullOrEmpty(browserFromEnv) && !browserFromEnv!.StartsWith("/vscode/"))
{
ValidateBrowserName(browserFromEnv!, "'BROWSER' environment variable", "\nTry to remove 'BROWSER' environment variable for using default browser");
return browserFromEnv!;
}
if (_settings != null && !string.IsNullOrEmpty(_settings.BrowserName))
{
var browser = _settings.BrowserName!.ToLowerInvariant();
ValidateBrowserName(browser, "run settings", string.Empty);
return browser;
}
return BrowserType.Chromium;
}
}
public static float? ExpectTimeout
{
get
{
if (_settings == null)
{
return null;
}
if (_settings.ExpectTimeout.HasValue)
{
return _settings.ExpectTimeout.Value;
}
return null;
}
}
public static BrowserTypeLaunchOptions LaunchOptions
{
get
{
var launchOptions = _settings?.LaunchOptions ?? new BrowserTypeLaunchOptions();
if (Environment.GetEnvironmentVariable("HEADED") == "1")
{
launchOptions.Headless = false;
}
else if (_settings != null && _settings.Headless.HasValue)
{
launchOptions.Headless = _settings.Headless.Value;
}
return launchOptions;
}
}
private static void ValidateBrowserName(string browserName, string fromText, string suffix)
{
if (browserName != BrowserType.Chromium &&
browserName != BrowserType.Firefox &&
browserName != BrowserType.Webkit)
{
throw new ArgumentException($"Invalid browser name from {fromText}.\n" +
$"Supported browsers: '{BrowserType.Chromium}', '{BrowserType.Firefox}', and '{BrowserType.Webkit}'\n" +
$"Actual browser: '{browserName}'{suffix}");
}
}
public void Load(XmlReader reader)
{
// NOTE: ISettingsProvider::Load is not called when there are no runsettings (either file or passed via command line).
_settings = new PlaywrightSettingsXml(reader);
Environment.SetEnvironmentVariable("PW_INTERNAL_ADAPTER_SETTINGS", JsonSerializer.Serialize(_settings));
}
}