2025-12-04 18:38:12 +00:00
using System.Runtime.InteropServices ;
2025-11-09 03:01:46 +01:00
namespace ElectronNET.IntegrationTests.Tests
{
2025-12-06 20:13:08 +01:00
using ElectronNET.API ;
2025-11-09 03:01:46 +01:00
using ElectronNET.API.Entities ;
[Collection("ElectronCollection")]
public class WebContentsTests
{
private readonly ElectronFixture fx ;
public WebContentsTests ( ElectronFixture fx )
{
this . fx = fx ;
}
2025-11-12 15:56:36 +00:00
[Fact(Timeout = 20000)]
2025-11-09 03:01:46 +01:00
public async Task Can_get_url_after_navigation ( )
{
var wc = this . fx . MainWindow . WebContents ;
await wc . LoadURLAsync ( "https://example.com" ) ;
var url = await wc . GetUrl ( ) ;
url . Should ( ) . Contain ( "example.com" ) ;
}
2025-11-12 15:56:36 +00:00
[Fact(Timeout = 20000)]
2025-11-09 03:01:46 +01:00
public async Task ExecuteJavaScript_returns_title ( )
{
var wc = this . fx . MainWindow . WebContents ;
await wc . LoadURLAsync ( "https://example.com" ) ;
2025-11-09 12:05:07 +01:00
var title = await wc . ExecuteJavaScriptAsync < string > ( "document.title" ) ;
2025-11-09 03:01:46 +01:00
title . Should ( ) . NotBeNull ( ) ;
}
2025-11-12 15:56:36 +00:00
[Fact(Timeout = 20000)]
2025-11-09 03:01:46 +01:00
public async Task DomReady_event_fires ( )
{
var wc = this . fx . MainWindow . WebContents ;
var fired = false ;
wc . OnDomReady + = ( ) = > fired = true ;
await wc . LoadURLAsync ( "https://example.com" ) ;
await Task . Delay ( 500 ) ;
fired . Should ( ) . BeTrue ( ) ;
}
2025-11-12 15:56:36 +00:00
[Fact(Timeout = 20000)]
2025-11-09 03:01:46 +01:00
public async Task Can_print_to_pdf ( )
{
var html = "data:text/html,<html><body><h1>PDF Test</h1><p>Electron.NET</p></body></html>" ;
await this . fx . MainWindow . WebContents . LoadURLAsync ( html ) ;
var tmp = Path . Combine ( Path . GetTempPath ( ) , $"electronnet_pdf_{Guid.NewGuid():N}.pdf" ) ;
try
{
var ok = await this . fx . MainWindow . WebContents . PrintToPDFAsync ( tmp ) ;
ok . Should ( ) . BeTrue ( ) ;
File . Exists ( tmp ) . Should ( ) . BeTrue ( ) ;
new FileInfo ( tmp ) . Length . Should ( ) . BeGreaterThan ( 0 ) ;
}
finally
{
if ( File . Exists ( tmp ) )
{
File . Delete ( tmp ) ;
}
}
}
2025-11-12 15:56:36 +00:00
[Fact(Timeout = 20000)]
2025-11-09 03:01:46 +01:00
public async Task Can_basic_print ( )
{
var html = "data:text/html,<html><body><h2>Print Test</h2></body></html>" ;
await this . fx . MainWindow . WebContents . LoadURLAsync ( html ) ;
var ok = await this . fx . MainWindow . WebContents . PrintAsync ( new PrintOptions { Silent = true , PrintBackground = true } ) ;
ok . Should ( ) . BeTrue ( ) ;
}
2025-11-15 08:05:31 +01:00
2025-11-12 15:56:36 +00:00
[SkippableFact(Timeout = 20000)]
2025-11-12 10:43:32 +00:00
public async Task GetPrintersAsync_check ( )
{
2025-12-06 20:13:08 +01:00
////Skip.If(Environment.GetEnvironmentVariable("GITHUB_RUN_ID") != null, "Skipping printer test in CI environment.");
2025-11-12 10:43:32 +00:00
var info = await fx . MainWindow . WebContents . GetPrintersAsync ( ) ;
info . Should ( ) . NotBeNull ( ) ;
}
2025-12-04 17:52:01 +00:00
[Fact(Timeout = 20000)]
public async Task GetSetZoomFactor_check ( )
{
await fx . MainWindow . WebContents . GetZoomFactorAsync ( ) ;
var ok = await fx . MainWindow . WebContents . GetZoomFactorAsync ( ) ;
2025-12-04 18:38:12 +00:00
ok . Should ( ) . BeGreaterThan ( 0.0 ) ;
2025-12-04 17:52:01 +00:00
fx . MainWindow . WebContents . SetZoomFactor ( 2.0 ) ;
2025-12-04 19:07:26 +00:00
await Task . Delay ( 500 ) ;
2025-12-04 17:52:01 +00:00
ok = await fx . MainWindow . WebContents . GetZoomFactorAsync ( ) ;
ok . Should ( ) . Be ( 2.0 ) ;
}
2025-12-04 20:36:41 +00:00
[SkippableFact(Timeout = 20000)]
2025-12-04 17:52:01 +00:00
public async Task GetSetZoomLevel_check ( )
{
2025-12-06 20:11:27 +01:00
BrowserWindow window = null ;
try
{
////Skip.If(Environment.GetEnvironmentVariable("GITHUB_RUN_ID") != null && RuntimeInformation.IsOSPlatform(OSPlatform.Windows), "Skipping test on Windows CI.");
window = await Electron . WindowManager . CreateWindowAsync ( new BrowserWindowOptions { Show = true } , "about:blank" ) ;
await Task . Delay ( 100 ) ;
window . WebContents . SetZoomLevel ( 0 ) ;
await Task . Delay ( 500 ) ;
var ok = await window . WebContents . GetZoomLevelAsync ( ) ;
ok . Should ( ) . Be ( 0 ) ;
window . WebContents . SetZoomLevel ( 2 ) ;
await Task . Delay ( 500 ) ;
ok = await window . WebContents . GetZoomLevelAsync ( ) ;
ok . Should ( ) . Be ( 2 ) ;
}
finally
{
window ? . Destroy ( ) ;
}
2025-12-04 17:52:01 +00:00
}
2025-12-04 18:38:12 +00:00
[SkippableFact(Timeout = 20000)]
2025-12-04 17:52:01 +00:00
public async Task DevTools_check ( )
{
2025-12-06 20:13:08 +01:00
BrowserWindow window = null ;
try
{
////Skip.If(Environment.GetEnvironmentVariable("GITHUB_RUN_ID") != null && RuntimeInformation.IsOSPlatform(OSPlatform.Windows), "Skipping test on Windows CI.");
window = await Electron . WindowManager . CreateWindowAsync ( new BrowserWindowOptions { Show = true } , "about:blank" ) ;
await Task . Delay ( 3000 ) ;
window . WebContents . IsDevToolsOpened ( ) . Should ( ) . BeFalse ( ) ;
window . WebContents . OpenDevTools ( ) ;
await Task . Delay ( 5000 ) ;
window . WebContents . IsDevToolsOpened ( ) . Should ( ) . BeTrue ( ) ;
window . WebContents . CloseDevTools ( ) ;
await Task . Delay ( 2000 ) ;
window . WebContents . IsDevToolsOpened ( ) . Should ( ) . BeFalse ( ) ;
}
finally
{
window ? . Destroy ( ) ;
}
2025-12-04 17:52:01 +00:00
}
2025-12-04 18:38:12 +00:00
2025-12-04 17:52:01 +00:00
[Fact(Timeout = 20000)]
public async Task GetSetAudioMuted_check ( )
{
fx . MainWindow . WebContents . SetAudioMuted ( true ) ;
2025-12-04 19:26:24 +00:00
await Task . Delay ( 500 ) ;
2025-12-04 17:52:01 +00:00
var ok = await fx . MainWindow . WebContents . IsAudioMutedAsync ( ) ;
ok . Should ( ) . BeTrue ( ) ;
fx . MainWindow . WebContents . SetAudioMuted ( false ) ;
2025-12-04 19:26:24 +00:00
await Task . Delay ( 500 ) ;
2025-12-04 17:52:01 +00:00
ok = await fx . MainWindow . WebContents . IsAudioMutedAsync ( ) ;
ok . Should ( ) . BeFalse ( ) ;
// Assuming no audio is playing, IsCurrentlyAudibleAsync should return false
// there is no way to play audio in this test
ok = await fx . MainWindow . WebContents . IsCurrentlyAudibleAsync ( ) ;
ok . Should ( ) . BeFalse ( ) ;
}
2025-12-04 18:38:12 +00:00
2025-12-04 20:36:41 +00:00
[SkippableFact(Timeout = 20000)]
2025-12-04 17:52:01 +00:00
public async Task GetSetUserAgent_check ( )
{
2025-12-06 20:13:08 +01:00
////Skip.If(Environment.GetEnvironmentVariable("GITHUB_RUN_ID") != null && RuntimeInformation.IsOSPlatform(OSPlatform.Windows), "Skipping test on Windows CI.");
2025-12-06 20:52:45 +01:00
await Task . Delay ( 1000 ) ;
2025-12-04 17:52:01 +00:00
fx . MainWindow . WebContents . SetUserAgent ( "MyUserAgent/1.0" ) ;
2025-12-06 20:52:45 +01:00
2025-12-04 19:07:26 +00:00
await Task . Delay ( 1000 ) ;
2025-12-06 20:52:45 +01:00
2025-12-06 20:13:08 +01:00
var ok = await fx . MainWindow . WebContents . GetUserAgentAsync ( ) ;
2025-12-04 17:52:01 +00:00
ok . Should ( ) . Be ( "MyUserAgent/1.0" ) ;
}
2025-12-04 18:38:12 +00:00
2025-11-09 03:01:46 +01:00
}
}