How to use IdleOverrideTests method of PuppeteerSharp.Tests.IdleOverrideTests.IdleOverrideTests class

Best Puppeteer-sharp code snippet using PuppeteerSharp.Tests.IdleOverrideTests.IdleOverrideTests.IdleOverrideTests

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

IdleOverrideTests.cs

Source: IdleOverrideTests.cs Github

copy
1using System.Collections.Generic;
2using System.Linq;
3using System.Threading.Tasks;
4using PuppeteerSharp.Tests.Attributes;
5using PuppeteerSharp.Xunit;
6using Xunit;
7using Xunit.Abstractions;
8
9namespace PuppeteerSharp.Tests.IdleOverrideTests
10{
11    [Collection(TestConstants.TestFixtureCollectionName)]
12    public class IdleOverrideTests : PuppeteerPageBaseTest
13    {
14        public IdleOverrideTests(ITestOutputHelper output) : base(output)
15        {
16        }
17
18        private async Task<string> GetIdleStateAsync()
19        {
20            var stateElement = await Page.QuerySelectorAsync("#state");
21            return await Page.EvaluateFunctionAsync<string>(
22                @"(element) => {
23                    return element.innerText;
24                }",
25                stateElement);
26        }
27
28        private async Task VerifyStateAsync(string expectedState)
29        {
30            var actualState = await GetIdleStateAsync();
31            Assert.Equal(expectedState, actualState);
32        }
33
34        [PuppeteerTest("idle_override.spec.ts", "Emulate idle state", "changing idle state emulation causes change of the IdleDetector state")]
35        [SkipBrowserFact(skipFirefox: true)]
36        public async Task ChangingIdleStateEmulationCausesChangeOfTheIdleDetectorState()
37        {
38            await Context.OverridePermissionsAsync(
39                TestConstants.ServerUrl + "/idle-detector.html",
40                new[]
41                {
42                    OverridePermission.IdleDetection,
43                });
44
45            await Page.GoToAsync(TestConstants.ServerUrl+ "/idle-detector.html");
46
47            // Store initial state, as soon as it is not guaranteed to be `active, unlocked`.
48            var initialState = await GetIdleStateAsync();
49
50            // Emulate Idle states and verify IdleDetector updates state accordingly.
51            await Page.EmulateIdleStateAsync(new EmulateIdleOverrides {
52                IsUserActive = false,
53                IsScreenUnlocked =  false,
54            });
55
56            await VerifyStateAsync("Idle state: idle, locked.");
57
58            await Page.EmulateIdleStateAsync(new EmulateIdleOverrides {
59                IsUserActive = true,
60                IsScreenUnlocked = false,
61            });
62            await VerifyStateAsync("Idle state: active, locked.");
63
64            await Page.EmulateIdleStateAsync(new EmulateIdleOverrides {
65                IsUserActive = true,
66                IsScreenUnlocked = true,
67            });
68            await VerifyStateAsync("Idle state: active, unlocked.");
69
70            await Page.EmulateIdleStateAsync(new EmulateIdleOverrides {
71                IsUserActive = false,
72                IsScreenUnlocked = true,
73            });
74            await VerifyStateAsync("Idle state: idle, unlocked.");
75
76            // Remove Idle emulation and verify IdleDetector is in initial state.
77            await Page.EmulateIdleStateAsync();
78            await VerifyStateAsync(initialState);
79
80            // Emulate idle state again after removing emulation.
81            await Page.EmulateIdleStateAsync(new EmulateIdleOverrides {
82                IsUserActive = false,
83                IsScreenUnlocked = false,
84            });
85            await VerifyStateAsync("Idle state: idle, locked.");
86
87            // Remove emulation second time.
88            await Page.EmulateIdleStateAsync();
89            await VerifyStateAsync(initialState);
90        }
91    }
92}
93
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Trigger IdleOverrideTests code on LambdaTest Cloud Grid

Execute automation tests with IdleOverrideTests on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)