 
         callback invitationUrl
 
-
-  enableAndDisableApiAccess: (browser, enableApi = no, disableApi = no) ->
-
-    apiAccessSelector       = '.kdtabhandle-tabs .api-access.AppModal-navItem'
-    toggleSwitch            = '.settings-row .koding-on-off.small.'
-    toggledOffSwitch        = "#{toggleSwitch}off"
-    toggledOnSwitch         = "#{toggleSwitch}on"
-    addNewApiDisabledButton = '.kdtabhandlecontainer.hide-close-icons [disabled="disabled"]'
-    addnewApiButton         = '.kdtabhandlecontainer.hide-close-icons .add-new'
-
-    browser
-      .waitForElementVisible     apiAccessSelector, 20000
-      .click                     apiAccessSelector
-      .element 'css selector', toggledOffSwitch, (result) ->
-        if result.status is 0
-          if enableApi
-            browser
-              .waitForElementVisible     toggledOffSwitch, 20000
-              .pause                     2000
-              .waitForElementVisible     addNewApiDisabledButton, 20000
-              .click                     toggledOffSwitch
-              .pause                     2000
-
-          if disableApi
-            browser
-              .waitForElementVisible     toggledOnSwitch, 20000
-              .waitForElementVisible     addnewApiButton, 20000
-              .click                     toggledOnSwitch
-              .pause                     2000
-              .waitForElementVisible     addNewApiDisabledButton, 20000
-
-
-  addNewApiToken: (browser) ->
-
-    addnewApiButton = '.kdtabhandlecontainer.hide-close-icons .add-new'
-    confirmDelete   = '.kddraggable.with-buttons .clearfix .solid.red'
-    deleteButton    = '.kdlistitemview-member .role.subview .delete'
-    tokenTimeStamp  = '.kdlistitemview-member .details .time'
-
-    browser
-      .click                     addnewApiButton
-      .waitForElementVisible     deleteButton, 20000
-      .assert.containsText       tokenTimeStamp, 'Created less than a minute ago by'
-      .click                     deleteButton
-      .waitForElementVisible     deleteButton, 20000
-      .click                     confirmDelete
-      .waitForElementNotVisible  tokenTimeStamp, 20000
-
-
-  editStackName: (browser) ->
-
-    stackTemplateSettingsButton = '.kdbutton.stack-settings-menu'
-    saveAndTestButton           = '.buttons button:nth-of-type(5)'
-    cancelButton                = '.buttons button:nth-of-type(2)'
-    stacksLogsSelector          = '.step-define-stack .kdscrollview'
-    myStackTemplatesButton      = '.kdview.kdtabhandle-tabs .my-stack-templates'
-    templateInputSelector       = '.template-title-form .template-title .input-wrapper input'
-    editedText                  = 'Edit stack name'
-    stackNameSelector           = '.stacktemplate-info.clearfix .title'
-    editButtonSelector          = '.kdbuttonmenu .context-list-wrapper .edit'
-
-    browser
-      .waitForElementVisible      stackTemplateSettingsButton, 20000
-      .click                      stackTemplateSettingsButton
-      .waitForElementVisible      editButtonSelector, 20000
-      .click                      editButtonSelector
-      .waitForElementVisible      templateInputSelector, 20000
-      .clearValue                 templateInputSelector
-      .setValue                   templateInputSelector, editedText
-      .click                      saveAndTestButton
-      .pause                      2000 #for stack creation logs to appear
-      .waitForElementVisible      stacksLogsSelector, 20000
-      .assert.containsText        stacksLogsSelector, 'An error occured: Required credentials are not provided yet'
-      .click                      myStackTemplatesButton
-      .waitForElementVisible      templateInputSelector, 20000
-      .click                      cancelButton
-      .waitForElementVisible      stackNameSelector, 20000
-      .assert.containsText        stackNameSelector, editedText
-
-
-  closeTeamSettingsModal: (browser) ->
-
-    adminModal  = '.AppModal.AppModal--admin.team-settings'
-    closeButton = "#{adminModal} .closeModal"
-
-    browser
-      .waitForElementVisible adminModal, 20000
-      .click                 closeButton
 
 
   logoutTeam: (browser, callback) ->

        done()



  clickSaveAndTestButton: (browser) ->

    saveAndTestButton    = '.template-title-form .buttons .save-test'
    editorSelector       = '.stack-template .output .output-view'
    loaderIconNotVisible = "#{saveAndTestButton} .kdloader.hidden"

    browser
      .waitForElementVisible     saveAndTestButton, 20000
      .scrollToElement           saveAndTestButton
      .click                     saveAndTestButton
      .waitForElementVisible     editorSelector, 35000
      .waitForElementVisible     '.template-title-form .buttons .save-test .kdloader', 20000
      .waitForElementNotVisible  loaderIconNotVisible, 500000
      .pause                     3000


  saveTemplate: (browser, deleteStack = yes, checkTags = yes, doExtra = yes) ->

    stackModal           = '.stack-modal'
    modalCloseButton     = "#{stackModal} .gray"
    stackTabSelector     = '.team-stack-templates .kdtabhandle.stack-template.active'
    finalizeStepsButton  = "#{sidebarSelector} a[href='/Home/Welcome']:not(.SidebarSection-headerTitle)"

    if doExtra
      @seeTemplatePreview(browser)
      @updateStackReadme(browser)
      @defineCustomVariables(browser)

    @clickSaveAndTestButton(browser)

    browser.element 'css selector', stackModal, (result) ->
      if result.status is 0
        browser
          .assert.containsText    stackModal, 'Your stack script has been successfully saved'
          .waitForElementVisible  modalCloseButton, 20000
          .click                  modalCloseButton

    browser
      .waitForElementVisible     stackTabSelector, 20000
      .waitForElementVisible     stackModalCloseButton, 20000
      .click                     stackModalCloseButton
      .pause                     3000
      .waitForElementVisible     sidebarStackSection, 20000
      .waitForElementNotPresent  finalizeStepsButton, 20000
      .waitForElementVisible     envMachineStateModal, 20000

    if checkTags
      @checkStackTemplateTags(browser, deleteStack)


  checkStackTemplateTags: (browser, deleteStack) ->

    inUseTag       = "#{stackCatalogModal} [testpath=StackInUseTag]"
    defaultTag     = "#{stackCatalogModal} [testpath=StackDefaultTag]"
    accessTag      = "#{stackCatalogModal} [testpath=StackAccessLevelTag]"
    deleteMenuItem = '.kdbuttonmenu .context-list-wrapper .delete'

    @openStackCatalog(browser, no)

    browser
      .waitForElementVisible  stackTemplateList, 20000
      .waitForElementVisible  inUseTag, 20000
      .waitForElementVisible  defaultTag, 20000
      .waitForElementVisible  accessTag, 20000
      .assert.containsText    accessTag, 'GROUP'

    if deleteStack
      browser
        .waitForElementVisible    stackSettingsMenuIcon, 20000
        .click                    stackSettingsMenuIcon
        .pause                    2000
        .waitForElementVisible    deleteMenuItem, 20000
        .click                    deleteMenuItem
        .assert.containsText      '.kdnotification.main', 'This template currently in use by the Team'
        .waitForElementNotVisible '.kdnotification.main', 30000

    browser
      .waitForElementVisible  stackModalCloseButton, 20000
      .click                  stackModalCloseButton


  editStack: (browser, openEditorAndClone = no) ->

    editMenuItem             = '.kdbuttonmenu .context-list-wrapper .edit'
    openEditorButton         = '.kdmodal-inner .kdmodal-buttons .red'
    openEditorAndCloneButton = '.kdmodal-inner .kdmodal-buttons .green'
    stackTemplatePage        = '.define-stack-view .stack-template'
    stackEditorSelector      = "#{stackTemplatePage} .editor-pane"
    numberNotification       = "#{sidebarStackSection} .SidebarListItem-unreadCount"
    myStacksPage             = '.environments-modal.My-Stacks'
    stackItems               = "#{myStacksPage} .environment-item"
    makeTeamDefaultButton    = '.stacks .template-title-form .set-default'
    makeTeamDefaultLoader    = "#{makeTeamDefaultButton} .kdloader.hidden"
    newStackTemplate         = '.stacktemplates .stack-template-list .stacktemplate-item:last-child .title'
    newStackInSidebar        = '.SidebarStackSection .SidebarMachinesListItem .SidebarMachinesListItem--MainLink'
    updateNotification       = "#{stackItems} .update-notification"
    updateStackButton        = "#{updateNotification} .reinit-stack"
    reinitModal              = '.kdmodal[testpath=reinitStack]'
    proceedButton            = "#{reinitModal} .red"
    sidebarStackWidget       = '.SidebarStackWidgets'


    browser.pause  3000
    @openStackCatalog(browser, no)

    browser
      .waitForElementVisible  stackTemplateList, 20000
      .waitForElementVisible  stackSettingsMenuIcon, 20000
      .click                  stackSettingsMenuIcon
      .pause                  2000
      .waitForElementVisible  editMenuItem, 20000
      .click                  editMenuItem
      .pause                  2000

    if openEditorAndClone
      browser
        .waitForElementVisible  openEditorAndCloneButton, 20000
        .click                  openEditorAndCloneButton
    else
      browser
        .waitForElementVisible  openEditorButton, 20000
        .click                  openEditorButton

    browser
      .waitForElementVisible  stackTemplatePage, 20000
      .waitForElementVisible  stackEditorSelector, 20000

    @setTextToEditor browser, 'template', staticContents.multiMachineStackTemplate

    if openEditorAndClone
      @createCredential(browser, no, no, yes)

    @clickSaveAndTestButton(browser)

    if openEditorAndClone
      browser
        .waitForElementVisible  makeTeamDefaultButton, 20000
        .click                  makeTeamDefaultButton
        .pause                  3000
        .waitForElementVisible  '.stacktemplates', 20000

    browser
      .waitForElementVisible  stackModalCloseButton, 20000
      .click                  stackModalCloseButton

    if openEditorAndClone
      browser
        .waitForElementVisible  sidebarStackWidget, 20000
        .waitForTextToContain   sidebarStackWidget, 'Team admin has changed the default stack'
        .click                  "#{sidebarStackWidget} a"
        .waitForElementVisible  stackItems, 20000
        .click                  "#{stackItems} .red"

    else
      browser
        .waitForElementVisible  newStackInSidebar, 20000
        .waitForElementVisible  numberNotification, 20000
        .click                  numberNotification
        .waitForElementVisible  stackItems, 20000
        .waitForElementVisible  updateNotification, 20000
        .assert.containsText    updateNotification, 'has updated this stack'
        .waitForElementVisible  updateStackButton, 20000
        .click                  updateStackButton

    browser
      .waitForElementVisible  reinitModal, 20000
      .click                  proceedButton
      .waitForTextToContain   sidebarSelector, 'mexample'


  deleteStack: (browser) ->

    deleteMenuItem = '.kdbuttonmenu .context-list-wrapper .delete'
    stackTemplate  = '.stacktemplates .stack-template-list [testpath=privateStackListItem]'
    stackMenuIcon  = "#{stackTemplate} .stack-settings-menu"
    deleteModal    = '.kdmodal[testpath=RemoveStackModal]'
    deleteButton   = "#{deleteModal} .red"


    browser.element 'css selector', stackCatalogModal, (result) ->
      if result.status is 0
        browser
          .waitForElementVisible  stackCatalogModal, 20000
          .waitForElementVisible  closeButton, 20000
          .click                  closeButton

    @openStackCatalog(browser)

    browser
      .waitForElementVisible    myStackTemplatesButton, 20000
      .click                    myStackTemplatesButton
      .waitForElementVisible    stackTemplate, 20000
      .pause                    3000
      .waitForElementVisible    stackMenuIcon, 20000
      .click                    stackMenuIcon
      .waitForElementVisible    deleteMenuItem, 20000
      .click                    deleteMenuItem
      .pause                    3000
      .waitForElementVisible    deleteModal, 20000
      .assert.containsText      "#{deleteModal} .kdmodal-title", 'Remove stack template ?'
      .waitForElementVisible    deleteButton, 20000
      .click                    deleteButton
      .waitForElementNotPresent deleteModal, 20000
      .waitForElementVisible    closeButton, 20000
      .click                    closeButton


  buildStack: (browser) ->

    buildStackButton     = "#{envMachineStateModal} .content-container .state-button"
    progressbarContainer = "#{envMachineStateModal} .progressbar-container"
    credentialsModal     = '.kdmodal[testpath=BuildRequirementsModal]'
    addedCredential      = "#{credentialsModal} .credential-item"
    titleInput           = "#{credentialsModal} input[name=title]"
    usernameInput        = "#{credentialsModal} input[name=username]"
    passwordInput        = "#{credentialsModal} input[name=password]"
    credentialSaveButton = "#{credentialsModal} button.green"
    useCredentialButton  = "#{addedCredential} .kdbutton:not(.secondary)"
    readmeMarkdown       = "#{envMachineStateModal} .content-readme"
    readmeHeader         = "#{readmeMarkdown} #test-stack-readme"
    readmeLink           = "#{readmeMarkdown} a"

    browser
      .waitForElementVisible     buildStackButton, 20000
      .waitForElementVisible     readmeMarkdown, 20000
      .waitForElementVisible     readmeHeader, 20000
      .waitForElementVisible     readmeLink, 20000
      .click                     buildStackButton
      .pause                     3500 # wait for credentials modal

    browser.element 'css selector', credentialsModal, (result) ->
      if result.status is 0

        browser.element 'css selector', addedCredential, (result) ->
          if result.status isnt 0
            browser
              .waitForElementVisible   titleInput, 20000
              .waitForElementVisible   usernameInput, 20000
              .waitForElementVisible   passwordInput, 20000
              .setValue                titleInput, 'Build requirement data for testing'
              .setValue                usernameInput, 'FOO'
              .setValue                passwordInput, 'BAR'
              .click                   credentialSaveButton
              .waitForElementVisible   addedCredential, 20000

          browser
            .waitForElementVisible     useCredentialButton, 20000
            .click                     useCredentialButton

      browser
        .waitForElementVisible     progressbarContainer, 20000
        .waitForElementNotPresent  progressbarContainer, 500000
        .pause                     3000 # wait for machine
        .waitForElementVisible     stackMachine, 20000

    browser.isStackBuilt = yes


  defineCustomVariables: (browser) ->

    wrongCustomVariable    = "foo: '"

    @setTextToEditor browser, 'template', staticContents.stackTemplate


  getAwsKey: -> return awsKey


  # possible values of tabName variable is 'stack', 'variables' or 'readme'
  switchTabOnStackCatalog: (browser, tabName) ->
