In my Selenium-TestNG-Maven automation framework, I am have properties file for storing 'application-url'. The Jenkins job is configured for this. I have to run this test suite on QA/Stage/Production server based on need. But not sure how can I dynamically provide or override the 'application-url' property used in the code. (Environment Injector plugin? How?)
As per code, Selenium WebDriver instance in created, then java code reads the properties file and same URL is used by Selenium instance to open a webpage.
I dynamically provide URLs by using the choice parameter for my Jenkins job.
First select this project is parameter as pictured below:
Then select the Add Parameter > Choice Parameter as pictured below:
Then save.
Now when you are building you will be able to select the url to use for that particular build, like this:
And when you need to reference the url, you can pass it through to maven by using the $URL in the Maven Goals.
ADDED: Here is an example of how I am using the choice parameter in the Maven goals
I am passing the ${browserName} parameter that is defined using the Choice Parameter through maven which will be referenced using the system property "browser". So when I want to get the value in my code, I just use
System.getProperty("browser");
And I am able to access that passed parameter.
You can configure your jenkins to pull the latest code from any of the repository (Github, SVN) and in the Execute Shell build step, you can write a shell script to achieve the same.
Related
I have a Windows 7 and I am testing an Android Native app using Appium using Java. The framework used is TestNG. I would like to use Jenkins for running smoke testing for builds available in Jenkins. My desired capabilities are listed in #Before. Can we dynamically paremetrize all of the following in Jenkins?
“automationName”
“platformName”
“platformVersion”
“deviceName”
“app”
“appPackage”
“appActivity“
If so How?
Now if I dynamically parametrize, what would happen to the desired capabilities in my script #Before
its quite simple,
in your class you have to include the code which will listen for input from jenkin job.
String deviceName = PropertyUtils.getProperty("deviceName");
and while you configure your job in Jenkins you have to include string parameter like the image below,
and while building, pass the parameter you want for each build,
The value which you passing above will be processed in your code.
I have Eclipse and also installed manually Maven on win7-64 machine.
I need to be able to pass data to the Java and Java/JUnit test app.
It works in the console. F.e. if I do :
mvn -Dvar1=blah1 -Dvar2=blah2 test
I can read the data in the Java/Junit code like this :
String var1 = System.getPropertiy("var1")
String var2 = System.getPropertiy("var2")
But if I do "maven test" with the Eclipse "internal" maven (m2e) and ofcourse specify "-Dvar1=blah1 -Dvar2=blah2" in VM-properties in the Run-as box, the values are "null" when I try to print them.
Can you point me to what to look for, so I can solve this problem.
(It has to work in both environments).
Btw. I don't add anything to pom.xml to make this work in the first case.
I'm saying this because the Q/A I see that closely resemble my problem, seems to imply I have to add something to pom.xml!!
My more broad question is passing small configuration tokens of data via this properties-mechanism the best-practice, correct method to do that.
I'm not sure, I understood your question correctly. I'm speculating that you are having trouble in passing params for 'maven test' in eclipse.
If so, Run As --> Run Configurations --> Create a new maven configuration from 'Maven Build' on left pane
In 'Main' tab, choose your project --> Then use 'Add' button below to add parameters. Then you will be able to read those params as system properties in program
I would like to create a simple Jenkins plugin.
Basically it is a custom build step (Builder extension) with a dropdown list. The trick is that I want to fill this dropdown list from the result of a shell script/command executed during configuration time.
Here is my method stub.
public ListBoxModel doFill...Items(#AncestorInPath AbstractProject project) {
// determine workspace
// project.getSomeWorkspace() or project.getSomeWorkspace().getRemote()
...
// invoke some shell commands
String[] results = ...
ListBoxModel items = new ListBoxModel();
for (String item : results) {
items.add(item);
}
return items;
}
My questions are the following:
If there are slaves too, does Jenkins maintain 2 workspaces (so the freshest source code will exist both locations?
My understanding is that (after the first build) there are always two workspaces: on the master there are meta informations (and source code too?), on the slave there are the source code and the build intermediates, information, artifacts. (unless we extract artifacts or use the copy-to-slave-plugin)
Where will be the workspace what I get with the project.getSomeWorkspace() or project.getSomeWorkspace().getRemote()? (master/slave?)
How can I invoke a shell command on the machine that WILL execute the build? Or at least is there a way to choose the master / one of the slaves particularly? (Suppose that I already configured a label on which group of machines I want to run the job.)
I don't have access to AbstractBuild, BuildListener and Launcher (since they don't exist yet...)
How can I find out which properties can I get with #AncestorInPath.
I understand that this is a shorthand, an injection from the StaplerRequest invoked by Jenkins? How can I see the request?
It is important where the execution of the shell command takes place, even if there are two identical workspaces on master and slave. In my case there may be a Windows master (in the future) and an OSX slave. I do need the OSX to run my commands. (Currently there is only a master on OSX.)
EDIT:
Here is an example, part of what I am trying to do. I created a simple Xcode project in Swift (JenkinsSwift). In terminal from the project's directory I can issue the following command:
xcodebuild -project JenkinsSwift.xcodeproj -list
And get the following response:
Targets:
JenkinsSwift
JenkinsSwiftTests
Build Configurations:
Debug
Release
If no build configuration is specified and -scheme is not passed then "Release" is used.
Schemes:
JenkinsSwift
During configuration time I want to navigate to the project workspace on an OS X machine I want to issue the previous command. This way I could parse this response and allow the users to choose a Target / Configuration / Scheme during configuration. (Of course this can be done manually from with bash scripts, but I wanted to make it easier.)
This works differently, there is no workspace for a job by default. One is allocated as soon as build is run on the build machine (be it master or a slave). There can be any number of workspaces for a given job based on where and how many times the job run. Though, there is no guarantee there will be some on master. Do not confuse terms workspace (living on build machine) and build result directory on master.
Project#getSomeWorkspace(), gives you a workspace used by some past build. Note, this is done on purely best effort bases as there might be none.
There is no way to know where the build will run at the configuration time unless you tie the job to one particular machine. See hudson.model.Node#createLauncher(TaskListener) on how to run processes in Jenkins grid.
#AncestorInPath allows you to inject some of the domain object that ware traversed by stapler during URL binding. You should not try to inject anything that might not be part of the url. There is no way known to me for you to inject the request, stapler uses the one that initiated the action method invocation.
The bottom line is that what you try sounds highly unusual. Perhaps there is an easier way to solve your original problem.
I need to run project tests both locally and automatically on TeamCity server.
Local test execution must use local database connection, and when run on TeamCity, tests must use a remote connection to the database.
So I need to tell my tests, when to use local connection and when to use remote and pass URL, username and password in this case.
To tell that I decided to use java system properties. I found built-in support in Gradle for that
systemProperty 'some.prop', 'value'
The question is, how can I create a standard test task for local test run, that will not pass any properties, and a custom test task, that will set system properties before run?
I tried something like
task teamCityTest(type : Test) {
scanForTestClasses = false
includes = ['**/*Test.class']
systemProperty 'some.prop', 'value'
}
but it failed with NPE, that means I'm doing something wrong.
The approach is fine (you can use the Java plugin's test task for running tests locally), but you'll have to configure further properties for teamCityTest such as classpath = configurations.testRuntime (or even classpath = test.classpath). For a full example, see samples/java/withIntegrationTests in the gradle-all distribution.
Is there any possibility to get a map of environment variables that are used in Jenkins?
I am trying to develop my own plugin and need to access the variable BUILD_USER_ID, which is provided by the build user vars plugin.
There is a class called EnvVars, which inherits from TreeMap, but it is obviously empty, if you create an object of this.
EnvVars env = new EnvVars();
Where are these variables stored , so that I can get them and use them in my plugin?
I expect you mean build variables injected by Build User Vars Plugin into build.
If you are interested in reading static jobs configuration (from system groovy script for instance), use job.getBuildWrappersList().get(org.jenkinsci.plugins.builduser.BuildUser.class) to get user configured instance of BuildUser. In case of Build User Vars Plugin there is not much configuration to read, though.
If you would like to access actual variable values injected into the build process (from a BuildStep for instance), call build.getEnvironment(TaskListener) to get populated EnvVars instance with all variables. Note that these variables are not available outside of build context.