How to set up Pipeline for Jenkins, Selenoid, Allure

Carrying out autotests for speed and reliability of testing, the following technology stack is often used in development and CI / CD processes – Jenkins, Selenoid and Allure. There are several of their advantages: Jenkins has free access, a large number of features and plugins for extension; Selenoid has environment independence, each browser runs in a separate container; Allure, in turn, in recent years has become a popular tool for generating reports based on the results of autotests (we wrote more about this in the last article). At the same time, there is still little information on how these tools can be combined, and we want to share our example.

We have previously written about test automation tasks and possible checks… In this article, we will look at the processes of building a pipeline for Jenkins and setting up a build. To accomplish this task, we need the following:

  • Jenkins installed (we used Jenkins on a local machine with Ubuntu 20.04).

  • A locally running project in Python, in which all the necessary dependencies are written in requirements.txt, Allure is connected and there are autotests with added annotations. For instance, @allure.title("Позитивный тест расчёта") or @allure.step("Заполнить поле {locator_name} текстом {text}")… With this we get the “magic” in Allure – a report with all the details of the tests.

  • The project must be uploaded to the repository (in our case, GitHub).

  • Installed Docker and Docker Compose.

If your Jenkins hasn’t “made friends” with Docker, you can find out about it in the logs after building the build. You will need to add Jenkins to the Docker group – in particular, on Ubuntu this is done like this: sudo usermod -a -G docker jenkins

On this we can assume that the base is ready. Let’s start setting up the pipeline itself.

What the structure of the described project looks like:

/ pages

page for describing locators

base POM page

page with actions and checks in the test

data.py

Dockerfile

Jenkinsfile

browsers.json

conftest.py

pytest.ini

requirements.txt

test_main_page.py

Jenkins build launch settings

Below is an example of settings. Let’s clarify right away that we used Jenkins in Russian, but its localization is not completely complete, so some examples are given in English:

  • Press “New Item”, enter a name, choose Pipeline – Ok.

  • Check the box next to “Do not allow concurrent builds” so that one build waits for the other to complete.

  • We select “Remove obsolete assemblies”. We indicate how many recent assemblies should be stored (in our example, 5).

  • Select the parameterized assembly, click “Add Parameter” – String Parameter – and then:

    In the “Name” field, enter the CMD_PARAMS variable.
    In the default value, we indicate the path to the tests that we want to run. We have this test_main_page.py
    In the description, you can add comments about what features the assembly has.
    These actions will be useful when launching assemblies, they will allow you to change the launch flags and the tests submitted to the work.

  • Let’s move on to filling in the Pipeline in the settings. Select “Pipeline script from SCM”.

  • Git is selected as SCM. Fill in the “Repository URL”, click “Add Credentials” to enter the login and password for the git. We register the branch from which it is necessary to take the code, in our case it is * / master. Repository Viewer – Automatically.

    “Script Path” – Jenkinsfile, which lies at the root of the project.

Adding the Allure Plugin

In order for Allure to work, you need to configure the plugin. Go to Jenkins Dashboard> Jenkins Settings> Global Installs Configuration> Allure Commandline at the end of the list.

Configuring Jenkinsfile

Now let’s move on to Jenkinsfile and the pipeline itself.

pipeline {
  agent any
  stages {
     stage("Build image") {
        steps {
    	catchError {
      	   script {
        	      docker.build("python-web-tests", "-f Dockerfile .")
      	 }
          }
       }
    }
     stage('Pull browser') {
        steps {
           catchError {
              script {
      	    docker.image('selenoid/chrome:92.0')
      	  }
           }
        }
     }
     stage('Run tests') {
        steps {
           catchError {
              script {
          	     docker.image('aerokube/selenoid:1.10.4').withRun('-p 4444:4444 -v /run/docker.sock:/var/run/docker.sock -v $PWD:/etc/selenoid/',
            	'-timeout 600s -limit 2') { c ->
              	docker.image('python-web-tests').inside("--link ${c.id}:selenoid") {
                    	sh "pytest -n 2 --reruns 1 ${CMD_PARAMS}"
                	}
                    }
        	     }
      	 }
         }
     }
     stage('Reports') {
        steps {
           allure([
      	   includeProperties: false,
      	   jdk: '',
      	   properties: [],
      	   reportBuildPolicy: 'ALWAYS',
      	   results: [[path: 'report']]
    	   ])
  	}
         }
     }
}

