dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.27k stars 9.96k forks source link

404 Errors for all assets in Test Server – Unable to load assets and check UI tests #58072

Open Cyrus-Sushiant opened 6 days ago

Cyrus-Sushiant commented 6 days ago

Is there an existing issue for this?

Describe the bug

I'm encountering an issue when trying to open the test server app in the browser. Some assets (e.g., CSS, JS, images, etc.) are returning 404 errors, which prevents the application from loading correctly.

As a result, the app does not display with full functionality, and I am unable to properly check the UI for testing purposes. This issue makes it difficult to run effective UI tests.

Expected Behavior

The assets should load correctly, allowing the app to display and function as expected to proceed with UI testing.

Steps To Reproduce

Example steps:

  1. Clone my repo at https://github.com/Cyrus-Sushiant/PlaywrightIssue
  2. Run the GotoCounterPage test in the MyTest class in the tests project
  3. Open http://127.0.0.1:2001 in your browser
  4. Some assets (e.g., CSS, JS, images, etc.) are returning 404 errors, which prevents the application from loading correctly.

image

Exceptions (if any)

No response

.NET Version

8.0.401

Anything else?

No response

javiercn commented 6 days ago

@Cyrus-Sushiant thanks for contacting us.

That likely is caused by you running the build output in Production without explicitly setting up static web assets.

Static Web Assets are only loaded in Developmnet environment. You need to call StaticWebAssetsLoader.UseStaticWebAssets if you are not running in development.

ysmoradi commented 6 days ago

The environment has been set to development, so I assume calling StaticWebAssetsLoader.UseStaticWebAssets is not required (Even though I called it without success)

Note that if we set Sdk of test project to Microsoft.NET.Sdk.Web the staticwebassets.development.json file gets generated, but for some reason, aspnetcore doesn't use it.

Cyrus-Sushiant commented 6 days ago

As @ysmoradi said, the environment has been set to development. You can see in this class: https://github.com/Cyrus-Sushiant/PlaywrightIssue/blob/master/PlaywrightIssue.Tests/MyTest.cs

image

javiercn commented 5 days ago

@ysmoradi You need to call this one StaticWebAssetsLoader.UseStaticWebAssets(builder.Environment, builder.Configuration); not the one you called.

You also need to pass in the file to load, since otherwise is going to try and load the one from the test project, not the one from your host project.

You have all the logic https://github.com/dotnet/aspnetcore/blob/main/src/Hosting/Hosting/src/StaticWebAssets/StaticWebAssetsLoader.cs

dotnet-policy-service[bot] commented 5 days ago

Hi @Cyrus-Sushiant. We have added the "Needs: Author Feedback" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

ysmoradi commented 5 days ago

Thanks for your efforts, I really appreciate it. Two notes to consider:

1- Changing test project sdk to Microsoft.NET.Sdk.Web will result into generating all required files such as staticwebassets.development.json, so we don't have to look for these files in host project (aspnetcore project)

2- Event though calling StaticWebAssetsLoader.UseStaticWebAssets(builder.Environment, builder.Configuration); is not required if dev environment, but even if you call it, it won't work! you can easily test that in the sample source code.

ysmoradi commented 5 days ago

The builder.Environment.ApplicationName is testhost (for the builder that is being built in mstest method) Assigning appropriate ApplicationName resolved the issue

dotnet-policy-service[bot] commented 1 day ago

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. If it is closed, feel free to comment when you are able to provide the additional information and we will re-investigate.

See our Issue Management Policies for more information.