<commit_before>'use strict';
/* globals gT: true */
/* globals gIn: true */

function createFuncClickCbByInputEl(jsWaitBoundList, jsGetListItem, isDblClick, logAction) {
  return function clickCb(inputEl, count) {
    count = count || 0;
    if (count > gT.engineConsts.cbRetryClicksCount) {
      var errStr = 'Exceeded count of attempts to click combobox, wait condition: ' + jsWaitBoundList;
      gIn.tracer.traceErr(errStr);
      return gT.sOrig.promise.rejected(new Error(errStr));
    }
    return gT.e.lClick.clickAndWaitForAjaxFinish(inputEl)
      .then(function () {
        gIn.tracer.trace3('Before wait after cb inputEl click');
        return gT.sOrig.driver.wait(
          function () {
            return gT.s.browser.executeScriptWrapper(jsWaitBoundList);
          }, gT.engineConsts.cbBoundListTimeout
        )
          .catch(function (err) {
            // Sometimes combobox treated one click as two and closes immediately after open.
            // So this code gives a second chance to it.
            gIn.tracer.trace1('Using one more chance to click combo box (wait failed)');
            return clickCb(inputEl, count + 1);
          });
      })
      .catch(function (err) {
        gIn.tracer.trace1('Using one more chance to click combo box (click failed)');
        return clickCb(inputEl, count + 1);
      })
      .then(function () {
        gIn.tracer.trace3('Before get list item');
        return gT.s.browser.executeScript(jsGetListItem, false);
      })
      .then(gT.e.lClick.createFuncPrintTextDelayClick(isDblClick, logAction));
  };
}

function createFuncClickCbByJsToGetInputEl(jsGetInputEl, jsWaitBoundList, jsGetListItem, isDblClick, logAction) {
  return function () {
    return gT.s.browser.executeScript(jsGetInputEl, false)
      .then(createFuncClickCbByInputEl(jsWaitBoundList, jsGetListItem, isDblClick, logAction));
  }
}

function clickCb(logText, jsGetInputEl, jsWaitBoundList, jsGetListItem, isDblClick, logAction) {
  return gIn.wrap(logText, logAction, function () {
    return gT.s.browser.executeScript(jsGetInputEl, false)
      .then(createFuncClickCbByInputEl(jsWaitBoundList, jsGetListItem, isDblClick, logAction));
  });
}

exports.idIndex = function (cbId, itemIndex, logAction) {
  return gIn.wrap(``, logAction, function () {
    return gT.s.browser.executeScript(`return tiaEJ.hEById.getNameAndLabels('${cbId}');`, false)
      .then(function (obj) {
        gIn.logger.logIfNotDisabled(
          `Click combo box(name: '${obj.name}', label: '${obj.label}'), item #${itemIndex}`, logAction);
      })
      .then(createFuncClickCbByJsToGetInputEl(<commit_msg>Added some more stability to cb clicks.<commit_after>'use strict';
/* globals gT: true */
/* globals gIn: true */

function createFuncClickCbByInputEl(jsWaitBoundList, jsGetListItem, isDblClick, logAction) {
  return function clickCb(inputEl, count) {
    count = count || 0;
    if (count > gT.engineConsts.cbRetryClicksCount) {
      var errStr = 'Exceeded count of attempts to click combobox, wait condition: ' + jsWaitBoundList;
      gIn.tracer.traceErr(errStr);
      return gT.sOrig.promise.rejected(new Error(errStr));
    }
    return gT.e.lClick.clickAndWaitForAjaxFinish(inputEl)
      .then(function () {
        gIn.tracer.trace3('Before wait after cb inputEl click');
        return gT.sOrig.driver.wait(
          function () {
            return gT.s.browser.executeScriptWrapper(jsWaitBoundList);
          }, gT.engineConsts.cbBoundListTimeout
        )
          .catch(function (err) {
            // Sometimes combobox treated one click as two and closes immediately after open.
            // So this code gives a second chance to it.
            gIn.tracer.trace1('Using one more chance to click combo box (wait failed)');
            return clickCb(inputEl, count + 1);
          });
      })
      .catch(function (err) {
        gIn.tracer.trace1('Using one more chance to click combo box (inputEl click failed)');
        return clickCb(inputEl, count + 1);
      })
      .then(function () {
        gIn.tracer.trace3('Before get list item');
        return gT.s.browser.executeScript(jsGetListItem, false);
      })
      .then(gT.e.lClick.createFuncPrintTextDelayClick(isDblClick, logAction))
      .catch(function (err) {
        gIn.tracer.trace1('Using one more chance to click combo box (item click failed)');
        return clickCb(inputEl, count + 1);
      });
  };
}

function createFuncClickCbByJsToGetInputEl(jsGetInputEl, jsWaitBoundList, jsGetListItem, isDblClick, logAction) {
  return function () {
    return gT.s.browser.executeScript(jsGetInputEl, false)
      .then(createFuncClickCbByInputEl(jsWaitBoundList, jsGetListItem, isDblClick, logAction));
  }
}

function clickCb(logText, jsGetInputEl, jsWaitBoundList, jsGetListItem, isDblClick, logAction) {
  return gIn.wrap(logText, logAction, function () {
    return gT.s.browser.executeScript(jsGetInputEl, false)
      .then(createFuncClickCbByInputEl(jsWaitBoundList, jsGetListItem, isDblClick, logAction));
  });
}

exports.idIndex = function (cbId, itemIndex, logAction) {
  return gIn.wrap(``, logAction, function () {
    return gT.s.browser.executeScript(`return tiaEJ.hEById.getNameAndLabels('${cbId}');`, false)
      .then(function (obj) {
        gIn.logger.logIfNotDisabled(
          `Click combo box(name: '${obj.name}', label: '${obj.label}'), item #${itemIndex}`, logAction);
      })
      .then(createFuncClickCbByJsToGetInputEl(
{'fraction_non_alphanumeric': 0.11779722830051058, 'fraction_numerical': 0.0029175784099197666, 'mean_word_length': 3.3257097791798107, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5370570', 'n_tokens_mistral': 1772, 'n_tokens_neox': 1751, 'n_words': 375}