Skip to content

Commit

Permalink
Merge pull request #764 from AngelRodriguez8008/users/angel/fixGetLoo…
Browse files Browse the repository at this point in the history
…kupValue

Fix #756 GetValue From Lookup don't work properly
  • Loading branch information
TYLEROL committed Feb 18, 2020
2 parents 2cbf460 + fbc2582 commit 535245d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 39 deletions.
48 changes: 11 additions & 37 deletions Microsoft.Dynamics365.UIAutomation.Api.UCI/WebClient.cs
Expand Up @@ -2387,32 +2387,11 @@ public BrowserCommandResult<string> GetValue(LookupItem control)
return lookupValue;
});
}

private static string TryGetValue(IWebElement fieldContainer, LookupItem control)
private string TryGetValue(IWebElement fieldContainer, LookupItem control)
{
Exception ex = null;
try
{
bool found = fieldContainer.TryFindElement(By.TagName("input"), out var input);
if (found)
{
string lookupValue = input.GetAttribute("value");
return lookupValue;
}

found = fieldContainer.TryFindElement(By.XPath(".//label"), out var label);
if (found)
{
string lookupValue = label.GetAttribute("innerText");
return lookupValue;
}
}
catch (Exception e)
{
ex = e;
}

throw new InvalidOperationException($"Field: {control.Name} Does not exist", ex);
string[] lookupValues = TryGetValue(fieldContainer, new[] { control });
string result = string.Join("; ", lookupValues);
return result;
}

/// <summary>
Expand All @@ -2427,22 +2406,22 @@ public BrowserCommandResult<string[]> GetValue(LookupItem[] controls)
{
var xpathToContainer = By.XPath(AppElements.Xpath[AppReference.Entity.TextFieldLookupFieldContainer].Replace("[NAME]", controlName));
var fieldContainer = driver.WaitUntilAvailable(xpathToContainer);
string[] lookupValues = TryGetValue(fieldContainer, controls);
return lookupValues;
string[] result = TryGetValue(fieldContainer, controls);
return result;
});
}

public BrowserCommandResult<string[]> TryGetValue(IWebElement fieldContainer, LookupItem[] controls)
private string[] TryGetValue(IWebElement fieldContainer, LookupItem[] controls)
{
var controlName = controls.First().Name;
var xpathToExistingValues = By.XPath(AppElements.Xpath[AppReference.Entity.LookupFieldExistingValue].Replace("[NAME]", controlName));
var existingValues = fieldContainer.FindElements(xpathToExistingValues);

var xpathToExpandButton = By.XPath(AppElements.Xpath[AppReference.Entity.LookupFieldExpandCollapseButton].Replace("[NAME]", controlName));
bool expandButtonFound = fieldContainer.TryFindElement(xpathToExpandButton, out var collapseButton);
bool expandButtonFound = fieldContainer.TryFindElement(xpathToExpandButton, out var expandButton);
if (expandButtonFound)
{
collapseButton.Click(true);
expandButton.Click(true);

int count = existingValues.Count;
fieldContainer.WaitUntil(fc => fc.FindElements(xpathToExistingValues).Count > count);
Expand All @@ -2455,17 +2434,12 @@ public BrowserCommandResult<string[]> TryGetValue(IWebElement fieldContainer, Lo
{
if (existingValues.Count > 0)
{
char[] trimCharacters =
{
'', '\r', '\n', '',
'', ''
}; //IE can return line breaks
string[] lookupValues = existingValues.Select(v => v.GetAttribute("innerText").Trim(trimCharacters)).ToArray();
string[] lookupValues = existingValues.Select(v => v.GetAttribute("innerText").ToLowerString()).ToArray(); //IE can return line breaks
return lookupValues;
}

if (fieldContainer.FindElements(By.TagName("input")).Any())
return null;
return new string[0];
}
catch (Exception e)
{
Expand Down
29 changes: 27 additions & 2 deletions Microsoft.Dynamics365.UIAutomation.Sample/UCI/GetValue.cs
Expand Up @@ -4,10 +4,12 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Dynamics365.UIAutomation.Api.UCI;
using System;
using System.Diagnostics;
using System.Linq;

namespace Microsoft.Dynamics365.UIAutomation.Sample.UCI
{
[TestClass]
[TestClass]
public class GetValueUci : TestsBase
{
[TestInitialize]
Expand All @@ -31,7 +33,7 @@ public void UCITestGetValueFromOptionSet()
}

[TestMethod]
public void UCITestGetValueFromLookup()
public void UCITestGetSingleValueFromLookup()
{
_xrmApp.Navigation.OpenSubArea("Accounts");

Expand All @@ -44,6 +46,29 @@ public void UCITestGetValueFromLookup()
Assert.IsNotNull(lookupValue);
}

[TestMethod]
public void UCITestGetValueFromLookup_MultiAndSingle_ReturnsTheSameResult()
{
_xrmApp.Navigation.OpenSubArea("Accounts");

_xrmApp.Grid.SwitchView("Active Accounts");

_xrmApp.Grid.OpenRecord(0);

_xrmApp.ThinkTime(2000);

var primaryContactLookupItem = new LookupItem {Name = "primarycontactid"};

string lookupValue = _xrmApp.Entity.GetValue(primaryContactLookupItem);
Debug.WriteLine($"Single-Value: {lookupValue ?? "null"}");

string[] lookupValues = _xrmApp.Entity.GetValue(new[]{ primaryContactLookupItem });
Assert.IsNotNull(lookupValues);
Assert.IsTrue(lookupValues.Length == 0 && lookupValue == string.Empty || string.Equals(lookupValue, lookupValues[0]));

Debug.WriteLine($"Multi-Value: {lookupValues.FirstOrDefault() ?? "null"}");
}

[TestMethod]
public void UCITestActivityPartyGetValue()
{
Expand Down

0 comments on commit 535245d

Please sign in to comment.