Search code examples
seleniumselenium-webdriverautomated-testsbddselenium-chromedriver

Selenium webdriver Assert click element worked


I am automating a web page, and I am having lots of issues in clicking in a element, so I implemented this

try {
    element.click();
} catch (WebDriverException e) {
    clickJS(element);
}

clickJS is a method I wrote to click using javascript approach, it usually works, however I am having issues when the expression to click does not throw any exception, but in the future steps will fail because it did nothing. Is there a way for assert a click has 'worked' even though it did not throw any exception. ps: Iam sure the webelement is clickable PS: I am using chrome webdriver


Solution

  • There's no generic way to determine if a click was successful because a click could do just about anything... navigate to another page, click a checkbox, dynamically load another part of the page, etc.

    In general, I would say that this is not the right approach if you are trying to automate a customer scenario. For example, you attempt a click but didn't anticipate some dialog popping up. Your normal click would throw an exception that another element would receive the click but your JS click would succeed. You shouldn't want that click to succeed because a user couldn't click that element without dealing with the dialog first. This may cause a strange failure down the road that will be hard to trace. Do a "normal" click each time. As you run the script, you will find intermittent failures. Investigate them and find solutions, e.g. wait for some dialog to close because 1 in 10 times it closes slowly and so on. In the end, you will have a more robust suite.