Skip to content

Commit

Permalink
support snapshot (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
licanhua committed Nov 19, 2020
1 parent bf91747 commit bf58a91
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ Because YWinAppDriver is for desktop application other than browser, `no` below
|maybe|POST |/session/:sessionId/refresh |Refresh the current page.
|maybe|POST |/session/:sessionId/execute |Inject a snippet of JavaScript into the page for execution in the context of the currently selected frame.
|maybe|POST| /session/:sessionId/execute_async |Inject a snippet of JavaScript into the page for execution in the context of the currently selected frame.
|maybe|GET| /session/:sessionId/screenshot |Take a screenshot of the current page.
|complete|GET| /session/:sessionId/screenshot |Take a screenshot of the current page.(Implementation: Full screen snapshot only)
|no|GET| /session/:sessionId/ime/available_engines |List all available engines on the machine.
|no|GET| /session/:sessionId/ime/active_engine |Get the name of the active IME engine.
|no|GET| /session/:sessionId/ime/activated |Indicates whether IME input is active at the moment (not if it's available.
Expand Down
1 change: 1 addition & 0 deletions src/Infra/CommandHandler/CommandHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class CommandHandlers: ICommandHandlers
{ Command.SetWindowPosition, new SetWindowPositionHandler() },
{ Command.GetWindowSize, new GetWindowSizeHandler() },
{ Command.SetWindowSize, new SetWindowSizeHandler() },
{ Command.TakeScreenshot, new TakeScreenshotHandler() },
};

public object ExecuteCommand(Command command, ISessionManager sessionManager, string sessionId, object req, string elementId)
Expand Down
1 change: 1 addition & 0 deletions src/Infra/CommandHandler/ICommandHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public enum Command
GetWindowSize,
GetWindowPosition,
MaximizeWindow,
TakeScreenshot,
}

public interface ICommandHandlers
Expand Down
8 changes: 8 additions & 0 deletions src/Infra/CommandHandler/SessionHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,14 @@ protected override object ExecuteSessionCommand(ISessionManager sessionManager,
}
}

class TakeScreenshotHandler : SessionCommandHandlerBase<string>
{
protected override string ExecuteSessionCommand(ISessionManager sessionManager, ISession session, string ignored)
{
return session.TakeScreenshot();
}
}

class GetWindowSizeHandler : SessionCommandHandlerBase<SizeResult>
{
protected override SizeResult ExecuteSessionCommand(ISessionManager sessionManager, ISession session, string windowHandle)
Expand Down
1 change: 1 addition & 0 deletions src/Infra/ISession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ public interface ISession
public string GetWindowHandle();
public IEnumerable<string> GetWindowHandles();
public IElement GetWindow(string windowId);
public string TakeScreenshot();
}
}
1 change: 1 addition & 0 deletions src/Infra/Infra.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0" />
<PackageReference Include="Microsoft.Windows.Apps.Test" Version="1.0.181205002" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="System.Drawing.Common" Version="5.0.0" />
</ItemGroup>

</Project>
18 changes: 18 additions & 0 deletions src/Infra/Session.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using Microsoft.Windows.Apps.Test.Foundation;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
Expand Down Expand Up @@ -300,5 +302,21 @@ public IElement GetWindow(string windowId)
return window;
}
}

public string TakeScreenshot()
{
using var bitmap = new Bitmap(1920, 1080);
using (var g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(0, 0, 0, 0,
bitmap.Size, CopyPixelOperation.SourceCopy);
}

using (MemoryStream ms = new MemoryStream())
{
bitmap.Save(ms, ImageFormat.Png);
return System.Convert.ToBase64String(ms.ToArray());
}
}
}
}
13 changes: 10 additions & 3 deletions src/WinAppDriver/Controllers/SessionController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public IActionResult GetFocusedElement(string sessionId)

[HttpPost]
[Route("{sessionId}/element/{elementId}/value")]
public IActionResult SetValue(string sessionId, string elementId,[FromBody] object body)
public IActionResult SetValue(string sessionId, string elementId, [FromBody] object body)
{
return ExecuteCommand(Command.ElementSendKeys, sessionId, body, elementId);
}
Expand Down Expand Up @@ -323,15 +323,21 @@ public IActionResult AppLaunchApp(string sessionId)
return ExecuteCommand(Command.AppiumLaunchApp, sessionId, null, null);
}

[HttpPost]
[HttpGet]
[Route("{sessionId}/screenshot")]
public IActionResult TakeScreenshot(string sessionId)
{
return ExecuteCommand(Command.TakeScreenshot, sessionId, null, null);
}

[HttpPost]
[Route("{sessionId}/timeouts/async_script")]
[Route("{sessionId}/url")]
[Route("{sessionId}/forward")]
[Route("{sessionId}/back")]
[Route("{sessionId}/refresh")]
[Route("{sessionId}/execute")]
[Route("{sessionId}/execute_async")]
[Route("{sessionId}/screenshot")]
[Route("{sessionId}/ime/{any}")]
[Route("{sessionId}/frame")]
[Route("{sessionId}/frame/parent")]
Expand All @@ -351,6 +357,7 @@ public IActionResult UnknownPost(string sessionId)
}

[HttpGet]
[Route("{sessionId}/element/{elementId}/screenshot")]
[Route("{sessionId}/url")]
[Route("{sessionId}/cookie")]
[Route("{sessionId}/element/{id}/location_in_view")]
Expand Down

0 comments on commit bf58a91

Please sign in to comment.