Skip to content

AsyncWebDriver is .Net WebDriver rewritten to async/await pattern

License

Notifications You must be signed in to change notification settings

ToCSharp/AsyncWebDriver

Repository files navigation

AsyncWebDriver

AsyncWebDriver is .Net WebDriver rewritten to async/await pattern from selenium project. It uses browser drivers via IAsyncWebBrowserClient interfaces. AsyncWebDriver has synchronous wrapper SyncWebDriver and SeleniumAdapter.

Join the chat at https://gitter.im/AsyncWebDriver/Lobby
Look video of BrowsersDevToolsExample: https://youtu.be/dNMuoeDmEME

It is Firefox driver. It connects directly to Marionette and is async from this connection. No need in geckodriver. AsyncFirefoxDriver implements IAsyncWebBrowserClient interfaces and can be used as AsyncWebDriver.

But the main MISSION of this project is to provide Firefox specific capabilities. Reqest listener and profiles worker already here.

More extensions in AsyncFirefoxDriverExtensions project.

Debugger, all what addons and extensions can do, we can do with AsyncFirefoxDriver (coming soon).

Chrome WebDriver and Chrome DevTools in one library.
It connects directly to Chrome DevTools and is async from this connection. No need in chromedriver.exe.

Opera WebDriver and Opera DevTools in one library.
It connects directly to Opera DevTools and is async from this connection. No need in operadriver.exe.

Other browsers

If it will be interestring we can add async drivers.

Usage

Install via NuGet

If you want to include AsyncFirefoxDriver in your project, you can install it directly from NuGet

PM> Install-Package AsyncFirefoxDriver

If you want to include AsyncChromeDriver, you can install it directly from NuGet

PM> Install-Package AsyncChromeDriver

And Opera NuGet

PM> Install-Package AsyncOperaDriver

Write code example

Chrome

     var asyncChromeDriver = new AsyncChromeDriver();
     var webDriver = new WebDriver(asyncChromeDriver);
     await webDriver.GoToUrl("https://www.google.com/");
     var query = await webDriver.WaitForElementWithName("q");
     //await query.SendKeys("ToCSharp");
     foreach (var v in "ToCSharp".ToList())
     {
        await Task.Delay(500 + new Random().Next(500));
        await query.SendKeys(v.ToString());
      }
      await Task.Delay(500);
      var prevQuery = await webDriver.FindElement(By.Name("q"));
      await query.SendKeys(Keys.Enter);
      query = await webDriver.WaitForElementWithName("q", prevQuery?.Id);
      var allCookies = await asyncChromeDriver.DevTools.Session.Network.GetAllCookies(new GetAllCookiesCommand());

Firefox

     var webDriver = new WebDriver(new AsyncFirefoxDriver());
     await webDriver.GoToUrl("https://www.google.com/");
     var query = await webDriver.FindElement(By.Name("q"));
     await query.SendKeys("ToCSharp");
     await Task.Delay(500);
     await query.SendKeys(Keys.Enter);
Firefox Developer Tools

Look BrowsersDevToolsExample.

     await FirefoxProfilesWorker.CreateFirefoxProfile(dir, profileName);
     asyncFirefoxDriver = new AsyncFirefoxDriver(new FirefoxDriverConfig()
         .SetProfileName(profileName)
         .SetIsMultiprocessFalse()
         .SetDoSetDebuggerRemoteEnabled());
     webDriver = new WebDriver(asyncFirefoxDriver);
     await webDriver.GoToUrl("https://www.google.com/");

     await asyncFirefoxDriver.OpenBrowserDevTools();
     // asyncFirefoxDriver.BrowserDevTools is AsyncFirefoxDriver
     devToolsWebDriver = new WebDriver(asyncFirefoxDriver.BrowserDevTools);

     // Then we can debug Firefox developer tools itself: 
     await asyncFirefoxDriver.BrowserDevTools.OpenBrowserDevTools(9654);

Open

     IAsyncWebBrowserClient browserClient = null;
     DriverConfig config = null;
     if (chbOpenProfileHeadless.IsChecked == true)
     {
         var (width, height) = GetWidthHeight();
         config = new DriverConfig().SetHeadless().SetWindowSize(width, height).SetIsDefaultProfile();
     }
     else config = new DriverConfig().SetIsDefaultProfile();

     if (rbOpenFirefox.IsChecked == true)
     {
         asyncFirefoxDriver = new AsyncFirefoxDriver(config);
         browserClient = asyncFirefoxDriver;
     }
     else if (rbOpenChrome.IsChecked == true)
     {
         asyncChromeDriver = new AsyncChromeDriver(config);
         browserClient = asyncChromeDriver;
     }
     else if (rbOpenOpera.IsChecked == true)
     {
         asyncOperaDriver = new AsyncOperaDriver(config);
         browserClient = asyncOperaDriver;
     }

     webDriver = new WebDriver(browserClient);
     driversToClose.Add(webDriver);
     await webDriver.GoToUrl("https://www.google.com/"); // browser opens here
     var mess = $"opened on port {config.Port} in dir {config.UserDir} \nWhen close, dir will NOT be DELETED";

Unit tests

AsyncWebDriver is async and is different from Selenium. SyncWebDriver is synchronous wrapper over AsyncWebDriver.

SeleniumAdapter is adapter of Selenium interfaces to SyncWebDriver. So we can run Selenium tests on AsyncWebDriver. Here is Unit Tests from Selenuim, which we can run to test functionality of all projects and its connections.

Examples

Look at AsyncFirefoxDriverExample, AllDriversExample, AsyncChromeDriverExample.

Run built Example in release tab.

  • LiveIp to get ip
  • LivePreferences to view and edit Firefox preferences of running profile
  • AddonManager have methods GetAddonsList, InstallAddon, InstallTemporaryAddon, UninstallAddon.

About

AsyncWebDriver is .Net WebDriver rewritten to async/await pattern

Resources

License

Stars

Watchers

Forks

Packages

No packages published