From e1f38b1fc57439b8228b361f2d92ac3248dd4257 Mon Sep 17 00:00:00 2001 From: wanggaofeng <15601716045@163.com> Date: Tue, 30 Jan 2024 21:07:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0VS=E9=A1=B9=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PlaywrightStudy.Test/GlobalUsings.cs | 5 + .../PlaywrightStudy.Test.csproj | 26 ++ PlaywrightStudy.Test/PlaywrightTest.cs | 420 ++++++++++++++++++ PlaywrightStudy.sln | 25 ++ 4 files changed, 476 insertions(+) create mode 100644 PlaywrightStudy.Test/GlobalUsings.cs create mode 100644 PlaywrightStudy.Test/PlaywrightStudy.Test.csproj create mode 100644 PlaywrightStudy.Test/PlaywrightTest.cs create mode 100644 PlaywrightStudy.sln diff --git a/PlaywrightStudy.Test/GlobalUsings.cs b/PlaywrightStudy.Test/GlobalUsings.cs new file mode 100644 index 0000000..1035ba8 --- /dev/null +++ b/PlaywrightStudy.Test/GlobalUsings.cs @@ -0,0 +1,5 @@ +global using Xunit; + +global using Microsoft.Playwright; +global using Microsoft.Playwright.Core; +global using Microsoft.Playwright.Transport; \ No newline at end of file diff --git a/PlaywrightStudy.Test/PlaywrightStudy.Test.csproj b/PlaywrightStudy.Test/PlaywrightStudy.Test.csproj new file mode 100644 index 0000000..f361b30 --- /dev/null +++ b/PlaywrightStudy.Test/PlaywrightStudy.Test.csproj @@ -0,0 +1,26 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + diff --git a/PlaywrightStudy.Test/PlaywrightTest.cs b/PlaywrightStudy.Test/PlaywrightTest.cs new file mode 100644 index 0000000..4d0f4de --- /dev/null +++ b/PlaywrightStudy.Test/PlaywrightTest.cs @@ -0,0 +1,420 @@ +namespace PlaywrightStudy.Test +{ + public class PlaywrightTest + { + #region Edge + [Fact] + public async void Baidu_Edge_Test() + { + using var playwright = await Playwright.CreateAsync(); + + //await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions() + //{ + // SlowMo = 50, //ÂýËÙ + // Headless = false, // ¹Ø±ÕÎÞͷģʽ(ÓнçÃæ) + // Channel = "msedge", // Ö¸¶¨²ÉÓÃchromeä¯ÀÀÆ÷ÀàÐÍ() + // Devtools = false, // ÆôÓÿª·¢Õß¹¤¾ß + // ChromiumSandbox = false, // ¹Ø±Õä¯ÀÀÆ÷ɳºÐ + // ExecutablePath = string.Empty, // ²»Ö¸¶¨ä¯ÀÀÆ÷¿ÉÖ´ÐÐÎļþλÖ㬻á×Ô¶¯Ñ°ÕÒ ms-playwright ÏÂÔصÄä¯ÀÀÆ÷ + // Args = new[] { "--enable-automation=true", "--disable-blink-features=AutomationControlled" }, // ·ÀÖ¹selenium±»¼ì²â + //}); + + //ä¯ÀÀÆ÷£ºÄ¬ÈÏEdgeÓÅÏÈ + await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions() + { + //¹Ø±ÕÎÞͷģʽ(ÓнçÃæ) + Headless = false, + }); + + //ä¯ÀÀÆ÷ÉÏÏÂÎÄ + var browserContexts = browser.Contexts; + + //Ò³Ãæ + var page = await browser.NewPageAsync(); + + //Ò³ÃæÏìÓ¦ + var response = await page.GotoAsync("https://www.baidu.com/"); + + //¶¨Î»Æ÷£ºËÑË÷¿ò + var searchBoxLocator = page.GetByRole(AriaRole.Textbox).Nth(0); + + //ÔªËظöÊý + var count = await searchBoxLocator.CountAsync(); + + //xUnit¶ÏÑÔ + Assert.True(count > 0); + + //Playwright¶ÏÑÔ + await Microsoft.Playwright.Assertions.Expect(searchBoxLocator).ToHaveCountAsync(1); + + //ÊäÈëËÑË÷ÄÚÈÝÇ°²¿·Ö(Ò»´ÎÐÔÈ«²¿ÊäÈë) + await searchBoxLocator.FillAsync("Playwright"); + + //ÊäÈëËÑË÷ÄÚÈݺ󲿷Ö(Ä£ÄâÓû§ÊäÈ룺ÿ¸ö×Ö·ûÖ®¼äÓÐÑÓʱ)£¬ÒÑÆúÓà + //await textboxLocator.TypeAsync(".net", new LocatorTypeOptions { Delay = 1000 }); + + //ÊäÈëËÑË÷ÄÚÈݺ󲿷Ö(Ä£ÄâÓû§ÊäÈ룺ÿ¸ö×Ö·ûÖ®¼äÓÐÑÓʱ) + await searchBoxLocator.PressSequentiallyAsync(".NET", new LocatorPressSequentiallyOptions { Delay = 200 }); + + //ÊäÈë»Ø³µ:È·ÈÏËÑË÷[¿ÉÊäÈëÌØÊâ×Ö·û,Ò²¿ÉÒÔÊÇ×éºÏ¼ü("Shift+Home"¡¢"Alt+Enter")] + //await searchBoxLocator.PressAsync("Enter"); + + //·½·¨¶þ£ºµ¥»÷ËÑË÷°´Å¥ + var searchButtonLocator = page.Locator("#su"); + + var searchButtonCount = await searchButtonLocator.CountAsync(); + + //¶ÏÑÔ:°´Å¥ÕÒµ½ + await Microsoft.Playwright.Assertions.Expect(searchButtonLocator).ToHaveCountAsync(1); + + //µ¥»÷ËÑË÷°´Å¥ + await searchButtonLocator.ClickAsync(); + + //ʹÓà page.Keyboard ºÍ page.Mouse ¶ÔÏóÀ´Ä£Äâ¼üÅ̺ÍÊó±êµÄ²Ù×÷ + //await page.Keyboard.PressAsync("Enter"); + //await page.Mouse.ClickAsync(100, 100); + + await page.WaitForLoadStateAsync(); + + //½Øͼ + await page.ScreenshotAsync(new() + { + Path = "Baidu.Edge.Screenshot.png" + }); + + //ÍøÒ³±£´æΪPdfÎļþ + await page.PdfAsync(new() + { + Path = "Baidu.Edge.pdf", + DisplayHeaderFooter = false, + Format = "A4" + }); + } + + [Fact] + public async void Bing_Edge_Test() + { + using var playwright = await Playwright.CreateAsync(); + + //ä¯ÀÀÆ÷£ºÄ¬ÈÏEdgeÓÅÏÈ + await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions() + { + //¹Ø±ÕÎÞͷģʽ(ÓнçÃæ) + Headless = false, + }); + + //ä¯ÀÀÆ÷ÉÏÏÂÎÄ + var browserContexts = browser.Contexts; + + //Ò³Ãæ + var page = await browser.NewPageAsync(); + + //Ò³ÃæÏìÓ¦ + var response = await page.GotoAsync("https://cn.bing.com/"); + + //ÇëÇó³É¹¦ + Assert.Equal(200, response?.Status); + + //¶¨Î»Æ÷£ºËÑË÷¿ò + var searchBoxLocator = page.Locator("input#sb_form_q"); + + //ÊäÈë¿ò¸öÊý + var count = await searchBoxLocator.CountAsync(); + + //xUnit¶ÏÑÔ + Assert.True(count > 0); + + //Playwright¶ÏÑÔ + await Microsoft.Playwright.Assertions.Expect(searchBoxLocator).ToHaveCountAsync(1); + + //ÊäÈëËÑË÷ÄÚÈÝÇ°²¿·Ö(Ò»´ÎÐÔÈ«²¿ÊäÈë) + //await searchBoxLocator.FillAsync("Playwright"); + + //ÊäÈëËÑË÷ÄÚÈݺ󲿷Ö(Ä£ÄâÓû§ÊäÈ룺ÿ¸ö×Ö·ûÖ®¼äÓÐÑÓʱ) + await searchBoxLocator.PressSequentiallyAsync("Playwright.NET", new LocatorPressSequentiallyOptions { Delay = 200 }); + + //ÊäÈë»Ø³µ:È·ÈÏËÑË÷[¿ÉÊäÈëÌØÊâ×Ö·û,Ò²¿ÉÒÔÊÇ×éºÏ¼ü("Shift+Home"¡¢"Alt+Enter")] + await searchBoxLocator.PressAsync("Enter"); + + await page.WaitForLoadStateAsync(); + + //½Øͼ + await page.ScreenshotAsync(new() + { + Path = "Bing.Edge.Screenshot.png" + }); + + //ÍøÒ³±£´æΪPdfÎļþ + await page.PdfAsync(new() + { + Path = "Bing.Edge.pdf", + DisplayHeaderFooter = false, + Format = "A4" + }); + } + + #endregion + + #region Chrome + + /// + /// ±È½ÏÏêϸ + /// + [Fact] + public async void Baidu_Chrome_Test() + { + using var playwright = await Playwright.CreateAsync(); + + //ä¯ÀÀÆ÷£ºÖ¸¶¨ chrome + await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions() + { + SlowMo = 50, //ÂýËÙ + Headless = false, // ¹Ø±ÕÎÞͷģʽ(ÓнçÃæ) + Channel = "chrome", // Ö¸¶¨²ÉÓÃchromeä¯ÀÀÆ÷ÀàÐÍ() + Devtools = false, // ÆôÓÿª·¢Õß¹¤¾ß + ChromiumSandbox = true, // ¹Ø±Õä¯ÀÀÆ÷ɳºÐ + ExecutablePath = string.Empty, // ²»Ö¸¶¨ä¯ÀÀÆ÷¿ÉÖ´ÐÐÎļþλÖ㬻á×Ô¶¯Ñ°ÕÒ ms-playwright ÏÂÔصÄä¯ÀÀÆ÷ + DownloadsPath = "download", + }); + + //Ò³Ãæ + var page = await browser.NewPageAsync(); + + //Ò³ÃæÏìÓ¦ + var response = await page.GotoAsync("https://www.baidu.com/"); + + //¶¨Î»Æ÷£ºËÑË÷¿ò + var searchBoxLocator = page.GetByRole(AriaRole.Textbox).Nth(0); + + //ÔªËظöÊý + var count = await searchBoxLocator.CountAsync(); + + //xUnit¶ÏÑÔ + Assert.True(count > 0); + + //Playwright¶ÏÑÔ + await Microsoft.Playwright.Assertions.Expect(searchBoxLocator).ToHaveCountAsync(1); + + //ÊäÈëËÑË÷ÄÚÈÝÇ°²¿·Ö(Ò»´ÎÐÔÈ«²¿ÊäÈë) + //await searchBoxLocator.FillAsync("Playwright"); + + //ÊäÈëËÑË÷ÄÚÈݺ󲿷Ö(Ä£ÄâÓû§ÊäÈ룺ÿ¸ö×Ö·ûÖ®¼äÓÐÑÓʱ) + await searchBoxLocator.PressSequentiallyAsync("Playwright.NET", new LocatorPressSequentiallyOptions { Delay = 200 }); + + //ÊäÈë»Ø³µ:È·ÈÏËÑË÷[¿ÉÊäÈëÌØÊâ×Ö·û,Ò²¿ÉÒÔÊÇ×éºÏ¼ü("Shift+Home"¡¢"Alt+Enter")] + await searchBoxLocator.PressAsync("Enter"); + + //µÈ´ý + await page.WaitForLoadStateAsync(LoadState.DOMContentLoaded); + + //½Øͼ + await page.ScreenshotAsync(new() + { + Path = "Baidu.Chrome.Screenshot.png" + }); + + //ÍøÒ³±£´æΪPdfÎļþ + await page.PdfAsync(new() + { + Path = "Baidu.Chrome.pdf", + DisplayHeaderFooter = false, + Format = "A4" + }); + } + + + #endregion + + #region Firefox + [Fact] + public async void Baidu_Firefox_Test() + { + using var playwright = await Playwright.CreateAsync(); + + //ä¯ÀÀÆ÷£ºÖ¸¶¨ chrome + await using var browser = await playwright.Firefox.LaunchAsync(new BrowserTypeLaunchOptions() + { + SlowMo = 50, //ÂýËÙ + Headless = false, // ¹Ø±ÕÎÞͷģʽ(ÓнçÃæ + Devtools = false, // ÆôÓÿª·¢Õß¹¤¾ß + ChromiumSandbox = true, // ¹Ø±Õä¯ÀÀÆ÷ɳºÐ + ExecutablePath = string.Empty, // ²»Ö¸¶¨ä¯ÀÀÆ÷¿ÉÖ´ÐÐÎļþλÖ㬻á×Ô¶¯Ñ°ÕÒ ms-playwright ÏÂÔصÄä¯ÀÀÆ÷ + DownloadsPath = "download", + }); + + //Ò³Ãæ + var page = await browser.NewPageAsync(); + + //Ò³ÃæÏìÓ¦ + var response = await page.GotoAsync("https://www.baidu.com/"); + + //¶¨Î»Æ÷£ºËÑË÷¿ò + var searchBoxLocator = page.GetByRole(AriaRole.Textbox).Nth(0); + + //ÔªËظöÊý + var count = await searchBoxLocator.CountAsync(); + + //xUnit¶ÏÑÔ + Assert.True(count > 0); + + //Playwright¶ÏÑÔ + await Microsoft.Playwright.Assertions.Expect(searchBoxLocator).ToHaveCountAsync(1); + + //ÊäÈëËÑË÷ÄÚÈÝÇ°²¿·Ö(Ò»´ÎÐÔÈ«²¿ÊäÈë) + //await searchBoxLocator.FillAsync("Playwright"); + + //ÊäÈëËÑË÷ÄÚÈݺ󲿷Ö(Ä£ÄâÓû§ÊäÈ룺ÿ¸ö×Ö·ûÖ®¼äÓÐÑÓʱ) + await searchBoxLocator.PressSequentiallyAsync("Playwright.NET", new LocatorPressSequentiallyOptions { Delay = 200 }); + + //ÊäÈë»Ø³µ:È·ÈÏËÑË÷[¿ÉÊäÈëÌØÊâ×Ö·û,Ò²¿ÉÒÔÊÇ×éºÏ¼ü("Shift+Home"¡¢"Alt+Enter")] + await searchBoxLocator.PressAsync("Enter"); + + //µÈ´ý + await page.WaitForLoadStateAsync(LoadState.DOMContentLoaded); + + //½Øͼ + await page.ScreenshotAsync(new() + { + Path = "Baidu.Firefox.Screenshot.png" + }); + + //ÍøÒ³±£´æΪPdfÎļþ:FireFox²»Ö§³Ö + //await page.PdfAsync(new() + //{ + // Path = "Baidu.Firefox.pdf", + // DisplayHeaderFooter = false, + // Format = "A4" + //}); + } + + [Fact] + public async void Bing_Firefox_Test() + { + using var playwright = await Playwright.CreateAsync(); + + //ä¯ÀÀÆ÷£ºÖ¸¶¨ chrome + await using var browser = await playwright.Firefox.LaunchAsync(new BrowserTypeLaunchOptions() + { + SlowMo = 50, //ÂýËÙ + Headless = false, // ¹Ø±ÕÎÞͷģʽ(ÓнçÃæ + Devtools = false, // ÆôÓÿª·¢Õß¹¤¾ß + ChromiumSandbox = true, // ¹Ø±Õä¯ÀÀÆ÷ɳºÐ + ExecutablePath = string.Empty, // ²»Ö¸¶¨ä¯ÀÀÆ÷¿ÉÖ´ÐÐÎļþλÖ㬻á×Ô¶¯Ñ°ÕÒ ms-playwright ÏÂÔصÄä¯ÀÀÆ÷ + DownloadsPath = "download", + }); + + //ä¯ÀÀÆ÷ÉÏÏÂÎÄ + var browserContexts = browser.Contexts; + + //Ò³Ãæ + var page = await browser.NewPageAsync(); + + //Ò³ÃæÏìÓ¦ + var response = await page.GotoAsync("https://cn.bing.com/"); + + //ÇëÇó³É¹¦ + Assert.Equal(200, response?.Status); + + //¶¨Î»Æ÷£ºËÑË÷¿ò + var searchBoxLocator = page.Locator("input#sb_form_q"); + + //ÊäÈë¿ò¸öÊý + var count = await searchBoxLocator.CountAsync(); + + //xUnit¶ÏÑÔ + Assert.True(count > 0); + + //Playwright¶ÏÑÔ + await Microsoft.Playwright.Assertions.Expect(searchBoxLocator).ToHaveCountAsync(1); + + //ÊäÈëËÑË÷ÄÚÈÝÇ°²¿·Ö(Ò»´ÎÐÔÈ«²¿ÊäÈë) + //await searchBoxLocator.FillAsync("Playwright"); + + //ÊäÈëËÑË÷ÄÚÈݺ󲿷Ö(Ä£ÄâÓû§ÊäÈ룺ÿ¸ö×Ö·ûÖ®¼äÓÐÑÓʱ) + await searchBoxLocator.PressSequentiallyAsync("Playwright.NET", new LocatorPressSequentiallyOptions { Delay = 200 }); + + //ÊäÈë»Ø³µ:È·ÈÏËÑË÷[¿ÉÊäÈëÌØÊâ×Ö·û,Ò²¿ÉÒÔÊÇ×éºÏ¼ü("Shift+Home"¡¢"Alt+Enter")] + await searchBoxLocator.PressAsync("Enter"); + + await page.WaitForLoadStateAsync(); + + //½Øͼ + await page.ScreenshotAsync(new() + { + Path = "Bing.Firefox.Screenshot.png" + }); + + //ÍøÒ³±£´æΪPdfÎļþ + //await page.PdfAsync(new() + //{ + // Path = "Bing.Firefox.pdf", + // DisplayHeaderFooter = false, + // Format = "A4" + //}); + } + #endregion + + #region WebKit + [Fact] + public async void Baidu_WebKit_Test() + { + using var playwright = await Playwright.CreateAsync(); + + //ä¯ÀÀÆ÷£ºÖ¸¶¨ chrome + await using var browser = await playwright.Webkit.LaunchAsync(new BrowserTypeLaunchOptions() + { + SlowMo = 50, //ÂýËÙ + Headless = false, // ¹Ø±ÕÎÞͷģʽ(ÓнçÃæ + Devtools = false, // ÆôÓÿª·¢Õß¹¤¾ß + ChromiumSandbox = true, // ¹Ø±Õä¯ÀÀÆ÷ɳºÐ + ExecutablePath = string.Empty, // ²»Ö¸¶¨ä¯ÀÀÆ÷¿ÉÖ´ÐÐÎļþλÖ㬻á×Ô¶¯Ñ°ÕÒ ms-playwright ÏÂÔصÄä¯ÀÀÆ÷ + DownloadsPath = "download", + }); + + //Ò³Ãæ + var page = await browser.NewPageAsync(); + + //Ò³ÃæÏìÓ¦ + var response = await page.GotoAsync("https://www.baidu.com/"); + + //¶¨Î»Æ÷£ºËÑË÷¿ò + var searchBoxLocator = page.GetByRole(AriaRole.Textbox).Nth(0); + + //ÔªËظöÊý + var count = await searchBoxLocator.CountAsync(); + + //xUnit¶ÏÑÔ + Assert.True(count > 0); + + //Playwright¶ÏÑÔ + await Microsoft.Playwright.Assertions.Expect(searchBoxLocator).ToHaveCountAsync(1); + + //ÊäÈëËÑË÷ÄÚÈÝÇ°²¿·Ö(Ò»´ÎÐÔÈ«²¿ÊäÈë) + //await searchBoxLocator.FillAsync("Playwright"); + + //ÊäÈëËÑË÷ÄÚÈݺ󲿷Ö(Ä£ÄâÓû§ÊäÈ룺ÿ¸ö×Ö·ûÖ®¼äÓÐÑÓʱ) + await searchBoxLocator.PressSequentiallyAsync("Playwright.NET", new LocatorPressSequentiallyOptions { Delay = 200 }); + + //ÊäÈë»Ø³µ:È·ÈÏËÑË÷[¿ÉÊäÈëÌØÊâ×Ö·û,Ò²¿ÉÒÔÊÇ×éºÏ¼ü("Shift+Home"¡¢"Alt+Enter")] + await searchBoxLocator.PressAsync("Enter"); + + //µÈ´ý + await page.WaitForLoadStateAsync(LoadState.DOMContentLoaded); + + //½Øͼ + await page.ScreenshotAsync(new() + { + Path = "Baidu.WebKit.Screenshot.png" + }); + + //ÍøÒ³±£´æΪPdfÎļþ:FireFox²»Ö§³Ö + //await page.PdfAsync(new() + //{ + // Path = "Baidu.WebKit.pdf", + // DisplayHeaderFooter = false, + // Format = "A4" + //}); + } + #endregion + } +} \ No newline at end of file diff --git a/PlaywrightStudy.sln b/PlaywrightStudy.sln new file mode 100644 index 0000000..7495dee --- /dev/null +++ b/PlaywrightStudy.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.9.34518.117 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PlaywrightStudy.Test", "PlaywrightStudy.Test\PlaywrightStudy.Test.csproj", "{809E556A-9E9E-4110-A2ED-586CADBE6899}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {809E556A-9E9E-4110-A2ED-586CADBE6899}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {809E556A-9E9E-4110-A2ED-586CADBE6899}.Debug|Any CPU.Build.0 = Debug|Any CPU + {809E556A-9E9E-4110-A2ED-586CADBE6899}.Release|Any CPU.ActiveCfg = Release|Any CPU + {809E556A-9E9E-4110-A2ED-586CADBE6899}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {C5435415-B2CB-4326-9381-1B9389336AB5} + EndGlobalSection +EndGlobal