I'm working on knockout based site using selenium web driver + c# to automate. my test methods fail because of elements not found / not able to click as the site is slow / or the binding/ rendering of html is slow after the ajax calls are finished.
below is sample code i used to know the ajax calls have finished and i can proceed to search elements.
public static void waitForAjax(IWebDriver driver, String action)
{
try
{
TimeSpan tp = TimeSpan.FromSeconds(1000);
driver.Manage().Timeouts().SetScriptTimeout(tp);
((IJavaScriptExecutor)driver).ExecuteAsyncScript(
"var callback = arguments[arguments.length - 1];" +
"var xhr = new XMLHttpRequest();" +
"xhr.open('GET', '/" + action + "', true);" +
"xhr.onreadystatechange = function() {" +
" if (xhr.readyState == 4 && xhr.status == 200) {" +
" callback(xhr.responseText);" +
" }" +
"};" +
"xhr.send();");
}
catch
{
}
}
Im calling the above method before going to find the elements
Test Method
try
{
IWebDriver _Driver;
IWebElement ele;
FirefoxDriverService _Service = FirefoxDriverService.CreateDefaultService(@"D:\CodePlex_C#\SeleniumTestWithHtml\SeleniumTestWithHtml\Drivers");
_Driver = new FirefoxDriver(_Service);
_Driver.Navigate().GoToUrl("HomepageURl");
System.Threading.Thread.Sleep(2000);
waitForAjax(_Driver,"Call1");
waitForAjax(_Driver,"Call2");
System.Threading.Thread.Sleep(2000);
ele = _Driver.FindElement(By.LinkText("DASHBOARD"));
ele.Click();
//System.Threading.Thread.Sleep(2000);
waitForAjax(_Driver,"Call1");
waitForAjax(_Driver,"Call2");
waitForAjax(_Driver,"Call3");
waitForAjax(_Driver,"Call4");
System.Threading.Thread.Sleep(2500);
ele = _Driver.FindElement(By.Id("QuickSearchCnt"));
ele.Click();
}
But My Code Fails. I suspect the code to find the calls have finished to execute is not working properly.
I Need help in knowing is my approach right? and need help in fixing the issue.
Aucun commentaire:
Enregistrer un commentaire