Skip to content

Commit 3dd6ae1

Browse files
authored
Rewrite the rest of the web UI tests as Playwright (#10675)
1 parent 4546124 commit 3dd6ae1

14 files changed

Lines changed: 425 additions & 17 deletions

File tree

tests/NuGetGallery.FunctionalTests.Core/EnvironmentSettings.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.IO;
56
using System.Linq;
67

78
namespace NuGetGallery.FunctionalTests
@@ -29,10 +30,28 @@ public static string ConfigurationFilePath
2930
#else
3031
bool required = true;
3132
#endif
32-
return GetEnvironmentVariable(ConfigurationFilePathVariableName, required) ?? "settings.DEV.json";
33+
return GetEnvironmentVariable(ConfigurationFilePathVariableName, required)
34+
?? Path.Combine(GetRepositoryRoot(Directory.GetCurrentDirectory()), "tests/NuGetGallery.FunctionalTests/settings.DEV.json");
3335
}
3436
}
3537

38+
private static string GetRepositoryRoot(string startingDirectory)
39+
{
40+
var current = new DirectoryInfo(startingDirectory);
41+
42+
while (current != null && !current.GetFiles("NuGetGallery.FunctionalTests.sln").Any())
43+
{
44+
current = current.Parent;
45+
}
46+
47+
if (current == null)
48+
{
49+
throw new InvalidOperationException("Could not find the repository root.");
50+
}
51+
52+
return current.FullName;
53+
}
54+
3655
private static string GetEnvironmentVariable(string key, bool required)
3756
{
3857
var output = Targets

tests/NuGetGallery.FunctionalTests.Core/Helpers/CommandlineHelper.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
55
using System.Collections.Generic;
66
using System.Diagnostics;
77
using System.IO;
8+
using System.Net.Http;
89
using System.Threading.Tasks;
910
using Xunit.Abstractions;
1011

@@ -173,6 +174,19 @@ public async Task<ProcessResult> InvokeNugetProcess(List<string> arguments, stri
173174
var nugetProcess = new Process();
174175
var pathToNugetExe = Path.Combine(Environment.CurrentDirectory, NugetExePath);
175176

177+
if (!File.Exists(pathToNugetExe))
178+
{
179+
#if DEBUG
180+
WriteLine("NuGet.exe will be downloaded to: " + pathToNugetExe);
181+
using var httpClient = new HttpClient();
182+
using var stream = await httpClient.GetStreamAsync("https://dist.nuget.org/win-x86-commandline/latest/nuget.exe");
183+
using var fileStream = File.Create(pathToNugetExe);
184+
await stream.CopyToAsync(fileStream);
185+
#else
186+
throw new FileNotFoundException("NuGet.exe not found at: " + pathToNugetExe);
187+
#endif
188+
}
189+
176190
arguments.Add(NonInteractiveSwitchString);
177191

178192
var argumentsString = string.Join(" ", arguments);
@@ -226,4 +240,4 @@ public ProcessResult(int exitCode, string standardError)
226240
public string StandardError { get; private set; }
227241
}
228242
}
229-
}
243+
}

tests/NuGetGallery.FunctionalTests.Core/Playwright/NuGetPageTest.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class NuGetPageTest : PageTest
1414
override public async Task InitializeAsync()
1515
{
1616
// Uncomment this to make Playwright run in headed mode (visible browser) for debugging.
17-
// Environment.SetEnvironmentVariable("HEADED", "1");
17+
// System.Environment.SetEnvironmentVariable("HEADED", "1");
1818
await base.InitializeAsync();
1919
}
2020

@@ -28,5 +28,36 @@ public async Task SignInAsync(string? email = null, string? password = null)
2828

2929
await Page.WaitForLoadStateAsync(LoadState.NetworkIdle);
3030
}
31+
32+
public async Task<string> UploadPackageAsync(string packageId, string version, string? owner)
33+
{
34+
var packageCreationHelper = new PackageCreationHelper();
35+
var packagePath = await packageCreationHelper.CreatePackage(packageId, version);
36+
37+
await Page.GotoAsync(UrlHelper.UploadPageUrl);
38+
39+
// Cancel any pending uploads
40+
var cancelButton = Page.Locator("input[type='button'][value='Cancel']");
41+
if (await cancelButton.IsVisibleAsync())
42+
{
43+
await cancelButton.ClickAsync();
44+
await Page.WaitForLoadStateAsync(LoadState.NetworkIdle);
45+
}
46+
47+
// Upload the package file
48+
await Page.Locator("input[name='UploadFile']").SetInputFilesAsync(packagePath);
49+
await Page.WaitForLoadStateAsync(LoadState.NetworkIdle);
50+
await Page.GetByText(packageId).First.WaitForAsync();
51+
52+
if (owner is not null)
53+
{
54+
await Page.Locator("select[name='Owner']").SelectOptionAsync(owner);
55+
}
56+
57+
await Page.Locator("input[type='button'][value='Submit']").ClickAsync();
58+
await Page.WaitForLoadStateAsync(LoadState.NetworkIdle);
59+
60+
return packagePath;
61+
}
3162
}
3263
}

tests/NuGetGallery.FunctionalTests/ODataFeeds/CuratedFeedTest.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
55
using System.ComponentModel;
6-
using System.IO;
76
using System.Net;
87
using System.Net.Http;
98
using System.Threading.Tasks;

