Introduction
Using Easyrepro we can perform UI Automation testing in Dynamics CRM by setting values in fields of different datatypes.
Recently while working with Easyrepro we wanted to set a value in the Regarding field of the Email record.
However, the code below which is used to set the value in a lookup field doesn’t seem to work for the Regarding field.
xrmApp.Entity.SetValue(new LookupItem { Name = “regardingobjectid”, Value = “Abraham McCormick” });
Note: In our scenario, Abraham McCormick is the name of the contact record regarding which we want to create an email.
Now, let us understand our piece of code.
Step 1:
Staying on the current window is necessary to find the elements. The below code will make sure that we are on the current window where the field is present.
var xrmBrowser = client.Browser;
var win = xrmBrowser.Driver.SwitchTo().Window(xrmBrowser.Driver.CurrentWindowHandle);
xrmApp.ThinkTime(5000);
Step 2:
First, we need to validate if we have the correct Regarding element for access.
To get the Regarding field element you can quickly check in the dev tool as below
And then use the below if statement to validate the existence of the element.
if (win.HasElement(By.XPath(“.//input[@aria-label=’Regarding, Lookup’]”)))
{
}
Step 3:
Once the existence of the Regarding field element is validated in Step 2, find the clickable element for the same using the below piece of code which needs to be added inside the if statement from Step 2.
//Find the clickable element of the Regarding field
var regardingElements = win.FindClickable(By.XPath(“.//input[@aria-label=’Regarding, Lookup’]”));
Step 4:
Now once the element is found in Step 3 you just need to access it by clicking on it and then pass the record name which you want to set as regarding.
xrmApp.ThinkTime(5000);
regardingElements.Click();
regardingElements.SendKeys(recordName);//Set the name of the record which you want to set
xrmApp.ThinkTime(5000);
Step 5:
Once the name of the record is passed in the Regarding field from the previous step you will see a drop down with the actual record in it as shown in the screenshot below.
And the below lines of code select the respective record from the list.
Note: There could be chances of being more than 1 record with the same name. So to select the correct one from the list you will have to use the number to select the right element as can be seen in the code below. The number is optional to set the regarding field. If not provided, then it will choose the record at the 0th position by default.
win = xrmBrowser.Driver.SwitchTo().Window(xrmBrowser.Driver.CurrentWindowHandle);
xrmApp.ThinkTime(5000);
//Find the element in the dropdown for which the name was passed
if (win.HasElement(By.XPath(“//li[contains(@aria-label,‘” + recordName + “‘)]”)))
{
var records = win.FindElements(By.XPath(“//li[contains(@aria-label,'” + recordName + “‘)]”));
if (records.Count> number)
{
records[number].Click();
}
elseif (records.Count>0)
{
records[0].Click();
}
xrmApp.ThinkTime(5000);
}
And as can be seen from the screenshot below the value gets set in the Regarding field.
We have created a generic function named SetRegarding with 4 parameters as below, which can be seen at the end of the blog post.
• XrmApp xrmApp- This will carry forward the object of XrmApp from the test method.(Required)
• WebClient client – This will carry forward the object of WebClient from the test method. (Required)
• string recordName-This will hold the name of the record which we want to set as regarding. In our scenario Abraham McCormick is the name of the record.(Required)
• int number – This will hold a numeric value which shall be used to select a record if there are more than 1 record with the same name. (Optional)
So in the test method after you have set the other attribute values, use our function to set the value in the Regarding field as below.
SetRegarding(xrmApp, client, “Abraham McCormick”, 0);
Given below is the generic function to set the value of the Regarding field while creating an Email record:
///<summary>
/// Generic function to set the regarding field
///</summary>
///<param name=”xrmApp“>This will carry forward the object of XrmApp from the test method.</param>
///<param name=”client“>This will carry forward the object of WebClient from the test method.</param>
///<param name=”recordName“>This will hold the name of the record which we want to set as regarding.</param>
///<param name=”number“>This will hold a numeric value which shall be used to select a record if there are more than 1 records with same name. In case no value is passed by default the record at the 0th position will get set</param>
public void SetRegarding(XrmApp xrmApp, WebClient client, string recordName, int number = 0)
{
//Local Variables
string functionName = “setRegarding”;
try
{
var xrmBrowser = client.Browser;
var win = xrmBrowser.Driver.SwitchTo().Window(xrmBrowser.Driver.CurrentWindowHandle);
xrmApp.ThinkTime(5000);
if (win.HasElement(By.XPath(“.//input[@aria-label=’Regarding, Lookup’]”)))
{
var regardingElements = win.FindClickable(By.XPath(“.//input[@aria-label=’Regarding, Lookup’]”));
xrmApp.ThinkTime(5000);
regardingElements.Click();
regardingElements.SendKeys(recordName); //Set the name of the record which you want to set
xrmApp.ThinkTime(5000);
win = xrmBrowser.Driver.SwitchTo().Window(xrmBrowser.Driver.CurrentWindowHandle);
xrmApp.ThinkTime(5000);
if (win.HasElement(By.XPath(“//li[contains(@aria-label,'” + recordName + “‘)]”)))
{
var records = win.FindElements(By.XPath(“//li[contains(@aria-label,'” + recordName + “‘)]”));
if (records.Count> number)
{
records[number].Click();
}
elseif (records.Count> 0)
{
records[0].Click();
}
xrmApp.ThinkTime(5000);
}
}
}
catch (Exception ex)
{
throw new Exception(functionName + ” :: “ + ex.Message);
}
}
Conclusion
In this way set value in a regarding field using Easyrepro in Dynamics CRM.