<commit_before>module.exports = class ClickOnAndWaitForElementByCss extends require '../action'
  clickOnAndWaitForElementByCssAction: (context, clickOn, waitFor, asserter, timeout = 10000) ->
    asserter or= timeout
    timeout = undefined if typeof asserter is 'number'

    context = do context
      .elementByCss clickOn
      .click

    if typeof waitFor is 'object'
      return @waitForTheFirstElementAction context, waitFor, asserter, timeout

    context.waitForElementByCss waitFor, asserter, timeout

  waitForTheFirstElementAction: (context, waitFor, asserter, timeout = 2000) ->
    asserter or= timeout
    timeout = undefined if typeof asserter is 'number'
    selectors = Object.keys waitFor

    context
      .noop()
      .then =>
        waitedElements = (@_browser.waitForElementByCss selector, asserter, timeout for selector in selectors )
        (require 'wd').Q.allSettled waitedElements
      .then (states) ->
        for result, index in states
          if result.state is 'fulfilled'
            return do waitFor[selectors[index]]

        throw new Error "Not found any of the requested elements: #{selectors}"
<commit_msg>waitForTheFirstElementByCss action race the promises (ref #4)<commit_after>{Q} = require '../../main'
module.exports = class ClickOnAndWaitForElementByCss extends require '../action'
  clickOnAndWaitForElementByCssAction: (context, clickOn, waitFor, asserter, timeout) ->
    asserter or= timeout
    timeout = undefined if typeof asserter is 'number'

    context = do context
      .elementByCss clickOn
      .click

    if typeof waitFor is 'object'
      return @waitForTheFirstElementAction context, waitFor, asserter, timeout

    context.waitForElementByCss waitFor, asserter, timeout

  waitForTheFirstElementByCssAction: (context, waitFor, asserter, timeout) ->
    asserter or= timeout
    timeout = undefined if typeof asserter is 'number'
    selectors = Object.keys waitFor
    deferred = do Q.defer

    context
      .noop()
      .then =>
        for selector in selectors
          @_browser
            .waitForElementByCss selector, asserter, timeout
            .then (do (selector) ->->
              return true if not deferred.promise.isPending()
              deferred.resolve selector)

            .fail ->
              return true if not deferred.promise.isPending()
              do deferred.reject

    deferred.promise
      .fail -> throw new Error "Not found any of the requested elements: #{selectors}"
      .then (selector) -> do waitFor[selector]
{'fraction_non_alphanumeric': 0.07985837922895359, 'fraction_numerical': 0.003933910306845004, 'mean_word_length': 2.930448222565688, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '1880361', 'n_tokens_mistral': 721, 'n_tokens_neox': 692, 'n_words': 223}