Skip to content

Commit

Permalink
WindowHandle (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
licanhua committed Nov 16, 2020
1 parent 6d849f1 commit f0eee07
Show file tree
Hide file tree
Showing 17 changed files with 278 additions and 42 deletions.
19 changes: 14 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,11 @@ Because YWinAppDriver is for desktop application other than browser, `no` below
|no|POST| /session/:sessionId/frame/parent |Change focus to the parent context.
|completed|POST| /session/:sessionId/window |Change focus to another window.
|completed|DELETE| /session/:sessionId/window |Close the current window.
|in progress|POST| /session/:sessionId/window/:windowHandle/size |Change the size of the specified window.
|in progress|GET| /session/:sessionId/window/:windowHandle/size |Get the size of the specified window.
|in progress|POST| /session/:sessionId/window/:windowHandle/position |Change the position of the specified window.
|in progress|GET| /session/:sessionId/window/:windowHandle/position |Get the position of the specified window.
|in progress|POST| /session/:sessionId/window/:windowHandle/maximize |Maximize the specified window if not already maximized.
|completed|POST| /session/:sessionId/window/:windowHandle/size |Change the size of the specified window.
|completed|GET| /session/:sessionId/window/:windowHandle/size |Get the size of the specified window.
|completed|POST| /session/:sessionId/window/:windowHandle/position |Change the position of the specified window.
|completed|GET| /session/:sessionId/window/:windowHandle/position |Get the position of the specified window.
|completed|POST| /session/:sessionId/window/:windowHandle/maximize |Maximize the specified window if not already maximized.
|no|GET| /session/:sessionId/cookie |Retrieve all cookies visible to the current page.
|no|POST| /session/:sessionId/cookie |Set a cookie.
|no|DELETE| /session/:sessionId/cookie |Delete all cookies visible to the current page.
Expand Down Expand Up @@ -217,3 +217,12 @@ To adopt WinAppDriver, we need to resolve these problems first. Then we introduc
We finished the prototype. Because the legal concern and there is no business value from leader's aspect, we didn't make it into the end user.

I think YWinAppDriver is able to address above problems, and possible make every body happy.


## Known issue

### Window is not moved exactly the position you want.

The window is moved, but it didn't pass the API testing. I guess the Window position is not the same as the control position, so there is some adjustment before the move. But I didn't find this API yet.'
/session/:sessionId/window/:windowHandle/position
/session/:sessionId/window/:windowHandle/size
5 changes: 5 additions & 0 deletions src/Infra/CommandHandler/CommandHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public class CommandHandlers: ICommandHandlers
{ Command.SessionMouseAction, new SessionMouseActionHandler() },
{ Command.SessionTouchActionOnElement, new SessionTouchActionOnElementHandler() },
{ Command.SessionTouchUpDownMove, new SessionTouchUpDownMoveHandler() },
{ Command.MaximizeWindow, new MaximizeWindowHandler() },
{ Command.GetWindowPosition, new GetWindowPositionHandler() },
{ Command.SetWindowPosition, new SetWindowPositionHandler() },
{ Command.GetWindowSize, new GetWindowSizeHandler() },
{ Command.SetWindowSize, new SetWindowSizeHandler() },
};

public object ExecuteCommand(Command command, ISessionManager sessionManager, string sessionId, object req, string elementId)
Expand Down
5 changes: 5 additions & 0 deletions src/Infra/CommandHandler/ICommandHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public enum Command
SessionTouchActionOnElement,
SessionTouchUpDownMove,
SessionMouseAction,
SetWindowSize,
SetWindowPosition,
GetWindowSize,
GetWindowPosition,
MaximizeWindow,
}

public interface ICommandHandlers
Expand Down
51 changes: 48 additions & 3 deletions src/Infra/CommandHandler/SessionHandlers.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// Copyright (c) https://github.com/licanhua/YWinAppDriver. All rights reserved.
// Licensed under the MIT License.

using System.Linq;
using Microsoft.Windows.Apps.Test.Foundation;
using System.Collections.Generic;
using WinAppDriver.Infra.Communication;
using WinAppDriver.Infra.Result;

