Exemplo n.º 1
1
 public void Click(Session session, Guid elementId)
 {
     var element = GetUIAutomationElement(elementId);
     var invokePattern = (IUIAutomationInvokePattern)element.GetCurrentPattern(UIA_PatternIds.UIA_InvokePatternId);
     if (invokePattern != null)
     {
         invokePattern.Invoke();
     }
     else
     {
         var selectionItemPattern = (IUIAutomationSelectionItemPattern)element.GetCurrentPattern(UIA_PatternIds.UIA_SelectionItemPatternId);
         selectionItemPattern.Select();
     }
 }
Exemplo n.º 2
0
        public Session Create(Capabilities capabilities)
        {
            var session = new Session(capabilities);

            _cache.Add(
                session.SessionId.ToString("N"),
                session,
                new CacheItemPolicy { SlidingExpiration = TimeSpan.FromMinutes(5) });

            return session;
        }
 public LocateElementResponse(Session session, Guid? elementId)
     : base(session)
 {
     if (elementId.HasValue)
     {
         Value = new Element { Id = elementId.Value.ToString("N") };
     }
     else
     {
         Value = new Dictionary<string, string> { { "message", "no such element" } };
     }
 }
Exemplo n.º 4
0
        public Guid? FindElement(Session session, string locator, string value, Guid? elementId)
        {
            var sw = Stopwatch.StartNew();
            while (sw.Elapsed < session.Timeouts.Implicit)
            {
                var conditions = new List<IUIAutomationCondition>();

                switch (locator.ToLowerInvariant())
                {
                    case "name":
                        conditions.Add(_automation.CreatePropertyCondition(UIA_PropertyIds.UIA_ValueValuePropertyId, value));
                        conditions.Add(_automation.CreatePropertyCondition(UIA_PropertyIds.UIA_AutomationIdPropertyId, value));
                        break;
                    case "id":
                        conditions.Add(_automation.CreatePropertyCondition(UIA_PropertyIds.UIA_AutomationIdPropertyId, value));
                        break;
                    default:
                        throw new NotSupportedException();
                }

                var container = elementId.HasValue
                    ? GetUIAutomationElement(elementId.Value)
                    : _automation.ElementFromHandle(session.Process.MainWindowHandle);

                var condition = _automation.CreateOrConditionFromArray(conditions.ToArray());
                var element = container.FindFirst(TreeScope.TreeScope_Descendants, condition);
                if (element != null)
                {
                    return _elementRepository.AddByHandle(element.CurrentNativeWindowHandle.ToInt32());
                }

                Thread.Sleep(500);
            }

            return null;
        }
Exemplo n.º 5
0
 public void DoubleClick(Session session)
 {
     NativeMethods.DoubleClick();
     NativeMethods.WaitForInputIdle(session.Process);
 }
Exemplo n.º 6
0
 public void Clear(Session session, Guid elementId)
 {
     SendKeys(session, elementId, new char[0]);
 }
Exemplo n.º 7
0
        public bool SwitchToWindow(Session session, string title)
        {
            var window = NativeMethods.FindWindowByCaption(IntPtr.Zero, title);
            if (window != IntPtr.Zero)
            {
                NativeMethods.SetForegroundWindow(window);
                return true;
            }

            return false;
        }
 public LocateElementsResponse(Session session, IEnumerable<Guid> elementIds)
     : base(session)
 {
     Value = elementIds.Select(x => new Element { Id = x.ToString("N") }).ToArray();
 }
Exemplo n.º 9
0
 public SessionResponse(Session session)
 {
     SessionId = session.SessionId.ToString("N");
     Status = StatusCode.Success;
     Value = session.Capabilities;
 }
Exemplo n.º 10
0
 public IEnumerable<int> GetWindowHandles(Session session)
 {
     return NativeMethods.EnumerateProcessWindowHandles(session.Process.Id).Select(x => x.ToInt32());
 }
Exemplo n.º 11
0
 public int GetWindowHandle(Session session)
 {
     return session.Process.MainWindowHandle.ToInt32();
 }
Exemplo n.º 12
0
 public string GetTitle(Session session)
 {
     return session.Process.MainWindowTitle;
 }
Exemplo n.º 13
0
        public string GetElementText(Session session, Guid elementId)
        {
            var element = GetUIAutomationElement(elementId);
            var valuePattern = (IUIAutomationValuePattern)element.GetCurrentPattern(UIA_PatternIds.UIA_ValuePatternId);
            if (valuePattern != null)
            {
                return valuePattern.CurrentValue;
            }

            return element.CurrentName;
        }
Exemplo n.º 14
0
 public string GetElementName(Session session, Guid elementId)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 15
