Here is a quick and dirty solution. It is not 100% reliable, but it does not block the user interface flow, and it should be satisfactory for the WebBrowser control prototype. Automation Procedures:
private async void testButton_Click(object sender, EventArgs e) { await Task.Factory.StartNew( () => { stepTheWeb(() => wb.Navigate("www.yahoo.com")); stepTheWeb(() => wb.Navigate("www.microsoft.com")); stepTheWeb(() => wb.Navigate("asp.net")); stepTheWeb(() => wb.Document.InvokeScript("eval", new[] { "$('p').css('background-color','yellow')" })); bool testFlag = false; stepTheWeb(() => testFlag = wb.DocumentText.Contains("Get Started")); if (testFlag) { }
Here is a slightly more general version of the testButton_Click method:
private async void testButton_Click(object sender, EventArgs e) { var actions = new List<Action>() { () => wb.Navigate("www.yahoo.com"), () => wb.Navigate("www.microsoft.com"), () => wb.Navigate("asp.net"), () => wb.Document.InvokeScript("eval", new[] { "$('p').css('background-color','yellow')" }), () => { bool testFlag = false; testFlag = wb.DocumentText.Contains("Get Started"); if (testFlag) { } }
[Update]
I adapted my “quick and dirty” sample, borrowing and sligthly refactoring the @Noseratio NavigateAsync method from this section . The new version of the code automated / performed asynchronously in the context of the user interface stream not only navigation operations, but also Javascript / AJAX calls - any methods for implementing the "lamda" task / one automation step.
All comments and code comments are very welcome. Especially from @Noseratio . Together we will make this world a better place;)
public enum ActionTypeEnumeration { Navigation = 1, Javascript = 2, UIThreadDependent = 3, UNDEFINED = 99 } public class ActionDescriptor { public Action Action { get; set; } public ActionTypeEnumeration ActionType { get; set; } } /// <summary> /// Executes a set of WebBrowser control Automation actions /// </summary> /// <remarks> /// Test form shoudl ahve the following controls: /// webBrowser1 - WebBrowser, /// testbutton - Button, /// testCheckBox - CheckBox, /// totalHtmlLengthTextBox - TextBox /// </remarks> private async void testButton_Click(object sender, EventArgs e) { try { var cts = new CancellationTokenSource(60000); var actions = new List<ActionDescriptor>() { new ActionDescriptor() { Action = ()=> wb.Navigate("www.yahoo.com"), ActionType = ActionTypeEnumeration.Navigation} , new ActionDescriptor() { Action = () => wb.Navigate("www.microsoft.com"), ActionType = ActionTypeEnumeration.Navigation} , new ActionDescriptor() { Action = () => wb.Navigate("asp.net"), ActionType = ActionTypeEnumeration.Navigation} , new ActionDescriptor() { Action = () => wb.Document.InvokeScript("eval", new[] { "$('p').css('background-color','yellow')" }), ActionType = ActionTypeEnumeration.Javascript}, new ActionDescriptor() { Action = () => { testCheckBox.Checked = wb.DocumentText.Contains("Get Started"); }, ActionType = ActionTypeEnumeration.UIThreadDependent}