mirror of
https://github.com/microsoft/playwright.git
synced 2025-06-26 21:40:17 +00:00
docs(dotnet): use modern namespace syntax for api testing snippets (#17271)
This commit is contained in:
parent
c7367b7065
commit
ab4876242f
@ -39,42 +39,40 @@ using Microsoft.Playwright.NUnit;
|
|||||||
using Microsoft.Playwright;
|
using Microsoft.Playwright;
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
|
|
||||||
namespace PlaywrightTests
|
namespace PlaywrightTests;
|
||||||
|
|
||||||
|
public class TestGitHubAPI : PlaywrightTest
|
||||||
{
|
{
|
||||||
|
static string API_TOKEN = Environment.GetEnvironmentVariable("GITHUB_API_TOKEN");
|
||||||
|
|
||||||
public class TestGitHubAPI : PlaywrightTest
|
private IAPIRequestContext Request = null;
|
||||||
|
|
||||||
|
[SetUp]
|
||||||
|
public async Task SetUpAPITesting()
|
||||||
{
|
{
|
||||||
static string API_TOKEN = Environment.GetEnvironmentVariable("GITHUB_API_TOKEN");
|
await CreateAPIRequestContext();
|
||||||
|
}
|
||||||
|
|
||||||
private IAPIRequestContext Request = null;
|
private async Task CreateAPIRequestContext()
|
||||||
|
{
|
||||||
|
var headers = new Dictionary<string, string>();
|
||||||
|
// We set this header per GitHub guidelines.
|
||||||
|
headers.Add("Accept", "application/vnd.github.v3+json");
|
||||||
|
// Add authorization token to all requests.
|
||||||
|
// Assuming personal access token available in the environment.
|
||||||
|
headers.Add("Authorization", "token " + API_TOKEN);
|
||||||
|
|
||||||
[SetUp]
|
Request = await this.Playwright.APIRequest.NewContextAsync(new() {
|
||||||
public async Task SetUpAPITesting()
|
// All requests we send go to this API endpoint.
|
||||||
{
|
BaseURL = "https://api.github.com",
|
||||||
await CreateAPIRequestContext();
|
ExtraHTTPHeaders = headers,
|
||||||
}
|
});
|
||||||
|
}
|
||||||
|
|
||||||
private async Task CreateAPIRequestContext()
|
[TearDown]
|
||||||
{
|
public async Task TearDownAPITesting()
|
||||||
var headers = new Dictionary<string, string>();
|
{
|
||||||
// We set this header per GitHub guidelines.
|
await Request.DisposeAsync();
|
||||||
headers.Add("Accept", "application/vnd.github.v3+json");
|
|
||||||
// Add authorization token to all requests.
|
|
||||||
// Assuming personal access token available in the environment.
|
|
||||||
headers.Add("Authorization", "token " + API_TOKEN);
|
|
||||||
|
|
||||||
Request = await this.Playwright.APIRequest.NewContextAsync(new() {
|
|
||||||
// All requests we send go to this API endpoint.
|
|
||||||
BaseURL = "https://api.github.com",
|
|
||||||
ExtraHTTPHeaders = headers,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
[TearDown]
|
|
||||||
public async Task TearDownAPITesting()
|
|
||||||
{
|
|
||||||
await Request.DisposeAsync();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@ -91,76 +89,74 @@ using Microsoft.Playwright.NUnit;
|
|||||||
using Microsoft.Playwright;
|
using Microsoft.Playwright;
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
|
|
||||||
namespace PlaywrightTests
|
namespace PlaywrightTests;
|
||||||
|
|
||||||
|
[TestFixture]
|
||||||
|
public class TestGitHubAPI : PlaywrightTest
|
||||||
{
|
{
|
||||||
|
static string REPO = "test-repo-2";
|
||||||
|
static string USER = Environment.GetEnvironmentVariable("GITHUB_USER");
|
||||||
|
static string API_TOKEN = Environment.GetEnvironmentVariable("GITHUB_API_TOKEN");
|
||||||
|
|
||||||
[TestFixture]
|
private IAPIRequestContext Request = null;
|
||||||
public class TestGitHubAPI : PlaywrightTest
|
|
||||||
|
[PlaywrightTest]
|
||||||
|
public async Task ShouldCreateBugReport()
|
||||||
{
|
{
|
||||||
static string REPO = "test-repo-2";
|
var data = new Dictionary<string, string>();
|
||||||
static string USER = Environment.GetEnvironmentVariable("GITHUB_USER");
|
data.Add("title", "[Bug] report 1");
|
||||||
static string API_TOKEN = Environment.GetEnvironmentVariable("GITHUB_API_TOKEN");
|
data.Add("body", "Bug description");
|
||||||
|
var newIssue = await Request.PostAsync("/repos/" + USER + "/" + REPO + "/issues", new() { DataObject = data });
|
||||||
|
Assert.True(newIssue.Ok);
|
||||||
|
|
||||||
private IAPIRequestContext Request = null;
|
var issues = await Request.GetAsync("/repos/" + USER + "/" + REPO + "/issues");
|
||||||
|
Assert.True(issues.Ok);
|
||||||
[PlaywrightTest]
|
var issuesJsonResponse = await issues.JsonAsync();
|
||||||
public async Task ShouldCreateBugReport()
|
JsonElement? issue = null;
|
||||||
|
foreach (JsonElement issueObj in issuesJsonResponse?.EnumerateArray())
|
||||||
{
|
{
|
||||||
var data = new Dictionary<string, string>();
|
if (issueObj.TryGetProperty("title", out var title) == true)
|
||||||
data.Add("title", "[Bug] report 1");
|
|
||||||
data.Add("body", "Bug description");
|
|
||||||
var newIssue = await Request.PostAsync("/repos/" + USER + "/" + REPO + "/issues", new() { DataObject = data });
|
|
||||||
Assert.True(newIssue.Ok);
|
|
||||||
|
|
||||||
var issues = await Request.GetAsync("/repos/" + USER + "/" + REPO + "/issues");
|
|
||||||
Assert.True(issues.Ok);
|
|
||||||
var issuesJsonResponse = await issues.JsonAsync();
|
|
||||||
JsonElement? issue = null;
|
|
||||||
foreach (JsonElement issueObj in issuesJsonResponse?.EnumerateArray())
|
|
||||||
{
|
{
|
||||||
if (issueObj.TryGetProperty("title", out var title) == true)
|
if (title.GetString() == "[Bug] report 1")
|
||||||
{
|
{
|
||||||
if (title.GetString() == "[Bug] report 1")
|
issue = issueObj;
|
||||||
{
|
|
||||||
issue = issueObj;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Assert.NotNull(issue);
|
|
||||||
Assert.AreEqual("Bug description", issue?.GetProperty("body").GetString());
|
|
||||||
}
|
}
|
||||||
|
Assert.NotNull(issue);
|
||||||
[PlaywrightTest]
|
Assert.AreEqual("Bug description", issue?.GetProperty("body").GetString());
|
||||||
public async Task ShouldCreateFeatureRequests()
|
|
||||||
{
|
|
||||||
var data = new Dictionary<string, string>();
|
|
||||||
data.Add("title", "[Feature] request 1");
|
|
||||||
data.Add("body", "Feature description");
|
|
||||||
var newIssue = await Request.PostAsync("/repos/" + USER + "/" + REPO + "/issues", new() { DataObject = data });
|
|
||||||
Assert.True(newIssue.Ok);
|
|
||||||
|
|
||||||
var issues = await Request.GetAsync("/repos/" + USER + "/" + REPO + "/issues");
|
|
||||||
Assert.True(issues.Ok);
|
|
||||||
var issuesJsonResponse = await issues.JsonAsync();
|
|
||||||
var issuesJson = (await issues.JsonAsync())?.EnumerateArray();
|
|
||||||
|
|
||||||
JsonElement? issue = null;
|
|
||||||
foreach (JsonElement issueObj in issuesJsonResponse?.EnumerateArray())
|
|
||||||
{
|
|
||||||
if (issueObj.TryGetProperty("title", out var title) == true)
|
|
||||||
{
|
|
||||||
if (title.GetString() == "[Feature] request 1")
|
|
||||||
{
|
|
||||||
issue = issueObj;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Assert.NotNull(issue);
|
|
||||||
Assert.AreEqual("Feature description", issue?.GetProperty("body").GetString());
|
|
||||||
}
|
|
||||||
|
|
||||||
// ...
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[PlaywrightTest]
|
||||||
|
public async Task ShouldCreateFeatureRequests()
|
||||||
|
{
|
||||||
|
var data = new Dictionary<string, string>();
|
||||||
|
data.Add("title", "[Feature] request 1");
|
||||||
|
data.Add("body", "Feature description");
|
||||||
|
var newIssue = await Request.PostAsync("/repos/" + USER + "/" + REPO + "/issues", new() { DataObject = data });
|
||||||
|
Assert.True(newIssue.Ok);
|
||||||
|
|
||||||
|
var issues = await Request.GetAsync("/repos/" + USER + "/" + REPO + "/issues");
|
||||||
|
Assert.True(issues.Ok);
|
||||||
|
var issuesJsonResponse = await issues.JsonAsync();
|
||||||
|
var issuesJson = (await issues.JsonAsync())?.EnumerateArray();
|
||||||
|
|
||||||
|
JsonElement? issue = null;
|
||||||
|
foreach (JsonElement issueObj in issuesJsonResponse?.EnumerateArray())
|
||||||
|
{
|
||||||
|
if (issueObj.TryGetProperty("title", out var title) == true)
|
||||||
|
{
|
||||||
|
if (title.GetString() == "[Feature] request 1")
|
||||||
|
{
|
||||||
|
issue = issueObj;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Assert.NotNull(issue);
|
||||||
|
Assert.AreEqual("Feature description", issue?.GetProperty("body").GetString());
|
||||||
|
}
|
||||||
|
|
||||||
|
// ...
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -217,122 +213,120 @@ using Microsoft.Playwright.NUnit;
|
|||||||
using Microsoft.Playwright;
|
using Microsoft.Playwright;
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
|
|
||||||
namespace PlaywrightTests
|
namespace PlaywrightTests;
|
||||||
|
|
||||||
|
[TestFixture]
|
||||||
|
public class TestGitHubAPI : PlaywrightTest
|
||||||
{
|
{
|
||||||
|
static string REPO = "test-repo-2";
|
||||||
|
static string USER = Environment.GetEnvironmentVariable("GITHUB_USER");
|
||||||
|
static string API_TOKEN = Environment.GetEnvironmentVariable("GITHUB_API_TOKEN");
|
||||||
|
|
||||||
[TestFixture]
|
private IAPIRequestContext Request = null;
|
||||||
public class TestGitHubAPI : PlaywrightTest
|
|
||||||
|
[PlaywrightTest]
|
||||||
|
public async Task ShouldCreateBugReport()
|
||||||
{
|
{
|
||||||
static string REPO = "test-repo-2";
|
var data = new Dictionary<string, string>();
|
||||||
static string USER = Environment.GetEnvironmentVariable("GITHUB_USER");
|
data.Add("title", "[Bug] report 1");
|
||||||
static string API_TOKEN = Environment.GetEnvironmentVariable("GITHUB_API_TOKEN");
|
data.Add("body", "Bug description");
|
||||||
|
var newIssue = await Request.PostAsync("/repos/" + USER + "/" + REPO + "/issues", new() { DataObject = data });
|
||||||
|
Assert.True(newIssue.Ok);
|
||||||
|
|
||||||
private IAPIRequestContext Request = null;
|
var issues = await Request.GetAsync("/repos/" + USER + "/" + REPO + "/issues");
|
||||||
|
Assert.True(issues.Ok);
|
||||||
[PlaywrightTest]
|
var issuesJsonResponse = await issues.JsonAsync();
|
||||||
public async Task ShouldCreateBugReport()
|
JsonElement? issue = null;
|
||||||
|
foreach (JsonElement issueObj in issuesJsonResponse?.EnumerateArray())
|
||||||
{
|
{
|
||||||
var data = new Dictionary<string, string>();
|
if (issueObj.TryGetProperty("title", out var title) == true)
|
||||||
data.Add("title", "[Bug] report 1");
|
|
||||||
data.Add("body", "Bug description");
|
|
||||||
var newIssue = await Request.PostAsync("/repos/" + USER + "/" + REPO + "/issues", new() { DataObject = data });
|
|
||||||
Assert.True(newIssue.Ok);
|
|
||||||
|
|
||||||
var issues = await Request.GetAsync("/repos/" + USER + "/" + REPO + "/issues");
|
|
||||||
Assert.True(issues.Ok);
|
|
||||||
var issuesJsonResponse = await issues.JsonAsync();
|
|
||||||
JsonElement? issue = null;
|
|
||||||
foreach (JsonElement issueObj in issuesJsonResponse?.EnumerateArray())
|
|
||||||
{
|
{
|
||||||
if (issueObj.TryGetProperty("title", out var title) == true)
|
if (title.GetString() == "[Bug] report 1")
|
||||||
{
|
{
|
||||||
if (title.GetString() == "[Bug] report 1")
|
issue = issueObj;
|
||||||
{
|
|
||||||
issue = issueObj;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Assert.NotNull(issue);
|
|
||||||
Assert.AreEqual("Bug description", issue?.GetProperty("body").GetString());
|
|
||||||
}
|
}
|
||||||
|
Assert.NotNull(issue);
|
||||||
|
Assert.AreEqual("Bug description", issue?.GetProperty("body").GetString());
|
||||||
|
}
|
||||||
|
|
||||||
[PlaywrightTest]
|
[PlaywrightTest]
|
||||||
public async Task ShouldCreateFeatureRequests()
|
public async Task ShouldCreateFeatureRequests()
|
||||||
|
{
|
||||||
|
var data = new Dictionary<string, string>();
|
||||||
|
data.Add("title", "[Feature] request 1");
|
||||||
|
data.Add("body", "Feature description");
|
||||||
|
var newIssue = await Request.PostAsync("/repos/" + USER + "/" + REPO + "/issues", new() { DataObject = data });
|
||||||
|
Assert.True(newIssue.Ok);
|
||||||
|
|
||||||
|
var issues = await Request.GetAsync("/repos/" + USER + "/" + REPO + "/issues");
|
||||||
|
Assert.True(issues.Ok);
|
||||||
|
var issuesJsonResponse = await issues.JsonAsync();
|
||||||
|
var issuesJson = (await issues.JsonAsync())?.EnumerateArray();
|
||||||
|
|
||||||
|
JsonElement? issue = null;
|
||||||
|
foreach (JsonElement issueObj in issuesJsonResponse?.EnumerateArray())
|
||||||
{
|
{
|
||||||
var data = new Dictionary<string, string>();
|
if (issueObj.TryGetProperty("title", out var title) == true)
|
||||||
data.Add("title", "[Feature] request 1");
|
|
||||||
data.Add("body", "Feature description");
|
|
||||||
var newIssue = await Request.PostAsync("/repos/" + USER + "/" + REPO + "/issues", new() { DataObject = data });
|
|
||||||
Assert.True(newIssue.Ok);
|
|
||||||
|
|
||||||
var issues = await Request.GetAsync("/repos/" + USER + "/" + REPO + "/issues");
|
|
||||||
Assert.True(issues.Ok);
|
|
||||||
var issuesJsonResponse = await issues.JsonAsync();
|
|
||||||
var issuesJson = (await issues.JsonAsync())?.EnumerateArray();
|
|
||||||
|
|
||||||
JsonElement? issue = null;
|
|
||||||
foreach (JsonElement issueObj in issuesJsonResponse?.EnumerateArray())
|
|
||||||
{
|
{
|
||||||
if (issueObj.TryGetProperty("title", out var title) == true)
|
if (title.GetString() == "[Feature] request 1")
|
||||||
{
|
{
|
||||||
if (title.GetString() == "[Feature] request 1")
|
issue = issueObj;
|
||||||
{
|
|
||||||
issue = issueObj;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Assert.NotNull(issue);
|
|
||||||
Assert.AreEqual("Feature description", issue?.GetProperty("body").GetString());
|
|
||||||
}
|
}
|
||||||
|
Assert.NotNull(issue);
|
||||||
|
Assert.AreEqual("Feature description", issue?.GetProperty("body").GetString());
|
||||||
|
}
|
||||||
|
|
||||||
[SetUp]
|
[SetUp]
|
||||||
public async Task SetUpAPITesting()
|
public async Task SetUpAPITesting()
|
||||||
|
{
|
||||||
|
await CreateAPIRequestContext();
|
||||||
|
await CreateTestRepository();
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task CreateAPIRequestContext()
|
||||||
|
{
|
||||||
|
var headers = new Dictionary<string, string>();
|
||||||
|
// We set this header per GitHub guidelines.
|
||||||
|
headers.Add("Accept", "application/vnd.github.v3+json");
|
||||||
|
// Add authorization token to all requests.
|
||||||
|
// Assuming personal access token available in the environment.
|
||||||
|
headers.Add("Authorization", "token " + API_TOKEN);
|
||||||
|
|
||||||
|
Request = await this.Playwright.APIRequest.NewContextAsync(new()
|
||||||
{
|
{
|
||||||
await CreateAPIRequestContext();
|
// All requests we send go to this API endpoint.
|
||||||
await CreateTestRepository();
|
BaseURL = "https://api.github.com",
|
||||||
}
|
ExtraHTTPHeaders = headers,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
private async Task CreateAPIRequestContext()
|
private async Task CreateTestRepository()
|
||||||
|
{
|
||||||
|
var resp = await Request.PostAsync("/user/repos", new()
|
||||||
{
|
{
|
||||||
var headers = new Dictionary<string, string>();
|
DataObject = new Dictionary<string, string>()
|
||||||
// We set this header per GitHub guidelines.
|
|
||||||
headers.Add("Accept", "application/vnd.github.v3+json");
|
|
||||||
// Add authorization token to all requests.
|
|
||||||
// Assuming personal access token available in the environment.
|
|
||||||
headers.Add("Authorization", "token " + API_TOKEN);
|
|
||||||
|
|
||||||
Request = await this.Playwright.APIRequest.NewContextAsync(new()
|
|
||||||
{
|
{
|
||||||
// All requests we send go to this API endpoint.
|
["name"] = REPO,
|
||||||
BaseURL = "https://api.github.com",
|
},
|
||||||
ExtraHTTPHeaders = headers,
|
});
|
||||||
});
|
Assert.True(resp.Ok);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task CreateTestRepository()
|
[TearDown]
|
||||||
{
|
public async Task TearDownAPITesting()
|
||||||
var resp = await Request.PostAsync("/user/repos", new()
|
{
|
||||||
{
|
await DeleteTestRepository();
|
||||||
DataObject = new Dictionary<string, string>()
|
await Request.DisposeAsync();
|
||||||
{
|
}
|
||||||
["name"] = REPO,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
Assert.True(resp.Ok);
|
|
||||||
}
|
|
||||||
|
|
||||||
[TearDown]
|
private async Task DeleteTestRepository()
|
||||||
public async Task TearDownAPITesting()
|
{
|
||||||
{
|
var resp = await Request.DeleteAsync("/repos/" + USER + "/" + REPO);
|
||||||
await DeleteTestRepository();
|
Assert.True(resp.Ok);
|
||||||
await Request.DisposeAsync();
|
|
||||||
}
|
|
||||||
|
|
||||||
private async Task DeleteTestRepository()
|
|
||||||
{
|
|
||||||
var resp = await Request.DeleteAsync("/repos/" + USER + "/" + REPO);
|
|
||||||
Assert.True(resp.Ok);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
Loading…
x
Reference in New Issue
Block a user