How to set value for Multi-Select attribute in Dynamics 365 CRM using XPath in EasyRepro

Sending
User Review
0 (0 votes)

Introduction

Automate Testing can be done in a proficient way in Dynamics 365 CRM through EasyRepro. It covers mass functionalities for support in fields, command bar and much more. As we know, Option Set fields cannot be set like a string field so we use the below syntax to tackle the option set field.

xrmApp.Entity.SetValue (new OptionSet {Name = “OptionSetLabel”, Value = “OptionSetValue”});

However, we cannot achieve the same thing for multi-select option set field with this way.

Solution

We can achieve this with a little knowledge of XPath.  Consider a multi-select OptionSet field on contact form named as “Languages” in which you can select the languages your contacts used for their communication.

Multi-Select attribute

Now follow the below steps to select a value in this field.

Step 1:

You can choose any value from the field you like, in this example we will select “English” as a language that the contact used for his / her communication. We took the value in a variable “language” for further use.

var language = “English”;

Step 2:

We want to add the above value in the input tag of the multi-select field to search the value from the dropdown list.

We will use XPath to find the target input. There are two ways as shown below to get the XPath of the target element.

1. Right Click on the target element and select Inspect.

Multi-Select attribute

2. Dev Tools will open and highlight the target element.

Multi-Select attribute

3. Now the first way is to right click on the highlighted part and select Copy > Copy XPath and you will get your XPath for the target element.

Multi-Select attribute

4. Another way is to create an XPath. As we observe the highlighted part, we can get many attributes of input target. Here we found a unique id, so we will create an XPath consisting of input and its id. To create an XPath, click CTRL + F on the Dev Tools it will give a textbox on the bottom. Here we will write the XPath, as we know it is an input tag with a unique id named “new_languages_leedit”. We can write, //input[@id=’new_languages_leedit’]. It means to search an input tag with id = ‘new_languages_leedit’.

Multi-Select attribute

Also, remember that your target window is the main window, which is the form page.

var xrmBrowser = client.Browser;

var win = xrmBrowser.Driver.SwitchTo().Window(xrmBrowser.Driver.CurrentWindowHandle);

In win.FindElement method, use By.XPath to locate the target and store it in a variable.

var languageElement = win.FindElement(By.XPath(“//input[@id=’new_languages_leedit ‘]”));

Step 3:

Input the value in the target element from SendKeys() method.

languageElement.SendKeys(language);

Multi-Select attribute

After the input, the result can be seen in above image.

Step 4:

Now, we have to select the “English” label from the dropdown list.

Therefore, we need an XPath to select the dropdown label. Here, we have the dropdown labels with a unique value in its title. Therefore, we should create an XPath consisting of label and its title. Follow the above steps to get the XPath of the label. As shown in the image below we created, it’s XPath as

//label[@title=”English”].

Multi-Select attribute

Step 5:

The last step is to click on the target label and set it on the Languages field. To achieve that we use Click() method as shown in the below line of code.

win.FindElement(By.XPath(“//label[@title=’” + language + “‘]”)).Click();

Multi-Select attribute

We can see the output in the above image. We added language variable in above line of code as a dynamic value to make it generic for other values. Follow the same steps to add more values in the particular field, a generic function to choose these values or multiple values would be a bonus.

Hence, our multi-select value is set on the field!

Conclusion:

This guide will help those who need to work with Multi-Select values in Dynamics 365 CRM using EasyRepro.