C# Multiselect from listbox with List<string>

Paulius Paulaitis 1 Reputation point
2023-01-09T23:17:47.9+00:00

So I have this test page: https://demo.seleniumeasy.com/basic-select-dropdown-demo.html
On the bottom there is a listbox, from which I should select some States and verify the selection with buttons below.
I tried doing this:

string expectedResult = "First selected option is : Florida";  

  
List<string> selectedStates = new List<string> { "Florida", "New York", "Texas" };  

  
foreach (string state in selectedStates)  
{  
BasicSelectDropdownDemo.SelectMultiElements(state);  
}  

  
BasicSelectDropdownDemo.ClickFirstSelectedButton();  
string actualResult = BasicSelectDropdownDemo.GetMultipleSelectedMessage();  

  
Assert.AreEqual(expectedResult, actualResult);  

But I get the result: "First selected option is : Texas"
Visually it seems that the selection is ok, all three States from the List are selected, but practically it seems that only the last selection stays.
My "ClickFirstSelectedButton" methods looks like this:

public static void SelectMultiElements(string selectedState)  
{  
Common.MultiSelectByValue(selectStatesMultiSelectLocator, selectedState);  
}  

Futher, my "MultiSelectByValue" looks like this:

SelectElement selectElement = GetSelectElement(locator);  
Actions actions = new Actions(Driver.GetDriver());  
actions.KeyDown(Keys.Control);  
selectElement.SelectByValue(value);  
actions.Perform();  

Maybe it is something wrong with my Actions code?
It may be a little confusing, as I have all my methods separated in other classes, but as I mentioned - visually it looks like I'm getting there, I'm getting the selection, but somehow only the last one stays in the end. I'm I missing something to "save" the previous selections? I guess that after actions.Perform(); the Driver "forgets" the selection and starts with each variable from scratch. But then again - on the screen I see the grey-out selections.
![277662-screenshot-2023-01-10-011517.png][1]
Thank you in advance if someone would be willing to help out.

Community Center Not monitored
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.