Note: Advice in this article will only work for DotNetBrowser 1.
DotNetBrowser is compatible with Selenium WebDriver. To use Selenium with DotNetBrowser, perform the following steps:
1. Download ChromeDriver using NuGet packages: Selenium.WebDriver and Selenium.WebDriver.ChromeDrive.
2. Run the DotNetBrowser application with the --remote-debugging-port
switcher as shown in the Remote Debugging article.
3. Create Selenium WebDriver and use the remote debugging port (9222) from the previous step to connect to the DotNetBrowser application.
C#
Program.cs
using System.Diagnostics; using System.IO; using System.Threading; using OpenQA.Selenium; using OpenQA.Selenium.Chrome; namespace DotNetBrowserAndSelenium { class Program { private const string RemoteDebuggingAddress = "localhost:9222"; static void Main(string[] args) { string applicationFullPath = Path.Combine(Directory.GetCurrentDirectory(), "DotNetBrowserTestApplication.exe"); ProcessStartInfo startInfo = new ProcessStartInfo { CreateNoWindow = false, FileName = applicationFullPath, WindowStyle = ProcessWindowStyle.Normal }; Process applicationProcess = Process.Start(startInfo); ChromeOptions options = new ChromeOptions { DebuggerAddress = RemoteDebuggingAddress, BinaryLocation = applicationFullPath }; IWebDriver webDriver = new ChromeDriver(options) { Url = "https://www.teamdev.com/dotnetbrowser" }; Thread.Sleep(3000); IWebElement evaluateButton = webDriver.FindElement(By.XPath("//*[@id='header']/div[1]/div/ul/li[5]/a")); evaluateButton.Click(); IWebElement nameTextbox = webDriver.FindElement(By.Name("name")); nameTextbox.SendKeys("John Doe"); IWebElement emailTextbox = webDriver.FindElement(By.Name("email")); emailTextbox.SendKeys("sales@teamdev.com"); Thread.Sleep(5000); if(applicationProcess != null) { applicationProcess.CloseMainWindow(); applicationProcess.Dispose(); } webDriver.Quit(); } } }
Form1.cs
using System.Windows.Forms; using DotNetBrowser; using DotNetBrowser.WinForms; namespace DotNetBrowserTestApplication { public partial class Form1 : Form { public Form1() { BrowserPreferences.SetChromiumSwitches( "--disable-web-security", "--enable-automation", "--remote-debugging-port=9222"); InitializeComponent(); BrowserView browserView = new WinFormsBrowserView() { Dock = DockStyle.Fill }; Controls.Add((Control)browserView); } } }
VB.NET
Module1.vb
Imports System.IO Imports System.Threading Imports OpenQA.Selenium Imports OpenQA.Selenium.Chrome Module Module1 Private Const RemoteDebuggingAddress As String = "localhost:9222" Sub Main() Dim applicationFullPath As String = Path.Combine(Directory.GetCurrentDirectory(), "DotNetBrowserTestApplication.exe") Dim startInfo = New ProcessStartInfo With { .CreateNoWindow = false, .FileName = applicationFullPath, .WindowStyle = ProcessWindowStyle.Normal } Dim applicationProcess As Process = Process.Start(startInfo) Dim options = New ChromeOptions With { .DebuggerAddress = RemoteDebuggingAddress, .BinaryLocation = applicationFullPath } Dim webDriver As IWebDriver = new ChromeDriver(options) With { .Url = "https://www.teamdev.com/dotnetbrowser" } Thread.Sleep(3000) Dim evaluateButton As IWebElement = webDriver.FindElement(By.XPath("//*[@id='header']/div[1]/div/ul/li[5]/a")) evaluateButton.Click() Dim nameTextbox As IWebElement = webDriver.FindElement(By.Name("name")) nameTextbox.SendKeys("John Doe") Dim emailTextbox As IWebElement = webDriver.FindElement(By.Name("email")) emailTextbox.SendKeys("sales@teamdev.com") Thread.Sleep(5000) If (applicationProcess IsNot Nothing) Then applicationProcess.CloseMainWindow() applicationProcess.Dispose() End If webDriver.Quit() End Sub End Module
Form1.vb
Imports DotNetBrowser Imports DotNetBrowser.WinForms Public Class Form1 Sub New() BrowserPreferences.SetChromiumSwitches( "--disable-web-security", "--enable-automation", "--remote-debugging-port=9222") InitializeComponent() Dim browserView As BrowserView = New WinFormsBrowserView() With { .Dock = DockStyle.Fill } Controls.Add(browserView) End Sub End Class