コード例 #1
0
        /// <summary>
        /// Opens the drop down and returns the option.
        /// </summary>
        /// <param name="optionText">The option.</param>
        /// <param name="stringComparison">The string comparison.</param>
        /// <returns></returns>
        /// <exception cref="NoSuchElementException"></exception>
        public virtual MenuItemComponent SelectOption(string optionText,
                                                      StringComparison stringComparison = StringComparison.Ordinal)
        {
            // This isn't ideal but since the dropdown menu items are
            // generated on the first time the parent menu item is clicked,
            // click the parent element, wait 500 ms (should be enough time for
            // the dropdown to be generated), and move the mouse below the
            // parent element to locate the dropdown container.
            WrappedElement.Click();
            Thread.Sleep(500);

            // Determine where the float-menu will appear. Usually it's either
            // directly below or to the right.
            var(X, Y) = GetDirectionFloatMenuWillOpen();

            // Move below the WrappedElement.
            WrappedDriver.CreateActions()
            .MoveToElement(
                toElement: WrappedElement,
                offsetX: X,
                offsetY: Y,
                offsetOrigin: MoveToElementOffsetOrigin.Center)
            .Perform();

            // Get the container element under the mouse.
            var focusedElement = WrappedDriver
                                 .GetHoveredElements(allMenuDropDownsSelector)
                                 .First();

            var menuItemEl = focusedElement.FindElements(dropDownItemsSelector)
                             .FirstOrDefault(el =>
            {
                var textEl = el.FindElements(textSelector)
                             .FirstOrDefault();

                return(textEl == null
                        ? false
                        : String.Equals(
                           textEl.TextHelper().InnerText,
                           optionText,
                           stringComparison));
            });

            if (menuItemEl == null)
            {
                throw new NoSuchElementException();
            }

            var selector = ByElement.FromElement(menuItemEl);

            return(PageObjectFactory.PrepareComponent(
                       new MenuItemComponent(
                           selector,
                           PageObjectFactory,
                           WrappedDriver)));
        }