1

I am trying to launch my application url using karate framework. Issue is, this webportal does not have any login username/password functionality. This website works with the token associated with a deviceid and browser session of a particular machine. When we test it manually we have to make sure that, we are using same url on same browser and should never clear cache or else this url token gets expired.

Now, we have to automate it and we found a script that runs using selenium webdriver and java/javascript to restore the session using device id.

However, I am now trying to get solution for karate framework.

Can someone please guide me.

Please find below the karate feature file and code.

 var request = indexedDB.open("ZZ");
var db;
request.onupgradeneeded = function() {
  // The database did not previously exist, so create object stores and indexes.
  db = request.result;
  var store = db.createObjectStore("XX");
};

request.onsuccess = function() {
  db = request.result;
  var tx = db.transaction("XX", "readwrite");
  var store = tx.objectStore("XX");
  store.put("`+deviceId+`");
};

Feature file:

Feature: to test login flow
Scenario:
Given driver 'https://www.testing.com'
And param deviceid = qqqqqq
* def precondScript = Function(){
''
  var request = indexedDB.open("ZZ");
    var db;
    request.onupgradeneeded = function() {
      // The database did not previously exist, so create object stores and indexes.
      db = request.result;
      var store = db.createObjectStore("XX");
    };
    
    request.onsuccess = function() {
      db = request.result;
      var tx = db.transaction("XX", "readwrite");
      var store = tx.objectStore("XX");
      store.put("`+deviceId+`");
    };

''
Given driver 'https://www.testing.com'
When searchbox <element locator>
Then comparison

Now this is how we have done it in selenium and cucumber BDD code.

driver.get(https://www.testing.com);
String deviceId = qqqqqq  
jsHelper.executeScript("var request = indexedDB.open("ZZ");\n" +
                    "var db;\n" +
                    "request.onupgradeneeded = function() {\n" +
                    "// The database did not previously exist, so create object stores and indexes.\n" +
                    "db = request.result;\n" +
                    "var store = db.createObjectStore(\"XX\");\n" +
                    "};\n" +
                    "\n" +
                    "\n" +
                    "\n" +
                    "request.onsuccess = function() {\n" +
                    "db = request.result;\n" +
                    "var tx = db.transaction(\"XX\", \"readwrite\");\n" +
                    "var store = tx.objectStore(\"XX\");\n" +
                    "store.put(\"" + deviceId + "\", \"AAA");\n" +
                    "};");
  
 driver.get(https://www.testing.com);
driver.manage().window().maximize();
 log.info("navigating to homepage"); 
Niks
  • 85
  • 6
  • executing any JS in karate is easy, take some time - read this and you should be able to figure it out: https://stackoverflow.com/a/60800181/143475 - if still stuck, ask a question and keep it simple and specific and mention what you tried that did not work – Peter Thomas Nov 11 '21 at 04:29
  • Hi @PeterThomas, iam getting this error feature:19 - evaluation (js) failed: driver'https://test-aaa.com/wom4.3/?token=vvvv', javax.script.ScriptException: :1:7 Expected ; but found https://test-aaa.com/wom4.3/?token=vvvv driver'https://test-aaa.com/wom4.3/?token=vvvv' ^ in at line number 1 at column number 7 stack trace: jdk.scripting.nashorn/jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:477) i tried with this below feature file – Niks Nov 11 '21 at 06:50
  • Given driver'https://test-aaa.com/wom4.3/?token=vvvv' * def pre = """ var request = indexedDB.open("yyyyy"); var db; request.onupgradeneeded = function() { // The database did not previously exist, so create object stores and indexes. db = request.result; var store = db.createObjectStore("zzzz");}; request.onsuccess = function() { db = request.result; var tx = db.transaction("zzzz", "readwrite"); var store = tx.objectStore("zzzz"); store.put("2044cbdf-aaaaaaa-aaaaaaa", "wom_device_id");}; """ – Niks Nov 11 '21 at 06:58
  • sorry, can't help unless you follow this process exactly as described: https://github.com/karatelabs/karate/wiki/How-to-Submit-an-Issue – Peter Thomas Nov 11 '21 at 09:45

0 Answers0