public async Task ShouldReportFrameName()
        {
            await FrameUtils.AttachFrameAsync(Page, "theFrameId", Server.EmptyPage);

            await Page.EvaluateAsync(@"url => {
                const frame = document.createElement('iframe');
                frame.name = 'theFrameName';
                frame.src = url;
                document.body.appendChild(frame);
                return new Promise(x => frame.onload = x);
            }", Server.EmptyPage);

            Assert.IsEmpty(Page.Frames.First().Name);
            Assert.AreEqual("theFrameId", Page.Frames.ElementAt(1).Name);
            Assert.AreEqual("theFrameName", Page.Frames.ElementAt(2).Name);
        }
Beispiel #2
0
        public async Task ShouldWorkInCrossProcessIframe()
        {
            await using var context = await Browser.NewContextAsync(new()
            {
                ColorScheme = ColorScheme.Dark,
            });

            var page = await context.NewPageAsync();

            await page.GotoAsync(Server.EmptyPage);

            await FrameUtils.AttachFrameAsync(page, "frame1", Server.CrossProcessPrefix + "/empty.html");

            var frame = page.Frames.ElementAt(1);

            Assert.True(await frame.EvaluateAsync <bool>("() => matchMedia('(prefers-color-scheme: dark)').matches"));
        }
Beispiel #3
0
        public async Task ShouldRunInSpecifiedFrame()
        {
            await FrameUtils.AttachFrameAsync(Page, "frame1", Server.EmptyPage);

            await FrameUtils.AttachFrameAsync(Page, "frame2", Server.EmptyPage);

            var frame1 = Page.FirstChildFrame();
            var frame2 = Page.Frames.ElementAt(2);
            var waitForSelectorPromise = frame2.WaitForSelectorAsync("div", new() { State = WaitForSelectorState.Attached });
            await frame1.EvaluateAsync(AddElement, "div");

            await frame2.EvaluateAsync(AddElement, "div");

            var eHandle = await waitForSelectorPromise;

            Assert.AreEqual(frame2, await eHandle.OwnerFrameAsync());
        }
Beispiel #4
0
        public async Task ShouldClickTheButtonWithFixedPositionInsideAnIframe()
        {
            await Page.GotoAsync(Server.EmptyPage);

            await Page.SetViewportSizeAsync(500, 500);

            await Page.SetContentAsync("<div style=\"width:100px;height:2000px\">spacer</div>");

            await FrameUtils.AttachFrameAsync(Page, "button-test", Server.Prefix + "/input/button.html");

            var frame = Page.FirstChildFrame();
            await frame.EvalOnSelectorAsync("button", "button => button.style.setProperty('position', 'fixed')");

            await frame.ClickAsync("button");

            Assert.AreEqual("Clicked", await frame.EvaluateAsync <string>("window.result"));
        }
Beispiel #5
0
        public async Task ShouldRunInSpecifiedFrameXPath()
        {
            await FrameUtils.AttachFrameAsync(Page, "frame1", TestConstants.EmptyPage);

            await FrameUtils.AttachFrameAsync(Page, "frame2", TestConstants.EmptyPage);

            var frame1 = Page.Frames.First(f => f.Name == "frame1");
            var frame2 = Page.Frames.First(f => f.Name == "frame2");
            var waitForXPathPromise = frame2.WaitForSelectorAsync("//div", WaitForSelectorState.Attached);
            await frame1.EvaluateAsync(AddElement, "div");

            await frame2.EvaluateAsync(AddElement, "div");

            var eHandle = await waitForXPathPromise;

            Assert.Equal(frame2, await eHandle.OwnerFrameAsync());
        }
