I'm having a problem with Selenium2 using Firefox version 39.0 and Firefox webdriver while testing a page based on Apache MyFaces Trinidad 1.0.6.
When I imitate press on CheckBox, TextBox becomes visible, however, sendKeys doesn't work all the time, even if I wait until ExpectedConditions.presenceOfElementLocated
or use Thead.sleep(x)
(even though it's the worst case), Unable to locate
or Not found in cache
problems are being thrown.
Another problem - while clicking on tabs of page, some can be pressed and it works, and some while being pressed, Selenium just pretends to put "Mouse" on it and link is being showed in the bottom left corner. Double-press doesn't work either.
So is this some kind of AJAX background method that screws me up with cache problems and not clicking, or what?
My code:
public void bclick(String ids, int msec)
{
new WebDriverWait(driver, 25).until(ExpectedConditions.elementToBeClickable(By.id(ids)));
driver.findElement(By.id(ids)).click();
waitTime(msec);
}
This is my main method for clicking on elements, however it is unstable sometimes with some buttons(simple html buttons).
4 Answers 4
I also use the below to check for my jQuery.
((IJavaScriptExecutor)Driver).ExecuteScript("return jQuery.active");
-
1How does this piece of code workout?IAmMilinPatel– IAmMilinPatel2016年07月13日 03:28:30 +00:00Commented Jul 13, 2016 at 3:28
-
If you're using jQuery for your ajax requests it will tell you whether that has finished loading, you can have it loop and wait a set amount of times before you go looking for your elementsJohnnyTheJet– JohnnyTheJet2016年07月15日 20:11:15 +00:00Commented Jul 15, 2016 at 20:11
When I imitate press on CheckBox, TextBox becomes visible, however, sendKeys doesn't work all the time
Check out the official documentation on waits.
Another problem - while clicking on tabs of page, some can be pressed and it works, and some while being pressed, Selenium just pretends to put "Mouse" on it and link is being showed in the bottom left corner. Double-press doesn't work either.
Your DOM might be in a state of flux as you are trying to interact with it. After any action modifies the DOM your next step should wait for the element before interacting with it.
You can also use java script: document.readyState === "complete"
to determine whether a page has loaded.
In addition to that, note that in many cases, using the Selenium Actions
class for clicking on items is preferred over a direct driver.click()
as it better simulates a real click.
Answer for waiting right amount of time for elements to be ready for interaction using Apache MyFaces Trinidad 1.0.6. is to execute java script code with JavascriptExecutor
and check state of _Request Queue_
if it's ready:
public void ajaxWait() {
Boolean state = (Boolean)((JavascriptExecutor) driver).executeScript("return TrPage.getInstance().getRequestQueue().getDTSState() == TrRequestQueue.STATE_READY");
new WebDriverWait(driver, 180).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver) {
JavascriptExecutor js = (JavascriptExecutor) driver;
System.out.println(js.executeScript("return TrPage.getInstance().getRequestQueue().getDTSState() == TrRequestQueue.STATE_READY"));
return (Boolean) js.executeScript("return TrPage.getInstance().getRequestQueue().getDTSState() == TrRequestQueue.STATE_READY"); // return jQuery.active == 0
}
});
if (!state) {
waitTime(500);
}
}
waitTime(long mills)
method is a simple Thread.sleep()
method used for waiting some time for page to fully load.
public void bclick(String ids, int msec) { new WebDriverWait(driver, 25).until(ExpectedConditions.elementToBeClickable(By.id(ids))); driver.findElement(By.id(ids)).click(); waitTime(msec); }
This is my main method for clicking on elements, however it works unstable sometimes with some buttons(simple html buttons).