The pipeline is divided into stages, in which the following actions are performed:

  • stage (“Build image”) – here we build the container with our tests (we will describe what happens in the Dockerfile separately).

  • stage (‘Pull browser’) – at this stage we raise the container with the browser, if it has not been downloaded earlier, download it.

  • stage (‘Run tests’) is perhaps the most interesting and confusing stage. Here we download the image and connect the Selenoid container, and the script -p 4444: 4444 -v /run/docker.sock:/var/run/docker.sock -v $ PWD: / etc / selenoid / passes port 4444 for Selenoid and the path to the browser configuration (as you remember, browsers.json is in the root of $ PWD: / etc / selenoid /). Flag -limit 2 indicates the number of browsers launched, here it is important to remember that the number of browsers being brought up depends on the power of the machine, 1 CPU + 1 Gb of RAM per browser is recommended. You can read more about timeout flags. here… Next, we run tests from the container that we collected earlier:

  • And the last stage (‘Reports’) is the creation of the Allure report. Here you need to specify the path to the folder with the generated reports, in our case it is path: 'report'… You can skip this step if you are not using Allure.

Getting driver from Selenoid

The next point on which questions may arise is setting the driver parameters in order for everything to work in Jenkins.

In conftest.py, our example has the following fixture to get the driver:

@pytest.fixture(scope="function")
def browser():
	chrome_options = webdriver.ChromeOptions()
	browser = webdriver.Remote(
    	   command_executor="http://selenoid:4444/wd/hub",
    	   desired_capabilities={'browserName': 'chrome',
                          	'version': '92.0'},
    	   options=chrome_options)
browser.maximize_window()

yield browser
browser.quit()</code></pre><p>Здесь обращаемся к адресу selenoid и порту 4444. Если вы хотите подключить дополнительно Selenoid UI и наблюдать за тем, что происходит в браузере, необходимо передать параметр 'enableVNC': True в desired_capabilities и, конечно, добавить Selenoid UI в пайплайн. Версия браузера в conftest.py должна соответствовать версии в browser.json.  </p><pre><code>browser.json

{
"chrome": {
"default": "92.0",
"versions": {
"92.0": {
"https://habr.com/ru/company/simbirsoft/blog/597703/image": "selenoid/chrome:92.0",
"port": "4444"
}
}
}
}

Dockerfile setup

The Dockerfile will contain few instructions since the project is modest and does not require additional layers:

FROM python:3.8.12

COPY requirements.txt .

RUN pip install -r requirements.txt

COPY . .

We take the Python image, copy our dependencies, install them, and copy our code.

Saving the report

And a little touch in pytest.ini so that Allure reports are saved in the correct folder:

[pytest]

addopts = --alluredir=report

In the configured Jenkins project, click the “Build with parameters” button. In this case, it is possible to enter the path to the test, the number of threads -n or the number of restarts –reruns in the passed parameter in case you change your mind. Then you can click “Collect” and wait for the tests to run. Open the test run build, click on its number and then open the Allure report, where you can see the run results and a detailed description of the tests.

If something does not start for you, first of all it is worth checking the Selenoid documentation, the logs in the falling Jenkins build (and after that, if necessary, look for other ways).

Conclusion

In the given example, you can configure the build assembly once and get a “combine” to run autotests on the required number of independent browsers. It is important that you can conduct tests at any convenient time (see a separate item in the settings and a comment on filling in Jenkins), for example, at night. The city falls asleep, tests wake up, and in the morning the team receives reports on the hundreds and thousands of checks performed.

Thank you for your attention! We hope that our experience was helpful to you.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *