Home

Using WebDriver in LINQPad is pain

edited October 2018
Pain #1: Prevented installing Selenium native web drivers(such as "Selenium.Chrome.WebDriver")

In my previous post(https://forum.linqpad.net/discussion/1680/please-dont-prevent-installing-native-package-in-linqpad), LINQPad added support for installing native libraries, this is good.

But packages like Selenium.Chrome.WebDriver does not provide any AssemblyReferences or native libraries, instead, it provides an exe called "chromedriver.exe" that WebDriver used to do bridge to the browser.



Pain #2: Did not using the ".targets" file in nuget package

This "Selenium.Chrome.WebDriver" provide a targets file to code easily, the file like this:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Target Name="CopyChromeDriverToBin" BeforeTargets="AfterBuild"> <PropertyGroup> <ChromeDriverSrcPath>$(MSBuildThisFileDirectory)..\driver\chromedriver.exe</ChromeDriverSrcPath> <ChromeDriverTargetPath Condition=" '$(ChromeDriverTargetPath)' == '' ">$(TargetDir)</ChromeDriverTargetPath> </PropertyGroup> <Copy SourceFiles="$(ChromeDriverSrcPath)" DestinationFiles="$(ChromeDriverTargetPath)chromedriver.exe" SkipUnchangedFiles="true"></Copy> </Target> </Project>

Specifically, LINQPad5 did not respect the BeforeTargets="AfterBuild" field, and not copy anything to "%LocalAppData%\LINQPad\ProcessServer5AnyCPUB" or "%Temp%/LINQPad5/_sgxdmyqf" folder.

LINQPad should copy the "chromedriver.exe" to one of that folder so that I can write code easily and don't need to find where the "chromedriver.exe" is.

Comments

  • Hmm, is there any response for any of this?
  • LINQPad uses Roslyn for compiling queries, not MSBuild. There is no concept of a "project" in LINQPad, and it has no way to parse the .targets file.

    FWIW, this should be fixed in .NET Core 3 (there will be a new version of LINQPad to support this when it's released). It has the concept of a "runtimes" folder where you can put native dependencies via convention-based folders.
  • edited November 2018
    Well, sounds a plan, so I guess the best way is switching to Visual Studio for this scenario.
  • If that's the only or main package for which you have that problem, you could work around it by copying ChromeDriver.exe into the LINQPad program folder.
  • That's what I do now, thank you :smile: Still looking for better solution.
  • Had the same issue and my workaround is to find and set the path to the selenium-manager.exe which then takes care of finding and loading chromedriver. You could just set the path outside of LinqPad, but this method should work with updated packages (assuming they follow the same structure).

    One gotcha is that the check is performed in a static class in selenium and so if I forget to call this method, I can't just add the call to this method and rerun the script, but I have to close the script or close linqpad and re-open the script.

    public void SetSeleniumManagerPath()
    {
        string? path = AppDomain.CurrentDomain.GetAssemblies().Where(a => (a?.FullName ?? "").StartsWith("WebDriver,")).SingleOrDefault()?.Location;
        if (path != null)
        {
            var existingBinary = System.Environment.GetEnvironmentVariable("SE_MANAGER_PATH");
            if (existingBinary == null || !System.IO.File.Exists(existingBinary))
            {
                path = Path.Combine(path, @"..\..\..\manager\windows\selenium-manager.exe");
                if (System.IO.File.Exists(path))
                    System.Environment.SetEnvironmentVariable("SE_MANAGER_PATH", path);
            }
        }
    }
    
Sign In or Register to comment.