Hey. Tell me please,

There is a project, auto-tests, which, for example, create some essence. The data for these entities is taken either from .properties by keys or from .txt documents again from resources (directory).

Now the task, put it on jenkins and configure Joby with the ability to run with different values. Lay out something laid out, everything is going, but with the data that is in the files inside the project.

Found that in Jenkins you can customize parameterization, i.e. run with parameter.

I decided to do this (take an example, that when we build, we need to change the URL, this is an example, that is, instead of URL, there can be absolutely any value and there can be several of them):

  • I create a Job Item on Jenkins, specify the parameter "This project is parameterized" = True

Create a variable:

 String Parameter Name = generalUrl Default Value = http://ru.stackoverflow.com 

further, I specify that this job item should be built using maven , the path to pom.xml and all that. And in goal I register - clean test

Save.

  • I go to the project, in the pom.xml file I specify

http://maven.apache.org/maven-v4_0_0.xsd "> 4.0.0

 <groupId>com</groupId> <artifactId>erm-nav-qa-integration-test</artifactId> <version>1.0-SNAPSHOT</version> <properties> <selenium.java.version>2.53.1</selenium.java.version> <testng.version>6.9.10</testng.version> <selenide.version>3.9.2</selenide.version> <junit.vesrion>4.12</junit.vesrion> <allure.version>1.4.23</allure.version> <maven.surefire.version>2.19.1</maven.surefire.version> <aspectj.version>1.8.5</aspectj.version> <maven.compiler.version>3.5.1</maven.compiler.version> <jetty.version>9.3.11.v20160721</jetty.version> <allure.maven.version>2.5</allure.maven.version> <apache.poi.version>3.9</apache.poi.version> <suiteName>smoke_ui_tests.xml</suiteName> <generalUrl>http://ru.stackoverflow.com</generalUrl> </properties> <dependencies> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>${testng.version}</version> </dependency> <dependency> <groupId>com.codeborne</groupId> <artifactId>selenide</artifactId> <version>${selenide.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.vesrion}</version> </dependency> <dependency> <groupId>ru.yandex.qatools.allure</groupId> <artifactId>allure-testng-adaptor</artifactId> <version>${allure.version}</version> <exclusions> <exclusion> <groupId>junit</groupId> <artifactId>junit</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>${apache.poi.version}</version> <exclusions> <exclusion> <artifactId>xml-apis</artifactId> <groupId>xml-apis</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>${apache.poi.version}</version> <exclusions> <exclusion> <artifactId>xml-apis</artifactId> <groupId>xml-apis</groupId> </exclusion> </exclusions> </dependency> </dependencies> <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.0.1</version> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>${maven.surefire.version}</version> <configuration> <suiteXmlFiles> <suiteXmlFile>${suiteName}</suiteXmlFile> </suiteXmlFiles> <testFailureIgnore>true</testFailureIgnore> <argLine> -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar" </argLine> <properties> <testUrl>${generalUrl}</testUrl> </properties> </configuration> <dependencies> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>${aspectj.version}</version> </dependency> </dependencies> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>${maven.compiler.version}</version> <configuration> <source>1.8</source> <target>1.8</target> <showWarnings>true</showWarnings> <showDeprecation>true</showDeprecation> </configuration> </plugin> <!--Needed only to show reports locally. Run jetty:run and open localhost:8080 to show the report--> <plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>${jetty.version}</version> <configuration> <webAppSourceDirectory>${project.build.directory}/site/allure-maven-plugin</webAppSourceDirectory> <stopKey>stop</stopKey> <stopPort>1234</stopPort> </configuration> </plugin> </plugins> </pluginManagement> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> </build> <reporting> <excludeDefaults>true</excludeDefaults> <plugins> <plugin> <groupId>ru.yandex.qatools.allure</groupId> <artifactId>allure-maven-plugin</artifactId> <version>${allure.maven.version}</version> <configuration> <properties> <outputDirectory>${basedir}/target/allure-reports/</outputDirectory> <allureResultsDirectory>${basedir}/target/allure-results</allureResultsDirectory> </properties> </configuration> </plugin> </plugins> <outputDirectory>${basedir}/site</outputDirectory> </reporting> 

