Explorar o código

feat: 😀增加基于Selenium自动化测试项目

喵你个旺呀 hai 1 ano
pai
achega
678806bf82

+ 22 - 0
Admin.NET/Admin.NET.Test/Admin.NET.Test.csproj

@@ -0,0 +1,22 @@
+<Project Sdk="Microsoft.NET.Sdk">
+    <PropertyGroup>
+        <TargetFrameworks>net6.0;net8.0</TargetFrameworks>
+        <NoWarn>1701;1702;1591;8632</NoWarn>
+        <DocumentationFile></DocumentationFile>
+        <ImplicitUsings>enable</ImplicitUsings>
+        <PreserveCompilationContext>true</PreserveCompilationContext>
+        <Nullable>disable</Nullable>
+        <GenerateDocumentationFile>True</GenerateDocumentationFile>
+        <Copyright>Admin.NET</Copyright>
+        <Description>Admin.NET 通用权限开发平台</Description>
+    </PropertyGroup>
+
+    <ItemGroup>
+      <PackageReference Include="Furion.Xunit" Version="4.9.5.26" />
+      <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
+      <PackageReference Include="Selenium.RC" Version="3.1.0" />
+      <PackageReference Include="Selenium.Support" Version="4.27.0" />
+      <PackageReference Include="Selenium.WebDriver" Version="4.27.0" />
+      <PackageReference Include="Selenium.WebDriver.MSEdgeDriver" Version="131.0.2903.48" />
+    </ItemGroup>
+</Project>

+ 82 - 0
Admin.NET/Admin.NET.Test/BaseTest.cs

@@ -0,0 +1,82 @@
+// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
+// 
+// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
+// 
+// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
+
+using OpenQA.Selenium;
+using OpenQA.Selenium.Edge;
+
+namespace Admin.NET.Test;
+
+/// <summary>
+/// 测试基类
+/// </summary>
+public class BaseTest : IDisposable
+{
+    private readonly string _baseUrl = "http://localhost:8888";
+    protected readonly EdgeDriver Driver = new ();
+
+    protected BaseTest()
+    {
+        Driver.Manage().Window.Maximize();
+        Driver.Navigate().GoToUrl(_baseUrl);
+    }
+
+    /// <summary>
+    /// 用户登录
+    /// </summary>
+    /// <param name="account"></param>
+    /// <param name="password"></param>
+    protected async Task Login(string account = "superadmin", string password = "123456")
+    {
+        await Driver.Navigate().RefreshAsync();
+        await Task.Delay(6000);
+
+        var inputList = Driver.FindElements(By.CssSelector("#pane-account input"));
+        var button = Driver.FindElement(By.CssSelector("#pane-account button"));
+        var accountInput = inputList.First();
+        var passwordInput = inputList.Skip(1).First();
+        var captchaInput = inputList.Skip(2).First();
+
+        // 输入用户名
+        accountInput.Clear();
+        accountInput.SendKeys(account);
+
+        // 输入密码
+        passwordInput.Clear();
+        passwordInput.SendKeys(password);
+
+        // 输入验证码
+        captchaInput.Clear();
+        captchaInput.SendKeys("0");
+
+        // 提交
+        button.Click();
+    }
+
+    /// <summary>
+    /// 打开指定页面
+    /// </summary>
+    /// <param name="url"></param>
+    protected async Task GoToUrl(string url)
+    {
+        if (url.StartsWith("http")) await Driver.Navigate().GoToUrlAsync(url);
+        else await Driver.Navigate().GoToUrlAsync(_baseUrl + "/" + url.TrimStart('/'));
+    }
+
+    /// <summary>
+    /// 登录用户敲回车
+    /// </summary>
+    protected void WaitEnter()
+    {
+        Console.WriteLine("登录用户敲回车继续...");
+        Console.ReadLine();
+    }
+
+    public void Dispose()
+    {
+        Driver.Quit();
+        Driver.Dispose();
+    }
+}

+ 19 - 0
Admin.NET/Admin.NET.Test/User/UserTest.cs

@@ -0,0 +1,19 @@
+// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
+//
+// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
+//
+// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
+
+using Xunit;
+
+namespace Admin.NET.Test.User;
+
+public class UserTest : BaseTest
+{
+    [Fact]
+    public async Task Login()
+    {
+        await base.Login();
+        WaitEnter();
+    }
+}

+ 6 - 0
Admin.NET/Admin.NET.sln

@@ -28,6 +28,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Admin.NET.Plugin.ApprovalFl
 EndProject
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Admin.NET.Plugin.K3Cloud", "Plugins\Admin.NET.Plugin.K3Cloud\Admin.NET.Plugin.K3Cloud.csproj", "{72EB89AB-15F7-4F85-88DB-7C2EF7C3D588}"
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Admin.NET.Test", "Admin.NET.Test\Admin.NET.Test.csproj", "{57350E6A-B5A0-452C-9FD4-C69617C6DA30}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -70,6 +72,10 @@ Global
 		{72EB89AB-15F7-4F85-88DB-7C2EF7C3D588}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{72EB89AB-15F7-4F85-88DB-7C2EF7C3D588}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{72EB89AB-15F7-4F85-88DB-7C2EF7C3D588}.Release|Any CPU.Build.0 = Release|Any CPU
+		{57350E6A-B5A0-452C-9FD4-C69617C6DA30}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{57350E6A-B5A0-452C-9FD4-C69617C6DA30}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{57350E6A-B5A0-452C-9FD4-C69617C6DA30}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{57350E6A-B5A0-452C-9FD4-C69617C6DA30}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE