github.com/jaylevin/jenkins-library@v1.230.4/vars/seleniumExecuteTests.groovy (about)

     1  import static com.sap.piper.Prerequisites.checkScript
     2  
     3  import com.sap.piper.ConfigurationHelper
     4  import com.sap.piper.GenerateDocumentation
     5  import com.sap.piper.GitUtils
     6  import com.sap.piper.Utils
     7  import com.sap.piper.k8s.ContainerMap
     8  import groovy.transform.Field
     9  import groovy.text.GStringTemplateEngine
    10  
    11  @Field String STEP_NAME = getClass().getName()
    12  
    13  //TODO: limit parameter visibility
    14  @Field Set GENERAL_CONFIG_KEYS = [
    15      /**
    16       * Defines the tool which is used for executing the tests
    17       * @possibleValues `maven`, `npm`, `bundler`
    18       */
    19      'buildTool',
    20      /** @see dockerExecute */
    21      'containerPortMappings',
    22      /** @see dockerExecute */
    23      'dockerEnvVars',
    24      /** @see dockerExecute */
    25      'dockerImage',
    26      /** @see dockerExecute */
    27      'dockerName',
    28      /** @see dockerExecute */
    29      'dockerOptions',
    30      /** @see dockerExecute */
    31      'dockerWorkspace',
    32      /**
    33       * With `failOnError` the behavior in case tests fail can be defined.
    34       * @possibleValues `true`, `false`
    35       */
    36      'failOnError',
    37      /**
    38       * Only if `testRepository` is provided: Branch of testRepository, defaults to master.
    39       */
    40      'gitBranch',
    41      /**
    42       * Only if `testRepository` is provided: Credentials for a protected testRepository
    43       * @possibleValues Jenkins credentials id
    44       */
    45      'gitSshKeyCredentialsId',
    46      /**
    47       * Defines the id of the user/password credentials to be used to connect to a Selenium Hub. The credentials are provided in the environment variables `PIPER_SELENIUM_HUB_USER` and `PIPER_SELENIUM_HUB_PASSWORD`.
    48       */
    49      'seleniumHubCredentialsId',
    50      /** @see dockerExecute */
    51      'sidecarEnvVars',
    52      /** @see dockerExecute */
    53      'sidecarImage',
    54      /** @see dockerExecute */
    55      'sidecarName',
    56      /** @see dockerExecute */
    57      'sidecarVolumeBind',
    58      /** @see dockerExecute */
    59      'stashContent',
    60      /**
    61       * Define an additional repository where the test implementation is located.
    62       * For protected repositories the `testRepository` needs to contain the ssh git url.
    63       */
    64      'testRepository'
    65  ]
    66  @Field Set STEP_CONFIG_KEYS = GENERAL_CONFIG_KEYS
    67  @Field Set PARAMETER_KEYS = STEP_CONFIG_KEYS
    68  
    69  /**
    70   * Enables UI test execution with Selenium in a sidecar container.
    71   *
    72   * The step executes a closure (see example below) connecting to a sidecar container with a Selenium Server.
    73   *
    74   * When executing in a
    75   *
    76   * * local Docker environment, please make sure to set Selenium host to **`selenium`** in your tests.
    77   * * Kubernetes environment, plese make sure to set Seleniums host to **`localhost`** in your tests.
    78   *
    79   * !!! note "Proxy Environments"
    80   *     If work in an environment containing a proxy, please make sure that `localhost`/`selenium` is added to your proxy exclusion list, e.g. via environment variable `NO_PROXY` & `no_proxy`. You can pass those via parameters `dockerEnvVars` and `sidecarEnvVars` directly to the containers if required.
    81   */
    82  @GenerateDocumentation
    83  void call(Map parameters = [:], Closure body) {
    84      handlePipelineStepErrors(stepName: STEP_NAME, stepParameters: parameters) {
    85          def script = checkScript(this, parameters) ?: this
    86          def utils = parameters?.juStabUtils ?: new Utils()
    87          String stageName = parameters.stageName ?: env.STAGE_NAME
    88  
    89          // load default & individual configuration
    90          Map config = ConfigurationHelper.newInstance(this)
    91              .loadStepDefaults([:], stageName)
    92              .mixinGeneralConfig(script.commonPipelineEnvironment, GENERAL_CONFIG_KEYS)
    93              .mixinStepConfig(script.commonPipelineEnvironment, STEP_CONFIG_KEYS)
    94              .mixinStageConfig(script.commonPipelineEnvironment, stageName, STEP_CONFIG_KEYS)
    95              .mixin(parameters, PARAMETER_KEYS)
    96              .dependingOn('buildTool').mixin('dockerImage')
    97              .dependingOn('buildTool').mixin('dockerName')
    98              .dependingOn('buildTool').mixin('dockerWorkspace')
    99              .use()
   100  
   101          utils.pushToSWA([
   102              step: STEP_NAME,
   103              stepParamKey1: 'scriptMissing',
   104              stepParam1: parameters?.script == null
   105          ], config)
   106  
   107          // Inject config via env vars so that scripts running inside selenium can respond to that
   108          config.dockerEnvVars = config.dockerEnvVars ?: [:]
   109          config.dockerEnvVars.PIPER_SELENIUM_HOSTNAME = config.dockerName
   110          config.dockerEnvVars.PIPER_SELENIUM_WEBDRIVER_HOSTNAME = config.sidecarName
   111          if(config.containerPortMappings[config.sidecarImage]){
   112              config.dockerEnvVars.PIPER_SELENIUM_WEBDRIVER_PORT = '' + (config.containerPortMappings[config.sidecarImage][0]?.containerPort ?: '')
   113          }
   114  
   115          dockerExecute(
   116                  script: script,
   117                  containerPortMappings: config.containerPortMappings,
   118                  dockerEnvVars: config.dockerEnvVars,
   119                  dockerImage: config.dockerImage,
   120                  dockerName: config.dockerName,
   121                  dockerOptions: config.dockerOptions,
   122                  dockerWorkspace: config.dockerWorkspace,
   123                  sidecarEnvVars: config.sidecarEnvVars,
   124                  sidecarImage: config.sidecarImage,
   125                  sidecarName: config.sidecarName,
   126                  sidecarVolumeBind: config.sidecarVolumeBind
   127          ) {
   128              try {
   129                  sh returnStatus: true, script: """
   130                      node --version
   131                      npm --version
   132                  """
   133                  config.stashContent = config.testRepository
   134                      ?[GitUtils.handleTestRepository(this, config)]
   135                      :utils.unstashAll(config.stashContent)
   136                  if (config.seleniumHubCredentialsId) {
   137                      withCredentials([usernamePassword(credentialsId: config.seleniumHubCredentialsId, passwordVariable: 'PIPER_SELENIUM_HUB_PASSWORD', usernameVariable: 'PIPER_SELENIUM_HUB_USER')]) {
   138                          body()
   139                      }
   140                  } else {
   141                      body()
   142                  }
   143              } catch (err) {
   144                  if (config.failOnError) {
   145                      throw err
   146                  }
   147              }
   148          }
   149      }
   150  }