tests/NuGetGallery.FunctionalTests/ODataFeeds/SearchTest.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System.ComponentModel;
5-
using System.IO;
6-
using System.Net;
75
using System.Net.Http;
86
using System.Threading.Tasks;
97
using Xunit;

tests/NuGetGallery.FunctionalTests/ODataFeeds/V2FeedExtendedTests.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
55
using System.Collections.Generic;
66
using System.ComponentModel;
7-
using System.IO;
87
using System.Linq;
9-
using System.Net;
108
using System.Net.Http;
119
using System.Threading.Tasks;
1210
using NuGetGallery.FunctionalTests.Helpers;

tests/NuGetGallery.FunctionalTests/PackageCreation/SecurityPolicyTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
55
using System.ComponentModel;
66
using System.IO;
7-
using System.Linq;
87
using System.Net;
98
using System.Net.Http;
109
using System.Threading.Tasks;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.Threading.Tasks;
5+
using Xunit;
6+
7+
namespace NuGetGallery.FunctionalTests.Playwright.AccountManagement
8+
{
9+
public class LogonTest : NuGetPageTest
10+
{
11+
[Fact]
12+
[Priority(0)]
13+
[Category("P0Tests")]
14+
public async Task Login_DisplaysLoggedInUsername()
15+
{
16+
// Act
17+
await SignInAsync();
18+
19+
// Assert
20+
await Expect(Page.Locator("span.dropdown-username")).ToContainTextAsync(GalleryConfiguration.Instance.Account.Name);
21+
}
22+
}
23+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Net;
6+
using System.Threading.Tasks;
7+
using Xunit;
8+
9+
namespace NuGetGallery.FunctionalTests.Playwright.BasicPages
10+
{
11+
public class BrandingValidationTest : NuGetPageTest
12+
{
13+
[Fact]
14+
[Priority(0)]
15+
[Category("P0Tests")]
16+
public async Task HomePage_ContainsBrandingElements()
17+
{
18+
// Act
19+
var response = await Page.GotoAsync(UrlHelper.BaseUrl);
20+
21+
// Assert
22+
Assert.Equal(HttpStatusCode.OK, (HttpStatusCode)response.Status);
23+
24+
var content = await Page.ContentAsync();
25+
var branding = GalleryConfiguration.Instance.Branding;
26+
27+
if (string.IsNullOrEmpty(branding.Message)
28+
&& string.IsNullOrEmpty(branding.Url)
29+
&& string.IsNullOrEmpty(branding.AboutUrl)
30+
&& string.IsNullOrEmpty(branding.PrivacyPolicyUrl)
31+
&& string.IsNullOrEmpty(branding.TermsOfUseUrl)
32+
&& string.IsNullOrEmpty(branding.TrademarksUrl))
33+
{
34+
// Check for default .NET Foundation branding
35+
Assert.Contains(@"<a href=""https://www.dotnetfoundation.org"">", content);
36+
Assert.Contains($"&copy; {DateTime.UtcNow.Year} .NET Foundation", content);
37+
Assert.Contains($@"<a href=""{UrlHelper.BaseUrl}policies/Terms"">Terms of Use</a>", content);
38+
Assert.Contains($@"<a href=""{UrlHelper.BaseUrl}policies/Privacy"" id=""footer-privacy-policy-link"">Privacy Policy</a>", content);
39+
}
40+
else
41+
{
42+
// Check for custom branding
43+
if (!string.IsNullOrEmpty(branding.Message))
44+
{
45+
var expectedMessage = string.Format(branding.Message, DateTime.UtcNow.Year);
46+
Assert.Contains(expectedMessage, content);
47+
}
48+
49+
if (!string.IsNullOrEmpty(branding.Url))
50+
{
51+
Assert.Contains($@"<a href=""{branding.Url}"">", content);
52+
}
53+
54+
if (!string.IsNullOrEmpty(branding.AboutUrl))
55+
{
56+
Assert.Contains($@"<a href=""{branding.AboutUrl}"">", content);
57+
}
58+
59+
if (!string.IsNullOrEmpty(branding.PrivacyPolicyUrl))
60+
{
61+
Assert.Contains($@"<a href=""{branding.PrivacyPolicyUrl}"" id=""footer-privacy-policy-link"">", content);
62+
}
63+
64+
if (!string.IsNullOrEmpty(branding.TermsOfUseUrl))
65+
{
66+
Assert.Contains($@"<a href=""{branding.TermsOfUseUrl}"">", content);
67+
}
68+
69+
if (!string.IsNullOrEmpty(branding.TrademarksUrl))
70+
{
71+
Assert.Contains($@"<a href=""{branding.TrademarksUrl}"">", content);
72+
}
73+
}
74+
}
75+
}
76+
}

tests/NuGetGallery.FunctionalTests/Playwright/BasicPages/HomePageValidationTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33

44
using System.Net;
55
using System.Threading.Tasks;
6-
using Microsoft.Playwright.Xunit;
76
using Xunit;
87

98
namespace NuGetGallery.FunctionalTests.Playwright.BasicPages
109
{
1110
public class HomePageValidationTest : NuGetPageTest
1211
{
13-
[Fact]
12+
[Fact]
13+
[Priority(0)]
1414
[Category("P0Tests")]
1515
[Category("ReadOnlyModeTests")]
1616
public async Task HomePageLoads_ContainsExpectedText()

0 commit comments

Comments
 (0)