public async Task <WebPoint> GetElementClickableLocation(string elementId, CancellationToken cancellationToken = new CancellationToken())
        {
            var targetElementId = elementId;
            var tagName         = await GetElementTagName(targetElementId, cancellationToken).ConfigureAwait(false);

            if (tagName == "area")
            {
                var func    = "function (element) {" + "  var map = element.parentElement;" + "  if (map.tagName.toLowerCase() != 'map')" + "    throw new Error('the area is not within a map');" + "  var mapName = map.getAttribute('name');" + "  if (mapName == null)" + "    throw new Error ('area\\'s parent map must have a name');" + "  mapName = '#' + mapName.toLowerCase();" + "  var images = document.getElementsByTagName('img');" + "  for (var i = 0; i < images.length; i++) {" + "    if (images[i].useMap.toLowerCase() == mapName)" + "      return images[i];" + "  }" + "  throw new Error('no img is found for the area');" + "}";
                var frameId = Session == null ? "" : Session.GetCurrentFrameId();
                var res     = await WebView.CallFunction(func, $"{{\"{Session?.GetElementKey()}\":\"{targetElementId}\"}}", frameId, true, false, cancellationToken).ConfigureAwait(false);

                targetElementId = ResultValueConverter.ToElementId(res?.Result?.Value, Session?.GetElementKey());
                //return ResultValueConverter.ToWebPoint(res?.Result?.Value);
            }

            var isDisplayed = await IsElementDisplayed(targetElementId, cancellationToken).ConfigureAwait(false);

            if (isDisplayed)
            {
                var rect = await GetElementRegion(targetElementId, cancellationToken).ConfigureAwait(false);

                var location = await ScrollElementRegionIntoView(targetElementId, rect, true, elementId, cancellationToken).ConfigureAwait(false);

                //if (location == null) return null;
                //var res = location.Offset(rect.Width / 2, rect.Height / 2);
                return(location.Offset(rect.Width / 2, rect.Height / 2));
            }

            return(null);
        }
        public async Task <WebRect> GetElementRegion(string elementId, CancellationToken cancellationToken = new CancellationToken())
        {
            //var expression = $"({get_element_region.JsSource}).apply(null, {{\"{GetElementKey()}\":\"{elementId}\"}})";
            //var frameId = Session == null ? "" : Session.GetCurrentFrameId();
            //var res = await webView.EvaluateScript(expression, frameId, true, cancellationToken);
            var res = await webView.CallFunction(get_element_region.JsSource, $"{{\"{GetElementKey()}\":\"{elementId}\"}}", null, true, false, cancellationToken);

            return(ResultValueConverter.ToWebRect(res?.Result?.Value));
        }
        public async Task <string> GetActiveElement(CancellationToken cancellationToken = new CancellationToken())
        {
            var func    = "function() { return document.activeElement || document.body }";
            var frameId = Session == null ? "" : Session.GetCurrentFrameId();
            var res     = await webView.CallFunction(func, null, frameId, true, false, cancellationToken);

            return(ResultValueConverter.ToElementId(res?.Result?.Value, GetElementKey()));
            //return res?.Result?.Value as JToken;
        }
        public async Task <bool> IsOptionElementTogglable(string elementId, CancellationToken cancellationToken = new CancellationToken())
        {
            //var expression = $"({is_option_element_toggleable.JsSource}).apply(null, {{\"{GetElementKey()}\":\"{elementId}\"}})";
            //var frameId = Session == null ? "" : Session.GetCurrentFrameId();
            //var res = await webView.EvaluateScript(expression, frameId, true, cancellationToken);
            var res = await webView.CallFunction(is_option_element_toggleable.JsSource, $"{{\"{GetElementKey()}\":\"{elementId}\"}}", null, true, false, cancellationToken);

            return(ResultValueConverter.ToBool(res?.Result?.Value));
        }
        public async Task <string> GetElementEffectiveStyle(string elementId, string property, CancellationToken cancellationToken = new CancellationToken())
        {
            var res = await WebView.CallFunction(atoms.GET_EFFECTIVE_STYLE, $"{{\"{Session.GetElementKey()}\":\"{elementId}\"}}, \"{property}\"", Session?.GetCurrentFrameId(), true, false, cancellationToken).ConfigureAwait(false);

            var value     = res?.Result?.Value as JToken;
            var exception = ResultValueConverter.ToWebBrowserException(value);

            if (exception != null)
            {
                throw exception;
            }
            return(ResultValueConverter.AsString(res?.Result?.Value));
        }
        public async Task <bool> IsElementEnabled(string elementId, CancellationToken cancellationToken = new CancellationToken())
        {
            var res = await WebView.CallFunction(atoms.IS_ENABLED, $"{{\"{Session.GetElementKey()}\":\"{elementId}\"}}", Session?.GetCurrentFrameId(), true, false, cancellationToken).ConfigureAwait(false);

            var value     = res?.Result?.Value as JToken;
            var exception = ResultValueConverter.ToWebBrowserException(value);

            if (exception != null)
            {
                throw exception;
            }
            return(ResultValueConverter.ToBool(res?.Result?.Value));
        }
        public async Task <bool> SetOptionElementSelected(string elementId, bool selected = true, CancellationToken cancellationToken = new CancellationToken())
        {
            var res = await WebView.CallFunction(atoms.CLICK, $"{{\"{Session.GetElementKey()}\":\"{elementId}\"}}, {selected.ToString().ToLower()}", Session?.GetCurrentFrameId(), true, false, cancellationToken).ConfigureAwait(false);

            var value     = res?.Result?.Value as JToken;
            var exception = ResultValueConverter.ToWebBrowserException(value);

            if (exception != null)
            {
                throw exception;
            }
            return(ResultValueConverter.ToBool(res?.Result?.Value));
        }
        public async Task <string> GetElementTagName(string elementId, CancellationToken cancellationToken = new CancellationToken())
        {
            var func = "function(elem) { return elem.tagName.toLowerCase(); }";
            var res  = await WebView.CallFunction(func, $"{{\"{Session.GetElementKey()}\":\"{elementId}\"}}", Session?.GetCurrentFrameId(), true, false, cancellationToken).ConfigureAwait(false);

            var value     = res?.Result?.Value as JToken;
            var exception = ResultValueConverter.ToWebBrowserException(value);

            if (exception != null)
            {
                throw exception;
            }
            return(ResultValueConverter.AsString(res?.Result?.Value));
        }
        public async Task <bool> IsOptionElementTogglable(string elementId, CancellationToken cancellationToken = new CancellationToken())
        {
            //var expression = $"({is_option_element_toggleable.JsSource}).apply(null, {{\"{Session.GetElementKey()}\":\"{elementId}\"}})";
            //var frameId = Session == null ? "" : Session.GetCurrentFrameId();
            //var res = await webView.EvaluateScript(expression, frameId, true, cancellationToken);
            var res = await WebView.CallFunction(is_option_element_toggleable.JsSource, $"{{\"{Session.GetElementKey()}\":\"{elementId}\"}}", Session?.GetCurrentFrameId(), true, false, cancellationToken).ConfigureAwait(false);

            var value     = res?.Result?.Value as JToken;
            var exception = ResultValueConverter.ToWebBrowserException(value);

            if (exception != null)
            {
                throw exception;
            }
            return(ResultValueConverter.ToBool(res?.Result?.Value));
        }
        public async Task <WebPoint> ScrollElementRegionIntoViewHelper(string elementId, WebRect region, bool center = true, string clickableElementId = null, CancellationToken cancellationToken = new CancellationToken())
        {
            var res = await webView.CallFunction(atoms.GET_LOCATION_IN_VIEW, $"{{\"{GetElementKey()}\":\"{elementId}\"}}, {center.ToString().ToLower()}, {WebRectToJsonString(region)}", null, true, false, cancellationToken);

            var location = ResultValueConverter.ToWebPoint(res?.Result?.Value);

            if (clickableElementId != null)
            {
                var middle      = location.Offset(region.Width / 2, region.Height / 2);
                var isClickable = await VerifyElementClickable(clickableElementId, middle);

                if (!isClickable)
                {
                    return(null);
                }
            }
            return(location);
        }
