Skip to content

Commit 5830f01

Browse files
Develop (#7)
* initial commit * w3c support * minor fix * app binaries * README * Added Report * Update azure-pipelines.yml * Update ReportGeneration.cs * Update output.html * Delete output.html Co-authored-by: princebaretto99 <[email protected]> Co-authored-by: Princeton Baretto <[email protected]>
1 parent a47dbc8 commit 5830f01

File tree

6 files changed

+180
-14
lines changed

6 files changed

+180
-14
lines changed

azure-pipelines.yml

+16
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,23 @@ steps:
2828
displayName: 'Test Project'
2929
inputs:
3030
command: 'test'
31+
arguments: '--filter FullyQualifiedName\!~ReportGeneration'
3132
env:
3233
CAPABILITIES_FILENAME: capabilities.yml
3334
BROWSERSTACK_ANDROID_APP_ID: SimpleApp_Android
3435
BROWSERSTACK_IOS_APP_ID: SimpleApp_iOS
36+
BROWSERSTACK_BUILD_NAME: browserstack-examples-appium-nunit-$(Build.BuildId)
37+
- task: DotNetCoreCLI@2
38+
name: 'TestProject'
39+
displayName: 'Test Project'
40+
inputs:
41+
command: 'test'
42+
arguments: '--filter ReportGeneration'
43+
env:
44+
BROWSERSTACK_BUILD_NAME: browserstack-examples-appium-nunit-$(Build.BuildId)
45+
46+
- task: PublishPipelineArtifact@1
47+
displayName: 'Publish Pipeline Artifact'
48+
inputs:
49+
targetPath: ./Reports/output.html
50+
artifact: 'Build Report'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
using System;
2+
3+
using NUnit.Framework;
4+
using Newtonsoft.Json.Linq;
5+
using System.Threading.Tasks;
6+
using RestSharp;
7+
using RestSharp.Authenticators;
8+
using BrowserStack.App.Pages;
9+
using System.IO;
10+
11+
namespace browserstack_examples_appium_nunit.Browserstack.App.Tests
12+
{
13+
public class ReportGeneration
14+
{
15+
[Test]
16+
public async Task ReportGenerator()
17+
{
18+
var BuildId = "";
19+
var Limit = 100;
20+
var Offset = 0;
21+
var userName = Environment.GetEnvironmentVariable("BROWSERSTACK_USERNAME") ?? "";
22+
var accessKey = Environment.GetEnvironmentVariable("BROWSERSTACK_ACCESS_KEY") ?? "";
23+
var buildName = Environment.GetEnvironmentVariable("BROWSERSTACK_BUILD_NAME") ?? "";
24+
var routeAPI = "https://api.browserstack.com";
25+
26+
27+
var fileLocation = "../../../../Reports/output.html";
28+
string[] htmlStart =
29+
{
30+
"<!DOCTYPE html><html><head><style>table { font-family: arial, sans-serif; border-collapse: collapse; width: 100%;}td, th{ border: 1px solid #dddddd; text-align: left; padding: 8px;}</style></head><body><table>",
31+
"<tr><th>Build Name</th><th>Project Name</th><th>Session ID</th><th>Session Name</th><th>OS Name</th><th>OS Version</th><th>Device</th><th>Status</tr>"
32+
33+
};
34+
35+
string[] htmlEnd = { "</table></body></html>" };
36+
37+
await System.IO.File.WriteAllLinesAsync(fileLocation, htmlStart);
38+
39+
var client = new RestClient(routeAPI);
40+
client.Authenticator = new HttpBasicAuthenticator(userName, accessKey);
41+
42+
var buildApiRequest = new RestRequest(routeAPI+"/app-automate/builds.json?limit=40");
43+
var buildQueryResult = await client.ExecuteAsync(buildApiRequest);
44+
var buildJsonStr = buildQueryResult.Content ?? "";
45+
46+
//GET BUILD ID FROM BUILD NAME
47+
if (!buildJsonStr.Equals(""))
48+
{
49+
var buildList = JArray.Parse(buildJsonStr);
50+
if (buildList.ToString().Length != 0)
51+
{
52+
53+
foreach (JObject buildObj in buildList)
54+
{
55+
var build = buildObj["automation_build"];
56+
var bName = (string?)build["name"];
57+
if (buildName.Equals(bName))
58+
{
59+
BuildId = build["hashed_id"].ToString();
60+
break;
61+
}
62+
}
63+
}
64+
}
65+
66+
if (BuildId.Equals(""))
67+
{
68+
Console.Write("Could not find a Build");
69+
70+
}
71+
else
72+
{
73+
//GET SESSIONS FROM BUILD ID
74+
do
75+
{
76+
var request = new RestRequest("/app-automate/builds/" + BuildId + "/sessions.json?limit=" + Limit + "&offset=" + Offset, Method.Get);
77+
var queryResult = await client.ExecuteAsync(request);
78+
var jsonStr = queryResult.Content ?? "";
79+
80+
//Check if Not an empty array
81+
if (jsonStr.Length > 2)
82+
{
83+
var sessionList = JArray.Parse(jsonStr);
84+
if (sessionList.ToString().Length != 0)
85+
{
86+
87+
foreach (JObject sessionObj in sessionList)
88+
{
89+
90+
if (sessionObj != null)
91+
{
92+
var session = sessionObj["automation_session"];
93+
var browserUrl = session["browser_url"].ToString().Replace("/builds", "/dashboard/v2/builds");
94+
var sessionName = session["name"].ToString().Length == 0 ? session["name"] : session["hashed_id"];
95+
96+
string[] textContextToAdd = { "<tr><td>" + session["build_name"].ToString() +
97+
"</td><td>" + session["project_name"] +
98+
"</td><td><a href=" + browserUrl + ">" + sessionName + "</a>" +
99+
"</td><td>" + session["name"] +
100+
"</td><td>" + session["os"] +
101+
"</td><td>" + session["os_version"] +
102+
"</td><td>" + session["device"] +
103+
"</td><td>" + session["status"] + "</tr>" };
104+
105+
await System.IO.File.AppendAllLinesAsync(fileLocation, textContextToAdd);
106+
}
107+
}
108+
}
109+
}
110+
else
111+
{
112+
break;
113+
}
114+
115+
Offset += Limit;
116+
} while (true);
117+
118+
119+
await System.IO.File.AppendAllLinesAsync(fileLocation, htmlEnd);
120+
121+
}
122+
}
123+
124+
}
125+
}

browserstack-examples-appium-nunit/Browserstack/Webdriver/Core/MobileDriverFactory.cs

+7-13
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class MobileDriverFactory
2121
private readonly string BROWSERSTACK_ACCESS_KEY = "BROWSERSTACK_ACCESS_KEY";
2222
private readonly string BROWSERSTACK_ANDROID_APP_ID = "BROWSERSTACK_ANDROID_APP_ID";
2323
private readonly string BROWSERSTACK_IOS_APP_ID = "BROWSERSTACK_IOS_APP_ID";
24-
private readonly string BUILD_ID = "BROWSERSTACK_BUILD_NAME";
24+
private readonly string BUILD_NAME = "BROWSERSTACK_BUILD_NAME";
2525
private readonly string DEFAULT_BUILD_NAME = "browserstack-examples-appium_nunit";
2626
public readonly string CAPABILITIES_DIR = "/Browserstack/Webdriver/Resources/";
2727

@@ -184,7 +184,7 @@ public AppiumOptions CreateRemoteMobileCapabilities(Platform platform)
184184

185185
if (build is not null)
186186
{
187-
browserstackOptions["buildName"] = CreateBuildName(build.ToString());
187+
browserstackOptions["buildName"] = GetBuildName(build.ToString());
188188
}
189189

190190
appiumOptions.AddAdditionalCapability("bstack:options", browserstackOptions);
@@ -227,22 +227,16 @@ public AppiumDriver<AppiumWebElement> CreateRemoteMobileDriver(AppiumOptions app
227227
}
228228
}
229229

