Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions BrowserStack.API.Screenshots.sln
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.21005.1
# Visual Studio 14
VisualStudioVersion = 14.0.25123.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BrowserStack.API.Screenshots", "BrowserStack.API.Screenshots\BrowserStack.API.Screenshots.csproj", "{1025B377-7530-49D0-9C26-4B12B673C331}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BrowserStack.API.ScreenshotsTests", "BrowserStack.API.ScreenshotsTests\BrowserStack.API.ScreenshotsTests.csproj", "{F3F1F761-0502-40FA-9B66-974088EAF310}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -15,6 +17,10 @@ Global
{1025B377-7530-49D0-9C26-4B12B673C331}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1025B377-7530-49D0-9C26-4B12B673C331}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1025B377-7530-49D0-9C26-4B12B673C331}.Release|Any CPU.Build.0 = Release|Any CPU
{F3F1F761-0502-40FA-9B66-974088EAF310}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F3F1F761-0502-40FA-9B66-974088EAF310}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F3F1F761-0502-40FA-9B66-974088EAF310}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F3F1F761-0502-40FA-9B66-974088EAF310}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
8 changes: 7 additions & 1 deletion BrowserStack.API.Screenshots/BatchScreenshotsCapture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ internal BatchScreenshotsCapture(IScreenshotsApi screenshotsApi)
/// <param name="captureThumbnails">if set to <c>true</c> then the batch job will also save the thumbnails when saving the screenshots.</param>
/// <param name="username">The username.</param>
/// <param name="password">The password.</param>
public BatchScreenshotsCapture(int sessionLimit, bool captureThumbnails, string username, string password):
public BatchScreenshotsCapture(int sessionLimit, bool captureThumbnails, string username = null, string password = null):
this(string.IsNullOrEmpty(username) && string.IsNullOrEmpty(password) ? new ScreenshotsApi() : new ScreenshotsApi(username, password))
{
this.sessionLimit = sessionLimit;
Expand Down Expand Up @@ -312,6 +312,12 @@ await Task.WhenAll(
}));
}

public void ExecuteBatch(string rootPath, bool usingTunnel, params BatchCaptureJobInfo[] batchCaptureJobs)
{
var executeBatchAsync = ExecuteBatchAsync(rootPath, usingTunnel, batchCaptureJobs);
executeBatchAsync.GetAwaiter().GetResult(); //To properly re-throw an exception: http://stackoverflow.com/questions/20170527/how-to-correctly-rethrow-an-exception-of-task-already-in-faulted-state
}

#endregion

#region Methods
Expand Down
29 changes: 21 additions & 8 deletions BrowserStack.API.Screenshots/BrowserStack.API.Screenshots.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<RestorePackages>true</RestorePackages>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -31,19 +33,21 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Newtonsoft.Json.5.0.8\lib\net45\Newtonsoft.Json.dll</HintPath>
<Reference Include="Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.8.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Net.Http.Extensions">
<HintPath>..\packages\Microsoft.Net.Http.2.2.18\lib\net45\System.Net.Http.Extensions.dll</HintPath>
<Reference Include="System.Net.Http.Extensions, Version=2.2.29.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Net.Http.2.2.29\lib\net45\System.Net.Http.Extensions.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Net.Http.Primitives">
<HintPath>..\packages\Microsoft.Net.Http.2.2.18\lib\net45\System.Net.Http.Primitives.dll</HintPath>
<Reference Include="System.Net.Http.Primitives, Version=4.2.29.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Net.Http.2.2.29\lib\net45\System.Net.Http.Primitives.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Net.Http.WebRequest" />
<Reference Include="System.Xml.Linq" />
Expand Down Expand Up @@ -74,9 +78,18 @@
<Compile Include="ScreenshotsApi.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="packages.config">
<SubType>Designer</SubType>
</None>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\packages\Microsoft.Bcl.Build.1.0.21\build\Microsoft.Bcl.Build.targets" Condition="Exists('..\packages\Microsoft.Bcl.Build.1.0.21\build\Microsoft.Bcl.Build.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\Microsoft.Bcl.Build.1.0.21\build\Microsoft.Bcl.Build.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Bcl.Build.1.0.21\build\Microsoft.Bcl.Build.targets'))" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
Expand Down
29 changes: 17 additions & 12 deletions BrowserStack.API.Screenshots/Job.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,12 @@ public enum States
/// <summary>
/// The job has been queued for execution.
/// </summary>
Queue,
Queue,

/// <summary>
/// All jobs has been queued for execution.
/// </summary>
Queued_all,

/// <summary>
/// The job has timed out.
Expand All @@ -119,15 +124,15 @@ public enum States
/// </summary>
public enum WinResolutions
{
/// <summary>
/// The r_1024 x 768.
/// </summary>
R_1024x768,

/// <summary>
/// The r_1280 x 1024.
/// </summary>
R_1280x1024,
R_1024x768,
R_1280x800,
R_1280x1024,
R_1366x768,
R_1440x900,
R_1680x1050,
R_1600x1200,
R_1920x1080,
R_1920x1200,
}

#endregion
Expand Down Expand Up @@ -183,7 +188,7 @@ public class JobInfo
/// <summary>
/// Gets or sets the orientation to be used for mobile browsers.
/// </summary>
public Orientations? Orientation { get; set; }
public Orientations? Orientation { get; set; }

/// <summary>
/// Gets or sets the resolution that will be used to capture screenshots on Mac browsers.
Expand All @@ -198,7 +203,7 @@ public class JobInfo
/// <summary>
/// Gets or sets the wait time (in seconds).
/// </summary>
public int WaitTime { get; set; }
public int WaitTime { get; set; } = 10;

/// <summary>
/// Gets or sets the resolution that will be used to capture screenshots on Windows browsers.
Expand Down
86 changes: 82 additions & 4 deletions BrowserStack.API.Screenshots/ScreenshotsApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using System.Diagnostics;

namespace BrowserStack.API.Screenshots
{
#region Using Directives
Expand Down Expand Up @@ -148,12 +150,24 @@ public async Task<IEnumerable<Browser>> GetBrowsersAsync()
var response = await httpClient.GetAsync(screenshotsRestAPIBaseUrl + "browsers");
response.EnsureSuccessStatusCode();
var data = await response.Content.ReadAsStringAsync();
var browsers = await JsonConvert.DeserializeObjectAsync<IEnumerable<BrowserInfo>>(data);
var browsers = await Task.Factory.StartNew(() => JsonConvert.DeserializeObject<IEnumerable<BrowserInfo>>(data));

return browsers.Select(x => MapInfoToBrowser(x));
return browsers.Select(MapInfoToBrowser);
}
}

/// <summary>
/// Retrieves a list of the supported browsers from BrowserStack. See http://www.browserstack.com/list-of-browsers-and-platforms?product=screenshots.
/// </summary>
/// <returns>
/// The <see cref="Browser" />.
/// </returns>
public IEnumerable<Browser> GetBrowsers()
{
var browsersJob = GetBrowsersAsync();
return browsersJob.GetAwaiter().GetResult(); //To properly re-throw an exception: http://stackoverflow.com/questions/20170527/how-to-correctly-rethrow-an-exception-of-task-already-in-faulted-state
}

/// <summary>
/// Retrieves a job's information.
/// </summary>
Expand All @@ -174,12 +188,27 @@ public async Task<Job> GetJobInfoAsync(string jobId)
var response = await httpClient.SendAsync(request);
response.EnsureSuccessStatusCode();
var data = await response.Content.ReadAsStringAsync();
var screenshotJob = await JsonConvert.DeserializeObjectAsync<ScreenshotJob>(data);
var screenshotJob = await Task.Factory.StartNew(() => JsonConvert.DeserializeObject<ScreenshotJob>(data));

System.Diagnostics.Trace.WriteLineIf(Debugger.IsAttached,"GetJobInfoAsync(jobId:"+jobId+") Response Content\n" + data);
return MapToJob(screenshotJob);
}
}

/// <summary>
/// Retrieves a job's information.
/// </summary>
/// <param name="jobId">The job id.</param>
/// <returns>
/// The <see cref="Job" />.
/// </returns>
public Job GetJobInfo(string jobId)
{
var startJobAsync = GetJobInfoAsync(jobId);
return startJobAsync.GetAwaiter().GetResult(); //To properly re-throw an exception: http://stackoverflow.com/questions/20170527/how-to-correctly-rethrow-an-exception-of-task-already-in-faulted-state
}


/// <summary>
/// Saves a screenshot to a local path.
/// </summary>
Expand Down Expand Up @@ -220,6 +249,22 @@ public async Task SaveScreenshotToFileAsync(Screenshot screenshot, string path,
}
}

/// <summary>
/// Saves a screenshot to a local path.
/// </summary>
/// <param name="screenshot">The screenshot.</param>
/// <param name="path">The path where the image will be saved to.</param>
/// <param name="filename">
/// The name of the file where the image will be saved to. The extension will be added automatically according to the image path.
/// If no filename is supplied then the original filename from the BrowserStack url will be used.
/// </param>
/// <param name="overwrite">If set to true then the file will be overwritten, otherwise no attempt will be made to retrieve the file.</param>
public void SaveScreenshotToFile(Screenshot screenshot, string path, string filename = null, bool overwrite = false)
{
var saveScreenshotToFileAsync = SaveScreenshotToFileAsync(screenshot, path, filename, overwrite);
saveScreenshotToFileAsync.GetAwaiter().GetResult(); //To properly re-throw an exception: http://stackoverflow.com/questions/20170527/how-to-correctly-rethrow-an-exception-of-task-already-in-faulted-state
}