namespace WinAppDriver.Infra.CommandHandler
{
Expand Down Expand Up @@ -80,15 +83,15 @@ class GetWindowHandleHandler : SessionCommandHandlerBase<object>
{
protected override object ExecuteSessionCommand(ISessionManager sessionManager, ISession session, string elementId)
{
return session.GetApplicationRoot().GetWindowHandle();
return session.GetWindowHandle();
}
}

class ActivateWindowHandler : SessionCommandHandlerBase<ActivateWindowReq, object>
{
protected override object ExecuteSessionCommand(ISessionManager sessionManager, ISession session, ActivateWindowReq req, string elementId)
{
session.GetApplicationRoot().ActivateWindow(req.name);
session.GetWindow(req.name).SetFocus();
return null;
}
}
Expand Down Expand Up @@ -118,7 +121,7 @@ class GetWindowHandlesHandler : SessionCommandHandlerBase<object>
{
protected override object ExecuteSessionCommand(ISessionManager sessionManager, ISession session, string elementId)
{
return session.GetApplicationRoot().GetWindowHandles();
return session.GetWindowHandles();
}
}

Expand Down Expand Up @@ -176,4 +179,46 @@ protected override object ExecuteSessionCommand(ISessionManager sessionManager,
}
}

class GetWindowPositionHandler : SessionCommandHandlerBase<XYResult>
{
protected override XYResult ExecuteSessionCommand(ISessionManager sessionManager, ISession session, string windowHandle)
{
return session.GetWindow(windowHandle).GetWindowPosition();
}
}

class SetWindowPositionHandler : SessionCommandHandlerBase<XYReq, object>
{
protected override object ExecuteSessionCommand(ISessionManager sessionManager, ISession session, XYReq req, string windowHandle)
{
session.GetWindow(windowHandle).SetWindowPosition(req);
return null;
}
}

class GetWindowSizeHandler : SessionCommandHandlerBase<SizeResult>
{
protected override SizeResult ExecuteSessionCommand(ISessionManager sessionManager, ISession session, string windowHandle)
{
return session.GetWindow(windowHandle).GetWindowSize();
}
}

class SetWindowSizeHandler : SessionCommandHandlerBase<SizeReq, object>
{
protected override object ExecuteSessionCommand(ISessionManager sessionManager, ISession session, SizeReq req, string windowHandle)
{
session.GetWindow(windowHandle).SetWindowSize(req);
return null;
}
}

class MaximizeWindowHandler : SessionCommandHandlerBase<object>
{
protected override object ExecuteSessionCommand(ISessionManager sessionManager, ISession session, string windowHandle)
{
session.GetWindow(windowHandle).MaximizeWindow();
return null;
}
}
}
17 changes: 11 additions & 6 deletions src/Infra/Communication/DummyElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace WinAppDriver.Infra.Communication
{
public class DummyElement : IElement
{
public void ActivateWindow(string window)
public void SetFocus()
{
throw new NoSuchWindow();
}
Expand Down Expand Up @@ -78,12 +78,12 @@ public string GetId()
return "NoThisElement";
}

public GetLocationResult GetLocation()
public XYResult GetLocation()
{
throw new NoSuchWindow();
}

public GetSizeResult GetSize()
public SizeResult GetSize()
{
throw new NoSuchWindow();
}
Expand All @@ -108,14 +108,19 @@ public object GetUIObject()
throw new NoSuchWindow();
}

public string GetWindowHandle()
public IElement GetWindow(string windowId)
{
throw new NoSuchWindow();
}

public IEnumerable<string> GetWindowHandles()
public IElement GetWindowHandle()
{
return new List<string>();
throw new NoSuchWindow();
}

public IEnumerable<IElement> GetWindowHandles()
{
return new List<IElement>();
}

public XmlDocument GetXmlDocument()
Expand Down
28 changes: 16 additions & 12 deletions src/Infra/Communication/Element.cs
Original file line number Diff line number Diff line change
Expand Up @@ -357,14 +357,14 @@ private void EnsureNotOffScreen()
}
}

public GetSizeResult GetSize()
public SizeResult GetSize()
{
return new GetSizeResult() { height = _uiObject.BoundingRectangle.Height, width = _uiObject.BoundingRectangle.Width };
return new SizeResult() { height = _uiObject.BoundingRectangle.Height, width = _uiObject.BoundingRectangle.Width };
}

public GetLocationResult GetLocation()
public XYResult GetLocation()
{
return new GetLocationResult() { x = _uiObject.BoundingRectangle.X, y = _uiObject.BoundingRectangle.Y };
return new XYResult() { x = _uiObject.BoundingRectangle.X, y = _uiObject.BoundingRectangle.Y };
}

public bool IsDisplayed()
Expand All @@ -388,9 +388,9 @@ private UIObject GetActiveWindow()
return UIObject.Focused;
}
}
public string GetWindowHandle()
public IElement GetWindowHandle()
{
return GetActiveWindow().NativeWindowHandle.ToString();
return new Element(GetActiveWindow(), true);
}

private IEnumerable<UIObject> GetWindows()
Expand All @@ -410,23 +410,27 @@ private IEnumerable<UIObject> GetWindows()
}


