-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDevOpsAutomation.cs
More file actions
135 lines (112 loc) · 5.71 KB
/
Copy pathDevOpsAutomation.cs
File metadata and controls
135 lines (112 loc) · 5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using RestSharp;
using Newtonsoft.Json.Linq;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using System.Text;
public class DevOpsAutomation
{
public static void RunAutomation(int testCaseId)
{
string azureDevOpsUrl = "https://dev.azure.com/ABBDemo/ABBDevOpsDemoNew/_apis/wit/workitems/";
string personalAccessToken = "fzobflefbkmwfzzd4xrwsykgnd2qhhohqbrqwv6mpvfg4zjkn5na";
// Step 1: Get the Test Case Work Item from Azure DevOps
var testCase = GetTestCaseFromAzureDevOps(azureDevOpsUrl, personalAccessToken, testCaseId);
if (testCase != null)
{
// Step 2: Extract the website URL from the test case description (first line)
string description = testCase["fields"]["System.Description"].ToString();
// string[] descriptionLines = description.Split(new[] { "<div>", "</div>" }, StringSplitOptions.None);
// string websiteUrl = descriptionLines[0]; // Assuming first line contains the website URL
// Find the URL inside the href attribute
string urlPattern = "href=\"";
int startIndex = description.IndexOf(urlPattern) + urlPattern.Length;
int endIndex = description.IndexOf("\"", startIndex);
string websiteUrl = description.Substring(startIndex, endIndex - startIndex);
// Print the extracted URL
Console.WriteLine("Extracted Website URL: " + websiteUrl);
Console.WriteLine("Website URL: " + websiteUrl);
// Print the raw HTML content
Console.WriteLine("Work Item Description (Raw HTML):");
Console.WriteLine(description);
// Step 3: Use Selenium to verify if "Dashboard" is present on the webpage
bool testPassed = CheckDashboardTextOnWebsite(websiteUrl);
if (!testPassed)
{
Console.WriteLine("Test Failed. Creating a Bug...for Website URL" + websiteUrl);
// Step 4: Create a Bug in Azure DevOps
string testCaseTitle = testCase["fields"]["System.Title"].ToString();
string bugTitle = $"Test Case ID: {testCaseId}, Title: {testCaseTitle}, Test Failed during Automation";
CreateBugInAzureDevOps(azureDevOpsUrl, personalAccessToken, bugTitle, testCaseTitle, testCaseId);
}
else
{
Console.WriteLine("Test Passed.");
}
}
}
// Other methods (GetTestCaseFromAzureDevOps, CheckDashboardTextOnWebsite, CreateBugInAzureDevOps) remain unchanged
static JObject GetTestCaseFromAzureDevOps(string azureDevOpsUrl, string personalAccessToken, int workItemId)
{
string requestUrl = $"{azureDevOpsUrl}{workItemId}?api-version=6.0";
var client = new RestClient(requestUrl);
var request = new RestRequest(Method.GET);
string authToken = Convert.ToBase64String(Encoding.ASCII.GetBytes($":{personalAccessToken}"));
request.AddHeader("Authorization", $"Basic {authToken}");
IRestResponse response = client.Execute(request);
if (response.IsSuccessful)
{
return JObject.Parse(response.Content); // Return the test case as a JObject
}
Console.WriteLine("Failed to retrieve test case: " + response.ErrorMessage);
return null;
}
static bool CheckDashboardTextOnWebsite(string websiteUrl)
{
IWebDriver driver = new ChromeDriver();
try
{
driver.Navigate().GoToUrl(websiteUrl);
// Check if "Dashboard" text is present on the page
bool isDashboardPresent = driver.PageSource.Contains("Dashboard");
return isDashboardPresent;
}
finally
{
driver.Quit();
}
}
static void CreateBugInAzureDevOps(string azureDevOpsUrl, string personalAccessToken, string bugTitle, string testCaseTitle, int testCaseId)
{
string requestUrl = $"{azureDevOpsUrl}$Bug?api-version=6.0";
var client = new RestClient(requestUrl);
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json-patch+json");
string authToken = Convert.ToBase64String(Encoding.ASCII.GetBytes($":{personalAccessToken}"));
request.AddHeader("Authorization", $"Basic {authToken}");
Console.WriteLine("azureDevOpsUrl: " + azureDevOpsUrl);
Console.WriteLine("requestUrl: " + requestUrl);
Console.WriteLine("personalAccessToken: " + personalAccessToken);
Console.WriteLine("bugTitle: " + bugTitle);
Console.WriteLine("testCaseTitle: " + testCaseTitle);
Console.WriteLine("testCaseId: " + testCaseId);
// Construct Bug data
var bugData = new[]
{
new { op = "add", path = "/fields/System.Title", value = bugTitle },
new { op = "add", path = "/fields/System.Description", value = $"Test Case ID: {testCaseId}, Title: {testCaseTitle}. Failed during automation." },
new { op = "add", path = "/fields/System.AssignedTo", value = "shahab@tecoholic.com" }, // Change if you want to assign the bug
new { op = "add", path = "/fields/Microsoft.VSTS.TCM.ReproSteps", value = "See attached logs for detailed error." }
};
request.AddParameter("application/json-patch+json", Newtonsoft.Json.JsonConvert.SerializeObject(bugData), ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
if (response.IsSuccessful)
{
Console.WriteLine("Bug created successfully in Azure DevOps.");
}
else
{
Console.WriteLine("Failed to create bug: " + response.ErrorMessage);
}
}
}