【问题标题】:How to set a value of a Syncfusion SfDropDownList with Selenium and xUnit?如何使用 Selenium 和 xUnit 设置 Syncfusion SfDropDownList 的值?
【发布时间】:2021-11-04 06:57:01
【问题描述】:

我正在对 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 并从我的类“游戏”中设置“文本”。我必须这样做 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:

  1. 在 http://localhost:5000 启动服务器

    • 打开 VS -> 选择 SyncfusionSelenium 而不是 IIS Server 作为启动选项,然后按 Ctrl+F5

    • 或打开项目文件夹->打开控制台输入“dotnet watch run debug”

  2. 打开测试资源管理器并运行/调试“Test1”

我非常感谢这里的一些帮助,因为我找不到如何为 SfDropDownList 执行此操作的示例。

有用的链接:

亚历克斯

【问题讨论】:

    标签: selenium blazor xunit syncfusion


    【解决方案1】:

    我们可以借助 Blazor 中的 bUnit 测试用例来满足您的要求。这也是处理 blazor 组件的建议方式。请参考以下代码 sn-p 以供参考。

    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);  
    }  
     
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-13
      • 1970-01-01
      • 2013-04-25
      • 1970-01-01
      • 1970-01-01
      • 2020-05-12
      • 2021-03-16
      相关资源
      最近更新 更多