There is a key with the value baseUrl=${generalUrl} .properties file. It seems to have done everything as I found bit by bit somewhere, I run my tests, I get the error:

 org.openqa.selenium.WebDriverException: unknown error: unhandled inspector error: {"code":-32603,"message":"Cannot navigate to invalid URL"} (Session info: chrome=53.0.2785.89) 

I turn on the debugger and see that at the stage of reading the .properties file, the system takes just ${generalUrl} , i.e. just like text ...

Tell me how you can transfer variables from Jenkins. how to configure pom.xml , .properties so that you can perform a parameterized launch.

Thank.

UPD: Added pom.xml

  • Something I do not see you write to the file .properties - Aleksandr
  • @Aleksandr Agree. I felt that, somewhere something is wrong :). The question is how and at what point in the build build to write to the .properties file And what to write? ... - Andrey Yaremenko

2 answers 2

You basically have two tasks:

  • forward the values ​​of build parameters from Jenkins to the property of the maven project (property).
  • process the .properties file (s), replacing the ${generalUrl} with the values ​​of the maven properties.

  1. Maven interprets JVM arguments -Dfoo=bar as project properties. Jenkins build parameter values ​​are available as ${MYPARAM} view macros.

    To pass a parameter from Jenkins to maven, in the corresponding build step in the Goals and options field, it remains to associate the maven property with the Jenkins parameter like this:

     <!-- language: none --> clean install -DgeneralUrl=${generalUrl} 

    (replace clean install with what you use in your case)


  1. To substitute properties into resources, maven has a turnkey solution as part of the Maven Resources Plugin - filtering resources .

    Add the current version of the plugin to pom.xml :

     <!-- language: xml --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.0.1</version> </plugin> 

    Just in the <build> section of your pom-file, enable filtering for the necessary resources:

     <!-- language: xml --> <resource> <resource> <directory>src/test/resources</directory> <filtering>true</filtering> </resource> <resource> 

    Now, when executing the maven-phase process-test-resources resources:testResources , the resources:testResources task will be executed, which will replace the ${generalUrl} expression in the src/main/resources/data.properties with the generalUrl property from the pom-file, which in turn can wag from Jenkins.

  • Thanks for the answer. It turns out that I can actually perform the usual request through the console mvn clean -DgeneralUrl=http://google.com test , i.e. how to make a team imitation with Jenkins, right? And in this case, mvn will pass the generalUrl parameter with the value http://google.com , the plug-in inserted into the build module will perform the forwarding of this value into the .properties file and the method that reads the value will eventually get http://google.com . So? - Andrey Yaremenko
  • Yes, that's it . Nofate
  • Jenkins actually launches Maven "with the usual console query" and not magic) - Nofate
  • I added the description of the pom.xml file and when I run the command described above from the console, I get the error: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.19.1:test (default-test) on project erm-nav-qa-integration-test: Unable to parse configuration of mojo org.apache.maven.plugins:maven-surefire-plugin:2.19.1:test for parameter properties: Cannot assign configuration entry 'properties' with value 'http://google.com' of type java.lang.String to property of type java.util.Properties -> [Help 1] - Andrey Yaremenko
  • @AndreyYaremenko, the problem is obviously in the <properties>${generalUrl}</properties> . - Nofate

Thanks to all! I decided using profiles http://software-testing.ru/library/testing/general-testing/1984

And a little rewrote the download from the perperty files. According to the link above, everything is described in detail and there is a link to the archetype of the project, with an example of poma with profiles.

Now, the tests are run both locally from the IDE, the default value is taken from "dibag.property", and with the help of the command mvn clean -DgeneralUrl=http://google.com test , which forward this value to a variable. Further with setting in Jenkins, business everyday :).

  • Post it as an update to your question. - andreycha