230-
private String CreateBuildName(String buildPrefix)
230+
private String GetBuildName(String buildName)
231231
{
232-
if (String.IsNullOrEmpty(buildPrefix))
233-
{
234-
buildPrefix = DEFAULT_BUILD_NAME;
235-
}
236-
String buildName = buildPrefix;
237-
238-
String buildSuffix = Environment.GetEnvironmentVariable(BUILD_ID);
232+
string envbuildName = Environment.GetEnvironmentVariable(BUILD_NAME);
239233

240-
if (string.IsNullOrEmpty(buildSuffix))
234+
if (String.IsNullOrEmpty(envbuildName))
241235
{
242-
buildSuffix = this.DefaultBuildSuffix;
236+
envbuildName = buildName;
243237
}
244238

245-
return String.Format("{0}-{1}", buildName, buildSuffix);
239+
return envbuildName;
246240
}
247241

248242

browserstack-examples-appium-nunit/Browserstack/Webdriver/Resources/capabilities.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ CloudDriverConfig:
2222
PlatformOptions:
2323
platformName: ios
2424
"appium:deviceName": iPhone 12
25-
"appium:app": <BROWSERSTACK_IOS_APP_ID>
25+
"appium:app": <BROWSERSTACK_IOS_APP_ID>

browserstack-examples-appium-nunit/browserstack-examples-appium-nunit.csproj

+6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
2121
<PackageReference Include="log4net" Version="2.0.13" />
2222
<PackageReference Include="NunitXml.TestLogger" Version="3.0.117" />
23+
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
24+
<PackageReference Include="RestSharp" Version="107.3.0" />
2325
</ItemGroup>
2426

2527
<ItemGroup>
@@ -28,4 +30,8 @@
2830
<LinkBase>resources</LinkBase>
2931
</Content>
3032
</ItemGroup>
33+
<ItemGroup>
34+
<None Remove="Newtonsoft.Json" />
35+
<None Remove="RestSharp" />
36+
</ItemGroup>
3137
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.810.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "browserstack-examples-appium-nunit", "browserstack-examples-appium-nunit.csproj", "{BE8699C2-D22F-40D3-8604-632D5AAA98E1}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{BE8699C2-D22F-40D3-8604-632D5AAA98E1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{BE8699C2-D22F-40D3-8604-632D5AAA98E1}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{BE8699C2-D22F-40D3-8604-632D5AAA98E1}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{BE8699C2-D22F-40D3-8604-632D5AAA98E1}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {BC84036B-99B0-423B-A5D9-322E5BE5DC0C}
24+
EndGlobalSection
25+
EndGlobal

0 commit comments

Comments
 (0)