0
 public string GetElementAttribute(Session session, Guid elementId, string attribute)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 16
0
        public void MoveTo(Session session, Guid? elementId, int? xOffset, int? yOffset)
        {
            var point = System.Windows.Forms.Cursor.Position;

            double x = 0, y = 0;
            if (elementId.HasValue)
            {
                var automationElement = GetUIAutomationElement(elementId.Value);
                var rect = automationElement.CurrentBoundingRectangle;

                // start with the top left corner
                point = new System.Drawing.Point(rect.left, rect.top);

                // default offset to the middle of the element
                x = (rect.right - rect.left) / 2.0;
                y = (rect.bottom - rect.top) / 2.0;
            }

            if (xOffset.HasValue)
            {
                x = xOffset.Value;
            }

            if (yOffset.HasValue)
            {
                y = yOffset.Value;
            }

            point.Offset((int)x, (int)y);

            System.Windows.Forms.Cursor.Position = point;
        }
Exemplo n.º 17
0
        public IEnumerable<Guid> FindElements(Session session, string locator, string value, Guid? elementId)
        {
            var container = elementId.HasValue
                ? GetUIAutomationElement(elementId.Value)
                : _automation.ElementFromHandle(session.Process.MainWindowHandle);

            var conditions = new List<IUIAutomationCondition>();
            switch (locator.ToLowerInvariant())
            {
                case "name":
                    conditions.Add(_automation.CreatePropertyCondition(UIA_PropertyIds.UIA_ValueValuePropertyId, value));
                    conditions.Add(_automation.CreatePropertyCondition(UIA_PropertyIds.UIA_AutomationIdPropertyId, value));
                    break;
                case "tag name":
                    conditions.Add(_automation.CreatePropertyCondition(UIA_PropertyIds.UIA_ControlTypePropertyId, MapControlType(value)));
                    break;
                case "id":
                    conditions.Add(_automation.CreatePropertyCondition(UIA_PropertyIds.UIA_AutomationIdPropertyId, value));
                    break;
                default:
                    throw new VariableResourceNotFoundException(); // TODO: should this be method not supported?
            }

            var allElementIds = new List<Guid>();
            var condition = _automation.CreateOrConditionFromArray(conditions.ToArray());
            var results = container.FindAll(TreeScope.TreeScope_Descendants, condition);
            if (results != null)
            {
                for (int i = 0; i < results.Length; i++)
                {
                    var element = results.GetElement(i);
                    var id = elementId.HasValue && locator == "tag name" && value == "option"
                        ? _elementRepository.Add(new ListItemElement(_elementRepository.GetById(elementId.Value), i))
                        : _elementRepository.AddByHandle(element.CurrentNativeWindowHandle.ToInt32());
                    allElementIds.Add(id);
                }
            }

            if (allElementIds.Any())
            {
                return allElementIds;
            }

            throw new VariableResourceNotFoundException();
        }
Exemplo n.º 18
0
        public void SendKeys(Session session, Guid elementId, char[] keys)
        {
            var keysToSend = new String(keys);

            var element = GetUIAutomationElement(elementId);
            if (element.CurrentIsEnabled != 0 && element.CurrentIsKeyboardFocusable != 0)
            {
                var valuePattern = (IUIAutomationValuePattern)element.GetCurrentPattern(UIA_PatternIds.UIA_ValuePatternId);
                if (valuePattern != null)
                {
                    valuePattern.SetValue(keysToSend);
                }
                else
                {
                    element.SetFocus();
                    System.Windows.Forms.SendKeys.SendWait("^{HOME}");
                    System.Windows.Forms.SendKeys.SendWait("^+{END}");
                    System.Windows.Forms.SendKeys.SendWait("{DEL}");
                    System.Windows.Forms.SendKeys.SendWait(keysToSend);
                }
            }
        }
Exemplo n.º 19
0
 public TitleResponse(Session session, string title)
     : base(session)
 {
     Value = title;
 }
Exemplo n.º 20
0
        public void SendKeys(Session session, string[] keys)
        {
            var window = _automation.ElementFromHandle(session.Process.MainWindowHandle);
            window.SetFocus();

            foreach (var key in keys)
            {
                // TODO: support more keys
                switch (key)
                {
                    case "\ue034":
                        System.Windows.Forms.SendKeys.SendWait("{F4}");
                        break;

                    default:
                        throw new NotSupportedException();
                }
            }
        }
Exemplo n.º 21
0
 public WindowHandlesResponse(Session session, int[] handles)
     : base(session)
 {
     Value = handles;
 }
Exemplo n.º 22
0
 public WebDriverResponse(Session session)
 {
     SessionId = session.SessionId.ToString("N");
 }