1

Scenario:

  • Testing web application using Protractor
  • Element needs to be clicked on to open another page
  • Designed the test to have one click on the element

Issue: Sometimes elements needs one click and the test passes but if I run it again the same element needs double clicks

This is causing my test to fails randomly

I verified this by changing the command to have a double click in protractor and it passed.

This is causing inconsistency as I don't know when the element needs one or double clicks.

Any Suggestion is appreciated?

Kevin
  • 95
  • 3
  • 7

3 Answers3

0

You may just need to put the element into focus explicitly via mouseMove() and then issue a click() action:

browser.actions().mouseMove(elm).click().perform();
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • @ alecxe:That is how we have been doing it and still have problem – Kevin Jul 07 '16 at 17:59
  • @Kevin okay, how about you introduce a [custom sleep action](http://stackoverflow.com/questions/32789460/custom-browser-actions-in-protractor) and do it like this: `browser.actions().mouseMove(elm).sleep(50).click().perform();`. Not ideal, of course, but see if it actually helps. – alecxe Jul 07 '16 at 18:01
0

Inconsistency might be because of element is not yet ready to click. So you need to wait until element become clickable and click it. Below code will help you in achieving consistency.

 Code Snippet:

  var EC = protractor.ExpectedConditions;
  var elementToBeClick=element(locator);
  var timeOut=10000;

  browser.wait(EC.elementToBeClickable(elementToBeClick), timeOut).
      thenCatch(function () {
                assert.fail(' target element is not clickable');
             });
  elementToBeClick.click();
Optimworks
  • 2,537
  • 17
  • 20
0

you can use browser.executeScripts to inject native javascript code into the browser and click the required button. this will execute the click event on the required element that you pass into the function

try the below code,

var elementToBeClick=element(locator);
browser.executeScript("arguments[0].click()",elementToBeClick.getWebElement())
Sudharsan Selvaraj
  • 4,792
  • 3
  • 14
  • 22
  • Note that it is different from a regular click: http://stackoverflow.com/questions/34562061/webdriver-click-vs-javascript-click. – alecxe Jul 08 '16 at 13:20
  • I agree. But there are some cases where user can able to click a button but selenium cannot.those scenarios can be handled using native click event – Sudharsan Selvaraj Jul 08 '16 at 13:33