Ask a Question

run cucumber selenium tests in gitlab-ci

rezza
Occasional Contributor

run cucumber selenium tests in gitlab-ci

Hi
I have a selenium cucumber project with some tests.

The tests worked and the chromeDriver had launch when I run the -mvn clean compile test in my IDE.

I created this .gitlab-ci.yml in my project's root:

image: markhobson/maven-chrome:jdk-11

stages:
  - build
  - test

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


cache:
  paths:
    - .m2/repository/
    - target/

build:
  stage: build
  script:
    - mvn $MAVEN_OPTS clean package
    - mvn compile

test:
  stage: test
  script:
    - mvn test

When I run the pipeline, I face this error in the console:

Unable to load browser: Could not start a new session.
Possible causes are invalid address of the remote server or browser start-up failure.
Build info: version: '4.0.0-beta-1', revision: '9c03156c2b'
System info: host: 'runner-fa6cab46-project-25183469-concurrent-0', ip: '172.17.0.4', os.name: 'Linux', os.arch: 'amd64', os.version: '4.19.78-coreos', java.version: '11.0.10'
Driver info: org.openqa.selenium.chrome.ChromeDriver

What can I do for running the tests?

15 REPLIES 15

Re: run cucumber selenium tests in gitlab-ci

Hi,

 

You're gitlab-ci.yml looks good

 

Can you provide more logs of your pipeline?

Do you properly run chrome in headless mode?

 

        WebDriverManager.chromedriver().setup();

        ChromeOptions options = new ChromeOptions();
        options.setHeadless(true);

        driver = new ChromeDriver(options);

 

rezza
Occasional Contributor

Re: run cucumber selenium tests in gitlab-ci

@aurelien-reeves 
Hi

Thanks for replying.

What is needed to providing?

May I give all the pipeline's output?

This is my file which has the driver configurations.

And This is that file after adding the chromeDriver headLess option.

As you can see, I added the chromeDriver headLess option.

 

Before adding, everything is worked in IDE mode.

But when I add the chromeDriver headLess option, I face this error in the testNG's report:


feature (Login into account)
cucumber.runtime.CucumberException: java.lang.AssertionError: Unable to wait and click on the WebElement, using locator: <Proxy element for: DefaultElementLocator 'By.linkText: Sign in'>
	at cucumber.api.testng.TestNGCucumberRunner.runCucumber(TestNGCucumberRunner.java:69)
	at cucumber.api.testng.AbstractTestNGCucumberTests.feature(AbstractTestNGCucumberTests.java:21)
Caused by: java.lang.AssertionError: Unable to wait and click on the WebElement, using locator: <Proxy element for: DefaultElementLocator 'By.linkText: Sign in'>
	at org.junit.Assert.fail(Assert.java:89)
	at pageObjects.BasePage.waitAndClickElement(BasePage.java:59)
	at pageObjects.Login_page.clickOnSignInButton(Login_page.java:35)
	at stepDefinitions.LoginSteps.user_clicks_on_the_login_button_on_homepage(LoginSteps.java:18)
	at ✽.And User clicks on the login button on homepage(Login.feature:6)
... Removed 28 stack frames
(Runs Cucumber Feature)

 

I didn't run the test in GitLab mode. because first, I want to solve the problem in IDE mode.

I'm so new, please guide me.

Thanks in advanced.

Re: run cucumber selenium tests in gitlab-ci

For better readability, could you paste your code in the forum using the "insert code sample" snippet instead of JustPaste.it?

 

And I suggest you run your tests not in headless mode in your IDE. Maybe something is actually failing but as it is in Headless mode, you can not see it.

 

Can you remove the code about Firefox and IE to make it cleaner and easier to debug?

rezza
Occasional Contributor

Re: run cucumber selenium tests in gitlab-ci

sure.

here you are.

before adding headLess mode:

package utils;

import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.PageFactory;
import pageObjects.Login_page;

import java.io.FileInputStream;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

public class DriverFactory {
    public static WebDriver driver;
    public static Login_page loginPage;

    public WebDriver getDriver() {
        try {
            // Read Config
            Properties p = new Properties();
            FileInputStream fi = new FileInputStream(System.getProperty("user.dir") + "/src/main/java/properties/config.properties");
            p.load(fi);
            String browserName = p.getProperty("browser");

            switch (browserName) {
                case "chrome":
                    // code
                    if (null == driver) {
                        WebDriverManager.chromedriver().setup();
                        ChromeOptions options = new ChromeOptions();
                        options.setHeadless(true);
                    }
                    break;
            }
        } catch (Exception e) {
            System.out.println("Unable to load browser: " + e.getMessage());
        } finally {
            assert driver != null;
            driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
            loginPage = PageFactory.initElements(driver, Login_page.class);
        }
        return driver;
    }
}


after adding headLess mode:


package utils;

import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.PageFactory;
import pageObjects.Login_page;

import java.io.FileInputStream;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

public class DriverFactory {
    public static WebDriver driver;
    public static Login_page loginPage;

    public WebDriver getDriver() {
        try {
            // Read Config
            Properties p = new Properties();
            FileInputStream fi = new FileInputStream(System.getProperty("user.dir") + "/src/main/java/properties/config.properties");
            p.load(fi);
            String browserName = p.getProperty("browser");

            switch (browserName) {
                case "chrome":
                    // code
                    if (null == driver) {
                        WebDriverManager.chromedriver().setup();
                        ChromeOptions options = new ChromeOptions();
                        options.setHeadless(true);
                        driver = new ChromeDriver(options);
                        driver.manage().window().maximize();
                    }
                    break;
            }
        } catch (Exception e) {
            System.out.println("Unable to load browser: " + e.getMessage());
        } finally {
            assert driver != null;
            driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
            loginPage = PageFactory.initElements(driver, Login_page.class);
        }
        return driver;
    }
}

 

You mean I should two versions of the configuration. one for GitLab (with headLess mode) and one for IDE (without headLess mode)?

Re: run cucumber selenium tests in gitlab-ci

Thank you.

 

You mean I should two versions of the configuration. one for GitLab (with headLess mode) and one for IDE (without headLess mode)?


At least at first, while you are still setting it up, you should deactivate the headless mode in the IDE.

When you are sure it works everytime, you can try setting-up the headless mode.

Finally, setting-up gitlab-ci.

 

Also, regarding the following line:

driver.manage().window().maximize();

It is recommended to maximize the Google Chrome browser through ChromeOptions class as follows:

options.addArguments("start-maximized");

In your code, that may look like this:

WebDriverManager.chromedriver().setup();
ChromeOptions options = new ChromeOptions();
options.setHeadless(true);
options.addArguments('start-maximized');
driver = new ChromeDriver(options);
rezza
Occasional Contributor

Re: run cucumber selenium tests in gitlab-ci

I removed the maximization option at all for simplifying the codes.

There was no problem in IDE mode from the beginning. and everything worked.

But when I pushed my codes in GitLab and run the pipeline, I faced the error.

Which you guide me to adding the headLess mode.

And I still have this weird error in the pipeLine:

 

java.lang.AssertionError: Unable to wait and click on the WebElement, using locator: <[[ChromeDriver: chrome on LINUX (eef76c5b8bbbcbc0e66fa5d2ce0a6f17)] -> xpath: /html/body/div[1]/header/div/div[2]/div[2]/a[1]]>
	at org.junit.Assert.fail(Assert.java:89)
	at pageObjects.BasePage.waitAndClickElement(BasePage.java:59)
	at pageObjects.Login_page.clickOnSignInButton(Login_page.java:35)
	at stepDefinitions.LoginSteps.user_clicks_on_the_login_button_on_homepage(LoginSteps.java:18)

 

 

The weird part is that this error is not seen in the IDE mode (certainly without headLess option) and the tests work!

 

 

rezza
Occasional Contributor

Re: run cucumber selenium tests in gitlab-ci

@aurelien-reeves 
what should  I do my friend?

Re: run cucumber selenium tests in gitlab-ci

If I understand well, now, your tests are working well in your IDE, in both windowed and headless mode?

And in gitlab-ci, you do not have a "Unable to load browser" error anymore, but the "Unable to wait and click" one?

 

If so, I can see two reason for that:

- your test is not idempotent: the state of your page changes over time. To debug this, try to take a screenshot and to retrieve it as an artifact in gitlab-ci

- your app needs more time to properly load its content, and in gitlab-ci, docker images are not powerfull enough to process it in time. Try to globally increase the timeouts before a test is considered failing

 

To make sure the whole thing is properly working and to distinguish issues in tests from issues while trying to execute the tests, you can try to add a really simple "Hello world" test in your test-suite, which load the simplest possible page and check it is displayed by the browser.

 

If that test pass, you know that Selenium is properly set-up in the CI.

If not, you can continue working on setting it up.

rezza
Occasional Contributor

Re: run cucumber selenium tests in gitlab-ci

@aurelien-reeves 
Thanks again for guiding.

In IDE:
   without headLess mode:
       tests work fine without any error.
   with headLess mode:
       I have the "Unable to wait and click" error.

In gitlab:

    without headLess mode:
      I have the "Unable to load browser"
    with headLess mode:
      I have the "Unable to wait and click" error.

My test is including the login to Github site.
And I defined my feature file like this:


  • Scenario: Login into account with correct credentials
    Given User navigates to github website
    And User clicks on the login button on homepage
    And User enters a valid username
    And User enters a valid password
    When User clicks on the login button
    Then User should be taken to the successful login page


which in the build stage (in GitLab and with headLess) I have the "Unable to wait and click" error.

I deleted all the additional steps and I let the navigate step remain (which looks like "Hello World").
and when I run the pipeLine all the stages passed and the tests run. but I didn't see the chromeDriver be launched!!



I think I should add chromeDriver in my gitlab-ci.

What's your opinion?

cancel
Showing results for 
Search instead for 
Did you mean: