C#.Net Update nuget package using PackageManager

266 Views Asked by At

I have installed Selenium web driver in my .NET desktop application from the NuGet package manager.

Note: this is a .NET framework project, not an .NET Core or web project.

Now every time there is a update to Chrome, the older version of this installed package crashes my app. And I have to update it manually. So I thought I can check and update the web driver every time I run my app.

What I have tried

// ID of the package to be looked up
string packageID = "Selenium.WebDriver.ChromeDriver";
string pathToMyAppFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

// Connect to the official package repository IPackageRepository
IPackageRepository repo = PackageRepositoryFactory.Default.CreateRepository("https://packages.nuget.org/api/v2");

// Get the list of all NuGet packages with ID 'Selenium.WebDriver.ChromeDriver' 
List<IPackage> packages = repo.FindPackagesById(packageID).ToList();

// Filter the list of packages that are Release (Stable) versions and select the latest version
string latestVersion = packages.Where(item => (item.IsReleaseVersion() == true)).Max(p => p.Version).ToString();

// Initialize the package manager string path = <PATH_TO_WHERE_THE_PACKAGES_SHOULD_BE_INSTALLED>
PackageManager packageManager = new PackageManager(repo, path);

// Update the package 
packageManager.UpdatePackage(packageID, SemanticVersion.Parse(latestVersion), true, false);

Where I'm stuck

// Update the package 
packageManager.UpdatePackage(packageID, SemanticVersion.Parse(latestVersion), true, false);

On this line, I get an error:

Unable to find package 'Selenium.WebDriver.ChromeDriver'

When I checked my debug folder, I found only chromedriver.exe there. I found that NuGet has installed the entire webdriver package in the packages folder of the project that I'm working on. Something like this:

C:\path\to\VisualStudio\projects\ProjectFolder\packages\Selenium.WebDriver.ChromeDriver.101.0.4951.4100\driver\win32\chromedriver.exe

I want to update this package at form load event itself. I'm not sure if this method will update my .csproj file or not.

Can anyone please help me with this? Thanks in advance.

1

There are 1 best solutions below

0
pwrngr On

Instead of trying to handle the latest version of your WebDriver programmatically, you can use either WebDriver Service or WebDriverManager package - both automatically download a proper version of a WebDriver.

Selenium Manager (available since Selenium.WebDriver 4.6). It also comes with a benefit of managing your WebDriver's lifecycle for each browser instance.

var chromeOptions = new ChromeOptions();
chromeOptions.AddArgument("mute-audio"); //just for reference

var service = ChromeDriverService.CreateDefaultService(); //this automatically downloads a WebDriver and passes into your driver instance
IWebDriver DriverInstance = new ChromeDriver(service, chromeOptions);

WebDriverManager - some people still use it. I personally don't recommend it, as it's a third-party library that may eventually become obsolete due to the existence of Selenium Manager.

var chromeOptions = new ChromeOptions();
chromeOptions.AddArgument("mute-audio"); //just for reference

new DriverManager().SetUpDriver(new ChromeConfig(), VersionResolveStrategy.MatchingBrowser, Architecture.X64);
IWebDriver DriverInstance = new ChromeDriver(service, chromeOptions);