public BrowserCommandResult SetHeaderValue()

in Microsoft.Dynamics365.UIAutomation.Api/Pages/Entity.cs [1866:1946]


        public BrowserCommandResult<bool> SetHeaderValue(TwoOption option)
        {
            return this.Execute(GetOptions($"Set TwoOption Header Value: {option.Name}"), driver =>
            {
                var isBoolean = bool.TryParse(option.Value, out var optionValue);
                if (!isBoolean)
                    throw new ArgumentException($"Value {option.Value}: Cannot be converted to a boolean value");

                if (driver.HasElement(By.XPath(Elements.Xpath[Reference.Entity.CheckboxFieldContainer_Header].Replace("[NAME]", option.Name.ToLower()))))
                {
                    var fieldElement = driver.WaitUntilAvailable(By.XPath(Elements.Xpath[Reference.Entity.CheckboxFieldContainer_Header].Replace("[NAME]", option.Name.ToLower())));

                    var hasRadio = false;
                    var hasList = false;
                    var hasCheckbox = false;
                    string selectedValue = null;
                    ReadOnlyCollection<IWebElement> options = null;

                    if (fieldElement.HasAttribute("data-picklisttype"))
                    {
                        if (fieldElement.GetAttribute("data-picklisttype") == "0")
                            hasRadio = true;
                        else
                            hasList = true;

                        var radioDiv = driver.FindElement(By.XPath(Elements.Xpath[Reference.Entity.TwoOptionFieldDiv_Header].Replace("[NAME]", option.Name)));
                        selectedValue = radioDiv.GetAttribute("title");
                        var radioList = fieldElement.FindElement(By.XPath(Elements.Xpath[Reference.Entity.TwoOptionFieldList_Header].Replace("[NAME]", option.Name)));
                        options = radioList.FindElements(By.TagName("option"));
                    }
                    else
                    {
                        hasCheckbox = fieldElement.HasElement(By.XPath(Elements.Xpath[Reference.Entity.TwoOptionFieldCheckbox_Header].Replace("[NAME]", option.Name)));
                    }

                    if (hasRadio)
                    {
                        if (optionValue && selectedValue == options.FirstOrDefault(a => a.GetAttribute("value") == "0")?.GetAttribute("title") ||
                            !optionValue && selectedValue == options.FirstOrDefault(a => a.GetAttribute("value") == "1")?.GetAttribute("title"))
                        {
                            driver.ClickWhenAvailable(By.XPath(Elements.Xpath[Reference.Entity.CheckboxFieldContainer_Header].Replace("[NAME]", option.Name.ToLower())));
                            driver.ClearFocus();
                        }
                    }
                    else if (hasCheckbox)
                    {
                        var checkbox = fieldElement.FindElement(By.XPath(Elements.Xpath[Reference.Entity.TwoOptionFieldCheckbox_Header].Replace("[NAME]", option.Name)));

                        if (optionValue && !checkbox.Selected || !optionValue && checkbox.Selected)
                        {
                            driver.ClickWhenAvailable(By.XPath(Elements.Xpath[Reference.Entity.TwoOptionFieldCheckbox_Header].Replace("[NAME]", option.Name)));
                        }
                    }
                    else if (hasList)
                    {
                        var num = string.Empty;
                        if (optionValue && selectedValue == options.FirstOrDefault(a => a.GetAttribute("value") == "0")?.GetAttribute("title"))
                        {
                            num = "1";
                        }
                        else if (!optionValue && selectedValue == options.FirstOrDefault(a => a.GetAttribute("value") == "1")?.GetAttribute("title"))
                        {
                            num = "0";
                        }

                        if (!string.IsNullOrEmpty(num))
                        {
                            fieldElement.Hover(driver);
                            driver.ClickWhenAvailable(By.XPath(Elements.Xpath[Reference.Entity.CheckboxFieldContainer_Header].Replace("[NAME]", option.Name.ToLower())));
                            driver.ClickWhenAvailable(By.XPath(Elements.Xpath[Reference.Entity.TwoOptionFieldListOption_Header].Replace("[NAME]", option.Name).Replace("[VALUE]", num)));
                        }
                    }
                    else
                        throw new InvalidOperationException($"Field: {option.Name} Is not a TwoOption field");
                }
                else
                    throw new InvalidOperationException($"Unable to locate TwoOption field '{option.Name}' in the header. Please verify the TwoOption field exists and try again.");

                return true;
            });
        }