我正在做一个Blazor web应用程序的Selenium测试,我想设置一个SfDropDownList的值。
我用SfDropDownList创建了一个空的Blazor项目,用Selenium和XUnit创建了一个测试项目。项目下载网址:https://www.syncfusion.com/downloads/support/forum/167910/ze/SYncfusionSelenium_b2ea0a0e
<SfDropDownList ID="MyDropdown" TValue="string" TItem="Games" Placeholder="Select a game" DataSource="@LocalData">
<DropDownListFieldSettings Value="ID" Text="Text"></DropDownListFieldSettings>
</SfDropDownList>
@code {
public class Games
{
public string ID { get; set; }
public string Text { get; set; }
}
List<Games> LocalData = new List<Games> {
new Games() { ID= "Game1", Text= "American Football" },
new Games() { ID= "Game2", Text= "Badminton" },
new Games() { ID= "Game3", Text= "Basketball" },
new Games() { ID= "Game4", Text= "Cricket" },
new Games() { ID= "Game5", Text= "Football" },
};
}
这就是我如何设法设置/改变下拉列表的值:我通过ID访问SfDropDownList,并从我的类"Games"中设置'Text'。我必须这样做2次,以确保正确设置,这是目前为止我想到的最好的。
Thread.Sleep(TimeSpan.FromMilliseconds(250));
_driver.FindElement(By.Id("MyDropdown")).SendKeys("Basketball");
Thread.Sleep(TimeSpan.FromMilliseconds(250));
_driver.FindElement(By.Id("MyDropdown")).SendKeys("Basketball");
运行Selenium:
在http://localhost:5000启动服务器
open VS ->选择SyncfusionSelenium而不是IIS Server作为启动选项,并按Ctrl+F5
或打开项目文件夹->打开控制台,输入"dotnet watch run ">
打开测试资源管理器并运行/debug "Test1">
我非常感谢您的帮助,因为我找不到SfDropDownList的示例。
的有用链接:
- https://www.syncfusion.com/forums/167910/selenium-how-to-manage-a-sfdropdownlist-with-selenium
- https://blazor.syncfusion.com/documentation/dropdown-list/getting-started
- https://learn.microsoft.com/en us/aspnet/core/blazor/test?view=aspnetcore - 5.0 亚历克斯
我们可以借助Blazor中的bUnit测试用例实现您的要求。这也是处理blazor组件的建议方法。请参考以下代码片段作为参考。
using System;
using System.Collections.Generic;
using System.Text;
using Xunit;
using Bunit;
using Syncfusion.Blazor;
using Syncfusion.Blazor.DropDowns;
using Syncfusion.Blazor.Tests.Base;
using static Bunit.ComponentParameterFactory;
using Syncfusion.Blazor.Calendars;
using System.Threading.Tasks;
using AngleSharp.Css.Dom;
using Syncfusion.Blazor.Inputs;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components;
using System.Globalization;
private List<Countries> GetDataItems()
{
return new List<Countries>
{
new Countries() { Name = "Australia", Code = "AU" },
new Countries() { Name = "Bermuda", Code = "BM" },
new Countries() { Name = "Canada", Code = "CA" },
new Countries() { Name = "Cameroon", Code = "CM" },
new Countries() { Name = "Denmark", Code = "DK" },
new Countries() { Name = "France", Code = "FR" }
};
}
[Fact(DisplayName = "Value at dynamic changes")]
public async Task DynamicValueBinding()
{
var data = GetDataItems();
var dropdown = RenderComponent<SfDropDownList<string, Countries>>(parameters =>
parameters.Add(p => p.DataSource, data).AddChildContent<DropDownListFieldSettings>(field => field.Add(p => p.Text, "Name").Add(p => p.Value, "Code")));
dropdown.SetParametersAndRender(("Value", "AU"));
await Task.Delay(200);
var inputEle = dropdown.Find("input");
Assert.Equal(dropdown.Instance.Text, inputEle.GetAttribute("value"));
Assert.Equal(0, dropdown.Instance.Index);
dropdown.SetParametersAndRender(("Value", null));
Assert.Equal(dropdown.Instance.Text, inputEle.GetAttribute("value"));
Assert.Null(dropdown.Instance.Value);
Assert.Null(dropdown.Instance.Index);
dropdown.SetParametersAndRender(("Index", 3));
await Task.Delay(200);
inputEle = dropdown.Find("input");
Assert.Equal("CM", dropdown.Instance.Value);
Assert.Equal(3, dropdown.Instance.Index);
dropdown.SetParametersAndRender(("Index", null));
Assert.Null(dropdown.Instance.Value);
Assert.Null(dropdown.Instance.Index);
}