Skip to content

Commit 9ae3ae6

Browse files
Update Hooks.cs (#119)
* Update Hooks.cs * Add record video parameter
1 parent 7bb518f commit 9ae3ae6

File tree

2 files changed

+14
-3
lines changed
  • Behavioral.Automation.Playwright/Behavioral.Automation.Playwright

2 files changed

+14
-3
lines changed

Behavioral.Automation.Playwright/Behavioral.Automation.Playwright/Configs/Config.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ public class Config
1717
public float? SlowMoMilliseconds { get; set; }
1818

1919
[ConfigurationKeyName("HEADLESS")] public bool? Headless { get; set; } = true;
20+
[ConfigurationKeyName("RECORD_VIDEO")] public bool RecordVideo { get; set; } = false;
2021
}

Behavioral.Automation.Playwright/Behavioral.Automation.Playwright/Hooks.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class Hooks
2222
private readonly ScenarioContext _scenarioContext;
2323
private static readonly float? SlowMoMilliseconds = ConfigManager.GetConfig<Config>().SlowMoMilliseconds;
2424
private static readonly bool? Headless = ConfigManager.GetConfig<Config>().Headless;
25+
private static readonly bool RecordVideo = ConfigManager.GetConfig<Config>().RecordVideo;
2526
private readonly TestServicesBuilder _testServicesBuilder;
2627

2728
public Hooks(WebContext webContext, ScenarioContext scenarioContext, IObjectContainer objectContainer)
@@ -50,7 +51,15 @@ public static async Task InitBrowser()
5051
[BeforeScenario]
5152
public async Task CreateContextAsync()
5253
{
53-
_webContext.Context = await _browser!.NewContextAsync();
54+
if (_browser is null)
55+
{
56+
throw new NullReferenceException($"Playwright browser is not initialized.");
57+
}
58+
59+
_webContext.Context = RecordVideo
60+
? await _browser.NewContextAsync(new BrowserNewContextOptions { RecordVideoDir = "videos/" })
61+
: await _browser.NewContextAsync();
62+
5463
_webContext.Page = await _webContext.Context.NewPageAsync();
5564
}
5665

@@ -87,10 +96,11 @@ await _webContext.Page.ScreenshotAsync(new PageScreenshotOptions
8796
//TODO Implement configuration
8897
private static async Task<IBrowser?> InitBrowserAsync()
8998
{
90-
return await _playwright!.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
99+
if (_playwright is null) throw new NullReferenceException($"Playwright is not initialized.");
100+
return await _playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
91101
{
92102
Headless = Headless,
93103
SlowMo = SlowMoMilliseconds,
94104
});
95105
}
96-
}
106+
}

0 commit comments

Comments
 (0)