我正在对Blazor应用程序进行硒测试,我想设置一个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 -> select SyncfusionSelenium instead of IIS Server as a Launch option and hit Ctrl+F5- or open project folder -> open console a enter "dotnet watch run debug"我非常感谢这里的一些帮助,因为我找不到如何为SfDropDownList做到这一点的例子。
有用的链接:
亚历克斯
发布于 2021-09-16 16:18:22
我们可以在Blazor中借助bUnit测试用例实现您的需求。这也是处理blazor组件的建议方法,.Please引用下面的代码片段供您参考。
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);
} https://stackoverflow.com/questions/69087179
复制相似问题