1

I am creating an UI automated test using Karate framework. One of the AUT screens can be open only with double-click. When I try something like 'mouse('somelocator').doubleClick()' nothing happens. Then I decided to use JS that looks like this:

var myElement = document.getElementsByTagName("somelocator");
var clickEvent  = document.createEvent ('MouseEvents');
clickEvent.initEvent('dblclick', true, true);
myElement.dispatchEvent(clickEvent);

It's working perfectly when I try it in Chrome JS console. The problem is that when I try to use it in Karate feature file

* def myDblClick = 
    """
    function() { 
      var myElement = document.getElementsByTagName("somelocator");
      var clickEvent  = document.createEvent ('MouseEvents');
      clickEvent.initEvent('dblclick', true, true);
      myElement.dispatchEvent(clickEvent);
    }
    """
  * myDblClick()

I receive

failed features: js failed: 01: myDblClick()

org.graalvm.polyglot.PolyglotException: ReferenceError: "document" is not defined

What do I do wrong here?

1 Answer 1

1

The JS that needs to be run in the browser has to be expressed as a string. This is explained in detail here: https://github.com/karatelabs/karate/tree/master/karate-core#karate-vs-the-browser

So I think this will work (haven't tried it):

* def myDblClick = 
"""
function(tagName) {     
  def rawJs = `
    var myElement = document.getElementsByTagName('${tagName}');
    var clickEvent  = document.createEvent('MouseEvents');
    clickEvent.initEvent('dblclick', true, true);
    myElement.dispatchEvent(clickEvent);    
  `;
  var driver = karate.get('driver');
  driver.script(rawJs);
}
"""
* myDblClick('input')

You already mentioned that you tried double-click which is supposed to be possible via the mouse() API: https://github.com/karatelabs/karate/tree/master/karate-core#mouse - would be good if you can provide some hints on whether this can be improved in Karate.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.