I am trying to automate GUI testing with Selenium in Windows PowerShell (5.1). I used the dotnet CLI to add the Selenium package (4.34.0) which I then load with Add-Type in PowerShell. My script goes like this:
WorkingDir = "$env:USERPROFILE\Projects\ProjectName"
Set-Location $WorkingDir
Add-Type -Path .\bin\Debug\netstandard2.0\WebDriver.dll
EdgeDriverService = [OpenQA.Selenium.Edge.EdgeDriverService]::CreateDefaultService("$WorkingDir\bin\Debug\netstandard2.0")
EdgeDriver = [OpenQA.Selenium.Edge.EdgeDriver]::new([OpenQA.Selenium.Edge.EdgeDriverService]$EdgeDriverService)
The last line throws a TypeInitializationException from OpenQA.Selenium.Command. Wrapped in this is:
Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
msedgedriver.exe is in the same directory as all of the assemblies. I think I could be having the same issue as seen in this question. The [System.AppContext]::BaseDirectory is C:\WINDOWS\system32\WindowsPowerShell\v1.0\, and I was having issues earlier with Selenium trying to load Selenium Manager from there, as well.
I have tried:
- Working directly inside the
netstandard2.0directory. - Setting the current directory with
[System.IO.Directory]::SetCurrentDirectory. - Adding an event handler to the dependency resolver.
Checking [System.AppDomain]::CurrentDomain.GetAssemblies(), I can see that the necessary assemblies are loaded (I have omitted those in \WINDOWS):
GAC Version Location
--- ------- --------
False v4.0.30319 USERPROFILE\Projects\ProjectName\bin\Debug\netstandard2.0\WebDriver.dll
False v4.0.30319 USERPROFILE\Projects\ProjectName\bin\Debug\netstandard2.0\System.Text.Json.dll
False v4.0.30319 USERPROFILE\Projects\ProjectName\bin\Debug\netstandard2.0\System.Memory.dll
False v4.0.30319 USERPROFILE\Projects\ProjectName\bin\Debug\netstandard2.0\Microsoft.Bcl.AsyncInterfaces.dll
False v4.0.30319 USERPROFILE\Projects\ProjectName\bin\Debug\netstandard2.0\System.Threading.Tasks.Extensions.dll
False v4.0.30319 USERPROFILE\Projects\ProjectName\bin\Debug\netstandard2.0\WebDriver.Support.dll
False v4.0.30319 USERPROFILE\Projects\ProjectName\bin\Debug\netstandard2.0\System.Text.Encodings.Web.dll
False v4.0.30319 USERPROFILE\Projects\ProjectName\bin\Debug\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll
I would try adding them to the GAC, however, I do not have admin privileges. How could I ensure that dependencies are successfully loaded, and why is the AppDomain of the script seemingly not being used?
WindowsPowerShellhas nothing to do with it. Both Selenium and Edge work just fine with .NET Core though. Somehow, the wrong library is loaded. And since you don't use a .NET project, there's nothing to retrieve the correct NuGet packages if needed. It's quite possible you've downloaded the wrong binaries.dotnetCLI approach). I don't get assembly issues this way tho I can't make it work completely: i.imgur.com/WRqCgdM.png