Need sample snippet for validating dropdown (Selenium + Java)

818 views
Skip to first unread message

Dollar Strike

unread,
Mar 30, 2017, 5:00:47 AM3/30/17
to PrimaTest Automation
I am trying to validate dropdowns using Selenium and Java, I have a resuable component written as below

public static void verifyExpectedAndActualOptionsInDropdown(
WebElement element, List<String> listOfOptions) {

Select ele = new Select(element);
/*
* List<String> ds = new ArrayList<String>(); ds.add("Asia");
* ds.add("Europe"); ds.add("Africa");
*/
List<String> expectedOptions = listOfOptions;
List<String> actualOptions = new ArrayList<String>();
for (WebElement option : ele.getOptions()) {
System.out.println("Dropdown options are: " + option.getText());
actualOptions.add(option.getText());

}
System.out.println("Numbers of options present in the dropdown: "
+ actualOptions.size());

Assert.assertEquals(expectedOptions.toArray(), actualOptions.toArray());
}

I wanted to create a Java Action in RedwoodHQ for this method where Webelement has to be passed from TestCases Tab. Could anyone help?

Dollar Strike

unread,
Apr 1, 2017, 1:51:48 AM4/1/17
to PrimaTest Automation
Anyone care to help me on this?

dmi...@primatest.net

unread,
Apr 1, 2017, 3:31:20 PM4/1/17
to PrimaTest Automation
On Friday, March 31, 2017 at 10:51:48 PM UTC-7, Dollar Strike wrote:
> Anyone care to help me on this?

I'm not sure what you are trying to do. If this is an action, it can't have multiple parameters, only one hashmap is allowed. Why not pass idtype and id to this action to find a web element instead?

Dollar Strike

unread,
Apr 1, 2017, 4:06:03 PM4/1/17
to PrimaTest Automation
Thanks for the response. I am trying to validate the values in the drop down with expected list of strings. It's a generic method which can be used across all the projects. All I need to do, pass webelement and list of expected strings. 

Scenario01: verify and validate all the 25 values in the drop down
Scenario02: verify the values in the drop down are in Alphabetical order

Can't I make these kinda reusable methods in Redwood? While creating the action I need to pass IDType, ID, List of Strings

dmi...@primatest.net

unread,
Apr 2, 2017, 12:15:55 AM4/2/17
to PrimaTest Automation
Why not turn this common method into an action and pass IDType and ID as regular parameters to it? Or if you like create an action that uses this common code. Here is a tutorial on scripted actions:

http://www.manula.com/manuals/primatest/redwoodhq/2.0/en/topic/quick-start-3-new-scripted-action?q=action

Dollar Strike

unread,
Apr 2, 2017, 2:27:43 AM4/2/17
to PrimaTest Automation
public class validateValuesDropdown {
     public void run(HashMap<String, Object> params) {
WebElement element = Browser.Driver.findElement(By.Id(params.get("Enter ID")));
Map<String, List<String>> values = new HashMap<String, List<String>>(); values.put("items",params.get("comma separated values"));
List<String> actualOptions = new ArrayList<String>();
for (WebElement option : element.getOptions()) {
System.out.println("Dropdown options are: " + option.getText());
actualOptions.add(option.getText());
        
        }
Assert.assertEquals(values.get("items").toArray(), actualOptions.toArray());

     }

I have created a common action like above. But still no luck. My intention is to get Webelement ID, List of Strings from the user input (Test case tab) and I am comparing expected and actual values.

dmi...@primatest.net

unread,
Apr 2, 2017, 12:08:32 PM4/2/17
to PrimaTest Automation
I need a little more info than "no luck". What is the problem? Any errors?

dmi...@primatest.net

unread,
Apr 2, 2017, 12:09:36 PM4/2/17
to PrimaTest Automation
Also which assert are you using? For arrays assertArrayEquals is appropriate.

Dollar Strike

unread,
Apr 2, 2017, 1:25:20 PM4/2/17
to PrimaTest Automation
Sorry and Yes, I have faced an exception. Couldn't recall exact exception, I'll post it once I reach my Lab. Exception says it could not pass params.get("comma separated values")) into a List

I am using TestNG assert, I'll try using assertArrayEquals as well. 

Dollar Strike

unread,
Apr 3, 2017, 8:49:21 AM4/3/17
to PrimaTest Automation
I got it worked finally, here's the code to validate values in the dropdown (Expected values should be given when designing Testcases)
This can be used for any application

class VerifyDropdownValues{
   public void run(HashMap<String, Object> params) {
WebElement element = Browser.Driver.findElement(By.id((String) params.get("Enter ID")));
Select ss = new Select(element);
String s = (String) params.get("comma separated values");
String[] listString = s.split(",");
List<String> list = new ArrayList<String>();
for (String str : listString) {
list.add(str.trim());
}
Collections.sort(list);
Map<String, List<String>> values = new HashMap<String, List<String>>();
values.put("items", list);
List<String> actualOptions = new ArrayList<String>();
for (WebElement option : ss.getOptions()) {
System.out.println("Dropdown options are: " + option.getText());
actualOptions.add(option.getText());

}
Collections.sort(actualOptions);

Assert.assertEquals(list, actualOptions);

}
}

Madhuri Medarametla

unread,
Apr 6, 2017, 5:22:49 AM4/6/17
to PrimaTest Automation
Hi Dollar,


where can we see the output which we have given in syso.out.

Can you please help me on this?

Dollar Strike

unread,
Apr 7, 2017, 12:40:44 AM4/7/17
to PrimaTest Automation
I don't think Redwood has a console. I coded in Eclipse and Copy-paste in RedwoodHQ Scripts. I should've removed that actually. Did the action worked for you?

Freddy Vega

unread,
Apr 10, 2017, 8:18:46 AM4/10/17
to PrimaTest Automation
If you print to stdout from your action script code it will be displayed in the Logs section of the Execution details page.

Auto Generated Inline Image 1
Reply all
Reply to author
Forward
0 new messages