0

I am trying to run cucumber maven tests on gitlab ci using selenium grid, from the output I have observed that containers for grid setup are up and running.

But I am not able to run my cucumber tests using maven.

Below is my docker-compose.yml

version: "3"
services:
  chrome:
    image: selenium/node-chrome:110.0
    depends_on:
      - selenium-hub
    environment:
      - SE_EVENT_BUS_HOST=selenium-hub
      - SE_EVENT_BUS_PUBLISH_PORT=4442
      - SE_EVENT_BUS_SUBSCRIBE_PORT=4443

  edge:
    image: selenium/node-edge:110.0
    shm_size: 2gb
    depends_on:
      - selenium-hub
    environment:
      - SE_EVENT_BUS_HOST=selenium-hub
      - SE_EVENT_BUS_PUBLISH_PORT=4442
      - SE_EVENT_BUS_SUBSCRIBE_PORT=4443

  firefox:
    image: selenium/node-firefox:110.0
    shm_size: 2gb
    depends_on:
      - selenium-hub
    environment:
      - SE_EVENT_BUS_HOST=selenium-hub
      - SE_EVENT_BUS_PUBLISH_PORT=4442
      - SE_EVENT_BUS_SUBSCRIBE_PORT=4443

  selenium-hub:
    image: selenium/hub:latest
    container_name: selenium-hub
    depends_on:
      - java-maven
    ports:
      - "4442:4442"
      - "4443:4443"
      - "4444:4444"

  java-maven:
    image: maven:3.8-openjdk-11

When I tried with below gitlab-ci.yml

variables:
  MAVEN_OPTS: "-Dmaven.repo.local=$CI_PROJECT_DIR/.m2/repository"

stages:
  - test
  
services:
  - docker:20.10.16-dind

test:
  stage: test
  image: docker:latest
  before_script:
    - docker-compose up -d
    - sleep 10
  script:
    - mvn ${MAVEN_OPTS} test -Dcucumber.filter.tags=${FEATURE_TAG} 
  artifacts:
    when: always
    name: "report"
    paths:
      - test-output/*
    expire_in: 1 h

I got "mvn: not found" error.

So I tried to run it inside the java-maven service by changing snippet of script section in the above yml with the below snippet:

docker run -it --rm -v "$(pwd)":/opt/maven -w /opt/maven maven:3.8-openjdk-11 mvn ${MAVEN_OPTS} test -Dcucumber.filter.tags=${FEATURE_TAG}

When I tried to run it like that, the job got stuck for a while and afterwards it responded with "the input device is not a TTY".

Kindly requesting to help me resolve the issue and run the tests successfully.

2 Answers 2

1

The problem appears to be with the command you're using to execute Maven inside the java-maven service.

Instead of running the command within the docker run command, you may define it in the GitLab CI job's script section, as seen below:

 script:
  - docker-compose up -d
  - sleep 10
  - docker run --rm --network host -v "$(pwd)":/app -w /app maven:3.8-openjdk-11 mvn ${MAVEN_OPTS} test -Dcucumber.filter.tags=${FEATURE_TAG}

Now, we're putting the current working directory ($(pwd)) within the container at /app and then running the Maven command inside the container. We're also using the —network host option to guarantee that the container can communicate with the Selenium Grid containers operating on the host network.

Additionlly, ensure that mvn is installed locally on your workstation or in the GitLab runner image that you are using. If mvn is not found, you may install it using the following command:

apt-get update && apt-get install -y maven

Install mvn before running the tests by include this command in the before script section.

I hope that help

0
1

As per the error "mvn: not found", it seems like Maven is not installed in the docker container. To install Maven, you can add the following command in the before_script section of the gitlab-ci.yml file:

- apk add maven

Also, you can try running the tests inside the java-maven service by using the below script:

script:
  - docker-compose up -d
  - docker exec -it java-maven mvn ${MAVEN_OPTS} test -Dcucumber.filter.tags=${FEATURE_TAG}

This should execute the Maven test command inside the container, which has Maven installed.

In addition, you may need to check if the correct version of Selenium WebDriver and Grid dependencies are added in the Maven project. You can add the following dependencies in your pom.xml file:

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.14.0</version>
</dependency>

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-server</artifactId>
    <version>3.14.0</version>
</dependency>

Make sure you are using the latest version of Selenium Grid as well. You can download it from the official Selenium website and add it to the path.

If you have confirmed that Maven is installed on your GitLab Runner, but you are still getting the "mvn: not found" error, then it could be that the Maven executable is not included in the PATH environment variable of your GitLab Runner.

You can try updating the PATH environment variable in your .gitlab-ci.yml file as follows:

variables:
  MAVEN_OPTS: "-Dmaven.repo.local=$CI_PROJECT_DIR/.m2/repository"
  PATH: "/usr/local/apache-maven/bin:$PATH"

This assumes that Maven is installed in the default location at /usr/local/apache-maven. If it is installed in a different location, update the PATH variable accordingly.

If this does not resolve the issue, you can try running the command echo $PATH in the GitLab Runner to check if Maven is included in the PATH. If it is not, you can try manually adding it to the PATH by running the command export PATH=$PATH:/usr/local/apache-maven/bin before running the mvn command.

Here are some useful references for running Selenium tests with GitLab CI:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.