Skip to content

Instantly share code, notes, and snippets.

@elgatov
Created September 23, 2018 15:08
Show Gist options
  • Save elgatov/628d689e7e45ea63c299c4d8f2955b28 to your computer and use it in GitHub Desktop.
Save elgatov/628d689e7e45ea63c299c4d8f2955b28 to your computer and use it in GitHub Desktop.
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.Events;
using OpenQA.Selenium.Support.UI;
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading;
namespace InfraestructureSelenium.Helper
{
class EventListeners
{
public static EventFiringWebDriver driver { get; set; }
public static WebDriverWait wait { get; set; }
public EventFiringWebDriver GetWebDriver() { return driver; }
public EventListeners(EventFiringWebDriver firingWebDriver)
{
driver = firingWebDriver;
wait = new WebDriverWait(driver, TimeSpan.FromSeconds(60));
driver.FindingElement += new EventHandler<FindElementEventArgs>(_FindingElement);
driver.ElementClicked += new EventHandler<WebElementEventArgs>(_ElementClicked);
driver.ElementClicking += new EventHandler<WebElementEventArgs>(_ElementClicking);
driver.ExceptionThrown += new EventHandler<WebDriverExceptionEventArgs>(_ExceptionThrown);
}
#region Events
static void _ElementClicked(object sender, WebElementEventArgs e)
{
Trace.WriteLine("Elemento clicado: " + e.Element);
Console.WriteLine("Elemento clicado: " + e.Element);
}
static void _ElementClicking(object sender, WebElementEventArgs e)
{
//new WebDriverWait(driver, TimeSpan.FromSeconds(15))
// .Until(d => ((IJavaScriptExecutor)d).ExecuteScript("return document.readyState").Equals("complete"));
//WaitLoading();
Trace.WriteLine("Clicando elemento: " + e.Element);
Console.WriteLine("Clicando elemento: " + e.Element);
}
static void _ExceptionThrown(object sender, WebDriverExceptionEventArgs e)
{
//if (e.ThrownException.GetType().Equals(typeof(StaleElementReferenceException)))
//{
// driver.FindElement(By.(e.ThrownException.Data.)
//}
Trace.WriteLine("Excepción: " + e.ThrownException.Message);
//Console.WriteLine("Excepción: " + e.ThrownException.Message);
//TestContext.WriteLine("Excepción: " + e.ThrownException.Message);
//ObtenerWebDriver().Quit();
}
static void _FindingElement(object sender, FindElementEventArgs e)
{
//Trace.WriteLine("Excepción: " + e.ThrownException.Message);
//Console.WriteLine("Excepción: " + e.ThrownException.Message);
//TestContext.WriteLine("Excepción: " + e.ThrownException.Message);
//new WebDriverWait(driver, TimeSpan.FromSeconds(60))
//.Until(d => ((IJavaScriptExecutor)d).ExecuteScript("return document.readyState").Equals("complete"));
// .Until(d => ((IJavaScriptExecutor)d).ExecuteScript("return !!window.jQuery && window.jQuery.active == 0"));
wait.Timeout = TimeSpan.FromSeconds(30);
wait.PollingInterval = TimeSpan.FromMilliseconds(500);
wait.IgnoreExceptionTypes(typeof(NoSuchElementException));
wait.Message = "El Spinner ha tardado demasiado en cargar";
wait.Until(d =>
{
bool bool1, bool2, bool3;
var result1 = ((IJavaScriptExecutor)d).ExecuteScript("return document.readyState");
bool1 = result1.Equals("interactive") || result1.Equals("complete");
bool2 = (bool)((IJavaScriptExecutor)d).ExecuteScript("return !!window.jQuery || jQuery.active === 0");
return bool1 && bool2;
});
//wait.Until(d => ((IJavaScriptExecutor)d).ExecuteScript("return !!window.jQuery || jQuery.active === 0"));
//wait.Until(d => ((IJavaScriptExecutor)d).ExecuteScript("return document.readyState").ToString().Equals("complete"));
//wait.Until(d => ((IJavaScriptExecutor)d).ExecuteScript("return $(window).jQuery != undefined && $(window).jQuery.active === 0").Equals(true));
//wait.Until(d => (Boolean)((IJavaScriptExecutor)d).ExecuteScript("return !!window.jQuery || ($(jQuery.active == 0) && XMLHttp.readyState === 'complete')"));
//wait.Until(ExpectedConditions.InvisibilityOfElementLocated(By.Id("dvLoading")));
//WaitLoading();
//wait.Until(webDriver => !webDriver.FindElements(By.Id("backgroundLoading")).Any(webElement => webElement.Displayed));
//wait.Until(webDriver => !webDriver.FindElements(By.Id("dvLoading")).Any(webElement => webElement.Displayed));
}
#endregion
#region Waits
private static void WaitLoading()
{
wait.Timeout = TimeSpan.FromSeconds(10);
wait.PollingInterval = TimeSpan.FromMilliseconds(500);
wait.Message = "El spinner ha tardado demasiado en desaparecer";
//wait.Until(webDriver => !webDriver.FindElements(By.Id("dvLoading")).Any(webElement => webElement.Displayed));
wait.Until(ExpectedConditions.InvisibilityOfElementLocated(By.Id("dvLoading")));
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment