How to use OOPIFShouldReportGoogleComFrame method of PuppeteerSharp.Tests.HeadfulTests.HeadfulTests class

Best Puppeteer-sharp code snippet using PuppeteerSharp.Tests.HeadfulTests.HeadfulTests.OOPIFShouldReportGoogleComFrame

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

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

HeadfulTests.cs

Source: HeadfulTests.cs Github

copy
1using System;
2using System.Linq;
3using System.Threading.Tasks;
4using PuppeteerSharp.Helpers;
5using Xunit;
6using Xunit.Abstractions;
7
8namespace PuppeteerSharp.Tests.PuppeteerTests
9{
10    [Collection("PuppeteerLoaderFixture collection")]
11    public class HeadfulTests : PuppeteerBaseTest
12    {
13        public HeadfulTests(ITestOutputHelper output) : base(output)
14        {
15        }
16
17        [Fact]
18        public async Task BackgroundPageTargetTypeShouldBeAvailable()
19        {
20            using (var browserWithExtension = await Puppeteer.LaunchAsync(
21                TestConstants.BrowserWithExtensionOptions(),
22                TestConstants.LoggerFactory))
23            using (var page = await browserWithExtension.NewPageAsync())
24            {
25                var backgroundPageTarget = await WaitForBackgroundPageTargetAsync(browserWithExtension);
26                Assert.NotNull(backgroundPageTarget);
27            }
28        }
29
30        [Fact]
31        public async Task TargetPageShouldReturnABackgroundPage()
32        {
33            using (var browserWithExtension = await Puppeteer.LaunchAsync(
34                TestConstants.BrowserWithExtensionOptions(),
35                TestConstants.LoggerFactory))
36            {
37                var backgroundPageTarget = await WaitForBackgroundPageTargetAsync(browserWithExtension);
38                var page = await backgroundPageTarget.PageAsync();
39                Assert.Equal(6, await page.EvaluateFunctionAsync<int>("() => 2 * 3"));
40                Assert.Equal(42, await page.EvaluateFunctionAsync<int>("() => window.MAGIC"));
41            }
42        }
43
44        [Fact]
45        public async Task ShouldHaveDefaultUrlWhenLaunchingBrowser()
46        {
47            using (var browser = await Puppeteer.LaunchAsync(
48                TestConstants.BrowserWithExtensionOptions(),
49                TestConstants.LoggerFactory))
50            {
51                var pages = (await browser.PagesAsync()).Select(page => page.Url).ToArray();
52                Assert.Equal(new[] { "about:blank" }, pages);
53            }
54        }
55
56        [Fact]
57        public async Task HeadlessShouldBeAbleToReadCookiesWrittenByHeadful()
58        {
59            using (var userDataDir = new TempDirectory())
60            {
61                var launcher = new Launcher(TestConstants.LoggerFactory);
62                var options = TestConstants.DefaultBrowserOptions();
63                options.Args = options.Args.Concat(new[] { $"--user-data-dir=\"{userDataDir}\"" }).ToArray();
64                options.Headless = false;
65
66                var browser = await launcher.LaunchAsync(options);
67                var page = await browser.NewPageAsync();
68                await page.GoToAsync(TestConstants.EmptyPage);
69                await page.EvaluateExpressionAsync(
70                    "document.cookie = 'foo=true; expires=Fri, 31 Dec 9999 23:59:59 GMT'");
71                await browser.CloseAsync();
72
73                await TestUtils.WaitForCookieInChromiumFileAsync(userDataDir.Path, "foo");
74
75                options.Headless = true;
76                using (var browser2 = await Puppeteer.LaunchAsync(options, TestConstants.LoggerFactory))
77                {
78                    var page2 = await browser2.NewPageAsync();
79                    await page2.GoToAsync(TestConstants.EmptyPage);
80                    Assert.Equal("foo=true", await page2.EvaluateExpressionAsync<string>("document.cookie"));
81                }
82            }
83        }
84
85        [Fact]
86        public async Task OOPIFShouldReportGoogleComFrame()
87        {
88            // https://google.com is isolated by default in Chromium embedder.
89            var headfulOptions = TestConstants.DefaultBrowserOptions();
90            headfulOptions.Headless = false;
91            using (var browser = await Puppeteer.LaunchAsync(headfulOptions))
92            using (var page = await browser.NewPageAsync())
93            {
94                await page.GoToAsync(TestConstants.EmptyPage);
95                await page.SetRequestInterceptionAsync(true);
96                page.Request += async (sender, e) => await e.Request.RespondAsync(
97                    new ResponseData { Body = "{ body: 'YO, GOOGLE.COM'}" });
98                await page.EvaluateFunctionHandleAsync(@"() => {
99                    const frame = document.createElement('iframe');
100                    frame.setAttribute('src', 'https://google.com/');
101                    document.body.appendChild(frame);
102                    return new Promise(x => frame.onload = x);
103                }");
104                await page.WaitForSelectorAsync("iframe[src=\"https://google.com/\"]");
105                var urls = Array.ConvertAll(page.Frames, frame => frame.Url);
106                Array.Sort(urls);
107                Assert.Equal(new[] { TestConstants.EmptyPage, "https://google.com/" }, urls);
108            }
109        }
110
111        [Fact]
112        public async Task ShouldCloseBrowserWithBeforeunloadPage()
113        {
114            var headfulOptions = TestConstants.DefaultBrowserOptions();
115            headfulOptions.Headless = false;
116            using (var browser = await Puppeteer.LaunchAsync(headfulOptions))
117            using (var page = await browser.NewPageAsync())
118            {
119                await page.GoToAsync(TestConstants.ServerUrl + "/beforeunload.html");
120                // We have to interact with a page so that 'beforeunload' handlers fire.
121                await page.ClickAsync("body");
122            }
123        }
124
125        [Fact]
126        public async Task BringToFrontShouldWork()
127        {
128            using (var browserWithExtension = await Puppeteer.LaunchAsync(
129                TestConstants.BrowserWithExtensionOptions(),
130                TestConstants.LoggerFactory))
131            using (var page = await browserWithExtension.NewPageAsync())
132            {
133                await page.GoToAsync(TestConstants.EmptyPage);
134                Assert.Equal("visible", await page.EvaluateExpressionAsync<string>("document.visibilityState"));
135
136                var newPage = await browserWithExtension.NewPageAsync();
137                await newPage.GoToAsync(TestConstants.EmptyPage);
138                Assert.Equal("hidden", await page.EvaluateExpressionAsync<string>("document.visibilityState"));
139                Assert.Equal("visible", await newPage.EvaluateExpressionAsync<string>("document.visibilityState"));
140
141                await page.BringToFrontAsync();
142                Assert.Equal("visible", await page.EvaluateExpressionAsync<string>("document.visibilityState"));
143                Assert.Equal("hidden", await newPage.EvaluateExpressionAsync<string>("document.visibilityState"));
144
145                await newPage.CloseAsync();
146            }
147        }
148
149        private Task<Target> WaitForBackgroundPageTargetAsync(Browser browser)
150        {
151            var target = browser.Targets().FirstOrDefault(t => t.Type == TargetType.BackgroundPage);
152            if (target != null)
153            {
154                return Task.FromResult(target);
155            }
156            var targetCreatedTcs = new TaskCompletionSource<Target>();
157            void targetCreated(object sender, TargetChangedArgs e)
158            {
159                if (e.Target.Type != TargetType.BackgroundPage)
160                {
161                    return;
162                }
163                targetCreatedTcs.TrySetResult(e.Target);
164                browser.TargetCreated -= targetCreated;
165            }
166            browser.TargetCreated += targetCreated;
167
168            return targetCreatedTcs.Task;
169        }
170    }
171}
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 OOPIFShouldReportGoogleComFrame code on LambdaTest Cloud Grid

Execute automation tests with OOPIFShouldReportGoogleComFrame 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)