2

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
Marcello
  • 438
  • 5
  • 21
  • Pretty much any solution involving a loop and `Application.DoEvents()` is a **bad solution**, so you are only doing what's right by disliking it. For what it's worth, the .NET `WebBrowser` control has an event called [**`DocumentCompleted`**](https://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.documentcompleted(v=vs.110).aspx), which is raised whenever the page or an ` – Visual Vincent Jan 09 '18 at 23:56
  • Thanks Vincent. I know that WebBrowser have the DocumentCompleted event, but i recognise that when i load particular pages, this event was triggered before the page have completely load all java features. So i wrote 'the second piece of code' to show that previously, to recognise when a page it's fully loaded, and the 'DocumentCompleted' event it's not usable, i had find an integration between the WebBrowser and AgilityPack. But i'm looking for a program that let me help to manage interaction in a solid way. – Marcello Jan 10 '18 at 13:05
  • 1
    @Marcello This is common for pages featuring AJAX. Selenium contains commands which let you add a timeout or the ability to poll for an element. In the example provided above, you can handle the `DocumentCompleted` event and start a timer which polls the Web Browser control for you instead. – Jason Bayldon Jan 10 '18 at 15:07
  • @Jason, can you provide me a sample on how, your windows app. wait until Selenium load a page and poll for an Ajax element? I've verify that this can be made with Watin. But honestly i didn't find any example on how to do the same with Selenium. Hope i mistake, but seems to me that Selenium allow you only to manage the webBrowser, don't allow you to interact your windows form with browser. As explained here http://seleniumsimplified.com/2016/01/can-i-use-selenium-webdriver-to-automate-a-windows-desktop-application/ – Marcello Jan 10 '18 at 18:31

2 Answers2

0

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
Jason Bayldon
  • 1,296
  • 6
  • 24
  • 41
  • Thanks Jason. As i've understood Selenium let you simulate webbrowser path, but there is no interaction with windows form. The link you provided seems to me a too much complex way (i'm not friendly with javascript). But why use this complex and long solution when there are open source solution that offer ready and solid class and methods for interaction with win form, as i mentioned in my question (watin, white, winium..)? And if it's only to manage the dom, you can check that i made already in my previous code in a single instruction: DirectCast(wb.Document.DomDocument, mshtml.IHTMLDocument3) – Marcello Jan 10 '18 at 14:07
  • How do you define interaction with Windows Forms? Your Windows Form is controlling the selenium browser instance. It really comes down to if you intend to embed or have a need to embed the web browser control. You can look for Chrome Web Browser Controls, but the usability will vary project by project. – Jason Bayldon Jan 10 '18 at 15:06
  • Yes, Windows form control the selenium instance, but appear to me that you can only trigger methods consecutively (hope i mistake). i.e. i need to load a page, then wait until it's fully loaded (also AJAX elements), then populate a win form List and loop throught the elements searching for particular attributes, and then, load a new webpage, and when will be fully loaded, find and set some checkbox of textfield on the webbrowser, then click a button and your windows form will wait until new AjAX elements will be updated, and then your windows form do something else... – Marcello Jan 10 '18 at 18:43
  • @Marcello Your understanding is not correct. You create the driver instance within windows forms and you can keep reference to it as long as required. Selenium features the ability to wait for AJAX elements (implicit or explicit waiting) - or you can roll your own functionality with a Timer. Workflow could look like this: User clicks button, WinForms UI is disabled, ChromeDriver does work, UI becomes enabled again. Edited my answer to show an example. – Jason Bayldon Jan 11 '18 at 02:12
  • thanks i understand. So seems that for the basicuse i need Selenium would be the best choice. But two questions rise in my mind: whats the difference between Selenium and othesr software i mentioned? And the second is, am i correct if i say that at this web page http://seleniumsimplified.com/2016/01/can-i-use-selenium-webdriver-to-automate-a-windows-desktop-application/ users confirm that cannot be used Selenium to automatize a desktop application, but a desktop application can automatize Selenium? – Marcello Jan 11 '18 at 14:07
  • @Marcello It depends whether you need to automate desktop or web -- Selenium handles web automation and it appears that Winium handles thick Windows applications. I have seen proprietary drivers created which wrap AutoIt or Windows UI Automation for Selenium but not sure whats freely available - I do not see any "Desktop" Drivers. At the end of the day, you can use a combination of both to achieve automation. – Jason Bayldon Jan 11 '18 at 15:10
  • @Marcello After looking further at Winium it appears to be a driver that you can use with selenium for Windows Apps automation. – Jason Bayldon Jan 11 '18 at 15:21
  • Thanks Jason, i'll take this thread open for a couple of days, waiting if someone have something to add, helping us to understand something about the differences between theese softwares.. – Marcello Jan 12 '18 at 15:55
0

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.

Rescis
  • 547
  • 5
  • 19
  • Hi, I didn't know about CodeUI. Thank you very much to offering me to share your work. But can't buy Enterprise. I tried Selenium, but It seems to me that there are no 'document completed events', and the only way should be to wait for a generic delay, and 'hope' that the page has been loaded. I tried Watin Using the 'GoTo' method the form wait until the page has fully loaded.. With Watin i felt well, but seems that this project was 'abandoned' from lots of years, so compatibility couldn't assured. Please, can i ask you why you chosed a combination of White and Selenium? – Marcello Jan 10 '18 at 13:36
  • @marcello I find white is faster than CodedUI for WPF app automation, and selenium is faster for browser automation. I've not got any experience with watin, so I can't honestly comment there. Would you be able to state the steps of an example test in your question? I'm not 100 percent certain what you're trying to do – Rescis Jan 15 '18 at 17:58