Beispiel #6
0
        public async Task ShouldReturnMatchingResponses()
        {
            await Page.GotoAsync(Server.EmptyPage);

            // Attach three frames.
            var matchingData = new MatchingResponseData[]
            {
                new() { FrameTask = FrameUtils.AttachFrameAsync(Page, "frame1", Server.EmptyPage) },
                new() { FrameTask = FrameUtils.AttachFrameAsync(Page, "frame2", Server.EmptyPage) },
                new() { FrameTask = FrameUtils.AttachFrameAsync(Page, "frame3", Server.EmptyPage) }
            };

            await TaskUtils.WhenAll(matchingData.Select(m => m.FrameTask));

            // Navigate all frames to the same URL.
            var requestHandler = new RequestDelegate(async(context) =>
            {
                if (int.TryParse(context.Request.Query["index"], out int index))
                {
                    await context.Response.WriteAsync(await matchingData[index].ServerResponseTcs.Task);
                }
            });

            Server.SetRoute("/one-style.html?index=0", requestHandler);
            Server.SetRoute("/one-style.html?index=1", requestHandler);
            Server.SetRoute("/one-style.html?index=2", requestHandler);

            for (int i = 0; i < 3; ++i)
            {
                var waitRequestTask = Server.WaitForRequest("/one-style.html");
                matchingData[i].NavigationTask = matchingData[i].FrameTask.Result.GotoAsync($"{Server.Prefix}/one-style.html?index={i}");
                await waitRequestTask;
            }
            // Respond from server out-of-order.
            string[] serverResponseTexts = new string[] { "AAA", "BBB", "CCC" };
            for (int i = 0; i < 3; ++i)
            {
                matchingData[i].ServerResponseTcs.TrySetResult(serverResponseTexts[i]);
                var response = await matchingData[i].NavigationTask;
                Assert.AreEqual(matchingData[i].FrameTask.Result, response.Frame);
                Assert.AreEqual(serverResponseTexts[i], await response.TextAsync());
            }
        }
        public async Task ShouldReportDifferentFrameInstanceWhenFrameReAttaches()
        {
            var frame1 = await FrameUtils.AttachFrameAsync(Page, "frame1", TestConstants.EmptyPage);

            await Page.EvaluateAsync(@"() => {
                window.frame = document.querySelector('#frame1');
                window.frame.remove();
            }");

            Assert.True(frame1.IsDetached);

            var(frame2, _) = await TaskUtils.WhenAll(
                Page.WaitForEventAsync(PageEvent.FrameNavigated),
                Page.EvaluateAsync("() => document.body.appendChild(window.frame)")
                );

            Assert.False(frame2.IsDetached);
            Assert.NotSame(frame1, frame2);
        }
        public async Task ShouldWorkForSubframes()
        {
            await using (var context = await Browser.NewContextAsync())
            {
                var page = await context.NewPageAsync();

                StringAssert.Contains("Mozilla", await page.EvaluateAsync <string>("navigator.userAgent"));
            }

            await using (var context = await Browser.NewContextAsync(new() { UserAgent = "foobar" }))
            {
                var page = await context.NewPageAsync();

                var(userAgent, _) = await TaskUtils.WhenAll(
                    Server.WaitForRequest <string>("/empty.html", (request) => request.Headers["user-agent"]),
                    FrameUtils.AttachFrameAsync(page, "frame1", Server.EmptyPage));

                Assert.AreEqual("foobar", userAgent);
            }
        }
        public async Task ShouldReportDifferentFrameInstanceWhenFrameReAttaches()
        {
            var frame1 = await FrameUtils.AttachFrameAsync(Page, "frame1", Server.EmptyPage);

            await Page.EvaluateAsync(@"() => {
                window.frame = document.querySelector('#frame1');
                window.frame.remove();
            }");

            Assert.True(frame1.IsDetached);

            var frameEvent = new TaskCompletionSource <IFrame>();

            Page.FrameNavigated += (_, frame) => frameEvent.TrySetResult(frame);

            var(frame2, _) = await TaskUtils.WhenAll(
                frameEvent.Task,
                Page.EvaluateAsync("() => document.body.appendChild(window.frame)")
                );

            Assert.False(frame2.IsDetached);
            Assert.That(frame1, Is.Not.EqualTo(frame2));
        }
Beispiel #10
0
        public async Task ShouldBeIsolatedBetweenFrames()
        {
            await Page.GotoAsync(Server.EmptyPage);

            await FrameUtils.AttachFrameAsync(Page, "frame1", Server.EmptyPage);

            Assert.AreEqual(2, Page.Frames.Count);
            var frames = Page.Frames;

            Assert.That(frames.First(), Is.Not.EqualTo(frames.ElementAt(1)));

            await TaskUtils.WhenAll(
                frames.First().EvaluateAsync("() => window.a = 1"),
                frames.ElementAt(1).EvaluateAsync("() => window.a = 2")
                );

            var(result1, result2) = await TaskUtils.WhenAll(
                frames.First().EvaluateAsync <int>("() => window.a"),
                frames.ElementAt(1).EvaluateAsync <int>("() => window.a")
                );

            Assert.AreEqual(1, result1);
            Assert.AreEqual(2, result2);
        }
        public async Task ShouldHandleNestedFrames()
        {
            await Page.GotoAsync(TestConstants.ServerUrl + "/frames/nested-frames.html");

            Assert.Equal(TestConstants.NestedFramesDumpResult, FrameUtils.DumpFrames(Page.MainFrame));
        }