Selenium - how to manage a SfDropDownList with Selenium?

Dear Syncfusion team,

thank you for this great component.

I am doing Selenium testing and I'm interested how to manage SfDropDownList with Selenium.

I have created an empty Blazor project with SfDropDownList and test project with Selenium and XUnit so you can easier run this and provide some examples.

To run Selenium:

  1.  start server at 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"
  2. Open Tests Explorer and run/debug "Test1"

I would really appreciate your help here as I couldn't find examples how to do this for SfDropDownList .

Looking forward to your answer!
Alex


Attachment: SYncfusionSelenium_b2ea0a0e.zip


11 Replies

BC Berly Christopher Syncfusion Team August 9, 2021 02:14 PM UTC

Hi Aleksandar, 
  
We will check the requested requirement and update the further details in two business days (11th August 2021).  
  
Please find the information about testing library from the below common blogs. 
  
  
  
Regards, 
Berly B.C 



BC Berly Christopher Syncfusion Team August 11, 2021 03:29 PM UTC

Hi Aleksandar, 
  
Thanks for the patience. 
  
We can write the test case for the DropDownList component with Xunit by accessing the element and attribute of the component as mentioned below. 
  
        [Fact] 
        public void Test1() 
        { 
            _driver.Navigate().GoToUrl("http://localhost:5000"); 
 
            _driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10); 
 
            var title = _driver.FindElement(By.Id("Title")); 
            Assert.Equal("Hello, world!", title.Text); 
        } 
 
        [Fact] 
        public void DefaultInitialize() 
        { 
             
            _driver.Navigate().GoToUrl("http://localhost:5000"); 
            _driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10); 
            var inputEle = _driver.FindElement(By.ClassName("e-ddl")); 
            Assert.Equal("0", inputEle.GetAttribute("tabindex")); 
        } 
        [Fact] 
        public void Enabled() 
        { 
            _driver.Navigate().GoToUrl("http://localhost:5000"); 
            _driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10); 
            var inputEle = _driver.FindElement(By.TagName("input")); 
            Assert.Equal("false", inputEle.GetAttribute("aria-disabled")); 
        } 
        [Fact] 
        public void Placeholder() 
        { 
            _driver.Navigate().GoToUrl("http://localhost:5000"); 
            _driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10); 
            var inputEle = _driver.FindElement(By.TagName("input")); 
            Assert.Equal("Select a game", inputEle.GetAttribute("aria-placeholder")); 
        } 
 
 
  
For your convenience, we have attached the modified sample below. 
  
Regards, 
Berly B.C 



AL Aleksandar August 12, 2021 04:59 PM UTC

Dear Syncfusion team,


thank you for your response.


Could you please show how to change a value of a dropdown with Selenium? Can we somehow access it by ID and set another value from a dropdown list?


I want to select another Item from a dropdown and verify that it has been successfully changed.


<SfDropDownList ID="MyDropdown" TValue="string" TItem="Games" Placeholder="Select a game" DataSource="@LocalData">    <DropDownListFieldSettings Value="ID" Text="Text"></DropDownListFieldSettings></SfDropDownList>


Best wishes,

Alex



BC Berly Christopher Syncfusion Team August 13, 2021 03:02 PM UTC

Hi Aleksandar, 
  
You can achieve the requested requirement with help of bUnit test case in the Blazor as mentioned in the below code example. 
  
  
  
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<CountriesGetDataItems() 
{ 
    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<stringCountries>>(parameters => 
            parameters.Add(p => p.DataSourcedata).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.TextinputEle.GetAttribute("value")); 
    Assert.Equal(0dropdown.Instance.Index); 
    dropdown.SetParametersAndRender(("Value"null)); 
    Assert.Equal(dropdown.Instance.TextinputEle.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(3dropdown.Instance.Index); 
    dropdown.SetParametersAndRender(("Index"null)); 
    Assert.Null(dropdown.Instance.Value); 
    Assert.Null(dropdown.Instance.Index); 
} 
 
  
Regards, 
Berly B.C 



AL Aleksandar August 17, 2021 07:42 PM UTC

Thank you, but bUnit is not what I need. I want to set/change a value with Selenium, Do you know how do I achieve this with Selenium? 

I think an answer to this should be beneficial for other developers as well. 

Looking forward to your answer. 

Alex



BC Berly Christopher Syncfusion Team August 18, 2021 03:30 PM UTC

Hi Aleksandar, 
  
We will check the possibilities for achieve the requested requirement and update the further details in two business days (20th August 2021). We appreciate your patience until then. 
  
Regards, 
Berly B.C 



BC Berly Christopher Syncfusion Team August 20, 2021 01:04 PM UTC

Hi Aleksandar, 
  
We have raised the query in the stack overflow link for getting possible solution for the requested requirement. We will update the details once the solution we got from the link. 
  
  
Regards, 
Berly B.C 



AL Aleksandar September 7, 2021 10:32 AM UTC

This is how I managed to set/change a value of a dropdown:

Thread.Sleep(TimeSpan.FromMilliseconds(250));
_driver.FindElement(By.Id("MyDropdown")).SendKeys("Basketball");


Thread.Sleep(TimeSpan.FromMilliseconds(250));
_driver.FindElement(By.Id("MyDropdown")).SendKeys("Basketball");


 I access a SfDropDownList by ID and set 'Text' from my class "Games". I have to do this 2 times to ensure it is properly set. This is the best I came up with so far.


<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" },
        new Games() { ID= "Game6", Text= "Golf" },
        new Games() { ID= "Game7", Text= "Hockey" },
        new Games() { ID= "Game8", Text= "Rugby"},
        new Games() { ID= "Game9", Text= "Snooker" },
        new Games() { ID= "Game10", Text= "Tennis"},
  };
}


DR Deepak Ramakrishnan Syncfusion Team September 8, 2021 06:04 PM UTC

Hi Aleksandar, 
 
We deeply regret for the inconvenience caused  . 
 
Based upon our case-study we couldn’t find any solution related to your query regarding xUnit , So we request  you to use the bUnit testing instead of xUnit as suggested in previous update. 
 
Thanks, 
Deepak R. 



AC Allen Cook February 25, 2022 01:11 AM UTC

I have found a way to use selenium to select items via clicking


driver.findElement(By.xpath("//label[contains(text(),'floatLabel')]/preceding-sibling::input")).click();
WebElement popup = driver.findElement(By.id(queueFilter.getAttribute("id") + "_popup"));
popup.findElement(By.cssSelector("li[data-value='" + value + "']")).click();


The main part is using the id of the dropdown to find the generated popup in the DOM to select the value



PM Ponmani Murugaiyan Syncfusion Team February 25, 2022 04:59 AM UTC

Hi Allen, 

Thanks for the update. Please get back us if you need further assistance. 

Regards, 
Ponmani M 


Loader.
Up arrow icon