首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Selenium和xUnit设置同步xUnit的值?

如何使用Selenium和xUnit设置同步xUnit的值?
EN

Stack Overflow用户
提问于 2021-09-07 11:11:09
回答 1查看 788关注 0票数 1

我正在对Blazor应用程序进行硒测试,我想设置一个SfDropDownList的值。

我用SfDropDownList创建了一个空的Blazor项目,用Selenium和XUnit创建了一个测试项目。项目下载网址:https://www.syncfusion.com/downloads/support/forum/167910/ze/SYncfusionSelenium_b2ea0a0e

代码语言:javascript
复制
<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次,以确保它是正确的设置,这是我想出的最好的。

代码语言:javascript
复制
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上的

  1. 启动服务器

代码语言:javascript
复制
- open VS -> select SyncfusionSelenium instead of IIS Server as a Launch option and hit     Ctrl+F5
代码语言:javascript
复制
- or open project folder -> open console a enter "dotnet watch run debug"

  1. 打开测试资源管理器并运行/调试"Test1"

我非常感谢这里的一些帮助,因为我找不到如何为SfDropDownList做到这一点的例子。

有用的链接:

亚历克斯

EN

回答 1

Stack Overflow用户

发布于 2021-09-16 16:18:22

我们可以在Blazor中借助bUnit测试用例实现您的需求。这也是处理blazor组件的建议方法,.Please引用下面的代码片段供您参考。

代码语言:javascript
复制
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);  
}  
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69087179

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档