Skip to content

Commit 3c379fd

Browse files
authored
Implement OfflineMode tests (#150)
1 parent 24a1f91 commit 3c379fd

File tree

6 files changed

+48
-18
lines changed

6 files changed

+48
-18
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Net;
2+
using System.Threading.Tasks;
3+
using Xunit;
4+
5+
namespace PuppeteerSharp.Tests.Page
6+
{
7+
[Collection("PuppeteerLoaderFixture collection")]
8+
public class OfflineModeTests : PuppeteerPageBaseTest
9+
{
10+
[Fact]
11+
public async Task ShouldWork()
12+
{
13+
await Page.SetOfflineModeAsync(true);
14+
await Assert.ThrowsAsync<NavigationException>(async () => await Page.GoToAsync(TestConstants.EmptyPage));
15+
16+
await Page.SetOfflineModeAsync(false);
17+
var response = await Page.ReloadAsync();
18+
Assert.Equal(HttpStatusCode.OK, response.Status);
19+
}
20+
21+
[Fact]
22+
public async Task ShouldEmulateNavigatorOnLine()
23+
{
24+
Assert.True(await Page.EvaluateExpressionAsync<bool>("window.navigator.onLine"));
25+
26+
await Page.SetOfflineModeAsync(true);
27+
Assert.False(await Page.EvaluateExpressionAsync<bool>("window.navigator.onLine"));
28+
29+
await Page.SetOfflineModeAsync(false);
30+
Assert.True(await Page.EvaluateExpressionAsync<bool>("window.navigator.onLine"));
31+
}
32+
}
33+
}
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
using System;
2-
using Xunit;
1+
using Xunit;
32

43
namespace PuppeteerSharp.Tests
54
{
65
[CollectionDefinition("PuppeteerLoaderFixture collection")]
7-
public class DatabaseCollection : ICollectionFixture<PuppeteerLoaderFixture>
6+
public class PuppeteerLoaderCollection : ICollectionFixture<PuppeteerLoaderFixture>
87
{
98
// This class has no code, and is never created. Its purpose is simply
109
// to be the place to apply [CollectionDefinition] and all the
1110
// ICollectionFixture<> interfaces.
1211
//Recipe from https://xunit.github.io/docs/shared-context.html#class-fixture
1312
}
14-
1513
}

lib/PuppeteerSharp.Tests/PuppeteerSharp.Tests.csproj

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,22 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
2-
1+
<Project Sdk="Microsoft.NET.Sdk">
32
<PropertyGroup>
43
<TargetFrameworks>netcoreapp2.0;net471</TargetFrameworks>
54
<IsPackable>false</IsPackable>
65
</PropertyGroup>
7-
86
<ItemGroup>
97
<PackageReference Include="Microsoft.CSharp" Version="4.5.0-preview1-26216-02" />
10-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0-preview-20170628-02" />
11-
<PackageReference Include="xunit" Version="2.2.0" />
12-
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
8+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.0" />
9+
<PackageReference Include="xunit" Version="2.3.1" />
10+
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
1311
<PackageReference Include="PdfSharp" Version="1.32.3057" />
1412
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.0-beta0002" />
1513
</ItemGroup>
16-
1714
<ItemGroup>
1815
<Folder Include="Issues\" />
1916
</ItemGroup>
20-
2117
<PropertyGroup Condition="'$(TargetFramework)'=='net471'">
2218
<RuntimeIdentifier>win7-x64</RuntimeIdentifier>
2319
</PropertyGroup>
24-
2520
<ItemGroup>
2621
<ProjectReference Include="..\PuppeteerSharp.TestServer\PuppeteerSharp.TestServer.csproj" />
2722
<ProjectReference Include="..\PuppeteerSharp\PuppeteerSharp.csproj" />

lib/PuppeteerSharp/NavigationException.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
namespace PuppeteerSharp
55
{
66
[Serializable]
7-
internal class NavigationException : PuppeteerException
7+
public class NavigationException : PuppeteerException
88
{
99
public NavigationException()
1010
{

lib/PuppeteerSharp/Page.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,11 @@ public async Task<object> EvalAsync(string selector, string pageFunction, params
175175
public async Task SetRequestInterceptionAsync(bool value)
176176
=> await _networkManager.SetRequestInterceptionAsync(value);
177177

178+
/// <summary>
179+
/// Set offline mode for the page.
180+
/// </summary>
181+
/// <returns>Result task</returns>
182+
/// <param name="value">When <c>true</c> enables offline mode for the page.</param>
178183
public async Task SetOfflineModeAsync(bool value) => await _networkManager.SetOfflineModeAsync(value);
179184

180185
public async Task<object> EvalManyAsync(string selector, Func<object> pageFunction, params object[] args)

lib/PuppeteerSharp/PuppeteerSharp.csproj

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
2-
1+
<Project Sdk="Microsoft.NET.Sdk">
32
<PropertyGroup>
43
<TargetFramework>netstandard2.0</TargetFramework>
54
<PackOnBuild>true</PackOnBuild>
@@ -23,6 +22,6 @@
2322
<ItemGroup>
2423
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
2524
<PackageReference Include="System.Net.Http" Version="4.3.3" />
26-
<PackageReference Include="Microsoft.AspNetCore.WebUtilities" Version="2.0.1" />
25+
<PackageReference Include="Microsoft.AspNetCore.WebUtilities" Version="2.0.2" />
2726
</ItemGroup>
28-
</Project>
27+
</Project>

0 commit comments

Comments
 (0)