Saturday, May 31, 2014

Automating WebBrowser with C# and Open Twebst

The .Net WebBrowser class is actually an Internet Explorer control. You can add it to your Windows Forms application do display HTML content.

Using WebBrowser class from C# code you can have access to HTML document but automating the IE control is not as easy as it should be. You will have to take into account things like:
  • browser events and timeouts: navigating, document complete
  • an asynchronous programming model: web automation code mixed with event handlers
  • document ready state
  • HTML events - you'll have to fire the right events so web pages will react properly
  • dealing with DOM and cross frames security restrictions
  • manually writing web automation code
Open Twebst addresses all this issues with a consistent programming model and an integrated web recorder that assists you in generating web automation code. Basically, things work like this with Open Twebst:
  • attach to WebBrowser control so you can use Twebst objects
  • optionally you can register to OnCancel event so you can cancel web automation at any point (the current web automation method will throw an error when canceled)
  • generate web automation code with Open Twebst web recorder
  • use the code in your C#/VB.Net Windows Forms project
Web automation code executes on UI thread but Open Twebst methods dispatch windows messages under the hood so the application does not freeze. OnCancel event is raised from time to time so you have a chance to stop automation code at any point.

Here is a sample that comes with Open Twebst setup and you can also download from here:

Sunday, April 27, 2014

Excel web automation in 6 easy steps

This short tutorial is about automating Internet Explorer browser from an Excel VBA macro using Open Twebst, an open source IE automation framework.
  • download and install Open Twebst from: http://codecentrix.com/download.html
  • start recording your web actions and choose VBA code generator
  • open Excel, create an workbook and insert a new macro: View/Macros/View Macros => fill out a Macro name + Create
  • copy the VBA code from Twebst recorder window and paste it into VBA macro editor
  • add reference to Open Twebst library from Excel VBA macro editor: Tools/References => Open Twebst 1.0 Type Library
  • save the workbook; choose Excel Macro-Enabled Workbook (*.xslm) type in Save dialog box
Here is the whole story in pictures:






Wednesday, March 05, 2014

Web Automation - dealing with navigation timeout in Open Twebst

The most important aspect of web automation is object recognition. In Open Twebst, FindElement method makes use of HTML tag name and attributes to find objects inside web pages. But before accessing the DOM, the web page must be loaded. Deciding when the web page is completely loaded is not always an exact science, the loading time depends on various things like:

  • the speed of the internet connection
  • the size of the HTML document
  • the size and number of the images inside web document
  • some web pages seem to update and load continuously
  • sometimes Open Twebst library fails to detect if the page is completely loaded when the page embeds resources like PDF or office documents

To address page loading, Open Twebst has three useful properties: core.loadTimeout, core.searchTimeout and core.loadTimeoutIsError which affect Find methods and work together like this:

  • wait a maximum core.loadTimeout milliseconds for the page to be completely loaded. If core.loadTimeout is zero don’t wait and start searching the element right away.
  • if the page is completely loaded start searching the element in core.searchTimeout milliseconds. If the element is not found returns null.
  • if the page is not completely loaded and core.loadTimeoutIsError is true (which is default value) then throw an exception. If core.loadTimeoutIsError is false try  to find the element in core.searchTimeout milliseconds; returns null if not found.
  • searchTimeout is useful for elements that are dynamically created by web page scripts + Ajax after the DOM is created.
Usually we have to wait for the page to load before performing actions on HTML controls; some web pages just don't work if we access the DOM as soon as the controls are available because not all the content is loaded and the initialization scripts are not yet executed.

