Selenium tests works in local but not in Gitlab CI

Viewed 2101

I am currently working on a Java Spring Boot project which involves a classic backend/frontend architecture. I am trying to write some basic integration test by using the Selenium WebDriver.

The problem is that the tests I write pass without any problem on my local development machine but do not pass when I run them thorugh the continuous integration setup (Gitlab CI).

The code of the example test is the following:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
public class ExampleTest {
    
    @LocalServerPort 
    private int port;
    
    WebDriver wd;
    
    @Test
    public void successfulLogin(){
        String url = "http://localhost:" + port;
        wd = new HtmlUnitDriver();
        wd.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
        wd.get(url);
    }
}

The relative gitlab-ci.yml portion is:

stages:
  - test

maven-test:
  image: maven:3.5.0-jdk-8
  stage: test
  script: "mvn test -B -Dmaven.repo.local=/root/.m2/"
  only:
    - master

The CI has a single runner (version 9.5.0) with concurrency 1. It uses the docker executor with the docker:stable image. I don't know if it is needed, but it is running in priviledged mode. When running the tests on the CI environment, they fail with the following error:

org.openqa.selenium.TimeoutException: java.net.SocketTimeoutException: Read timed out

I have tried both with url = "http://localhost:" + port and url = InetAddress.getLocalHost().getHostName() + ":" + port, both passed locally, none passed in the CI environment.

What am I doing wrong?


EDIT: Alfageme suggested a more accurate testing methodology. On the same server on which the CI is running, I cloned my repository with git clone and then run the following command:

sudo gitlab-runner exec docker maven-test

The test passed without any problem. I am really running out of ideas, does someone have any?

1 Answers

I am not exactly sure why, but clearing the various runner-XXXXX-project-21-concurrent-0-cache-XXXXXXXXXXXXXXXXXXX docker containers on the CI machine seemed to have solved the issue.


EDIT: This fixed the issue only momentarely. The problem happened again, this time clearing the cache voulmes did not help. Anyone has any suggestion?

Related