Skip to content

Commit e8a8133

Browse files
authored
[BREAKING] Introduce Wait for device prompt (#2392)
* Implemented tests * Feature complete * code style * cr * cr * check null context * fix test
1 parent f6367df commit e8a8133

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1381
-203
lines changed

lib/PuppeteerSharp.Tests/BrowserContextTests/BrowserContextTests.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ namespace PuppeteerSharp.Tests.BrowserContextTests
99
{
1010
public class BrowserContextTests : PuppeteerBrowserBaseTest
1111
{
12-
public BrowserContextTests(): base()
13-
{
14-
}
15-
1612
[PuppeteerTest("browsercontext.spec.ts", "BrowserContext", "should have default context")]
1713
[PuppeteerTimeout]
1814
public void ShouldHaveDefaultContext()
@@ -22,7 +18,7 @@ public void ShouldHaveDefaultContext()
2218
Assert.False(defaultContext.IsIncognito);
2319
var exception = Assert.ThrowsAsync<PuppeteerException>(defaultContext.CloseAsync);
2420
Assert.AreSame(defaultContext, Browser.DefaultContext);
25-
StringAssert.Contains("cannot be closed", exception.Message);
21+
StringAssert.Contains("cannot be closed", exception!.Message);
2622
}
2723

2824
[PuppeteerTest("browsercontext.spec.ts", "BrowserContext", "should create new incognito context")]
@@ -188,7 +184,7 @@ public async Task ShouldTimeoutWaitingForNonExistentTarget()
188184
{
189185
var context = await Browser.CreateIncognitoBrowserContextAsync();
190186
Assert.ThrowsAsync<TimeoutException>(()
191-
=> context.WaitForTargetAsync((target) => target.Url == TestConstants.EmptyPage, new WaitForOptions { Timeout = 1 }));
187+
=> context.WaitForTargetAsync((target) => target.Url == TestConstants.EmptyPage, new WaitTimeoutOptions(1)));
192188
await context.CloseAsync();
193189
}
194190
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using NUnit.Framework;
4+
using PuppeteerSharp.Tests.Attributes;
5+
using PuppeteerSharp.Nunit;
6+
using PuppeteerSharp.Messaging;
7+
8+
namespace PuppeteerSharp.Tests.DeviceRequestPromptTests;
9+
10+
public class DeviceRequestPromptCancelTests : PuppeteerPageBaseTest
11+
{
12+
[PuppeteerTest("DeviceRequestPrompt.test.ts", "DeviceRequestPrompt.cancel", "should succeed on first call")]
13+
[PuppeteerTimeout]
14+
public async Task ShouldSucceedOnFirstCall()
15+
{
16+
var client = new MockCDPSession();
17+
var timeoutSettings = new TimeoutSettings();
18+
var prompt = new DeviceRequestPrompt(
19+
client,
20+
timeoutSettings,
21+
new DeviceAccessDeviceRequestPromptedResponse() { Id = "000" });
22+
23+
await prompt.CancelAsync();
24+
}
25+
26+
[PuppeteerTest("DeviceRequestPrompt.test.ts", "DeviceRequestPrompt.cancel", "should fail when canceling prompt twice")]
27+
[PuppeteerTimeout]
28+
public async Task ShouldFailWhenCancelingPromptTwice()
29+
{
30+
var client = new MockCDPSession();
31+
var timeoutSettings = new TimeoutSettings();
32+
var prompt = new DeviceRequestPrompt(
33+
client,
34+
timeoutSettings,
35+
new DeviceAccessDeviceRequestPromptedResponse() { Id = "000" });
36+
37+
await prompt.CancelAsync();
38+
Assert.ThrowsAsync<PuppeteerException>(async () => await prompt.CancelAsync());
39+
}
40+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
using NUnit.Framework;
2+
using PuppeteerSharp.Tests.Attributes;
3+
using PuppeteerSharp.Nunit;
4+
using PuppeteerSharp.Messaging;
5+
6+
namespace PuppeteerSharp.Tests.DeviceRequestPromptTests;
7+
8+
public class DeviceRequestPromptDevicesTests : PuppeteerPageBaseTest
9+
{
10+
[PuppeteerTest("DeviceRequestPrompt.test.ts", "DeviceRequestPrompt.devices", "lists devices as they arrive")]
11+
[PuppeteerTimeout]
12+
public void ShouldListDevicesAsTheyArrive()
13+
{
14+
var client = new MockCDPSession();
15+
var timeoutSettings = new TimeoutSettings();
16+
var prompt = new DeviceRequestPrompt(
17+
client,
18+
timeoutSettings,
19+
new DeviceAccessDeviceRequestPromptedResponse() { Id = "00000000000000000000000000000000" });
20+
21+
Assert.IsEmpty(prompt.Devices);
22+
23+
var promptData = new DeviceAccessDeviceRequestPromptedResponse()
24+
{
25+
Id = "00000000000000000000000000000000",
26+
Devices = new[]
27+
{
28+
new DeviceAccessDeviceRequestPromptedResponse.DeviceAccessDevice()
29+
{
30+
Name = "My Device", Id = "0000",
31+
}
32+
}
33+
};
34+
35+
client.OnMessage(new ConnectionResponse()
36+
{
37+
Method = "DeviceAccess.deviceRequestPrompted", Params = WaitForDevicePromptTests.ToJToken(promptData),
38+
});
39+
40+
Assert.AreEqual(1, prompt.Devices.Count);
41+
42+
promptData = new DeviceAccessDeviceRequestPromptedResponse()
43+
{
44+
Id = "00000000000000000000000000000000",
45+
Devices =
46+
[
47+
new DeviceAccessDeviceRequestPromptedResponse.DeviceAccessDevice()
48+
{
49+
Name = "My Device", Id = "0000",
50+
},
51+
new DeviceAccessDeviceRequestPromptedResponse.DeviceAccessDevice()
52+
{
53+
Name = "My Device 2", Id = "0001",
54+
}
55+
]
56+
};
57+
58+
client.OnMessage(new ConnectionResponse()
59+
{
60+
Method = "DeviceAccess.deviceRequestPrompted", Params = WaitForDevicePromptTests.ToJToken(promptData),
61+
});
62+
63+
Assert.AreEqual(2, prompt.Devices.Count);
64+
}
65+
66+
[PuppeteerTest("DeviceRequestPrompt.test.ts", "DeviceRequestPrompt.devices", "does not list devices from events of another prompt")]
67+
[PuppeteerTimeout]
68+
public void ShouldNotListDevicesFromEventsOfAnotherPrompt()
69+
{
70+
var client = new MockCDPSession();
71+
var timeoutSettings = new TimeoutSettings();
72+
var prompt = new DeviceRequestPrompt(
73+
client,
74+
timeoutSettings,
75+
new DeviceAccessDeviceRequestPromptedResponse() { Id = "00000000000000000000000000000000" });
76+
77+
var promptData = new DeviceAccessDeviceRequestPromptedResponse() { Id = "00000000000000000000000000000000", };
78+
79+
client.OnMessage(new ConnectionResponse()
80+
{
81+
Method = "DeviceAccess.deviceRequestPrompted", Params = WaitForDevicePromptTests.ToJToken(promptData),
82+
});
83+
84+
Assert.IsEmpty(prompt.Devices);
85+
86+
promptData = new DeviceAccessDeviceRequestPromptedResponse()
87+
{
88+
Id = "8888888888",
89+
Devices = new[]
90+
{
91+
new DeviceAccessDeviceRequestPromptedResponse.DeviceAccessDevice()
92+
{
93+
Name = "My Device", Id = "0000",
94+
},
95+
new DeviceAccessDeviceRequestPromptedResponse.DeviceAccessDevice()
96+
{
97+
Name = "My Device 2", Id = "0001",
98+
}
99+
}
100+
};
101+
102+
client.OnMessage(new ConnectionResponse()
103+
{
104+
Method = "DeviceAccess.deviceRequestPrompted", Params = WaitForDevicePromptTests.ToJToken(promptData),
105+
});
106+
107+
Assert.IsEmpty(prompt.Devices);
108+
}
109+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
using System.Threading.Tasks;
2+
using NUnit.Framework;
3+
using PuppeteerSharp.Tests.Attributes;
4+
using PuppeteerSharp.Nunit;
5+
using PuppeteerSharp.Messaging;
6+
7+
namespace PuppeteerSharp.Tests.DeviceRequestPromptTests;
8+
9+
public class DeviceRequestPromptSelectTests : PuppeteerPageBaseTest
10+
{
11+
[PuppeteerTest("DeviceRequestPrompt.test.ts", "DeviceRequestPrompt.select", "should succeed with listed device")]
12+
[PuppeteerTimeout]
13+
public async Task ShouldSucceedWithListedDevice()
14+
{
15+
var client = new MockCDPSession();
16+
var timeoutSettings = new TimeoutSettings();
17+
var prompt = new DeviceRequestPrompt(
18+
client,
19+
timeoutSettings,
20+
new DeviceAccessDeviceRequestPromptedResponse() { Id = "000" });
21+
22+
var deviceTask = prompt.WaitForDeviceAsync(device => device.Name == "My Device 1");
23+
24+
var promptData = new DeviceAccessDeviceRequestPromptedResponse()
25+
{
26+
Id = "000",
27+
Devices = new[]
28+
{
29+
new DeviceAccessDeviceRequestPromptedResponse.DeviceAccessDevice()
30+
{
31+
Name = "My Device 1", Id = "0000",
32+
}
33+
}
34+
};
35+
36+
client.OnMessage(new ConnectionResponse()
37+
{
38+
Method = "DeviceAccess.deviceRequestPrompted",
39+
Params = WaitForDevicePromptTests.ToJToken(promptData),
40+
});
41+
42+
var device = await deviceTask;
43+
await prompt.SelectAsync(device);
44+
}
45+
46+
[PuppeteerTest("DeviceRequestPrompt.test.ts", "DeviceRequestPrompt.select", "should error for device not listed in devices")]
47+
[PuppeteerTimeout]
48+
public void ShouldErrorForDeviceNotListedInDevices()
49+
{
50+
var client = new MockCDPSession();
51+
var timeoutSettings = new TimeoutSettings();
52+
var prompt = new DeviceRequestPrompt(
53+
client,
54+
timeoutSettings,
55+
new DeviceAccessDeviceRequestPromptedResponse() { Id = "000" });
56+
57+
var exception = Assert.ThrowsAsync<PuppeteerException>(() =>
58+
prompt.SelectAsync(new DeviceRequestPromptDevice("My Device 2", "0001")));
59+
60+
Assert.AreEqual("Cannot select unknown device!", exception!.Message);
61+
}
62+
63+
[PuppeteerTest("DeviceRequestPrompt.test.ts", "DeviceRequestPrompt.select", "should fail when selecting prompt twice")]
64+
[PuppeteerTimeout]
65+
public async Task ShouldFailWhenSelectingPromptTwice()
66+
{
67+
var client = new MockCDPSession();
68+
var timeoutSettings = new TimeoutSettings();
69+
var prompt = new DeviceRequestPrompt(
70+
client,
71+
timeoutSettings,
72+
new DeviceAccessDeviceRequestPromptedResponse() { Id = "000" });
73+
74+
var deviceTask = prompt.WaitForDeviceAsync(device => device.Name == "My Device 1");
75+
76+
var promptData = new DeviceAccessDeviceRequestPromptedResponse()
77+
{
78+
Id = "000",
79+
Devices = new[]
80+
{
81+
new DeviceAccessDeviceRequestPromptedResponse.DeviceAccessDevice()
82+
{
83+
Name = "My Device 1", Id = "0000",
84+
}
85+
}
86+
};
87+
88+
client.OnMessage(new ConnectionResponse()
89+
{
90+
Method = "DeviceAccess.deviceRequestPrompted",
91+
Params = WaitForDevicePromptTests.ToJToken(promptData),
92+
});
93+
94+
var device = await deviceTask;
95+
await prompt.SelectAsync(device);
96+
97+
var exception = Assert.ThrowsAsync<PuppeteerException>(() => prompt.SelectAsync(device));
98+
Assert.AreEqual("Cannot select DeviceRequestPrompt which is already handled!", exception!.Message);
99+
}
100+
}

0 commit comments

Comments
 (0)