However it is possible to set core.loadTimeout to zero (don't wait for the page to load) and set core.searchTimeout to a greater value like 1 minute. This way the element is retrieved as soon as it is loaded.

Other related features: core.isLoading, browser.WaitToLoad.

Sunday, February 23, 2014

Open Twebst has a new home

Open Twebst has a new home, the transition to hosting at Github is now complete: https://github.com/codecentrix/open-twebst

Twebst went open source on February 19, 2012. Two years, ~3700 downloads and 16 forks (on Google code) later, the version 1.3.0.0 is released: https://github.com/codecentrix/open-twebst/releases/


Sunday, June 03, 2012

Watir Recorder - Open Source

For those in a hurry here is the download link: https://github.com/codecentrix/open-twebst/releases/

Open Twebst is an open source web automation library and web recorder for Internet Explorer browser. It is somehow similar in features with Watir. Because of that I decided that I could reuse Twebst web recorder to generate Ruby code for Watir.

Just choose Watir from the language combo-box, press start recorder and you're ready to go! The web recorder also generates VBScript, JScript, C#, VB.Net and Python code for Open Twebst Library.

Future plans: I've already started to work on a new web spy + code generator wizard that will support Watir too.

For any questions, remarks, bug-reports, ideas or anything else, feel free to post on Twebst Google Group and/or Twebst Linkedin Group.

Watir Web Recorder - Open Source
Open Twebst - Watir Recorder

Friday, May 25, 2012

IE 64-bit Automation

Twebst Automation Studio Pro 2.7 new release is out!
Go get it from http://www.codecentrix.com/download.html

The new shiny feature is 64-bit support. What does this mean? It means you have now a 64-bit version of Twebst Web Automation Library. You no longer have to compile your .Net web automation code as x86. x64 or Any CPU will work too.

Now you can also automate Internet Explorer from 64-bit Office/Word/Excel using VBA.
As a bonus, you can automate Internet Explorer 64-bit or even IE web browser control embedded in 64-bit applications.

Sunday, February 19, 2012

Open source web testing and automation framework

10, 9, 8 ... ignition sequence start ...
3, 2, 1, zero ... All engines running. Lift-off! We have a lift-off.




I am happy to announce Open Twebst, open source web testing and automation framework!

http://open-twebst.codecentrix.com/

Get it while it's hot!

Happy web automation to y'all!

Thursday, May 12, 2011

IE9 Automation and Web Macros

Twebst Automation Studio 2.6 - New Release!

I am once again happy to announce a new version of Twebst Automation Studio. What's new:
  • IE9 web automation and macro support.
  • custom DLL injection instead of BHO browser extension (which in IE9 generates security warnings).
  • TwebstBHOps.dll is no longer needed.
  • FindBrowser and FindAllBrowsers now work with any application that embeds Internet Explorer web control.
  • Bug fixes and enhancements.


.

Thursday, July 08, 2010

IE8 close tab programmatically

Sometimes you may wish to close a browser instance and Twebst Library has support for that. You just have to call Close method against the Browser web automation object you want to close.

Let's see a short JScript web automation macro that closes the first IE8 tab loaded from google.com domain. If the closing tab is the last tab inside the IE frame then the whole browser will be closed.
// Create core object.
var core = new ActiveXObject("Twebst.Core");

// Find a browser for which the displayed URL contains "google.com".
core.useRegExp = true;
var b = core.FindBrowser("url=.*google.com");
b.Close();
There are more Twebst methods for automating IE7 and IE8 tabs that I'll talk about in future articles: OpenNewTab, activeTab, isTabActive, tabList

Wednesday, July 07, 2010

Twebst Automation Studio 2.5 - New Release!

This blog was too quiet for some time but after another development cycle I am happy to announce a new release: Twebst Automation Studio version 2.5

New features:

.

Download Twebst - IE Automation Software

Sunday, March 07, 2010

Move data from Excel to IE and back with VBA web macros

Let's continue my previous post (IE8 Automation using Excel VBA Macro) with some VBA Internet Explorer automation code:
' Don't forget to add reference to TwebstLib from menu Tools/References
Dim core As TwebstLib.ICoreVba
Set core = New TwebstLib.core

Dim browser As TwebstLib.IBrowserVba
Set browser = core.StartBrowserVba("http://www.google.com/")

For i = 1 To 5
' Get the current text to be translated
Dim textToTranslate As String
textToTranslate = ActiveSheet.Cells(i, 1).Formula

' Twebst web automation part
Call browser.Navigate("http://www.google.com/language_tools?hl=en")
Call browser.FindElementVba("textarea", "id=source").InputText(textToTranslate)
Call browser.FindElementVba("select", "name=sl").Select("English")
Call browser.FindElementVba("select", "name=tl").Select("French")
Call browser.FindElementVba("input submit", "text=Translate").Click

' Find result span object
Dim resultObj As TwebstLib.IElementVba
Set resultObj = browser.FindElementVba("span", "id=result_box")

' Get the text result
Dim translatedText As String
translatedText = resultObj.Text

' Save the result into Excel sheet
ActiveSheet.Cells(i, 2).Formula = translatedText
Next

Points of interest:
  • in VBA web macro new methods are used (see FindElementVba). The newly added methods (from version 2.1) are especially designed for VBA language and they work the same as they non-VBA counterparts except the fact no variable number of search conditions are supported.
  • Twebst web recorder was updated to automatically generate VBA macro code against the newly added methods.
  • all web page loading synchronization is provided by default by the library.
  • when automating web controls all necessary HTML events are fired up against target object (see fireEvent) to simulate user's web action.
Downloads:

Wednesday, March 03, 2010

IE8 Automation using Excel VBA Macro

In my previous posts (Excel and IE automation - the WSH way, Extract web data into Excel.Application with browser macro) I automated both IE and Excel using WSH. Now I'm going to automate Internet Explorer directly from an Excel macro using the VBA language.

The VBA macro will work against a sheet containing phrases to be translated using Google Translate tool. It automatically gets each phrase, opens IE browser, navigates to translation page, waits the web page to complete loading, fills out the text to be translated into HTML web controls, press submit button, gets back the result and saves it into the same Excel sheet.

It is possible to directly automate IE browser from VBA macro but it is not always as simple as it should be. (see my older post: What's wrong with Internet Explorer Automation?) Using Twebst Automation Studio when automating IE from VBA has some advangates:
  • it works with all Internet Explorer versions: IE6, IE7, IE8 and with IE protected mode.

  • it works on all Windows versions: XP, Vista, Win7

  • easily search of elements across all sub-documents inside frames/iframes hierarchy

  • search HTML elements using regular expressions

  • simulate user actions on all HTML controls (button, combo-box, list-box, edit-box, upload control) and fire all necessary HTML events

  • auto-generate VBA code with powerful web recorder

  • much more (see Twebst Automation Studio website)

That's all for now. Until our next encounter, check the download section below and have fun with the Excel VBA macro code.

Downloads:

Thursday, February 25, 2010

Extract web data into Excel.Application with browser macro

Let's continue my previous post where I presented a browser macro that automatically translates phrases in an Excel document using Google Translate tool. As you cand see in code, the WSH script performs the following steps:
  • instantiate "Twebst.Core" web automation object and start an IE browser.
  • instantiate "Excel.Application" automation object and open XLS document.
  • get a phrase to be translated from Excel document.
  • navigate to Google translate tool web page.
  • fill out web controls (the text to be translated, source and target language combo-boxes) and then automatically press submit button.
  • wait the web page to load, get the translated text and save it into Excel document.

Let's see some code. I will skip the initial setup phase and go directly to web automation part which is more interesting:

for (var i = 1; i <= 5; ++i)
{
// Get current text to translate from XLS document.
var textToTranslate = excelApp.ActiveSheet.Cells(i, 1).Formula;

// Web automation part implemented with Twebst Automatio Studio.
browser.Navigate('http://www.google.com/language_tools?hl=en');
browser.FindElement('textarea', 'id=source').InputText(textToTranslate);
browser.FindElement('select', 'name=sl').Select('English');
browser.FindElement('select', 'name=tl').Select('French');
browser.FindElement('input submit', 'text=Translate').Click();

// Get the reszult and save it into XLS document.
var translatedText = browser.FindElement('span', 'id=result_box').text;
excelApp.ActiveSheet.Cells(i, 2).Formula = translatedText;
}

Points of interest:

  • FindElement method waits for the IE web page to be completely loaded before searching the element. The timeout value can be changed programatically (see loadTimeout, searchTimeout properties).
  • InputText method sets the specified value in edit/password web controls and fires all necessary HTML events (onkeydown, onkeypress, onkeyup, onchange, etc).
  • Select method fully automates combo-boxes and list-boxes web control and fires all necessary HTML events (like onchange).

Downloads:

Wednesday, February 24, 2010

Excel and IE automation - the WSH way

I can think of two possible scenarios when automating Excel and Internet Explorer makes any sense:
  • you have data stored in Excel sheets and you want to push data into online web forms.

  • you need to extract data from web and save them into Excel xls file.
There are two ways to implement this:
  • automate Internet Explorer browser from Excel VBA macro.

  • automate both IE and Excel from an external process using OLE automation.
Today I will talk about the second option; I will automate both Internet Explorer and Excel application from a WSH script using Twebst Automation Studio. Here's a diagram showing the work-flow:


Fill out web forms with Excel data

The WSH web macro in this tutorial opens an XLS file containing phrases to be translated using Google Translate tool. It then automatically gets each phrase, opens an IE browser, navigates to translation page, fills out the text to be translated, press submit button, gets back the result and saves it into the same XLS file.

That's all for now! (as I try to keep articles relatively short) Meanwhile, you should download Twebst Automation Studio and the web macro sample to see it at work. Then, you can continue with the next article explaining the web macro code and comments: Extract web data into Excel.Application with browser macro

Downloads:

ExcelToWebMacro.zip

Twebst Automation Studio (FREE edition available btw)

Friday, February 05, 2010

IE8 automation with Excel VBA macro

Twebst Automation Studio Ver 2.1 New Release!

Many people requested for VBA (Excel, Word) direct web automation support in Twebst Library. The previous version could not be easily used from VBA because of the way Excel works with methods with variable number of parameters (which is the case for many Twebst methods like: FindElement, FindBrowser, etc).

The new Twebst 2.1 version includes a new full set of interfaces and methods specially designed for Excel VBA. The web recorder was also updated to generate VBA code that you can easily include in your Excel macro. To see what's new in Twebst ver 2.1 check changelog.txt

Automating Internet Explorer from Excel VBA macro is now possible with Twebst Automation Studio 2.1.

Monday, December 21, 2009

How to automate existing instances of Internet Explorer - Tutorial(4)

As explained in a previous tutorial, a web macro can start by connecting to an already running instance of Internet Explorer browser. A web macro usually starts a new Internet Explorer page and then navigates to a URL to begin with. There are times when automating an existing IE window is a more natural alternative. Let's start with a JScript/WSH simple web macro:
// Create core object.
var core = new ActiveXObject("Twebst.Core");

// Find a browser for which the displayed URL contains "yahoo".
core.useRegExp = true;
var b1 = core.FindBrowser("url=.*yahoo.*");
b1.Navigate("www.google.com");

// Find a browser for which the title is "Google" (exact match).
core.useRegExp = false;
var b2 = core.FindBrowser("title=Google");

b2.Close();

Basically FindBrowser method searches for a browser page based on TITLE and/or URL. If core.useRegExp is false then an exact search is used. If core.useRegExp is true then the search uses regular expressions. Once connected to a IE browser instance, you get a Browser object to play with.

Of course, this works for IE6, IE7, IE8 on Windows XP, Vista and Windows 7 (32 and 64 bit) and it's compatible with Internet Explorer Protected Mode.

What can you do with a Browser object? Quite a lot of things: Navigate to a URL, FindElement and FindFrame inside it, Close the browser, get information about the browser, automate modal and modeless HTML dialgos and wait to complete navigation and loading.

Enough talk! It's time to Download Twebst Automation Studio and see it for yourself.

.

Monday, December 14, 2009

IE macro - how to get programmatic control over HTML elements - Tutorial(3)

Finding elements inside HTML frames/iframes hierarchy with Twebst Web Automation Library

Once you have started a web macro and you've got a Browser object to play with, it's time to get programmatic access HTML controls inside the web page. As any web developer should know, you can easily find elements inside a document using document.getElementById.

In the world of web automation things are a bit different; web macros need to access HTML elements in any web document and at any level of DOM hierarchy. The target HTML element can be inside frames/iframes loaded from various domains, subject of cross-frame scripting security restrictions (see my older posts: "When IHTMLWindow2::get_document returns E_ACCESSDENIED" and "When IHTMLWindow2.document throws UnauthorizedAccessException").

Here's a simple VBscript web macro that automates a Google search in Internet Explorer:
Dim core
Dim browser
Set core = CreateObject("Twebst.Core")
Set browser = core.StartBrowser("http://www.google.com/")

Call browser.FindElement("input text", "name=q").InputText("codecentrix")
Call browser.FindElement("input submit", "name=btnG").Click()

What is so great about the code above is the way FindElement method works: first it waits for the browser to load the HTML document then it searches for the element thru all frame/iframe hierarchy.

Once an Element is found you can perform actions on it like: Click, RightClick, InputText, Check, Uncheck, Select, Highlight etc. What is even cooler than this, is that you don't have to write these statements by yourself. They will be automatically generated by Twebst Web Recorder which you can get for free:

Get Free Web Recorder for Internet Explorer

.

Wednesday, December 09, 2009

IE8 automation: How to programatically open an URL - Tutorial (2)

Get started with web macros in IE

Every web macro has to start somewhere, has to start somehow. There are basically two scenarios of automating Internet Explorer browser:
  • start a new IE browser and navigate to a URL to begin with

  • connect to an existing instance of Internet Explorer browser and continue automation
In this short tutorial I'll show you how to open a new Internet Explorer browser and open an URL with Twebst Automation Studio. Let's start with a short JScript web macro.
var core    = new ActiveXObject('Twebst.Core');
var browser = core.StartBrowser('http://www.google.com/');
The code is quite self-explaining. It creates a Twebst object and then opens a given URL in a new IE browser instance. What you get back from StartBrowser call is a Browser object that can be used to further automate Internet Explorer.

What can you do with a browser object? Quite a lot of things: navigate to a URL, find HTML elements and frames inside it, close the browser, get information about the browser, automate modal and modeless HTML dialgos and wait to complete navigation and loading.

That's all for now, but these feature will be covered in next tutorials!

(Automation Library and Macro Recorder for Internet Explorer)

Sunday, November 08, 2009

Automate HTML File Upload Control - Tutorial (1)

If you ever tried to programatically change the value of a HTML File Upload Control you probably noticed that <input type="file" /> element is read-only! This happens for good security reasons: web pages scripts should not be able to upload random files without user consent. Unfortunatelly the control is read-only for browser extensions and add-ons too (BHO, toolbars, side bars, etc).

For IE6 and IE7 the upload control may be automated by setting the focus to "Internet Explorer_Server" window and generating Win32 keyboard events. For IE8, the upload control is read-only, the user can only set a value by pressing "Browse" button and choosing a file.

With Twebst Automation Studio, automating HTML File Upload Control can't be easier. Here's a short VBScript sample of doing it:

Option Explicit
Dim core
Dim browser
Set core = CreateObject("Twebst.Core")
Set browser = core.StartBrowser("http://www.2shared.com/")

Call browser.FindElement("input file", "id=upField").InputText("C:\somepath\photo1.jpg")



Tuesday, November 03, 2009

New Web Macro Recorder released!

After several months of complete radio silence, things have been moving forward for Twebst Library which turned into a more powerful and mature product.
Please welcome Twebst Web Automation Studio!

Twebst Automation Studio is an advanced web automation framework for Internet Explorer that can be used within any environment that supports COM, from scripting languages (JScript, VBScript) to high level programming languages (C#, VB.Net, C++).

The framework includes two components: Twebst Web Recorder that automatically generates most of the automation code and Twebst Library which is a collection of programable objects that creates a web automation API.

But one image is better than thousand words...

Web Macro Recorder in Action

Twebst Web Recorder features:

  • Easily create web macros through an intuitive graphical interface.
  • Generate web automation code using the language of your choice: JScript, VBScript, C#, VB.Net, C++
  • Record web actions on all HTML controls (button, combo-box, list-box, edit-box)
And one more thing: FREE version available!
.