1

I'm new in protractor and I hope to have help regarding how to pass a variable in the executeAsyncScript function used in protractor tests.

I'm testing an application and i need to have a JSON file that contain the labels translation (French and English), at first I could correctly recuperate the language of the user from a first json file(let call getUser.json). Second on depending of the userlanguage I need to pass dynamically the url to get the labels json file (let call lang_fr.json and lang_en.json):

based on the code, 3rd exemple I could access to any json file.

  browser.executeAsyncScript(function() {
        var callback = arguments[arguments.length - 1];
        var xhr = new XMLHttpRequest();
        var url='path_to/getUser';
        xhr.open("GET", url , true);
        xhr.onreadystatechange = function() {
        if (xhr.readyState == 4 && xhr.status == 200) {
            callback(xhr.responseText);
        }
        };
        xhr.send('');
    }).then(function(str) {
        browser.params.lang=JSON.parse(str)['userLanguage'];
        if(JSON.parse(str)['userLanguage']==='fr')
        browser.params.url='path_to/lang_en.json';
        else
        browser.params.url='path_to/lang_fr.json';
        UserLanguage.UserLanguage(); /// excuting the fct that call the second jsonfile 
now I need to pass the browser.params.urlexecuteAsyncScript to get the json file labels So i do this in another export file:

var url = browser.params.url;
browser.logger.info(browser.params.url); // The url depending the user language is correctly displayed

browser.executeAsyncScript(function() {
      var callback = arguments[arguments.length - 1];
      var xhr = new XMLHttpRequest();
      xhr.open('GET', url /*here to pass the url depending the user language*/, true);
        xhr.onreadystatechange = function() {
          if (xhr.readyState == 4) {
            callback(xhr.responseText);
          }
        }
        xhr.send()
      }, url/*passed on second argument*/).then(function(jsonlabel) {
        /// rest of the code to resolve the labels

I got that the url is not defined

Error console]

I checked the following examples:

Executing Async Javascript in Protractor http://blog.ng-book.com/executing-async-javascript-in-protractor/

WebDriver executeAsyncScript vs executeScript

But in every example I got errors.

So could you please provide me with your suggestion how can I pass correctly the vaiable url in the script? note that when I put the absolute file path it work:

 xhr.open('GET', 'path_to/lang_en.json', true);

But I need to pass it dynamically. Hope it's clear and I am here for further details.

1 Answers1

0

So I tried to put the variable url on function(){ and it works!

{
  var url = browser.params.url;
  var languageUser = browser.params.lang
  browser.logger.info(browser.params.url); // The url depending the user language is correctly displayed
  browser.executeAsyncScript(function(url /// added here){
      var callback = arguments[arguments.length - 1];
      var xhr = new XMLHttpRequest();
      xhr.open('GET', url, true); xhr.onreadystatechange = function() {
        if (xhr.readyState == 4 && xhr.status == 200) {
          callback(xhr.responseText);
        }
      }
      xhr.send()
    }, url).then(function(resJson) {//rest of the code