diff --git a/Docs/说明.md b/Docs/说明.md new file mode 100644 index 0000000..ed8001b --- /dev/null +++ b/Docs/说明.md @@ -0,0 +1,3 @@ +# ��Ŀ˵�� + +Playwright���˵��˲��Կ��ѧϰ��ʹ��ԭ��.net���(��MSTest��NUnit��Ԫ���Կ�ܼ���)��xUnit���Կ�ܡ� \ No newline at end of file diff --git a/PlaywrightStudy.Core/PlaywrightStudy.Core.csproj b/PlaywrightStudy.Core/PlaywrightStudy.Core.csproj new file mode 100644 index 0000000..60384ec --- /dev/null +++ b/PlaywrightStudy.Core/PlaywrightStudy.Core.csproj @@ -0,0 +1,13 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <TargetFramework>net6.0</TargetFramework> + <ImplicitUsings>enable</ImplicitUsings> + <Nullable>enable</Nullable> + </PropertyGroup> + + <ItemGroup> + <PackageReference Include="Microsoft.Playwright" Version="1.41.2" /> + </ItemGroup> + +</Project> diff --git a/PlaywrightStudy.Core/TenAPI/GetIP.cs b/PlaywrightStudy.Core/TenAPI/GetIP.cs new file mode 100644 index 0000000..b76994e --- /dev/null +++ b/PlaywrightStudy.Core/TenAPI/GetIP.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PlaywrightStudy.Core.TenAPI +{ + /// <summary> + /// 获取IP信息 响应类 + /// </summary> + public class GetIP + { + public string ip { get; set; } + public string country { get; set; } + public string province { get; set; } + public string city { get; set; } + public string area { get; set; } + public string isp { get; set; } + public string os { get; set; } + public string browser { get; set; } + } +} + diff --git a/PlaywrightStudy.Core/TenAPI/TenApiReturnBase.cs b/PlaywrightStudy.Core/TenAPI/TenApiReturnBase.cs new file mode 100644 index 0000000..227b225 --- /dev/null +++ b/PlaywrightStudy.Core/TenAPI/TenApiReturnBase.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PlaywrightStudy.Core.TenAPI +{ + public class TenApiReturnBase<T> + { + public int code { get; set; } + + public string msg { get; set; } + + public T data { get; set; } + } +} diff --git a/PlaywrightStudy.Test/GlobalUsings.cs b/PlaywrightStudy.Test/GlobalUsings.cs index 1035ba8..82fcb1c 100644 --- a/PlaywrightStudy.Test/GlobalUsings.cs +++ b/PlaywrightStudy.Test/GlobalUsings.cs @@ -2,4 +2,7 @@ global using Xunit; global using Microsoft.Playwright; global using Microsoft.Playwright.Core; -global using Microsoft.Playwright.Transport; \ No newline at end of file +global using Microsoft.Playwright.Transport; + +global using PlaywrightStudy.Core; +global using PlaywrightStudy.Core.TenAPI; \ No newline at end of file diff --git a/PlaywrightStudy.Test/PlaywrightStudy.Test.csproj b/PlaywrightStudy.Test/PlaywrightStudy.Test.csproj index f361b30..de25c21 100644 --- a/PlaywrightStudy.Test/PlaywrightStudy.Test.csproj +++ b/PlaywrightStudy.Test/PlaywrightStudy.Test.csproj @@ -23,4 +23,8 @@ </PackageReference> </ItemGroup> + <ItemGroup> + <ProjectReference Include="..\PlaywrightStudy.Core\PlaywrightStudy.Core.csproj" /> + </ItemGroup> + </Project> diff --git a/PlaywrightStudy.Test/PlaywrightTest.cs b/PlaywrightStudy.Test/PlaywrightTest.cs index 4d0f4de..3527434 100644 --- a/PlaywrightStudy.Test/PlaywrightTest.cs +++ b/PlaywrightStudy.Test/PlaywrightTest.cs @@ -1,3 +1,4 @@ + namespace PlaywrightStudy.Test { public class PlaywrightTest @@ -416,5 +417,81 @@ namespace PlaywrightStudy.Test //}); } #endregion + + #region WebAPI + /// <summary> + /// ʹ����ѽӿ� + /// </summary> + [Fact] + public async Task WebAPI_TestAsync() + { + //https://docs.tenapi.cn/ ��ѽӿ� + //IP��ѯ https://tenapi.cn/v2/getip?ip=192.168.1.1 + + using var playwright = await Playwright.CreateAsync(); + var apiRequestContext = await playwright.APIRequest.NewContextAsync(new APIRequestNewContextOptions() + { + BaseURL="https://tenapi.cn", + ExtraHTTPHeaders = new Dictionary<string, string>() + { + { "Accept","text/html"}, + + //ȫ����֤ + { "Authorization","Bear a.b.c"} + }, + //HttpCredentials = new HttpCredentials() { Origin="http://localhost:8060",Username="admin",Password="123456" }, + //IgnoreHTTPSErrors = true, //���ô���ʱ������֤����� + Timeout = 30*1000, + }); + + var apiResponse = await apiRequestContext.GetAsync("v2/getip?ip=192.168.1.1"); + + Assert.True(apiResponse.Ok); + + var ipResult = await apiResponse.JsonAsync<TenApiReturnBase<GetIP>>(); + + Assert.NotNull(ipResult); + Assert.IsType<TenApiReturnBase<GetIP>>(ipResult); + + Assert.Equal("��ȡ�ɹ�", ipResult.msg); + + } + + /// <summary> + /// ʹ��Fiddlerץ��������FiddlerΪ����������,����������Fiddler + /// </summary> + [Fact] + public async Task WebAPI_FiddleProxy_TestAsync() + { + //https://docs.tenapi.cn/ ��ѽӿ� + //IP��ѯ https://tenapi.cn/v2/getip?ip=192.168.1.1 + + using var playwright = await Playwright.CreateAsync(); + var apiRequestContext = await playwright.APIRequest.NewContextAsync(new APIRequestNewContextOptions() + { + BaseURL = "https://tenapi.cn", + ExtraHTTPHeaders = new Dictionary<string, string>() + { + { "Accept","text/html"}, + { "Authorization","Bear a.b.c"} + }, + IgnoreHTTPSErrors = true, //���ô���ʱ������֤����� + Timeout = 30 * 1000, + Proxy = new Proxy() { Server = "http://127.0.0.1:8888" } + }); + + var apiResponse = await apiRequestContext.GetAsync("v2/getip?ip=192.168.1.1"); + + Assert.True(apiResponse.Ok); + + var ipResult = await apiResponse.JsonAsync<TenApiReturnBase<GetIP>>(); + + Assert.NotNull(ipResult); + Assert.IsType<TenApiReturnBase<GetIP>>(ipResult); + + Assert.Equal("��ȡ�ɹ�", ipResult.msg); + + } + #endregion } } \ No newline at end of file diff --git a/PlaywrightStudy.sln b/PlaywrightStudy.sln index 7495dee..b09820e 100644 --- a/PlaywrightStudy.sln +++ b/PlaywrightStudy.sln @@ -3,7 +3,14 @@ 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}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PlaywrightStudy.Test", "PlaywrightStudy.Test\PlaywrightStudy.Test.csproj", "{809E556A-9E9E-4110-A2ED-586CADBE6899}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Docs", "Docs", "{91229A84-D283-4F4F-BA18-6CD45A874B4B}" + ProjectSection(SolutionItems) = preProject + Docs\说明.md = Docs\说明.md + EndProjectSection +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PlaywrightStudy.Core", "PlaywrightStudy.Core\PlaywrightStudy.Core.csproj", "{230AC9DC-D0E2-41F8-BAF2-D15A6978FC62}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -15,6 +22,10 @@ Global {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 + {230AC9DC-D0E2-41F8-BAF2-D15A6978FC62}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {230AC9DC-D0E2-41F8-BAF2-D15A6978FC62}.Debug|Any CPU.Build.0 = Debug|Any CPU + {230AC9DC-D0E2-41F8-BAF2-D15A6978FC62}.Release|Any CPU.ActiveCfg = Release|Any CPU + {230AC9DC-D0E2-41F8-BAF2-D15A6978FC62}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE