 |
modified on 22 July 2010 at 08:53 ••• 1,345 views
Requested API changes/27-May-2010
From
27/May/2010, by Dinis Cruz
file:API_BTOpenZone.cs
// This file is part of the OWASP O2 Platform (http://www.owasp.org/index.php/OWASP_O2_Platform) and is released under the Apache 2.0 License (http://www.apache.org/licenses/LICENSE-2.0)
using System;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using System.Text;
using O2.Kernel;
using O2.Kernel.ExtensionMethods;
using O2.DotNetWrappers.ExtensionMethods;
using O2.Views.ASCX.ExtensionMethods;
using O2.Views.ASCX.classes.MainGUI;
using O2.External.IE.ExtensionMethods;
using SHDocVw;
using WatiN.Core;
using O2.XRules.Database._Rules._Interfaces;
//O2File:C:\O2\_XRules_Local\Extra_methods.cs
//O2File:C:\O2\_XRules_Local\WatiN_IE_ExtensionMethods.cs
//O2File:C:\O2\_XRules_Local\WatiN_IE.cs
//O2Ref:Interop.SHDocVw.dll
//O2Ref:WatiN.Core.1x.dll
namespace O2.Script
{
public class API_BTOpenZone
{
public WatiN_IE ie;
public string defaultWebPage;
public API_BTOpenZone()
{
ie = "".ie(0,450,800,700);
defaultWebPage = "http://www.google.co.uk";
}
public API_BTOpenZone login()
{
return login("");
}
public API_BTOpenZone login(string fileWithLoginDetails)
{
ie.open(defaultWebPage);
if (ie.title("Google"))
"we are already conneted to the internet".info();
else if (ie.title("BT Openzone"))
{
"detected BT Openzone page".info();
// get the login details
ICredential credential = fileWithLoginDetails.credential("BtOpenZone");
if (credential==null)
{
"no file with credentials provided, or no credential of type BtOpenZone found".debug();
credential = ie.askUserForUsernameAndPassword();
}
var BTOpenZone_UserName = credential.username();
var BTOpenZone_Password = credential.password();
// populate fields and submit form
if (ie.hasField("username") && ie.hasField("password"))
{
"detected BT Openzone login Form".info();
"submitting login details".debug();
ie.field("username").value(BTOpenZone_UserName);
ie.field("password").value(BTOpenZone_Password);
ie.buttons()[0].click();
if (ie.hasField("username"))
{
"Login failed, Aborting worklow".error();
return this;
}
ie.open(defaultWebPage);
if (ie.title("Google").isFalse())
{
"Expected Google page did not load".error();
}
}
}
return this;
}
public API_BTOpenZone logout()
{
// logout sequence
"logging out".debug();
var logoutPage = "https://www.btopenzone.com:8443/accountLogoff/home";
var logoutConfirmedPage = "https://www.btopenzone.com:8443/accountLogoff/home?confirmed=true";
ie.open(logoutPage);
ie.open(logoutConfirmedPage);
ie.title().info();
if (ie.hasField("username"))
"logout ok".info();
else
"logout failed".info();
return this;
}
public API_BTOpenZone closeInNSeconds(int seconds)
{
ie.closeInNSeconds(seconds);
return this;
}
public API_BTOpenZone waitNSeconds(int seconds)
{
ie.waitNSeconds(seconds);
return this;
}
}
}
file:ascx_AskUserForLoginDetails.cs
// This file is part of the OWASP O2 Platform (http://www.owasp.org/index.php/OWASP_O2_Platform) and is released under the Apache 2.0 License (http://www.apache.org/licenses/LICENSE-2.0)
using System;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using System.Text;
using O2.Kernel;
using O2.Kernel.ExtensionMethods;
using O2.DotNetWrappers.ExtensionMethods;
using O2.Views.ASCX.ExtensionMethods;
using O2.Views.ASCX.classes.MainGUI;
using O2.External.IE.ExtensionMethods;
using O2.XRules.Database._Rules._Interfaces;
//O2File:C:\O2\_XRules_Local\Extra_methods.cs
namespace O2.Script
{
public class ascx_AskUserForLoginDetails : ContainerControl
{
public string UserName { get;set;}
public string Password { get;set;}
public AutoResetEvent HaveAnswer { get;set;}
public TextBox UserNameTextBox { get; set;}
public TextBox PasswordTextBox { get; set;}
public Button OKButton { get; set;}
public static ICredential ask()
{
var loginDetailsGui = O2Gui.open<ascx_AskUserForLoginDetails>("Enter Login Details", 250,115);
loginDetailsGui.buildGui();
var credential = loginDetailsGui.getAnswer();
loginDetailsGui.close();
return credential;
}
public ascx_AskUserForLoginDetails()
{
HaveAnswer = new AutoResetEvent(false);
UserName = "";
Password = "";
}
public void buildGui()
{
UserNameTextBox = this.add_Label("Username:",10,0)
.append_TextBox("");
UserNameTextBox.onTextChange((text)=> UserName = text)
.align_Right(this);
PasswordTextBox = this.add_Label("Password: ",35,0)
.append_TextBox("")
.isPasswordField();
PasswordTextBox.onTextChange((text)=> Password = text)
.align_Right(this);
var OKButton = this.add_Button("OK", 60,0);
OKButton.onClick(answerAvailable)
.left(this.width() - OKButton.width() - 1)
.anchor_BottomRight();
this.parentForm().Closed += (sender,e) => answerAvailable();
}
public ICredential getAnswer()
{
HaveAnswer.WaitOne();
var credential = new Credential();
credential.UserName = UserName;
credential.Password = Password;
"u:{0}".debug(credential.UserName);
"p:{0}".debug(credential.Password);
return credential;
}
public void answerAvailable()
{
HaveAnswer.Set();
}
public void close()
{
if (this.parentForm() != null)
this.parentForm().Close();
}
}
}
file:ascx_CaptchaQuestion.cs
// This file is part of the OWASP O2 Platform (http://www.owasp.org/index.php/OWASP_O2_Platform) and is released under the Apache 2.0 License (http://www.apache.org/licenses/LICENSE-2.0)
using System;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using System.Text;
using O2.Kernel;
using O2.Kernel.ExtensionMethods;
using O2.DotNetWrappers.ExtensionMethods;
using O2.Views.ASCX.ExtensionMethods;
using O2.Views.ASCX.classes.MainGUI;
using O2.External.IE.ExtensionMethods;
namespace O2.Script
{
public class ascx_CaptchaQuestion : ContainerControl
{
public string CaptchaQuestionUrl { get;set;}
public string CaptchaAnswer { get;set;}
public AutoResetEvent HaveAnswer { get;set;}
public TextBox AnswerTextBox { get; set;}
public ascx_CaptchaQuestion()
{
HaveAnswer = new AutoResetEvent(false);
CaptchaAnswer = "";
}
public void buildGui(string captchaQuestionUrl)
{
CaptchaQuestionUrl = captchaQuestionUrl;
var browser = this.add_Browser();
var panelAbove = browser.insert_Above<Panel>(25);
var label = panelAbove.add_Label("What does the CAPTCHA say?").top(6);
var button = panelAbove.add_Button("Submit");
button.left(panelAbove.width()-button.width())
.anchor_TopRight();
AnswerTextBox = label.append_TextBox("");
AnswerTextBox.onTextChange((text)=>CaptchaAnswer = text)
.onEnter((text) => answerAvailable())
.width(panelAbove.width() -button.width() - label.width() - 10)
.anchor_TopLeftRight()
.top(2);
button.onClick(() => answerAvailable());
browser.open(CaptchaQuestionUrl);
this.parentForm().Closed += (sender,e) => answerAvailable();
}
public string getAnswer()
{
HaveAnswer.WaitOne();
return AnswerTextBox.get_Text();
}
public void answerAvailable()
{
"answerAvailable".debug();
HaveAnswer.Set();
}
public void close()
{
if (this.parentForm() != null)
this.parentForm().Close();
}
public static string askQuestion(string captchaQuestionUrl)
{
var captchaQuestion = O2Gui.open<ascx_CaptchaQuestion>("Answer CAPTCHA Question", 400,200);
captchaQuestion.buildGui(captchaQuestionUrl);
var captchaAnswer = captchaQuestion.getAnswer();
captchaQuestion.close();
return captchaAnswer;
}
}
}
file:Extra Extension methods
public static class ExtraMethods2
{
// Controls Extension methods
public static T controls<T>(this Control control)
where T : Control
{
foreach(var childControl in control.controls())
if (childControl is T)
return (T)childControl;
return null;
}
//TreeView
public static TextBox isPasswordField(this TextBox textBox)
{
textBox.invokeOnThread(()=> textBox.PasswordChar = '*');
return textBox;
}
// ascx_TableList Extension Methods
public static ascx_TableList clearTable(this ascx_TableList tableList)
{
var listViewControl = tableList.getListViewControl();
listViewControl.invokeOnThread(()=> listViewControl.Clear());
return tableList;
}
public static ascx_TableList add_Column(this ascx_TableList tableList, string columnName)
{
var listViewControl = tableList.getListViewControl();
listViewControl.invokeOnThread(()=> listViewControl.Columns.Add(columnName));
return tableList;
}
public static ascx_TableList add_Columns(this ascx_TableList tableList, params string[] columnsName)
{
tableList.add_Columns(columnsName.toList());
return tableList;
}
public static ascx_TableList add_Row(this ascx_TableList tableList, params string[] cellValues)
{
tableList.add_Row(cellValues.toList());
return tableList;
}
// SecretData extensionMethods O2.XRules.Database._Rules._Interfaces;
public static ICredential credential(this string fileWithSecretData, string credentialType)
{
if (fileWithSecretData.fileExists())
{
var secretData = fileWithSecretData.deserialize<SecretData>();
return secretData.credential(credentialType);
}
return null;
}
public static ICredential credential(this SecretData secretData, string credentialType)
{
if (secretData!= null)
{
var credentials = secretData.credentialTypes(credentialType);
if (credentials != null && credentials.size() > 0)
return credentials[0];
}
return null;
}
public static string username(this ICredential credential)
{
return credential.UserName;
}
public static string password(this ICredential credential)
{
return credential.Password;
}
}
file:WatiN_IE
// This file is part of the OWASP O2 Platform (http://www.owasp.org/index.php/OWASP_O2_Platform) and is released under the Apache 2.0 License (http://www.apache.org/licenses/LICENSE-2.0)
using System;
using System.IO;
using System.Drawing;
using System.Threading;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Reflection;
using System.Text;
using O2.Interfaces.O2Core;
using O2.Kernel;
using O2.Kernel.ExtensionMethods;
using O2.DotNetWrappers.ExtensionMethods;
using O2.DotNetWrappers.Windows;
using O2.DotNetWrappers.DotNet;
using O2.Views.ASCX;
using O2.External.SharpDevelop.AST;
using O2.External.SharpDevelop.ExtensionMethods;
using WatiN.Core;
using SHDocVw;
//O2Ref:WatiN.Core.1x.dll
//O2Ref:Interop.SHDocVw.dll
//O2File:C:\O2\_XRules_Local\Extra_methods.cs
namespace O2.Script
{
public class WatiN_IE
{
public Thread IEThread { get; set; }
public IE IE { get; set; }
public SHDocVw.InternetExplorerClass InternetExplorer { get; set; }
public AutoResetEvent WaitForIELaunch {get;set;}
public AutoResetEvent WaitForIEClose {get;set;}
public int maxExecutionWaitTime = 5000;
public WatiN_IE()
{
WaitForIEClose = new AutoResetEvent(false);
WaitForIELaunch = new AutoResetEvent(false);
}
public WatiN_IE(InternetExplorer ieInstanceToAttach) : this()
{
attachTo(ieInstanceToAttach);
}
public WatiN_IE(System.Windows.Forms.WebBrowser webBrowser) : this()
{
attachTo(webBrowser);
}
public WatiN_IE createIEObject()
{
return createIEObject("about:blank");
}
public WatiN_IE createIEObject(string url)
{
return createIEObject(url, 0,0,500,500);
}
public WatiN_IE attachTo(System.Windows.Forms.WebBrowser webBrowser)
{
// need to do this or the attach is not going to work
WatiN.Core.Settings.AutoStartDialogWatcher = false;
attachTo(webBrowser.ActiveXInstance as InternetExplorer);
return this;
}
public WatiN_IE attachTo(InternetExplorer ieInstanceToAttach)
{
IEThread = O2Thread.staThread(
()=>{
try
{
//WatiN.Core.Settings.AutoStartDialogWatcher = false;
WaitForIEClose.Reset();
"attaching to IE with LocationName '{0}'".info(ieInstanceToAttach.LocationName);
IE = new IE(ieInstanceToAttach);
//this doesn't work on external processes attach
//InternetExplorer = (SHDocVw.InternetExplorerClass)IE.InternetExplorer;
(ieInstanceToAttach as DWebBrowserEvents2_Event).OnQuit +=
()=>
{
//"ON WatiN_IE attachTo Quit EVENT".debug();
close();
};
WaitForIELaunch.Set();
WaitForIEClose.WaitOne();
//"AFTER WaitForIEClose".error();
}
catch(Exception ex)
{
ex.log("in attachTo(InternetExplorer)",true);
WaitForIELaunch.Set();
WaitForIEClose.Set();
}
});
//"before WaitForIELaunch".error();
WaitForIELaunch.WaitOne();
//"after WaitForIELaunch".error();
return this;
}
public WatiN_IE createIEObject(string url, int top, int left, int width, int height)
{
IEThread = O2Thread.staThread(
()=>{
"launching a new WatIN InternetExplorer Process".info();
try
{
if (url.valid().isFalse())
url = "about:blank";
Settings.MakeNewIeInstanceVisible = false;
IE = new IE(url);
InternetExplorer = (SHDocVw.InternetExplorerClass)IE.InternetExplorer;
InternetExplorer.Top= top;
InternetExplorer.Left= left;
InternetExplorer.Width= width;
InternetExplorer.Height= height;
InternetExplorer.Visible = true;
InternetExplorer.OnQuit += close;
WaitForIELaunch.Set();
WaitForIEClose.WaitOne();
}
catch(Exception ex)
{
ex.log("in WatiN_IE createIEObject");
WaitForIELaunch.Set();
WaitForIEClose.Set();
}
});
WaitForIELaunch.WaitOne();
return this;
}
public void close()
{
"closing WatiN_IE".info();
try
{
IE.Close();
}
catch(Exception ex)
{
ex.log("in WatiN_IE.close");
}
detach();
}
public void detach()
{
WaitForIEClose.Set();
}
public WatiN_IE execute(MethodInvoker callback)
{
var executionComplete = new AutoResetEvent(false);
IEThread.invoke(
()=>{
try
{
callback();
}
catch(Exception ex)
{
ex.log("in WatiN_IE execute");
}
executionComplete.Set();
});
if (executionComplete.WaitOne(maxExecutionWaitTime).isFalse())
"in WatiN_IE executeOnThread, maxExecutionWaitTime ({0} ms} was reached for action".error(maxExecutionWaitTime);
return this;
}
public T execute<T>(Func<T> callback)
{
object returnData = null;
var executionComplete = new AutoResetEvent(false);
IEThread.invoke(
()=>{
try
{
returnData = callback();
}
catch(Exception ex)
{
ex.log("in WatiN_IE execute<T>");
}
executionComplete.Set();
});
if (executionComplete.WaitOne(maxExecutionWaitTime).isFalse())
"in WatiN_IE executeOnThread, maxExecutionWaitTime ({0} ms} was reached for action".error(maxExecutionWaitTime);
if (returnData is T)
return (T)returnData;
return default(T);
}
public static List<InternetExplorer> ieInstances()
{
var ieInstances = new List<InternetExplorer>();
ShellWindows shellWindows = new ShellWindowsClass();
for(int i = 0 ; i < shellWindows.Count ; i++)
{
if (shellWindows.Item(i) is InternetExplorer)
{
var instance = (InternetExplorer)shellWindows.Item(i);
//make sure it is a browser instance (since we don't want the Windows Explorer cases)
if(instance.FullName.contains("IEXPLORE.EXE"))
ieInstances.Add(instance);
}
}
return ieInstances;
}
public static WatiN_IE attachTo(string locationName)
{
var ieInstance = ieInstances().locationName(locationName);
if (ieInstance!= null)
{
return new WatiN_IE(ieInstance);
}
"in WatiN_IE attachTo(...) could not find an instance of IE to attach with locationName='{0}'".error(locationName);
return null;
}
public static void stopAllIEProcesses()
{
Processes.getProcessesCalled("iexplore").stop();
}
}
public static class InternetExplorer_ExtensionMethods
{
// InternetExplorer extension methods
public static InternetExplorer locationName(this List<InternetExplorer> ieInstances, string locationToMatch)
{
if (ieInstances.size() >0 )
{
foreach(var instance in ieInstances)
if (instance.LocationName == locationToMatch)
return instance;
"in I InternetExplorer locationName(...) it was not possible to find an IE instance with LocationName == '{0}'".debug(locationToMatch);
}
return null;
}
public static List<string> locationNames(this List<InternetExplorer> ieInstances)
{
return (from instance in ieInstances
select instance.LocationName).toList();
}
public static List<string> locationUrls(this List<InternetExplorer> ieInstances)
{
return (from instance in ieInstances
select instance.LocationURL).toList();
}
}
}
file:WatiN_IE_ExtensionMethods
// This file is part of the OWASP O2 Platform (http://www.owasp.org/index.php/OWASP_O2_Platform) and is released under the Apache 2.0 License (http://www.apache.org/licenses/LICENSE-2.0)
using System;
using System.IO;
using System.Drawing;
using System.Threading;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Reflection;
using System.Text;
using O2.Interfaces.O2Core;
using O2.Kernel;
using O2.Kernel.ExtensionMethods;
using O2.DotNetWrappers.ExtensionMethods;
using O2.DotNetWrappers.Windows;
using O2.DotNetWrappers.DotNet;
using O2.Views.ASCX;
using O2.Views.ASCX.classes.MainGUI;
using O2.External.SharpDevelop.AST;
using O2.External.SharpDevelop.ExtensionMethods;
using O2.XRules.Database._Rules._Interfaces;
using WatiN.Core;
using SHDocVw;
//O2Ref:WatiN.Core.1x.dll
//O2Ref:Interop.SHDocVw.dll
//O2File:C:\O2\_XRules_Local\WatiN_IE.cs
//O2File:C:\O2\_XRules_Local\ascx_CaptchaQuestion.cs
//O2File:C:\O2\_XRules_Local\ascx_AskUserForLoginDetails.cs
namespace O2.Script
{
public static class WatiN_IE_ExtensionMethods
{
//WatIN ExtensionMethods
public static WatiN_IE ie(this string url)
{
int top = 0;
int left = 900;
return url.ie(top, left);
}
public static WatiN_IE ie(this string url, int top, int left)
{
int width = 385;
int height = 500;
return url.ie(top, left, width, height);
}
public static WatiN_IE ie(this string url, int top, int left, int width, int height)
{
var ie = new WatiN_IE();
ie.createIEObject(url, top, left, width, height);
return ie;
}
public static WatiN_IE ie(this O2.External.IE.Wrapper.O2BrowserIE o2BrowserIE)
{
return (o2BrowserIE as System.Windows.Forms.WebBrowser).ie();
}
public static WatiN_IE ie(this System.Windows.Forms.WebBrowser webBrowser)
{
return new WatiN_IE(webBrowser);
}
// uri & url
public static Uri uri(this WatiN_IE watinIe)
{
return watinIe.IE.Uri;
}
public static string url(this WatiN_IE watinIe)
{
return watinIe.uri().str();
}
public static string title(this WatiN_IE watinIe)
{
return watinIe.IE.Title;
}
public static bool title(this WatiN_IE watinIe, string title)
{
return watinIe.IE.Title == title;
}
public static string processId(this WatiN_IE watinIe)
{
return watinIe.IE.ProcessID.str();
}
// region close
public static WatiN_IE close(this WatiN_IE watinIe)
{
"closing WatIN InternetExplorer Process".info();
watinIe.close();
//watinIe.Close();
return watinIe;
}
public static WatiN_IE closeInNSeconds(this WatiN_IE watinIe, int seconds)
{
if (seconds > 60)
{
"in WatiN_IE closeInNSeconds, provided value bigger than 60 secs, so changing the delay (before close) to 60".error();
seconds = 60;
}
"IE instance will be closed in {0} seconds".info(seconds);
O2Thread.mtaThread(
()=>{
watinIe.wait(5000);
watinIe.close();
});
return watinIe;
}
// internet explorer
public static InternetExplorerClass internetExplorer(this WatiN_IE watinIe)
{
return watinIe.InternetExplorer;
}
public static WatiN_IE open(this WatiN_IE watinIe, string url)
{
return watinIe.open(url,0);
}
public static WatiN_IE open(this WatiN_IE watinIe, string url, int miliseconds)
{
"[WatIN] open: {0}".info(url);
watinIe.execute(
()=>{
watinIe.IE.GoTo(url);
watinIe.wait(miliseconds);
});
return watinIe;
}
public static WatiN_IE wait(this WatiN_IE watinIe)
{
return watinIe.wait(1000);
}
public static WatiN_IE wait(this WatiN_IE watinIe, int miliseconds)
{
if (miliseconds > 0)
watinIe.sleep(miliseconds);
return watinIe;
}
public static WatiN_IE waitNSeconds(this WatiN_IE watinIe, int seconds)
{
if (seconds > 0)
watinIe.sleep(seconds* 1000);
return watinIe;
}
public static T wait<T>(this T element, int miliseconds)
where T : Element
{
if (miliseconds > 0)
element.sleep(miliseconds);
return element;
}
/*
public static WatiN_IE linkClick(this WatiN_IE watinIe, string findByUrl, int miliseconds)
{
watinIe.execute(
()=>{
var link = watinIe.link(Find.ByUrl(findByUrl));
link.click(miliseconds);
});
return watinIe;
}
public static Link link(this WatiN_IE watinIe, AttributeConstraint attributeConstraint)
{
return watinIe.link(attributeConstraint, 0);
}
public static Link link(this WatiN_IE watinIe, AttributeConstraint attributeConstraint, int miliseconds)
{
Link link = null;
watinIe.execute(
()=>{
link = watinIe.IE.Link(attributeConstraint);
watinIe.wait(miliseconds);
});
return link;
}
*/
// WatiN Image Extension Methods
public static WatiN.Core.Image image(this WatiN_IE watinIe, string name)
{
foreach(var image in watinIe.images())
if (image.id() == name)//|| link.text() == name)
return image;
"in WatiN_IE could not find Image with name:{0}".error(name ?? "[null value]");
return null;
}
public static List<WatiN.Core.Image> images(this WatiN_IE watinIe)
{
return (from image in watinIe.IE.Images
select image).toList();
}
public static Uri uri(this WatiN.Core.Image image)
{
return (image != null)
? image.Uri
: null;
}
public static string url(this WatiN.Core.Image image)
{
return (image != null)
? image.Uri.str()
: "";
}
public static string src(this WatiN.Core.Image image)
{
return (image != null)
? image.Src
: "";
}
// WatiN Link Extension methods
public static Link link(this WatiN_IE watinIe, string name)
{
foreach(var link in watinIe.links())
if (link.id() == name || link.text() == name)
return link;
"in WatiN_IE could not find Link with name:{0}".error(name ?? "[null value]");
return null;
}
public static List<Link> links(this WatiN_IE watinIe)
{
return (from link in watinIe.IE.Links
select link).toList();
}
public static string url(this Link link)
{
return (link != null)
? link.Url
: "";
}
public static Link click(this Link link)
{
return link.click(0);
}
public static Link click(this Link link, int miliseconds)
{
if (link != null)
{
link.Click();
link.wait(miliseconds);
}
return link;
}
public static List<string> texts(this List<Link> links)
{
return (from link in links
select link.text()).toList();
}
public static List<string> urls(this List<Link> links)
{
return (from link in links
select link.url()).toList();
}
// WatiN Button Extension methods
public static WatiN.Core.Button button(this WatiN_IE watinIe, string name)
{
foreach(var button in watinIe.buttons())
if (button.id() == name || button.value() == name)
return button;
"in WatiN_IE could not find Button with name:{0}".error(name ?? "[null value]");
return null;
}
public static List< WatiN.Core.Button> buttons(this WatiN_IE watinIe)
{
return (from button in watinIe.IE.Buttons
select button).toList();
}
public static List<string> texts(this List<WatiN.Core.Button> buttons)
{
return (from button in buttons
select button.text()).toList();
}
public static List<string> values(this List<WatiN.Core.Button> buttons)
{
return (from button in buttons
select button.value()).toList();
}
public static List<string> ids(this List<WatiN.Core.Button> buttons)
{
return (from button in buttons
select button.id()).toList();
}
public static List<string> names(this List<WatiN.Core.Button> buttons)
{
return buttons.ids();
}
public static string value(this WatiN.Core.Button button)
{
return (button != null)
? button.Value
: "";
}
public static WatiN.Core.Button click(this WatiN.Core.Button button)
{
if (button != null)
button.Click();
return button;
}
// WatiN SelectLists Extension methods
public static SelectList selectList(this WatiN_IE watinIe, string name)
{
//watinIe.textFields(); // after some events
foreach(var selectList in watinIe.selectLists())
if (selectList.id() == name) // || checkBox.title() == name)
return selectList;
"in WatiN_IE could not find SelectList with name:{0}".error(name ?? "[null value]");
return null;
}
public static List<SelectList> selectLists(this WatiN_IE watinIe)
{
return (from selectList in watinIe.IE.SelectLists
select selectList).toList();
}
public static string id(this SelectList selectList)
{
return (selectList != null)
? selectList.Id
: "";
}
public static List<string> ids(this List<SelectList> selectLists)
{
return (from selectList in selectLists
select selectList.id()).toList();
}
public static List<Option> options(this SelectList selectList)
{
return (from option in selectList.Options
select option).toList();
}
public static Option select(this Option option)
{
try
{
if (option != null)
option.Select();
}
catch(Exception ex)
{
ex.log("in Option select");
}
return option;
}
public static SelectList select(this SelectList selectList, int index)
{
var options = selectList.options();
if (index < options.size())
options[index].select();
return selectList;
}
// WatiN CheckBox Extension methods
public static WatiN.Core.CheckBox checkBox(this WatiN_IE watinIe, string name)
{
//watinIe.textFields(); // after some events
foreach(var checkBox in watinIe.checkBoxes())
if (checkBox.id() == name) // || checkBox.title() == name)
return checkBox;
"in WatiN_IE could not find CheckBox with name:{0}".error(name ?? "[null value]");
return null;
}
public static List<WatiN.Core.CheckBox> checkBoxes(this WatiN_IE watinIe)
{
return (from checkBox in watinIe.IE.CheckBoxes
select checkBox).toList();
}
public static string id(this WatiN.Core.CheckBox checkBox)
{
return (checkBox != null)
? checkBox.Id
: "";
}
public static List<string> ids(this List<WatiN.Core.CheckBox> checkBoxes)
{
return (from checkBox in checkBoxes
select checkBox.id()).toList();
}
public static bool value(this WatiN.Core.CheckBox checkBox)
{
return (checkBox != null)
? checkBox.Checked
: false;
}
public static List<bool> values(this List<WatiN.Core.CheckBox> checkBoxes)
{
return (from checkBox in checkBoxes
select checkBox.value()).toList();
}
public static WatiN.Core.CheckBox value(this WatiN.Core.CheckBox checkBox, bool value)
{
if (checkBox!= null)
try
{
checkBox.Checked = value;
}
catch(Exception ex)
{
ex.log("in WatiN.Core.CheckBox value");
}
return checkBox;
}
public static WatiN.Core.CheckBox check(this WatiN.Core.CheckBox checkBox)
{
return checkBox.value(true);
}
public static WatiN.Core.CheckBox uncheck(this WatiN.Core.CheckBox checkBox)
{
return checkBox.value(false);
}
// WatiN TextField Extension methods
public static TextField field(this WatiN_IE watinIe, string name)
{
return watinIe.textField(name);
}
public static List<TextField> fields(this WatiN_IE watinIe)
{
return watinIe.textFields();
}
public static bool hasField(this WatiN_IE watinIe, string name)
{
return watinIe.textFieldExists(name);
}
public static TextField textField(this WatiN_IE watinIe, string name)
{
//watinIe.textFields(); // after some events
foreach(var textField in watinIe.textFields())
if (textField.name() == name || textField.title() == name)
return textField;
"in WatiN_IE could not find TextField with name:{0}".error(name ?? "[null value]");
return null;
}
public static bool textFieldExists(this WatiN_IE watinIe, string name)
{
foreach(var textField in watinIe.textFields())
if (textField.name() == name || textField.title() == name)
return true;
return false;
}
public static List<TextField> textFields(this WatiN_IE watinIe)
{
return (from textField in watinIe.IE.TextFields
select textField).toList();
}
public static string name(this TextField textField)
{
return (textField != null)
? textField.Name
: "";
}
public static List<string> names(this List<TextField> textFields)
{
return (from textField in textFields
select textField.name()).toList();
}
public static string value(this TextField textField)
{
return (textField != null)
? textField.Value
: "";
}
public static List<string> values(this List<TextField> textFields)
{
return (from textField in textFields
select textField.value()).toList();
}
public static TextField value(this TextField textField, string value)
{
if (textField!= null)
textField.Value = value;
return textField;
}
// WatiN Forms Extension methods
public static List<WatiN.Core.Form> forms(this WatiN_IE watinIe)
{
return (from form in watinIe.IE.Forms
select form).toList();
}
// WatiN Elemetns Extension methods
public static List<Element> elements(this WatiN_IE watinIe, string tagName)
{
return (from element in watinIe.IE.Elements
where element.TagName == tagName
select element).toList();
}
public static List<Element> elements(this WatiN_IE watinIe)
{
return (from element in watinIe.IE.Elements
select element).toList();
}
public static List<string> tagNames(this List<Element> elements)
{
return (from element in elements
select element.TagName).Distinct().toList();
}
public static Dictionary<string, List<Element>> indexedByTagName(this List<Element> elements)
{
var result = new Dictionary<string,List<Element>>();
foreach(var element in elements)
result.add(element.TagName, element);
return result;
}
public static string tagName(this Element element)
{
return element.TagName;
}
// WatiN Divs Extension methods
public static Div div(this WatiN_IE watinIe, string id)
{
foreach(var div in watinIe.divs())
if (div.Id != null && div.Id == id)
return div;
return null;
}
public static List<Div> divs(this WatiN_IE watinIe)
{
return (from div in watinIe.IE.Divs
select div).toList();
}
public static List<string> ids(this List<Div> divs)
{
return (from div in divs
where div.Id != null
select div.Id).toList();
}
// WatiN Element Extension methods
public static string id(this Element element)
{
return (element != null)
? element.Id
: "";
}
public static string text(this Element element)
{
return (element != null)
? element.Text
: "";
}
public static string title(this Element element)
{
return (element != null)
? element.Title
: "";
}
public static string innerHtml(this Element element)
{
return (element != null)
? element.InnerHtml
: "";
}
public static string outerHtml(this Element element)
{
return (element != null)
? element.OuterHtml
: "";
}
public static string html(this Element element)
{
return element.outerHtml();
}
// Captcha Extension methods
public static string resolveCaptcha(this WatiN_IE watinIe, string captchaImageUrl)
{
return ascx_CaptchaQuestion.askQuestion(captchaImageUrl);
}
public static string resolveCaptcha(this WatiN_IE watinIe, TextField textField)
{
return watinIe.resolveCaptcha(textField.value());
}
public static WatiN_IE resolveCaptcha(this WatiN_IE watinIe, string questionField, string answerField)
{
var questionUrl = watinIe.textField(questionField).value();
if (questionUrl.valid())
{
var captchaAnswer = watinIe.resolveCaptcha(questionUrl);
watinIe.textField(answerField).value(captchaAnswer);
}
return watinIe;
}
public static string askUserQuestion(this WatiN_IE watinIe, string question, string title, string defaultValue)
{
var assembly = "Microsoft.VisualBasic".assembly();
var intercation = assembly.type("Interaction");
var parameters = new object[] {question,title,defaultValue,-1,-1};
return intercation.invokeStatic("InputBox",parameters).str();
}
// user interaction
public static WatiN_IE askUserToContinue(this WatiN_IE watinIe)
{
MessageBox.Show("Click OK to Continue the WatiN IE workflow", "O2 Message",MessageBoxButtons.OK, MessageBoxIcon.Question);
return watinIe;
}
public static ICredential askUserForUsernameAndPassword(this WatiN_IE watinIe)
{
return watinIe.askUserForUsernameAndPassword("");
}
public static ICredential askUserForUsernameAndPassword(this WatiN_IE watinIe, string loginType)
{
var credential = ascx_AskUserForLoginDetails.ask();
if (loginType.valid())
credential.CredentialType = loginType;
return credential;
}
// TreeView helper
public static Panel showElementsInTreeView(this WatiN_IE watinIe)
{
var hostPanel = O2Gui.open<Panel>("WatiN element details",400,400);
var controls = hostPanel.add_1x1("Html elements", "Propeties");
var propertyGrid = controls[1].add_PropertyGrid();
controls[0].add_TreeView()
.add_Nodes(watinIe.elements().indexedByTagName())
.sort()
.showSelection()
.beforeExpand<List<Element>>(
(treeNode, elements) =>
{
try { treeNode.add_Nodes(elements);}
catch(Exception ex) { ex.log("in beforeExpand<List<Element>>");}
})
.afterSelect<Element>((element)=> propertyGrid.show(element))
.afterSelect<List<Element>>((elements)=> propertyGrid.show(elements[0]));
return hostPanel;
}
// Control Extensionmethods
public static WatiN_IE add_IE(this Control control)
{
var browser = control.add_Control<System.Windows.Forms.WebBrowser>();
return browser.ie();
}
}
}
|  |