473,396 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Events in class IEDriver

Rob
I recently downloaded some code from codeproject
written by Leslie Hanks. From this code a class written
in C# contains many subroutines that automate the web
pages downloaded into Internet Explorer. Whats
missing from the class is a DocumentComplete event.

I need some C# assistance in adding this event to the
class. If you look in the code below there is a line 45
that uses IE.DocumentComplete . . . . However I do
not know how to turn this into an event. My final
objective is to create an assembly from the IEDriver
class.

The code for the IEDriver class is as follows:

using System;
using System.Collections;
using System.Diagnostics;
using System.Threading;
using mshtml;
using SHDocVw;

namespace IEAutomation {
/// <summary>
/// Summary description for IEDriver.
/// </summary>
public class IEDriver {
public int TimeoutSeconds {
get { return _timeoutSeconds; }
set { _timeoutSeconds = value; }
}

private InternetExplorer IE {
get { return _IE; }
}

private bool isDocumentComplete = false;

private InternetExplorer _IE;
private int _timeoutSeconds = 30;

public IEDriver() {
Process m_Proc = Process.Start("IExplore.exe");

Thread.Sleep(500);
_IE = null;
ShellWindows m_IEFoundBrowsers = new ShellWindowsClass();
foreach(InternetExplorer Browser in m_IEFoundBrowsers) {
if(Browser.HWND == (int)m_Proc.MainWindowHandle) {
_IE = Browser;
break;
}
}

IE.Visible = true;
IE.Left = 750;
IE.Top = 500;
IE.Height = 500;
IE.Width = 800;
IE.DocumentComplete += new
DWebBrowserEvents2_DocumentCompleteEventHandler(IE _DocumentComplete);
}

public void ClickAnchor(string anchorId) {
isDocumentComplete = false;
HTMLAnchorElement input = GetAnchorElement(anchorId);
input.click();
WaitForComplete();
}

public void ClickAnchorWithParent(string parentControlId, string anchorId)
{
isDocumentComplete = false;
anchorId = parentControlId + anchorId;
HTMLAnchorElement input = GetAnchorElement(anchorId);
input.click();
WaitForComplete();
}

public void ClickAnchorWithValue(string anchorValue) {
HTMLAnchorElement anchor = (HTMLAnchorElement) GetElementByValue("A",
anchorValue);
anchor.click();
WaitForComplete();
}

public void ClickButton(string buttonId) {
isDocumentComplete = false;
HTMLInputElementClass input = GetInputElement(buttonId);
input.click();
WaitForComplete();
}

public void ClickCheckbox(string anchorId) {
isDocumentComplete = false;
HTMLInputElement input = GetCheckboxElement(anchorId);

input.click();
}

public bool DoesElementExist(string elementId) {
IHTMLElement input = GetElementById(elementId);
return input != null;
}

public object GetElementAttribute(string elementId, string attributeName)
{
IHTMLElement element = GetElementById(elementId);
if (element == null) {
return null;
}
return element.getAttribute(attributeName, 0);
}

public IHTMLElement GetElementById(string elementId) {
HTMLDocument document = ((HTMLDocument) IE.Document);
IHTMLElement element = document.getElementById(elementId);

int nullElementCount = 0;
// The following loop is to account for any latency that IE
// might experience. Tweak the number of times to attempt
// to continue checking the document before giving up.
while (element == null && nullElementCount < 10) {
Thread.Sleep(500);
element = document.getElementById(elementId);
nullElementCount++;
}

return element;
}

public string GetInputValue(string inputId) {
HTMLInputElementClass input = GetInputElement(inputId);

if (input == null) {
return null;
}

return input.value;
}

public void Navigate(string url) {
isDocumentComplete = false;
object empty = "";
IE.Navigate(url, ref empty, ref empty, ref empty, ref empty);
WaitForComplete();
}

public void Quit() {
IE.Quit();
}

public void SetCheckboxValue(string checkboxId, bool isChecked, bool
failIfNotExist) {
HTMLInputElementClass input = GetInputElement(checkboxId);

if (input == null && failIfNotExist) {
throw new ApplicationException("CheckBox ID: " + checkboxId + " was not
found.");
}
if (input != null) {
input.@checked = isChecked;
}
}

public void SetInputStringValue(string inputId, string elementValue) {
HTMLInputElementClass input = GetInputElement(inputId);
input.value = elementValue;
}

public void SetInputIntValue(string inputId, int elementValue) {
HTMLInputElementClass input = GetInputElement(inputId);
input.value = elementValue.ToString();
}

public void SelectValueByIndex(string inputId, int index) {
HTMLSelectElementClass input = (HTMLSelectElementClass)
GetSelectElement(inputId);
input.selectedIndex = index;
}

protected IHTMLElement GetElementByValue(string tagName, string
elementValue) {
int nullElementCount = 0;
IHTMLElement element = GetElementByValueOnce(tagName, elementValue);

// The following loop is to account for any latency that IE
// might experience. Tweak the number of times to attempt
// to continue checking the document before giving up.
while (element == null && nullElementCount < 10) {
Thread.Sleep(500);
element = GetElementByValueOnce(tagName, elementValue);
nullElementCount++;
}

return element;
}

private HTMLAnchorElement GetAnchorElement(string inputId) {
return (HTMLAnchorElement) GetElementById(inputId);
}

private HTMLInputElement GetCheckboxElement(string inputId) {
return (HTMLInputElement) GetElementById(inputId);
}

private IHTMLElement GetElementByValueOnce(string tagName, string
elementValue) {
HTMLDocument document = ((HTMLDocument) IE.Document);
IHTMLElementCollection tags = document.getElementsByTagName(tagName);

IEnumerator enumerator = tags.GetEnumerator();

while (enumerator.MoveNext()) {
IHTMLElement element = (IHTMLElement) enumerator.Current;
if (element.innerText == elementValue) {
return element;
}
}

return null;
}

private HTMLInputElementClass GetInputElement(string inputId) {
return (HTMLInputElementClass) GetElementById(inputId);
}

private HTMLSelectElement GetSelectElement(string inputId) {
return (HTMLSelectElement) GetElementById(inputId);
}

private void WaitForComplete() {
int elapsedSeconds = 0;
while (!isDocumentComplete && elapsedSeconds != TimeoutSeconds) {
Thread.Sleep(1000);
elapsedSeconds++;
}
}

private void IE_DocumentComplete(object pDisp, ref object URL) {
isDocumentComplete = true;
}
}
}
I know this is asking alot, but sometimes you get lucky
Thanks for any assistance RS
Nov 17 '05 #1
0 4492

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Edward Diener | last post by:
Is it possible for a derived class to override a property and/or event of its base class ?
3
by: Sasha | last post by:
Hi everyone, Here is my problem: I have the following classes: - DataNode - this class is designed to hold some data and will be contained in a tree like data structure DataTree. When...
14
by: JPRoot | last post by:
Hi I use the following syntax to have events inherited from base to child classes which works nicely (virtual and override keyword on events). But I am wondering if it is a "supported" way of using...
8
by: JPRoot | last post by:
Hi M. Jeffrey Tan, Just hopping you didn't forget me? :) Thanks JPRoot ----- \"Jeffrey Tan\" wrote: -----
11
by: Nicky Smith | last post by:
Hello, I'm studying a book on VB.net Win apps, and I'm reading a section on events and delegates and raising events. Is it just me, or is this not just subs dressed up as something else? I...
18
by: Brett | last post by:
What are reasons to create your own events? Why not just call a class method/function instead? Thanks, Brett
30
by: Burkhard | last post by:
Hi, I am new to C# (with long year experience in C++) and I am a bit confused by the language construct of events. What is it I can do with events that I cannot do with delegates? At the moment...
2
by: mswlogo | last post by:
I looked high and low for code to do this and finally found some VB code that did it right. This is a C# flavor of it. public event EventHandler<EventArgsMyEventToBeFired; public void...
5
by: Sin Jeong-hun | last post by:
class Manager { public event ItemEventHandler ItHappened; public Manager { Item i; i.ItHappend+=new ItemEventHandler(OnItHappened); } void OnItHappened(...) {
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.