Skip to content

Commit dbdbdfb

Browse files
authored
Add Page.EmulateTimezoneAsync (#1323)
* Add Page.EmulateTimezoneAsync * Update lib/PuppeteerSharp/Page.cs Co-Authored-By: Lucas G. Devescovi <[email protected]> * cr
1 parent 74488cf commit dbdbdfb

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System.Threading.Tasks;
2+
using PuppeteerSharp.Media;
3+
using Xunit;
4+
using Xunit.Abstractions;
5+
6+
namespace PuppeteerSharp.Tests.PageTests
7+
{
8+
[Collection(TestConstants.TestFixtureCollectionName)]
9+
public class EmulateTimezoneTests : PuppeteerPageBaseTest
10+
{
11+
public EmulateTimezoneTests(ITestOutputHelper output) : base(output)
12+
{
13+
}
14+
15+
[Fact]
16+
public async Task ShouldWork()
17+
{
18+
await Page.EvaluateExpressionAsync("globalThis.date = new Date(1479579154987);");
19+
await Page.EmulateTimezoneAsync("America/Jamaica");
20+
Assert.Equal(
21+
"Sat Nov 19 2016 13:12:34 GMT-0500 (Eastern Standard Time)",
22+
await Page.EvaluateExpressionAsync<string>("date.toString()"));
23+
24+
await Page.EmulateTimezoneAsync("Pacific/Honolulu");
25+
Assert.Equal(
26+
"Sat Nov 19 2016 08:12:34 GMT-1000 (Hawaii-Aleutian Standard Time)",
27+
await Page.EvaluateExpressionAsync<string>("date.toString()"));
28+
29+
await Page.EmulateTimezoneAsync("America/Buenos_Aires");
30+
Assert.Equal(
31+
"Sat Nov 19 2016 15:12:34 GMT-0300 (Argentina Standard Time)",
32+
await Page.EvaluateExpressionAsync<string>("date.toString()"));
33+
34+
await Page.EmulateTimezoneAsync("Europe/Berlin");
35+
Assert.Equal(
36+
"Sat Nov 19 2016 19:12:34 GMT+0100 (Central European Standard Time)",
37+
await Page.EvaluateExpressionAsync<string>("date.toString()"));
38+
}
39+
40+
[Fact]
41+
public async Task ShouldThrowForInvalidTimezoneId()
42+
{
43+
var exception = await Assert.ThrowsAnyAsync<PuppeteerException>(
44+
() => Page.EmulateTimezoneAsync("Foo/Bar"));
45+
Assert.Contains("Invalid timezone ID: Foo/Bar", exception.Message);
46+
47+
exception = await Assert.ThrowsAnyAsync<PuppeteerException>(
48+
() => Page.EmulateTimezoneAsync("Baz/Qux"));
49+
Assert.Contains("Invalid timezone ID: Baz/Qux", exception.Message);
50+
}
51+
}
52+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using Newtonsoft.Json;
2+
3+
namespace PuppeteerSharp.Messaging
4+
{
5+
internal class EmulateTimezoneRequest
6+
{
7+
public string TimezoneId { get; set; }
8+
}
9+
}

lib/PuppeteerSharp/Page.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1694,6 +1694,27 @@ public Task SetBurstModeOffAsync()
16941694
/// <returns>A task that resolves when the message has been sent to Chromium.</returns>
16951695
public Task BringToFrontAsync() => Client.SendAsync("Page.bringToFront");
16961696

1697+
/// <summary>
1698+
/// Changes the timezone of the page.
1699+
/// </summary>
1700+
/// <param name="timezoneId">Timezone to set. See <seealso href="https://cs.chromium.org/chromium/src/third_party/icu/source/data/misc/metaZones.txt?rcl=faee8bc70570192d82d2978a71e2a615788597d1" >ICU’s `metaZones.txt`</seealso>
1701+
/// for a list of supported timezone IDs. Passing `null` disables timezone emulation.</param>
1702+
/// <returns>The viewport task.</returns>
1703+
public async Task EmulateTimezoneAsync(string timezoneId)
1704+
{
1705+
try
1706+
{
1707+
await Client.SendAsync("Emulation.setTimezoneOverride", new EmulateTimezoneRequest
1708+
{
1709+
TimezoneId = timezoneId ?? string.Empty
1710+
}).ConfigureAwait(false);
1711+
}
1712+
catch (Exception ex) when (ex.Message.Contains("Invalid timezone"))
1713+
{
1714+
throw new PuppeteerException($"Invalid timezone ID: { timezoneId }");
1715+
}
1716+
}
1717+
16971718
#endregion
16981719

16991720
internal void OnPopup(Page popupPage) => Popup?.Invoke(this, new PopupEventArgs { PopupPage = popupPage });

0 commit comments

Comments
 (0)