一、流程如下
C#代码:
using Microsoft.Playwright;
using Newtonsoft.Json;
using System;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Xml.Linq;
namespace TestProject3
{
[TestClass]
public class UnitTest1 : PageTest
{
[TestMethod]
public async Task HomepageHasPlaywrightInTitleAndGetStartedLinkLinkingtoTheIntroPage()
{
await using var browser = await Playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
{
Headless = false,
});
var context = await browser.NewContextAsync();
var page = await context.NewPageAsync();
await page.GotoAsync("https://boot3.jeecg.com/login?redirect=/dashboard/analysis");
await page.GetByRole(AriaRole.Textbox, new() { NameString = "账号" }).ClickAsync();
await page.GetByRole(AriaRole.Textbox, new() { NameString = "账号" }).FillAsync("jeecg");
await page.GetByRole(AriaRole.Textbox, new() { NameString = "账号" }).PressAsync("Tab");
await page.GetByRole(AriaRole.Textbox, new() { NameString = "密码" }).FillAsync("jeecg#123456");
await page.GetByRole(AriaRole.Textbox, new() { NameString = "密码" }).PressAsync("Tab");
while (true)
{
var imageEle = page.Locator("#app > div > div:nth-child(4) > div > div > div > div.aui-formBox > div > div.aui-form-box > form > div > div:nth-child(3) > div.aui-code > img");
var codePngName = DateTime.Now.ToFileTime().ToString() + ".png";
var codePngPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ScreenshotImges", codePngName);
await imageEle.ScreenshotAsync(new LocatorScreenshotOptions { Path = codePngPath });
//验证码图片识别
var address = "http://localhost:5252/api/hellos/upload";
var httpClient = new HttpClient();
var fileInfo = new FileInfo(codePngPath);
using (var stream = fileInfo.OpenRead())
{
var content = new MultipartFormDataContent
{
{ new StreamContent(stream,(int)stream.Length), "file", fileInfo.Name }
};
var httpResponseMessage = await httpClient.PostAsync(address, content);
if (httpResponseMessage.StatusCode == System.Net.HttpStatusCode.OK)
{
var jsonResult = await httpResponseMessage.Content.ReadAsStringAsync();
}
}
//获取识别结果
var code = "";
address = "http://localhost:5252/api/hellos/page";
var jsonContent = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(new
{
PageIndex = 1,
PageSize = 1,
Name = ""
}), Encoding.UTF8, "application/json");
var responseMessage = await httpClient.PostAsync(address, jsonContent);
if (responseMessage.StatusCode == System.Net.HttpStatusCode.OK)
{
var jsonResult = await responseMessage.Content.ReadAsStringAsync();
var ocrResponse = Newtonsoft.Json.JsonConvert.DeserializeObject<OcrResponse>(jsonResult);
code = ocrResponse.Data.Items.First().Name;
}
await page.GetByRole(AriaRole.Textbox, new() { NameString = "验证码" }).FillAsync(code);
System.Threading.Thread.Sleep(1000);
await page.GetByRole(AriaRole.Button, new() { NameString = "登 录" }).ClickAsync();
try
{
await page.WaitForURLAsync("https://boot3.jeecg.com/dashboard/analysis", new PageWaitForURLOptions { Timeout =1000 });
}
catch (Exception ex)
{
}
if (page.Url == "https://boot3.jeecg.com/dashboard/analysis")
{
break;
}
}
}
}
public class OcrResponse
{
public bool Success { get; set; }
public OcrData Data { get; set; }
public string Message { get; set; }
}
public class OcrData
{
[JsonProperty("data")]
public List<OcrItem> Items { get; set; }
}
public class OcrItem
{
public string Name { get; set; }
}
}