/// <summary>
/// Saves a screenshot's thumbnail to a local path.
/// </summary>
Expand Down Expand Up @@ -259,6 +304,22 @@ public async Task SaveThumbnailToFileAsync(Screenshot screenshot, string path, s
}
}

/// <summary>
/// Saves a screenshot's thumbnail to a local path.
/// </summary>
/// <param name="screenshot">The screenshot.</param>
/// <param name="path">The path where the image will be saved to.</param>
/// <param name="filename">
/// The name of the file where the image will be saved to. The extension will be added automatically according to the image path.
/// If no filename is supplied then the original filename from the BrowserStack url will be used.
/// </param>
/// <param name="overwrite">If set to true then the file will be overwritten, otherwise no attempt will be made to retrieve the file.</param>
public void SaveThumbnailToFile(Screenshot screenshot, string path, string filename = null, bool overwrite = false)
{
var saveThumbnailToFileAsync = SaveThumbnailToFileAsync(screenshot, path, filename, overwrite);
saveThumbnailToFileAsync.GetAwaiter().GetResult(); //To properly re-throw an exception: http://stackoverflow.com/questions/20170527/how-to-correctly-rethrow-an-exception-of-task-already-in-faulted-state
}

/// <summary>
/// Starts a BrowserStack screenshot job asynchronously.
/// </summary>
Expand Down Expand Up @@ -305,12 +366,29 @@ public async Task<Job> StartJobAsync(string url, Job.JobInfo jobInfo, bool using
string.Format("Error while starting the job.\nResponse status is {0} ({1}).\nResponse is: {2}", response.ReasonPhrase, response.StatusCode, responseString));
}

var screenshotJob = await JsonConvert.DeserializeObjectAsync<ScreenshotJob>(responseString);
var screenshotJob = await Task.Factory.StartNew(()=> JsonConvert.DeserializeObject<ScreenshotJob>(responseString));

return MapToJob(screenshotJob);
}
}

/// <summary>
/// Starts a BrowserStack screenshot job synchronously.
/// </summary>
/// <param name="url">The url for which screenshots are required.</param>
/// <param name="jobInfo">The job information that will be used to start the job.</param>
/// <param name="usingTunnel">set to <c>true</c> if the BrowserStack jobs need to run under a tunnel. The tunnel must have been initiated externally.</param>
/// <param name="browsers">The browsers that will be used to start the job.</param>
/// <returns>
/// The <see cref="Job" />.
/// </returns>
/// <exception cref="ApplicationException">Thrown when the call to the BrowserStack API results in an http code other than 200 (OK).</exception>
public Job StartJob(string url, Job.JobInfo jobInfo, bool usingTunnel = false, params Browser[] browsers)
{
var startJobAsync = StartJobAsync(url, jobInfo, usingTunnel, browsers);
return startJobAsync.GetAwaiter().GetResult(); //To properly re-throw an exception: http://stackoverflow.com/questions/20170527/how-to-correctly-rethrow-an-exception-of-task-already-in-faulted-state
}

#endregion

#region Methods
Expand Down
8 changes: 4 additions & 4 deletions BrowserStack.API.Screenshots/packages.config
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Bcl" version="1.1.6" targetFramework="net45" />
<package id="Microsoft.Bcl.Build" version="1.0.13" targetFramework="net45" />
<package id="Microsoft.Net.Http" version="2.2.18" targetFramework="net45" />
<package id="Newtonsoft.Json" version="5.0.8" targetFramework="net45" />
<package id="Microsoft.Bcl" version="1.1.10" targetFramework="net45" />
<package id="Microsoft.Bcl.Build" version="1.0.21" targetFramework="net45" />
<package id="Microsoft.Net.Http" version="2.2.29" targetFramework="net45" />
<package id="Newtonsoft.Json" version="8.0.3" targetFramework="net45" />
</packages>
9 changes: 9 additions & 0 deletions BrowserStack.API.ScreenshotsTests/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="BrowserStackAPIScreenshotsSection" type="BrowserStack.API.Screenshots.Configuration.BrowserStackAPIScreenshotsSection,BrowserStack.API.Screenshots" />
</configSections>
<BrowserStackAPIScreenshotsSection>
<Authentication username="" password="" />
</BrowserStackAPIScreenshotsSection>
</configuration>
Loading