[Question]: How to set up a custom driver path? (Does not work with ASP.NET 4.8)
See original GitHub issueYour question
Hi, I use the Playwright 1.16.1 with an ASP.NET NET Application (.NET Framework 4.8) on the WIndows machine.
I would like to ship my application together with the next entities in the App_Data folder:
- A bundled browser - OK. I can set up a custom path via
ExecutablePathof theBrowserTypeLaunchOptionsclass. - Playwright driver - I don’t see a needed API that allows me to set up a custom path for the Playwright driver (the
.playwrightfolder).
Question:
How to have a custom path for the .playwright folder?
What I tried:
I see the BrowserTypeLaunchOptions class also contains the Env key-value property. I’ve tried to add the PLAYWRIGHT_DRIVER_PATH key with a custom path value but looks like it doesn’t work.
Problem and what I can see: I have debugged the Playwright tool itself and discovered the next things:
The responsible class for the driver path is Microsoft.Playwright.Helpers.Patch which has the GetExecutablePath method.
Please check the code with the 1-4 comments below:
internal static class Paths
{
internal static string GetExecutablePath()
{
//1 - The AppContexts.BaseDirectory for me is the root of the ASP.NET solution, at this level I can see the App_Data, bin folders etc.
DirectoryInfo assemblyDirectory = new(AppContext.BaseDirectory);
//2 - then there is a check which looking for the Microsoft.Playwright.dll at the previously mentioned path, but there is no such Playwright dll, because it's under the bin folder.
if (!assemblyDirectory.Exists || !File.Exists(Path.Combine(assemblyDirectory.FullName, "Microsoft.Playwright.dll")))
{
//3 - the dll haven't found at the root of website and now code looking for the type.
//The type was found under the Temporary ASP.NET Files (the path is: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\13163ddd\37e5e286\assembly\dl3\.playwright\node).
var assemblyLocation = typeof(Playwright).Assembly.Location;
//4 - then the rest of the code relies on the assemblyDirectory variable which is the Temporary ASP.NET Files.
// If manually add the .playwright folder there, my application works but I would like to avoid manually put the driver here.
assemblyDirectory = new FileInfo(assemblyLocation).Directory;
}
string executableFile = GetPath(assemblyDirectory.FullName);
if (File.Exists(executableFile))
{
return executableFile;
}
// if the above fails, we can assume we're in the nuget registry
executableFile = GetPath(assemblyDirectory.Parent.Parent.FullName);
if (File.Exists(executableFile))
{
return executableFile;
}
throw new PlaywrightException($"Driver not found: {executableFile}");
}
Should this class be more flexible? Is there any other approach that I don’t see? Thank you in advance! 😃 Please let me know if I should clarify something more.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:3
- Comments:12 (3 by maintainers)
Top Related StackOverflow Question
Hi, @mxschmitt, there is the sample project as you’ve asked: https://github.com/smitbmx/DemoPlaywrightASPNET48
As I described previously, there is a workaround to add the
.playwrightfolder under theTemporary ASP.NET Files, but I strongly would like to avoid it. It would be really nice to have a customizable path for the Playwright driver as if was done for the bundled browsers viaExecutablePathvariable of theBrowserTypeLaunchOptionsclass. Then it gives the ability to keep the driver somewhere under theApp_Datafolder of the ASP.NET solutions. Please let me know if you have additional questions.Hi @mxschmitt, sure, I’ll make it and let you know when it’s ready.