Exemple #11
0
        public async Task <JToken> FindElements(string strategy, string expr, string startNode = null, CancellationToken cancellationToken = new CancellationToken())
        {
            var func    = atoms.FIND_ELEMENTS;
            var frameId = _session == null ? "" : _session.GetCurrentFrameId();

            expr = Regex.Replace(expr, @"(['""\\#.:;,!?+<>=~*^$|%&@`{}\-/\[\]\(\)])", @"\$1");
            var args = $"{{\"{strategy}\":\"{expr}\"}}";

            if (startNode != null)
            {
                args += $", {{\"{_session.GetElementKey()}\":\"{startNode}\"}}";
            }
            var res = await _webView.CallFunction(func, args, frameId, true, false, cancellationToken).ConfigureAwait(false);

            var value     = res?.Result?.Value as JToken;
            var exception = ResultValueConverter.ToWebBrowserException(value);

            if (exception != null)
            {
                throw exception;
            }
            return(value?["value"]);
        }
        public async Task <bool> VerifyElementClickable(string elementId, WebPoint location, CancellationToken cancellationToken = new CancellationToken())
        {
            var res = await webView.CallFunction(atoms.IS_ELEMENT_CLICKABLE, $"{{\"{GetElementKey()}\":\"{elementId}\"}}, {location.X}, {location.Y}", null, true, false, cancellationToken);

            return(ResultValueConverter.ToBool(res?.Result?.Value));
        }
        public async Task <string> GetElementEffectiveStyle(string elementId, string property, CancellationToken cancellationToken = new CancellationToken())
        {
            var res = await webView.CallFunction(atoms.GET_EFFECTIVE_STYLE, $"{{\"{GetElementKey()}\":\"{elementId}\"}}, \"{property}\"", null, true, false, cancellationToken);

            return(ResultValueConverter.AsString(res?.Result?.Value));
        }
        public async Task <bool> SetOptionElementSelected(string elementId, bool selected = true, CancellationToken cancellationToken = new CancellationToken())
        {
            var res = await webView.CallFunction(atoms.CLICK, $"{{\"{GetElementKey()}\":\"{elementId}\"}}, {selected.ToString().ToLower()}", null, true, false, cancellationToken);

            return(ResultValueConverter.ToBool(res?.Result?.Value));
        }
        public async Task <bool> IsOptionElementSelected(string elementId, CancellationToken cancellationToken = new CancellationToken())
        {
            var res = await webView.CallFunction(atoms.IS_SELECTED, $"{{\"{GetElementKey()}\":\"{elementId}\"}}", null, true, false, cancellationToken);

            return(ResultValueConverter.ToBool(res?.Result?.Value));
        }
        public async Task <WebPoint> GetElementLocation(string elementId, CancellationToken cancellationToken = default(CancellationToken))
        {
            var res = await _webView.CallFunction(atoms.GET_LOCATION, $"{{\"{Session.GetElementKey()}\":\"{elementId}\"}}", _asyncChromeDriver.Session.GetCurrentFrameId(), cancellationToken : cancellationToken).ConfigureAwait(false);

            return(ResultValueConverter.ToWebPoint(res?.Result?.Value));
        }
        public async Task <string> GetElementAttribute(string elementId, string attributeName, CancellationToken cancellationToken = new CancellationToken())
        {
            var res = await WebView.CallFunction(atoms.GET_ATTRIBUTE, $"{{\"{Session.GetElementKey()}\":\"{elementId}\"}}, \"{attributeName}\"", Session?.GetCurrentFrameId(), true, false, cancellationToken).ConfigureAwait(false);

            return(ResultValueConverter.AsString(res?.Result?.Value)); // (res?.Result?.Value as JObject)?["value"]?.ToString();
        }
Exemple #18
0
        public async Task <WebPoint> GetElementLocation(string elementId)
        {
            var res = await webView.CallFunction(atoms.GET_LOCATION, $"{{\"{GetElementKey()}\":\"{elementId}\"}}");

            return(ResultValueConverter.ToWebPoint(res?.Result?.Value));
        }
        public async Task <WebSize> GetElementSize(string elementId, CancellationToken cancellationToken = new CancellationToken())
        {
            var res = await webView.CallFunction(atoms.GET_SIZE, $"{{\"{GetElementKey()}\":\"{elementId}\"}}", null, true, false, cancellationToken);

            return(ResultValueConverter.ToWebSize(res?.Result?.Value));
        }