0

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:

  1. Working directly inside the netstandard2.0 directory.
  2. Setting the current directory with [System.IO.Directory]::SetCurrentDirectory.
  3. 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?

19
  • Powershell 5.1 runs on .NET Core and won't load any .NET Framework library. WindowsPowerShell has 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. Commented Jul 17 at 14:48
  • I'd suggest creating a C# project first, to get things to work. Even better use Playwright which doesn't require a webdriver binary. You can use the codegen command to record your actions as Playwright calls and use the generated script as a starting point Commented Jul 17 at 14:51
  • You're likely missing the right libraries, use an approach like the one shown in this answer to download your package: stackoverflow.com/a/77276619/15339544 (the dotnet CLI approach). I don't get assembly issues this way tho I can't make it work completely: i.imgur.com/WRqCgdM.png Commented Jul 17 at 14:54
  • @PanagiotisKanavos I'm not sure I'm 100% getting what you're saying here. What do you mean by saying I'm not using a .NET project? Is using dotnet new not the way to create a .NET project? I will consider writing this in C# first. Commented Jul 17 at 14:59
  • @SantiagoSquarzon I will give that a try, thanks. Commented Jul 17 at 15:01

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.