Interaction Win Form and Web Browser, advice? Winium? Watin? White? Sikuli?

1.6k Views Asked by At

I want to create a .NET app which helps me to manage and automate a web browser. I did a quick search, and I found some solutions.

It seems that Selenium is the main reference software (open source), but it does not allow interaction between the Windows Form and the browser.

If I understand correctly, based on the source code of Selenium, have been built others software that allow to do so. In particular I've read about Winium, Watin, White and Sikuli.

At this page I've found a short comparison of some of them

https://blog.testproject.io/2016/12/22/open-source-test-automation-tools-for-desktop-applications/

I would like some advice on which one to use.

I'm an amateur programmer in VB.NET, but I've no problem in translating from C#.

I would like to use Chrome, but regarding Watin I read that it does not support Chrome, and it supports old version of other browser (Internet Explorer 9, and Firefox 2.x). Do you know if this actually create problems in loading pages?

I also need a software that can handle the delay while loading pages.

In example, with .NET WebBrowser class, I use the instruction:

Do Until wb.ReadyState = WebBrowserReadyState.Complete

Or previously I mix WebBrowser class with HtmlAgilityPack, in this mode:

Do
  Application.DoEvents()
  ' we create a delay, that allow the page to be loaded correctly:
  ' Thread.Sleep(...) Don't allow the page load correctly
  Dim delay as Date = Date.Now.AddMilliseconds(1000)
  Do While delay > Date.Now
  Loop
  documentAsIHtmlDocument3 = DirectCast(wb.Document.DomDocument, mshtml.IHTMLDocument3)
  sr = New StringReader(documentAsIHtmlDocument3.documentElement.outerHTML)
  docPreview.Load(sr)
  nodeTmp1 = docPreview.DocumentNode.SelectSingleNode("//div[@id='content']")
  nodeModule = nodeTmp1.SelectNodes("descendant:: div[@class='module']")
Loop Until IsNothing(nodeModule) = False

But I don't like this type of solution for lots of reasons, and reading the code, I think you could imagine why.

Do you have advices?

10/01/2018 Update: i've found a similar thread:

View Generated Source (After AJAX/JavaScript) in C#

Where users confirm they feel good using of Selenium and Watin. They rise an issue about slow performance. Others suggestions?

_________ in reply to Rescis _________

This is a piece of code i made with Watin

Imports WatiN.Core
...
Private browser As New IE
...
browser.GoTo("https://www.the_site_with-wich_i_have_my_account/")
...
Private Sub joinAccount()
    'Usually a website it's built with an HTML 'container' that contains Username and Password textBox and a Join button:
    'With chrome right click on that element, and 'inspect element' we can analize the HTML code and we can find the name of the class of the container
    Dim formLogin As WatiN.Core.Form = browser.Form(Find.ByClass("class_found_from_chrome_inspect_element"))
    'search for Username textBox
    Dim tbUsername As WatiN.Core.TextField = formLogin.TextField(Find.ById("Username_textField_found_from_chrome_inspect_element"))
    'set our username
    tbUsername.SetAttributeValue("value", "my_username")
    'search for Password textBox
    Dim tbPassword As WatiN.Core.TextField = formLogin.TextField(Find.ById("Password_textField_found_from_chrome_inspect_element"))
    'set the password
    tbPassword.SetAttributeValue("value", "my_password")
    'search the Join button and perform click
    formLogin.Button(Find.ById("Join_button_found_from_chrome_inspect_element")).Click()
End Sub
2

There are 2 best solutions below

8
Jason Bayldon On

In my experience, the web browser controls are far and few between - additionally mileage varies as they seem to rarely be updated. You can certainly control a Chrome window from a Windows Form albeit embedding a Chrome window into a Windows form is probably another story.

Selenium Web Driver is available through Nuget for .NET (https://www.nuget.org/packages/Selenium.WebDriver/) and that should let you create a Chrome session, navigate, pull elements, click buttons, etc - simulating the same actions a user would perform on a web page.

Additionally, WinForms is capable of interacting with Chrome through WebSockets and a custom Chrome extension (Previously detailed Accessing the DOM of a Chrome Tab from Visual Studio).

Edit: Adding sample code to show how Selenium works. A reference will be needed to the Selenium.WebDriver NuGet package. Note ImplicitWait which can handle AJAX elements... An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance (http://www.seleniumhq.org/docs/04_webdriver_advanced.jsp).

Public Class Form1
Dim driver As OpenQA.Selenium.Chrome.ChromeDriver
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    'create driver
    Dim driverFolderLocation = "c:\path\to\driver\folder"
    Dim driverService = OpenQA.Selenium.Chrome.ChromeDriverService.CreateDefaultService(driverFolderLocation)
    driver = New OpenQA.Selenium.Chrome.ChromeDriver(driverService, New OpenQA.Selenium.Chrome.ChromeOptions())

    'navigate to some url
    driver.Navigate.GoToUrl("http://google.com")

    'example to set implicit timeout for handling ajax elements
    driver.Manage().Timeouts().ImplicitWait = New TimeSpan(0, 0, 10)

End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    'show how we can send keys to an element in the driver
    Dim searchElement As OpenQA.Selenium.IWebElement = driver.FindElement(OpenQA.Selenium.By.Id("lst-ib"))
    searchElement.SendKeys("hello google")
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    'show we can pull data and display or add to a list or whatever
    MessageBox.Show(driver.FindElement(OpenQA.Selenium.By.Id("gb")).Text)
End Sub
End Class
2
Rescis On

I'm having a bit of trouble understanding the question, although assuming you are asking about automating the combination of a desktop app and a web application, here:

CodedUI, provided with Visual studio enterprise allows for the automation of WPF, Win, and Web applications. Although it is not open source, it fulfills your other requirements very well and allows for a seamless transition between automating native desktop apps and a web browser. If you are interested in using this, I have a bunch of personal methods I've created that make hand coding tests much easier.

I have moved away from CodedUI however towards a combination of White and Selenium.
White is much faster than CodedUI for WPF applications, which is very noticeable over a test suite. It is however not especially commonly used, and documentation is sparse. Selenium is very commonly used, faster than CodedUI, and has very good documentation. I've seen very little reason to use other web automation platforms.