public IEnumerable<string> GetWindowHandles()
public IEnumerable<IElement> GetWindowHandles()
{
return GetWindows().Select(window => window.NativeWindowHandle.ToString());
return GetWindows().Select(window => new Element(window, true));
}

public void ActivateWindow(string window)
public void SetFocus()
{
if (String.IsNullOrEmpty(window))
_uiObject.SetFocus();
}
public IElement GetWindow(string windowId)
{
if (String.IsNullOrEmpty(windowId))
{
throw new InvalidArgumentException("name can't be empty");
}
var found = GetWindows().Where(w => { return w.NativeWindowHandle.ToString() == window; }).FirstOrDefault();
var found = GetWindows().Where(w => { return w.NativeWindowHandle.ToString() == windowId; }).FirstOrDefault();
if (found == null)
{
throw new NoSuchWindow();
}
found.SetFocus();
return new Element(found);
}

public void CloseActiveWindow()
Expand Down
11 changes: 6 additions & 5 deletions src/Infra/Communication/IElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,17 @@ public interface IElement
public IElement FindElement(Locator locator, int msTimeout);
public IEnumerable<IElement> FindElements(Locator locator, int msTimeout);

public string GetWindowHandle();
public IEnumerable<string> GetWindowHandles();
public void ActivateWindow(string window);
public IElement GetWindowHandle();
public IEnumerable<IElement> GetWindowHandles();
public IElement GetWindow(string windowId);
public void SetFocus();
public void CloseActiveWindow();
public string GetTitle();
public void Click();
public void DoubleClick();
public void Clear();
GetSizeResult GetSize();
GetLocationResult GetLocation();
SizeResult GetSize();
XYResult GetLocation();
bool IsDisplayed();
bool IsEnabled();
bool ElementEquals(IElement element);
Expand Down
51 changes: 50 additions & 1 deletion src/Infra/Extension.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
using Microsoft.Windows.Apps.Test.Foundation;
using Microsoft.Windows.Apps.Test.Automation;
using Microsoft.Windows.Apps.Test.Foundation;
using Microsoft.Windows.Apps.Test.Foundation.Controls;
using Microsoft.Windows.Apps.Test.Foundation.Patterns;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using WinAppDriver.Infra.Communication;
using WinAppDriver.Infra.Result;

namespace WinAppDriver.Infra
{
Expand Down Expand Up @@ -186,5 +190,50 @@ public static void TouchUpDownMove(this XYReq req, string action)
}
else throw new InvalidArgumentException();
}

public static UIObject UI(this IElement element)
{
return (UIObject)element.GetUIObject();
}
public static XYResult GetWindowPosition(this IElement window)
{
var rect = window.UI().BoundingRectangle;
return new XYResult() { x = rect.X, y = rect.Y };
}

public static SizeResult GetWindowSize(this IElement window)
{
var rect = window.UI().BoundingRectangle;
return new SizeResult() { width = rect.Width, height = rect.Height };
}

public static int ToInt(this double value)
{
return (int)value;
}

public static WindowImplementation Window(this IElement window)
{
return new WindowImplementation(window.UI());
}

public static void SetWindowPosition(this IElement window, XYReq req)
{
window.Window().SetWindowVisualState(WindowVisualState.Normal); // can't move maximize and minimize window
TransformImplementation transform = new TransformImplementation(window.UI());
transform.Move(req.x, req.x);
}

public static void SetWindowSize(this IElement window, SizeReq req)
{
window.Window().SetWindowVisualState(WindowVisualState.Normal); // can't move maximize and minimize window
TransformImplementation transform = new TransformImplementation(window.UI());
transform.Resize(req.width, req.height);
}

public static void MaximizeWindow(this IElement window)
{
window.Window().SetWindowVisualState(WindowVisualState.Maximized);
}
}
}
4 changes: 4 additions & 0 deletions src/Infra/ISession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,9 @@ public interface ISession
public bool IsElementEquals(string id, string other);
public string GetSource();
public IElement GetFocusedElement();

public string GetWindowHandle();
public IEnumerable<string> GetWindowHandles();
public IElement GetWindow(string windowId);
}
}
7 changes: 7 additions & 0 deletions src/Infra/Request/SessionReqs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,11 @@ public class XYReq
public double x;
public double y;
}

public class SizeReq
{
public double width;

public double height;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace WinAppDriver.Infra.Result
{
public class GetSizeResult
public class SizeResult
{
public int width;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace WinAppDriver.Infra.Result
{
public class GetLocationResult
public class XYResult
{
public int x;

Expand Down
Loading

0 comments on commit f0eee07

Please sign in to comment.