public void Cancel() { using var sleepHelper = new SleepHelper(); var signal = new ManualResetEventSlim(); var listener = new AsynchronousOperationListener(); var done = false; var continued = false; var asyncToken1 = listener.BeginAsyncOperation("Test"); var task = new Task(() => { signal.Set(); sleepHelper.Sleep(TimeSpan.FromMilliseconds(500)); var asyncToken2 = listener.BeginAsyncOperation("Test"); var queuedTask = new Task(() => { sleepHelper.Sleep(TimeSpan.FromSeconds(5)); continued = true; }); asyncToken2.Dispose(); queuedTask.Start(TaskScheduler.Default); done = true; }); task.CompletesAsyncOperation(asyncToken1); task.Start(TaskScheduler.Default); Wait(listener, signal); Assert.True(done, "Cancelling should have completed the current task."); Assert.False(continued, "Continued Task when it shouldn't have."); }
public void Nested() { using var sleepHelper = new SleepHelper(); var signal = new ManualResetEventSlim(); var listener = new AsynchronousOperationListener(); var outerDone = false; var innerDone = false; var asyncToken1 = listener.BeginAsyncOperation("Test"); var task = new Task(() => { signal.Set(); sleepHelper.Sleep(TimeSpan.FromMilliseconds(500)); using (listener.BeginAsyncOperation("Test")) { sleepHelper.Sleep(TimeSpan.FromMilliseconds(500)); innerDone = true; } sleepHelper.Sleep(TimeSpan.FromMilliseconds(500)); outerDone = true; }); task.CompletesAsyncOperation(asyncToken1); task.Start(TaskScheduler.Default); Wait(listener, signal); Assert.True(innerDone, "Should have completed the inner task"); Assert.True(outerDone, "Should have completed the outer task"); }
public void QueuedOperation() { using var sleepHelper = new SleepHelper(); var signal = new ManualResetEventSlim(); var listener = new AsynchronousOperationListener(); var done = false; var asyncToken1 = listener.BeginAsyncOperation("Test"); var task = new Task(() => { signal.Set(); sleepHelper.Sleep(TimeSpan.FromMilliseconds(500)); var asyncToken2 = listener.BeginAsyncOperation("Test"); var queuedTask = new Task(() => { sleepHelper.Sleep(TimeSpan.FromMilliseconds(500)); done = true; }); queuedTask.CompletesAsyncOperation(asyncToken2); queuedTask.Start(TaskScheduler.Default); }); task.CompletesAsyncOperation(asyncToken1); task.Start(TaskScheduler.Default); Wait(listener, signal); Assert.True(done, "Should have waited for the queued operation to finish!"); }
public void MultipleEnqueues() { using var sleepHelper = new SleepHelper(); var signal = new ManualResetEventSlim(); var listener = new AsynchronousOperationListener(); var outerDone = false; var firstQueuedDone = false; var secondQueuedDone = false; var asyncToken1 = listener.BeginAsyncOperation("Test"); var task = new Task( () => { signal.Set(); sleepHelper.Sleep(TimeSpan.FromMilliseconds(500)); var asyncToken2 = listener.BeginAsyncOperation("Test"); var firstQueueTask = new Task( () => { sleepHelper.Sleep(TimeSpan.FromMilliseconds(500)); var asyncToken3 = listener.BeginAsyncOperation("Test"); var secondQueueTask = new Task( () => { sleepHelper.Sleep(TimeSpan.FromMilliseconds(500)); secondQueuedDone = true; } ); secondQueueTask.CompletesAsyncOperation(asyncToken3); secondQueueTask.Start(TaskScheduler.Default); firstQueuedDone = true; } ); firstQueueTask.CompletesAsyncOperation(asyncToken2); firstQueueTask.Start(TaskScheduler.Default); outerDone = true; } ); task.CompletesAsyncOperation(asyncToken1); task.Start(TaskScheduler.Default); Wait(listener, signal); Assert.True(outerDone, "The outer task should have finished!"); Assert.True(firstQueuedDone, "The first queued task should have finished"); Assert.True(secondQueuedDone, "The second queued task should have finished"); }
public void IgnoredCancel() { using (var sleepHelper = new SleepHelper()) { var signal = new ManualResetEventSlim(); var listener = new AsynchronousOperationListener(); var done = false; var queuedFinished = false; var cancelledFinished = false; var asyncToken1 = listener.BeginAsyncOperation("Test"); var task = new Task(() => { using (listener.BeginAsyncOperation("Test")) { var cancelledTask = new Task(() => { sleepHelper.Sleep(TimeSpan.FromSeconds(10)); cancelledFinished = true; }); signal.Set(); cancelledTask.Start(TaskScheduler.Default); } sleepHelper.Sleep(TimeSpan.FromMilliseconds(500)); // Now that we've cancelled the first request, queue another one to make sure we wait for it. var asyncToken2 = listener.BeginAsyncOperation("Test"); var queuedTask = new Task(() => { sleepHelper.Sleep(TimeSpan.FromSeconds(1)); queuedFinished = true; }); queuedTask.CompletesAsyncOperation(asyncToken2); queuedTask.Start(TaskScheduler.Default); done = true; }); task.CompletesAsyncOperation(asyncToken1); task.Start(TaskScheduler.Default); Wait(listener, signal); Assert.True(done, "Cancelling should have completed the current task."); Assert.True(queuedFinished, "Continued didn't run, but it was supposed to ignore the cancel."); Assert.False(cancelledFinished, "We waited for the cancelled task to finish."); } }
public static void FillOutTextBoxByName(IWebDriver webDriver, string textBoxName, string textToFillIn, int timeOutInSeconds = 180) { IWebElement element = WaitUtility.GetControl(webDriver, By.Name(textBoxName), timeOutInSeconds); if (element != null) { if (!element.Enabled) { var js = (IJavaScriptExecutor)webDriver; js.ExecuteScript("arguments[0].click();", element); SleepHelper.Sleep(1000); } if (textToFillIn.Length > 100) { string javaScript = String.Format("document.getElementsByName(\"{0}\")[0].value = \"{1}\";", textBoxName, textToFillIn); ((IJavaScriptExecutor)webDriver).ExecuteScript(javaScript); } else { element.Clear(); element.SendKeys(textToFillIn); } } }
/// <summary> /// This method waits for the loading page to no longer be visible /// </summary> internal static void WaitForLoadingPageToComplete(IWebDriver webDriver, int timeOutInSeconds, bool logWarning = false) { var waitUntil = DateTime.UtcNow.AddSeconds(timeOutInSeconds); IWebElement hiddenLoadingPage; IWebElement loadingPage = null; do { try { loadingPage = webDriver.FindElement(By.ClassName("loading-page")); hiddenLoadingPage = webDriver.FindElement(By.CssSelector("div.loading-page.hide")); } catch (Exception) { // element not found, which means the loading page is still showing hiddenLoadingPage = null; SleepHelper.Sleep(1000); } }while (DateTime.UtcNow < waitUntil && hiddenLoadingPage == null && loadingPage != null); if (hiddenLoadingPage == null && logWarning) { Trace.TraceWarning("Loading page was found on the page for [{0}] seconds", timeOutInSeconds); } }
/// <summary> /// This method waits for the element to appear and checks its text. /// If the text is not empty, the element is returned. /// If the element is not present and/or the text is still empty when the timeout expires, a timeout exception is trhown. /// </summary> /// <param name="webdriver"></param> /// <param name="by"></param> /// <param name="timeOutInSeconds"></param> internal static IWebElement WaitForNonEmptyText(IWebDriver webdriver, By by, int timeOutInSeconds) { string text = string.Empty; Stopwatch stopWatch = Stopwatch.StartNew(); do { var element = webdriver.FindElement(by); if (element != null) { text = element.GetAttribute("value"); } if (string.IsNullOrEmpty(text)) { SleepHelper.Sleep(); } else { return(element); } }while (stopWatch.Elapsed.TotalSeconds <= timeOutInSeconds); throw new TimeoutException(); }
public void SecondCompletion() { using (var sleepHelper = new SleepHelper()) { var signal1 = new ManualResetEventSlim(); var signal2 = new ManualResetEventSlim(); var listener = new AsynchronousOperationListener(); var firstDone = false; var secondDone = false; var asyncToken1 = listener.BeginAsyncOperation("Test"); var firstTask = Task.Factory.StartNew(() => { signal1.Set(); sleepHelper.Sleep(TimeSpan.FromMilliseconds(500)); firstDone = true; }, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default); firstTask.CompletesAsyncOperation(asyncToken1); firstTask.Wait(); var asyncToken2 = listener.BeginAsyncOperation("Test"); var secondTask = Task.Factory.StartNew(() => { signal2.Set(); sleepHelper.Sleep(TimeSpan.FromMilliseconds(500)); secondDone = true; }, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default); secondTask.CompletesAsyncOperation(asyncToken2); // give it two signals since second one might not have started when WaitTask.Wait is called - race condition Wait(listener, signal1, signal2); Assert.True(firstDone, "First didn't finish"); Assert.True(secondDone, "Should have waited for the second task"); } }
public void IgnoredCancel() { using var sleepHelper = new SleepHelper(); var signal = new ManualResetEventSlim(); var listener = new AsynchronousOperationListener(); var done = new TaskCompletionSource <VoidResult>(); var queuedFinished = new TaskCompletionSource <VoidResult>(); var cancelledFinished = new TaskCompletionSource <VoidResult>(); var asyncToken1 = listener.BeginAsyncOperation("Test"); var task = new Task(() => { using (listener.BeginAsyncOperation("Test")) { var cancelledTask = new Task(() => { sleepHelper.Sleep(TimeSpan.FromSeconds(10)); cancelledFinished.SetResult(default);
public void Operation() { using var sleepHelper = new SleepHelper(); var signal = new ManualResetEventSlim(); var listener = new AsynchronousOperationListener(); var done = false; var asyncToken = listener.BeginAsyncOperation("Test"); var task = new Task(() => { signal.Set(); sleepHelper.Sleep(TimeSpan.FromSeconds(1)); done = true; }); task.CompletesAsyncOperation(asyncToken); task.Start(TaskScheduler.Default); Wait(listener, signal); Assert.True(done, "The operation should have completed"); }
public static bool WaitForPageRedirect(IWebDriver webDriver, string oldUrl, int timeOutInSeconds = 180, bool logWarning = false) { bool isPageTheSame; var timeOutTime = DateTime.UtcNow.Add(TimeSpan.FromSeconds(timeOutInSeconds)); do { SleepHelper.Sleep(1000); isPageTheSame = webDriver.Url == oldUrl; }while (isPageTheSame && DateTime.UtcNow < timeOutTime); if (!isPageTheSame && logWarning) { Trace.TraceWarning("Page did not change after [{0}] seconds", timeOutInSeconds); } return(!isPageTheSame); }
public void Nested() { using (var sleepHelper = new SleepHelper()) { var signal = new ManualResetEventSlim(); var listener = new Listener(); var outerDone = false; var innerDone = false; var asyncToken1 = listener.BeginAsyncOperation("Test"); var task = new Task(() => { signal.Set(); sleepHelper.Sleep(TimeSpan.FromMilliseconds(500)); using (listener.BeginAsyncOperation("Test")) { sleepHelper.Sleep(TimeSpan.FromMilliseconds(500)); innerDone = true; } sleepHelper.Sleep(TimeSpan.FromMilliseconds(500)); outerDone = true; }); task.CompletesAsyncOperation(asyncToken1); task.Start(); Wait(listener, signal); Assert.True(innerDone, "Should have completed the inner task"); Assert.True(outerDone, "Should have completed the outer task"); } }
/// <summary> /// Goes to advanced search page. /// </summary> /// <returns>Returns an instance of AdvancedSearchPage</returns> public SearchPage ClickOnExplorePackages() { Click(ExplorePackages); SleepHelper.Sleep(3); return(new SearchPage(baseDriver)); }
/// <summary> /// Goes to advanced search page. /// </summary> /// <returns>Returns an instance of AdvancedSearchPage</returns> public SearchPage ClickOnFirstPackage() { Click(FirstPackage); SleepHelper.Sleep(3); return(new SearchPage(baseDriver)); }
public void QueuedOperation() { using (var sleepHelper = new SleepHelper()) { var signal = new ManualResetEventSlim(); var listener = new Listener(); var done = false; var asyncToken1 = listener.BeginAsyncOperation("Test"); var task = new Task(() => { signal.Set(); sleepHelper.Sleep(TimeSpan.FromMilliseconds(500)); var asyncToken2 = listener.BeginAsyncOperation("Test"); var queuedTask = new Task(() => { sleepHelper.Sleep(TimeSpan.FromMilliseconds(500)); done = true; }); queuedTask.CompletesAsyncOperation(asyncToken2); queuedTask.Start(); }); task.CompletesAsyncOperation(asyncToken1); task.Start(); Wait(listener, signal); Assert.True(done, "Should have waited for the queued operation to finish!"); } }
public void Operation() { using (var sleepHelper = new SleepHelper()) { var signal = new ManualResetEventSlim(); var listener = new Listener(); var done = false; var asyncToken = listener.BeginAsyncOperation("Test"); var task = new Task(() => { signal.Set(); sleepHelper.Sleep(TimeSpan.FromSeconds(1)); done = true; }); task.CompletesAsyncOperation(asyncToken); task.Start(); Wait(listener, signal); Assert.True(done, "The operation should have completed"); } }
public void SecondCompletion() { using (var sleepHelper = new SleepHelper()) { var signal1 = new ManualResetEventSlim(); var signal2 = new ManualResetEventSlim(); var listener = new Listener(); var firstDone = false; var secondDone = false; var asyncToken1 = listener.BeginAsyncOperation("Test"); var firstTask = Task.Factory.StartNew(() => { signal1.Set(); sleepHelper.Sleep(TimeSpan.FromMilliseconds(500)); firstDone = true; }, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default); firstTask.CompletesAsyncOperation(asyncToken1); firstTask.Wait(); var asyncToken2 = listener.BeginAsyncOperation("Test"); var secondTask = Task.Factory.StartNew(() => { signal2.Set(); sleepHelper.Sleep(TimeSpan.FromMilliseconds(500)); secondDone = true; }, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default); secondTask.CompletesAsyncOperation(asyncToken2); // give it two signals since second one might not have started when WaitTask.Wait is called - race condition Wait(listener, signal1, signal2); Assert.True(firstDone, "First didn't finish"); Assert.True(secondDone, "Should have waited for the second task"); } }
public void IgnoredCancel() { using (var sleepHelper = new SleepHelper()) { var signal = new ManualResetEventSlim(); var listener = new Listener(); var done = false; var queuedFinished = false; var cancelledFinished = false; var asyncToken1 = listener.BeginAsyncOperation("Test"); var task = new Task(() => { using (listener.BeginAsyncOperation("Test")) { var cancelledTask = new Task(() => { sleepHelper.Sleep(TimeSpan.FromSeconds(10)); cancelledFinished = true; }); signal.Set(); cancelledTask.Start(); } sleepHelper.Sleep(TimeSpan.FromMilliseconds(500)); // Now that we've cancelled the first request, queue another one to make sure we wait for it. var asyncToken2 = listener.BeginAsyncOperation("Test"); var queuedTask = new Task(() => { sleepHelper.Sleep(TimeSpan.FromSeconds(1)); queuedFinished = true; }); queuedTask.CompletesAsyncOperation(asyncToken2); queuedTask.Start(); done = true; }); task.CompletesAsyncOperation(asyncToken1); task.Start(); Wait(listener, signal); Assert.True(done, "Cancelling should have completed the current task."); Assert.True(queuedFinished, "Continued didn't run, but it was supposed to ignore the cancel."); Assert.False(cancelledFinished, "We waited for the cancelled task to finish."); } }
public void MultipleEnqueues() { using (var sleepHelper = new SleepHelper()) { var signal = new ManualResetEventSlim(); var listener = new Listener(); var outerDone = false; var firstQueuedDone = false; var secondQueuedDone = false; var asyncToken1 = listener.BeginAsyncOperation("Test"); var task = new Task(() => { signal.Set(); sleepHelper.Sleep(TimeSpan.FromMilliseconds(500)); var asyncToken2 = listener.BeginAsyncOperation("Test"); var firstQueueTask = new Task(() => { sleepHelper.Sleep(TimeSpan.FromMilliseconds(500)); var asyncToken3 = listener.BeginAsyncOperation("Test"); var secondQueueTask = new Task(() => { sleepHelper.Sleep(TimeSpan.FromMilliseconds(500)); secondQueuedDone = true; }); secondQueueTask.CompletesAsyncOperation(asyncToken3); secondQueueTask.Start(); firstQueuedDone = true; }); firstQueueTask.CompletesAsyncOperation(asyncToken2); firstQueueTask.Start(); outerDone = true; }); task.CompletesAsyncOperation(asyncToken1); task.Start(); Wait(listener, signal); Assert.True(outerDone, "The outer task should have finished!"); Assert.True(firstQueuedDone, "The first queued task should have finished"); Assert.True(secondQueuedDone, "The second queued task should have finished"); } }
/// <summary> /// Goes to advanced search page. /// </summary> /// <returns>Returns an instance of AdvancedSearchPage</returns> public SearchPage ClickOnSearch() { Click(SearchLink); SleepHelper.Sleep(3); return(new SearchPage(baseDriver)); }
public void Cancel() { using (var sleepHelper = new SleepHelper()) { var signal = new ManualResetEventSlim(); var listener = new Listener(); var done = false; var continued = false; var asyncToken1 = listener.BeginAsyncOperation("Test"); var task = new Task(() => { signal.Set(); sleepHelper.Sleep(TimeSpan.FromMilliseconds(500)); var asyncToken2 = listener.BeginAsyncOperation("Test"); var queuedTask = new Task(() => { sleepHelper.Sleep(TimeSpan.FromSeconds(5)); continued = true; }); asyncToken2.Dispose(); queuedTask.Start(); done = true; }); task.CompletesAsyncOperation(asyncToken1); task.Start(); Wait(listener, signal); Assert.True(done, "Cancelling should have completed the current task."); Assert.False(continued, "Continued Task when it shouldn't have."); } }