0

I am still new to Selenium test world, and I am currently using the selenium chrome webdriver for testing.

what I want to do is in the below popup dialog:

  1. input a number in text input box
  2. click the Apply button
  3. then web application will be triggered to post info to server-side.

my code:

/*
 ....
 a bunch of assert and wait.until function to make sure everything is correct till this step.
 ....
*/
driver.findElement(By.xpath(DIALOGBOX + "/input")).sendKeys("10");
//Thread.sleep(500);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath(APPLY_BUTTON)));
new Actions(driver).moveToElement(driver.findElement(By.xpath(APPLY_BUTTON))).click().perform();

and here is what I discovered till now, If I uncomment Thread.sleep(500), everything will work perfectly. but without Thread.sleep(500), the web application will not make a post call to the server which the Apply button should trigger.

I do not know why it happens, what Thread.sleep(500) made difference to click action, is selenium takes the time to move to the element and performs the click with the mouse not yet reach the button position? and how to fix my cases without using thread sleep?

Update and solution:

first, I forget to mention that the web application is built on GWT(Google web toolkit). I have already make sure the driver has successfully got the button element before click(), so the bug is minimized to click() of Action. then I tried

new Actions(driver).moveToElement(driver.findElement(By.xpath(APPLY_BUTTON)).sendKeys(Keys.ENTER).perform();

it works perfectly, no need to make the thread sleep. so the cause of the bug is that selenium's click action is different from how GWT handle the onclick function. If you face the same issue, try using sendKeys and for the client side adding handler for onKeyPress

Rebecca
  • 111
  • 2
  • 12
  • 1
    The general answer is to use Selenium's [wait functionality](http://stackoverflow.com/questions/5868439/wait-for-page-load-in-selenium). – Paul Hicks Oct 18 '16 at 23:00
  • @PaulHicks Thanks for reply. did you mean using wait to replace thread.sleep? – Rebecca Oct 18 '16 at 23:08
  • Sort of. Thread.sleep isn't appropriate. You should wait for the apply button to be clickable. Check out the mentioned answer, the other answers references on that page, and [waits in Selenium](http://www.seleniumhq.org/docs/04_webdriver_advanced.jsp) for more information. – Paul Hicks Oct 18 '16 at 23:15
  • @PaulHicks thanks for your help, I actually think about this causes, and tried by adding wait for element clickable in front of click, but it is still not working – Rebecca Oct 18 '16 at 23:21
  • I will probably try the js.ExecuteScript method – Bala Oct 18 '16 at 23:22
  • Another complication can be that the dialog is not in the DOM at the time of the call to `findElement`. In which case, you will first have to wait for the dialog to appear (using wait until ElementExists). After that, the button will probably be clickable, but you may also have to wait until ElementToBeClickable. Check out [ExpectedConditions](https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html) for more options. – Paul Hicks Oct 18 '16 at 23:24
  • @PaulHicks DOM, ah, never think about it, thank you so much, I will have a try. – Rebecca Oct 18 '16 at 23:27
  • Is there some reason you are using `Actions` to move and click the element instead of just `element.click()`? – JeffC Oct 19 '16 at 01:20

3 Answers3

1

Just because the button is visible and selenium thinks it is clickable doesn't mean it is ready to use. Check with browser inspector and investigate if it is tied to some asynchronous javascript. Meaning, the alert may be waiting on some background process to complete before you can actually click it for an event to occur.

It's possible that upon entering a value into the input field, there is an async script going out to a server, and causing the "Apply" button to do nothing when clicked, until there is a response from the server. And that 500ms delay is just enough time for the ajax communication to complete.

To learn more about ajax: https://www.tutorialspoint.com/ajax/what_is_ajax.htm

robx
  • 3,093
  • 2
  • 26
  • 29
0

I will probably try the js.ExecuteScript method as ( below code is for c#)

IJavaScriptExecutor js = driver as IJavaScriptExecutor;
string jsOutput = (string)js.ExecuteScript(String.Format("document.getElementById('{0}').click();", elementId));

where the normal click fails

Bala
  • 512
  • 9
  • 16
  • This is what Selenium's wait functionality is for. Wheels are there for assisting you to get places, not reinventing. – Paul Hicks Oct 18 '16 at 23:25
0

first, I forget to mention that the web application is built on GWT(Google web toolkit). I have already make sure the driver has successfully got the button element before click(), so the bug is minimized to click() of Action. then I tried

new Actions(driver).moveToElement(driver.findElement(By.xpath(APPLY_BUTTON))
    .sendKeys(Keys.ENTER).perform();

it works perfectly, no need to make the thread sleep. so the cause of the bug is that selenium's click action is different from how GWT handle the onclick function. If you face the same issue, try using sendKeys and for the client side adding handler for onKeyPress

budi
  • 6,351
  • 10
  • 55
  • 80
Rebecca
  • 111
  • 2
  • 12