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 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <TargetFramework>net8.0</TargetFramework>
+    <ImplicitUsings>enable</ImplicitUsings>
+    <Nullable>enable</Nullable>
+
+    <IsPackable>false</IsPackable>
+    <IsTestProject>true</IsTestProject>
+  </PropertyGroup>
+
+  <ItemGroup>
+    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
+    <PackageReference Include="Microsoft.Playwright" Version="1.41.2" />
+    <PackageReference Include="xunit" Version="2.6.6" />
+    <PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
+      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
+      <PrivateAssets>all</PrivateAssets>
+    </PackageReference>
+    <PackageReference Include="coverlet.collector" Version="6.0.0">
+      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
+      <PrivateAssets>all</PrivateAssets>
+    </PackageReference>
+  </ItemGroup>
+
+</Project>
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 ������ģ����̺����IJ���
+            //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
+
+        /// <summary>
+        /// �Ƚ���ϸ
+        /// </summary>
+        [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