Jenkins ignores the exit code 1 from Gradle run task - testing

Jenkins 2.89.3
Gradle 3.5
I'm running a build using a pipeline:
node {
try {
stage('Fetch code') {
echo 'Pulling the Code...'
checkout scm: [$class : 'GitSCM',
branches : [[name: '*/$GIT_CLOUD_BRANCH']],
browser : [$class: 'GitLab', repoUrl: cloudRepo, version: gitlabVersion],
doGenerateSubmoduleConfigurations: false,
submoduleCfg : [],
userRemoteConfigs : scm.userRemoteConfigs]
}
wrap([$class: 'Xvfb', screen: '1920x1080x24']) {
dir('BUILD') {
stage('Clean DB and old reports') {
sh '''chmod +x cleanup_old_reports.sh
./cleanup_old_reports.sh $PWD/../../builds $BUILD_NUMBER'''
}
stage('Build the application') {
sh '''chmod +x application.sh
./application.sh -P${PROJECT}'''
}
stage('Run tests') {
sh 'gradle clean run -P${PROJECT} -P${RUN_TYPE}'
}
}
}
stage('Generate reports') {
allure([includeProperties: false,
jdk : '',
properties : [],
reportBuildPolicy: 'ALWAYS',
results : [[path: 'ExaTAGS/TagsRunner/allure-results']]])
archive 'catalina.log'
}
} catch (Exception ex) {
currentBuild.result = "FAILURE"
println "in catch currentBuild.result: " + currentBuild.result
throw ex
} finally {
notifyBuild(currentBuild.result)
}
}
The stage 'Run tests' executes a gradle task RUN. In a case if there are any failed tests the main method of test runner throws an exception and RUN task returns exit code 1.
But despite this stage is marked as FAILED, the whole build is SUCCESSFUL:
I believe that RUN task actually does not throw an exception, cause I do not see the output I make in catch block in the pipeline script. But it really returns exit code different from 0, why does Jenkins ignore it? And what is the appropriate way to force Jenkins fail the whole build if there are any of failed stages?
UPDATE
build.gradle file:
group 'project'
version '1.0'
apply plugin: 'application'
def aspectjVersion = "1.8.7"
mainClassName = 'tags.runner.TestManager'
applicationDefaultJvmArgs = ["-javaagent:APP_HOME_PLACEHOLDER/lib/aspectjweaver-${aspectjVersion}.jar"]
dependencies {
compile "org.aspectj:aspectjweaver:${aspectjVersion}"
}
def weaver = configurations.compile.find { it.name.contains("aspectjweaver") }
run {
doFirst {
jvmArgs = jvmArgs << "-javaagent:$weaver"
if (project.hasProperty('cloud')) {
args("cloud")
}
}
}
And the main method of TestManager.
runTests() throws the TestException if the count of failed tests is more than 0:
class TestManager {
static void main(String[] args) {
try {
runTests()
} catch (TestException ex) {
throw ex
} catch (Exception e) {
e.printStackTrace()
System.in.read()
}
}
}

Related

How can ı delete the history in the allure testng but not in the terminal, we can use code

ı want to delete my data test history in the allure testng but not in the terminal, ı want to use some functions or any annotation in my java code
Try this:
public void deleteDirectory(String directoryPath) throws IOException {
Path path = Paths.get(directoryPath);
try (Stream<Path> walk = Files.walk(path)) {
walk
.sorted(Comparator.reverseOrder())
.forEach(this::deleteTargetDirectory);
}
}
private void deleteTargetDirectory(Path path) {
try {
Files.delete(path);
} catch (IOException e) {
System.err.printf("Unable to delete this path : %s%n%s", path, e);
}
}

Get this warning with latest Chromedriver: Unable to evaluate script: disconnected: not connected to DevTools . How to resolve this?

I get this warning in eclipse : Unable to evaluate script: disconnected: not connected to DevTools when running cucumber selenium script.
image of the warning
I am using the latest Chrome driver version. Since this issue, the following code does not give the out put.
enter code here public boolean isMixedContentWarningsAppearing() {
String warning = null;
boolean result = true;
try {
LogEntries entry = driver.manage().logs().get(LogType.BROWSER);
List<org.openqa.selenium.logging.LogEntry> logs = entry.getAll();
for (org.openqa.selenium.logging.LogEntry e : logs) {
if (e.toString().contains("Accessing")) {
System.out.println("..... warnings...." + e);
warning = e.toString();
// Reporter.addStepLog(warning);
}
}
} catch (Exception e) {
}
if (warning == null) {
result = false;
Reporter.addStepLog("No MixedContent warnings");
} else {
result = true;
Reporter.addStepLog("The application contains Mixed Contents");
}
return result;
}
can anybody help me to run this code?

Pending steps with jbehave

I am getting pending steps for jbehave stories and please find the configuration details below: All the story steps are correct to my knowledge.I am not getting where i am going wrong in configuration. Could you please help me.
build.gradle:
def buildNumber = System.getProperty('buildNumber') ?: '0'
def buildVersion = '1.0.0-' + buildNumber
version = buildVersion
apply plugin: 'groovy'
apply plugin: 'java'
apply plugin: 'maven'
repositories {
maven { url 'http://repo1.maven.org/maven2/' }
maven { url 'https://mvnrepository.com/' }
maven {url 'https://mvnrepository.com/artifact/org.jbehave'}
mavenCentral()
}
dependencies {
compile 'com.google.guava:guava:18.0'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.8.8.1'
compile group: 'org.codehaus.jackson', name: 'jackson-core-asl', version: '1.1.0'
testCompile group: 'junit', name: 'junit', version: '4.11'
compile 'org.codehaus.groovy:groovy-all:2.3.11'
compile('org.springframework:spring-core:4.0.5.RELEASE') {
exclude(module: 'commons-logging')
}
compile(group: 'org.jbehave.web', name: 'jbehave-web-selenium', version: '4.0-beta-1') {
exclude(module: 'selenium-remote-control')
exclude(module: 'selenium-java')
exclude(module: 'jbehave-core')
}
compile 'javax.servlet:javax.servlet-api:3.0.1'
compile 'org.springframework:spring-webmvc:4.0.5.RELEASE'
compile 'org.springframework:spring-jdbc:4.0.5.RELEASE'
compile 'org.springframework:spring-test:4.0.5.RELEASE'
compile 'io.springfox:springfox-spring-web:2.2.2'
compile 'io.springfox:springfox-swagger-ui:2.2.2'
compile 'io.springfox:springfox-swagger2:2.2.2'
compile 'com.googlecode.json-simple:json-simple:1.1.1'
compile 'org.jbehave.site:jbehave-site-resources:3.1.1'
compile 'org.jbehave:jbehave-spring:3.9.3'
compile 'org.jbehave:jbehave-core:4.1.3'
compile 'commons-dbcp:commons-dbcp:1.2.2'
compile 'org.slf4j:slf4j-api:1.7.6'
compile 'com.jcraft:jsch:0.1.53'
compile 'org.json:json:20140107'
compile 'log4j:log4j:1.2.11'
compile 'org.assertj:assertj-core:2.3.0'
compile group: 'commons-io', name: 'commons-io', version: '2.5'
// https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-chrome-driver
compile group: 'org.seleniumhq.selenium', name: 'selenium-chrome-driver', version: '2.33.0'
compile group: 'org.seleniumhq.selenium', name: 'selenium-server', version: '3.7.1'
// https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java
compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.7.1'
compile group: 'org.picocontainer', name: 'picocontainer', version: '2.15'
compile group: 'joda-time', name: 'joda-time', version: '2.8.1'
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.0'
compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
compile group: 'org.apache.httpcomponents', name: 'httpclient', version:'4.3.1'
compile group: 'org.codehaus.plexus', name: 'plexus-utils', version:'3.0.22'
compile group: 'commons-lang', name: 'commons-lang', version: '2.3'
// testCompile 'org.jbehave:jbehave-core:3.9.3'
compile 'org.jbehave.site:jbehave-site-resources:3.1.1:#zip'
}
sourceSets.test.resources.srcDir 'src/test/java'
/** userDefined task to copy the srories from /src/main/stories into build/classes/test/stories*/
task copyStories(type: Copy) {
from 'src/main/resources' into "${buildDir}/classes/test"
from 'src/main/stories' into "${buildDir}/classes/test/stories"
}
sourceSets {
test {
java {
srcDir "src/main/java"
}
resources {
srcDir "src/main/resources"
}
}
}
/* This is to copy the system properties from gradle available to Junit test classes*/
test {
systemProperty "metaFilters", System.getProperty("metaFilters", "")
doFirst {
copy {
from(zipTree(jarPath("jbehave-core"))) {
include "style/*"
}
into("build/reports/jbehave/view")
}
copy {
from(zipTree(jarPath("jbehave-site-resources"))) {
include "js/**/*"
include "style/**/*"
include "images/*"
}
into("build/reports/jbehave/view")
}
}
systemProperties System.getProperties()
dependsOn([clean,copyStories])
}
def jarPath(String jarName) {
configurations.testCompile.find({ it.name.startsWith(jarName) }).absolutePath
}
Jbheave extending junitstories class as
public class JbehaveStories extends JUnitStories{
private static final String _STORIES_SEPARATOR = ",";
public static HtmlOutput qmoHtmlOutput;
public static StoryData storyData;
public static List<String> storyToExecute = null;
static CrossReference crossReference = new CrossReference().withJsonOnly().withOutputAfterEachStory(true);
Logger logger = Logger.getLogger(JbehaveStories.class);
Logger log = Logger.getLogger(JbehaveStories.class);
ContextView contextView = new LocalFrameContextView().sized(640, 120);
SeleniumContext seleniumContext = new SeleniumContext();
private org.jbehave.web.selenium.WebDriverProvider driverProvider = new org.jbehave.web.selenium.PropertyWebDriverProvider();
private WebDriverSteps lifecycleSteps = new PerStoriesWebDriverSteps(driverProvider);
// SeleniumStepMonitor stepMonitor = new SeleniumStepMonitor(contextView, seleniumContext,crossReference.getStepMonitor());
Format[] formats = new Format[]{new SeleniumContextOutput(seleniumContext), CONSOLE, XML, HTML};
StoryReporterBuilder reporterBuilder = new StoryReporterBuilder(storyData)
.withCodeLocation(codeLocationFromClass(WebJbehaveStories.class)).withFailureTrace(true)
.withFailureTraceCompression(true).withDefaultFormats().withFormats(formats)
.withCrossReference(crossReference);
private ApplicationContext applicationContext;
public WebJbehaveStories() {
super();
try {
System.setProperty("webdriver.chrome.driver", "src/main/resources/drivers/chromedriver.exe");
storyData = new StoryData();
// String path = BNSFWebJbehaveStories.class.getProtectionDomain().getCodeSource().getLocation().getPath() + "stories/log4j.properties";
// PropertyConfigurator.configure(path);
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public Configuration configuration() {
new WebDriverScreenshotOnFailure(driverProvider, new StoryReporterBuilder());
Class<? extends Embeddable> embeddableClass = this.getClass();
ParameterConverters parameterConverters = new ParameterConverters();
LoadFromClasspath resourceLoader = new LoadFromClasspath(embeddableClass);
TableTransformers tableTranformers = new TableTransformers();
ExamplesTableFactory examplesTableFactory = new ExamplesTableFactory(new LocalizedKeywords(), resourceLoader,
parameterConverters, tableTranformers);
parameterConverters.addConverters(new ParameterConverters.DateConverter(new SimpleDateFormat("yyyy-MM-dd")),
new ParameterConverters.ExamplesTableConverter(examplesTableFactory));
return new SeleniumConfiguration().useSeleniumContext(seleniumContext)
.usePendingStepStrategy(new FailingUponPendingStep())
.useStoryControls(new StoryControls().doResetStateBeforeScenario(true))
.useStoryLoader(new LoadFromClasspath(WebJbehaveStories.class))
.useStoryReporterBuilder(new StoryReporterBuilder())
.useStoryParser(new RegexStoryParser(examplesTableFactory))
.useStoryReporterBuilder(new StoryReporterBuilder(storyData)
.withCodeLocation(CodeLocations.codeLocationFromPath(getClassPath()))
.withCrossReference(crossReference)
.withFormats(Format.CONSOLE, Format.HTML, XML))
.useParameterControls(new ParameterControls()
.useDelimiterNamedParameters(true))
.useParameterConverters(parameterConverters)
.useStepPatternParser(new RegexPrefixCapturingPatternParser()).doDryRun(true);
}
private List<String> getMetaFilters() {
String metaFilterStr = System.getProperty("meta.filter");
metaFilterStr = metaFilterStr == null ? "" : metaFilterStr;
List<String> metaFilters = Arrays.asList(metaFilterStr.split(","));
log.info("**** MetaFilters = " + metaFilters);
return metaFilters;
}
private String getClassPath() {
URL fileUrl = codeLocationFromClass(this.getClass());
String classpath = getPathFromURL(fileUrl);
System.out.println("getClassPath classpath = " + classpath);
// class may come from a jar file
if (classpath.endsWith(".jar!")) {
int idx = classpath.lastIndexOf('.');
classpath = classpath.substring(0, idx);
}
return classpath;
}
#Override
public InjectableStepsFactory stepsFactory() {
if (this.applicationContext == null) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(WebJbehaveStories.class);
this.applicationContext = applicationContext;
}
return new SpringStepsFactory(configuration(), this.applicationContext);
}
#Override
public void run() throws Throwable {
Embedder embedder = configuredEmbedder();
embedder.embedderControls().useStoryTimeoutInSecs(450000);
embedder.useMetaFilters(getMetaFilters());
// These should be enabled so that even though the stories fail it wont go to catch method. Its useful when running multiple scenarios
embedder.embedderControls().doIgnoreFailureInStories(true);
embedder.embedderControls().doIgnoreFailureInView(false);
embedder.embedderControls().doGenerateViewAfterStories(true);
embedder.configuration().storyControls().doIgnoreMetaFiltersIfGivenStory(true);
List<String> storyPaths = storyPaths();
try {
embedder.runStoriesAsPaths(storyPaths);
} catch (Exception e) {
e.printStackTrace();
Loggers.CONSOLE_LOGGER.error("STORY FAILED DUE TO:");
e.printStackTrace(System.err);
Loggers.FILE_LOGGER.error("STORY FAILED DUE TO:");
e.printStackTrace(System.err);
Loggers.CONSOLE_LOGGER.info("Driver instance is closing");
Loggers.FILE_LOGGER.info("Driver instance is closing");
embedder.generateCrossReference();
throw new Throwable(e.fillInStackTrace());
} finally {
try {
Loggers.CONSOLE_LOGGER.info("Driver instance is closing");
driver.quit();
Loggers.FILE_LOGGER.info("Driver instance is closing");
} catch (Exception e) {
System.out.println("Issue in closing driver instance in run method: " + e.getMessage());// Wantedly not caught the exception
}
embedder.generateCrossReference();
}
}
public String getStoryFromStoryPaths(String storyName, List<String> storyPaths) {
for (String story : storyPaths) {
System.out.println("story = " + story);
int stroryLength = story.split("/").length;
if (story.split("/")[stroryLength - 1].equalsIgnoreCase(storyName)) return story;
}
return "";
}
#Override
protected List<String> storyPaths() {
final List<String> globList = new ArrayList<String>();
final String[] globs = storyFilter().split(_STORIES_SEPARATOR);
for (final String story : globs) {
globList.add("**/*" + story + (story.endsWith(".story") ? "" : ".story"));
}
log.info("******************************************************************************");
log.info("**** globList = " + globList);
log.info("******************************************************************************");
String classpath = getClassPath();
log.info("classpath=" + classpath);
List<String> paths = new StoryFinder().findPaths(classpath, globList, null);
log.info("**** story paths = " + paths.toString());
return paths;
}
private String storyFilter() {
String storyFilter = System.getProperty("story.filter");
if (storyFilter == null) {
storyFilter = "";
}
return storyFilter;
}
public String captureReportName() {
String storyName = null;
String fileNames[] = reporterBuilder.outputDirectory().list();
for (int i = 0; i < fileNames.length; i++) {
if (fileNames[i].contains("html") & !fileNames[i].contains("BeforeStories")) {
String name[] = fileNames[i].split(".html");
storyName = name[0];
}
}
return storyName;
}
}
The error i am getting is as follows:
(stories/HomePage.story)
Narrative:
As a user
I want to perform an action
So that I can achieve a business goal
Scenario: scenario description
Given I enter login details (PENDING)
#Given("I enter login details")
#Pending
public void givenIEnterLoginDetails() {
// PENDING
}
Failed to run story stories/HomePage.story
org.jbehave.core.failures.PendingStepsFound: [Given I enter login details]
HomePage.story
Meta:
Narrative:
As a user
I want to perform an action
So that I can achieve a business goal
Scenario: scenario description
Given I enter login details
public class HomePageSteps {
#Autowired HomePage homePage;
#When("I enter login details")
public void enter() {
try {
homePage.enterLoginDetails();
} catch (Exception e) {
e.printStackTrace(System.err);
}
}
#Given("I enter login details")
public void enterx() {
try {
homePage.enterLoginDetails();
} catch (Exception e) {
e.printStackTrace(System.err);
}
}
}

Getting add failed with exit code: 127 while executing add file for SCM Manager

I am using SCM Manager for HG version control. And I am trying to add an existing sc.txt file to HG repo using Java API provided by maven-scm-provider-hg dependency.
Method to add the file to HG repo:
public void add(ScmRepository scmRepository, ScmFileSet fileSet, String message,
boolean binary) {
try {
AddScmResult rs = scmManager.add(scmRepository, fileSet, message);
System.out.println("Result : " + rs.getAddedFiles());
} catch (Exception e) {
System.out.println("Execption occured while adding new file " + e);
}
}
Method to get the ScmRepository:
public ScmRepository getScmRepository(String scmUrl) throws Exception {
ScmRepository repository;
try {
repository = getScmManager().makeScmRepository(scmUrl);
System.out.println("Repo " + repository.getProviderRepository().getUser() + " Path : "
+ repository.getProvider());
return repository;
} catch (NoSuchScmProviderException ex) {
throw new Exception("Could not find a provider.", ex);
} catch (ScmRepositoryException ex) {
throw new Exception("Error while connecting to the repository", ex);
}
}
MainApp.java
ScmRepository repo = scmClient.getScmRepository("scm:hg:/Users/john/work/hgtest/v1");
ScmFileSet fileSet = new ScmFileSet(new File("/Users/john/work/hgtest/v1"),
new File("sc.txt"));
scmClient.add(repo, fileSet, "This is first file checking", false);
This is what error I get when I execute the code:
Adding new file to hg.....
EXECUTING: /bin/sh -c cd /Users/diwakar/work/hgtest/v2 && hg add --verbose sc.txt
[ERROR]
EXECUTION FAILED
Execution of cmd : add failed with exit code: 127.
Working directory was:
/Users/diwakar/work/hgtest/v2
Your Hg installation seems to be valid and complete.
Hg version: NA (OK)
Result : []
Done.....
POM.xml:
<dependency>
<groupId>org.apache.maven.scm</groupId>
<artifactId>maven-scm-provider-hg</artifactId>
<version>1.8</version>
</dependency>

How do I force a method in Groovy to throw an exception

I wanted to write a test for a method in Groovy that throws an IOException. The only way for me to simulate this in the test is to force the method to throw this exception
This is what the original code looks like:
public void cleanUpBermudaFiles(RequestMessage requestMessage)
{
final File sourceDirectory = new File(preferenceService.getPreference("bermuda.landingstrip") + File.separator + requestMessage.getWorkflowId().getValue());
if(sourceDirectory!=null && sourceDirectory.exists())
{
deleteDirectory(sourceDirectory);
}
else
{
LOG.error("Directory must exist in order to delete");
}
}
private void deleteDirectory(File directoryToDelete)
{
try {
FileUtils.deleteDirectory(directoryToDelete);
} catch (Exception e) {
LOG.error("Failed to delete Bermuda files directory located at:" + directoryToDelete.getPath() + "with an exception" + e.getMessage());
}
}
MY TEST: (I'm looking for a way to make deleteDirectory throw IOException)
public void testCleanUpBermudaFailure()
{
workflowId = new WorkflowId("123456")
workflowDirectory = new File(srcDirectory, workflowId.value)
workflowDirectory.mkdir()
File.createTempFile('foo','.lst', workflowDirectory)
def exception = {throw new IOException()}
expect(mockRequestMessage.getWorkflowId()).andReturn(workflowId)
expect(mockPreferenceService.getPreference("bermuda.landingstrip")).andReturn(srcDirectory.path)
replay(mockPreferenceService, mockRequestMessage)
fileCleanUpService.preferenceService = mockPreferenceService
fileCleanUpService.metaClass.deleteDirectory = exception
fileCleanUpService.cleanUpBermudaFiles(mockRequestMessage)
verify(mockPreferenceService, mockRequestMessage)
assert srcDirectory.listFiles().length == 0, 'CleanUp failed'
}
If the service class is a Groovy class, you would want to mock FileUtils like:
FileUtils.metaClass.static.deleteDirectory = { File f -> throw new IOException() }
However, as ataylor pointed out, you cannot intercept calls if it's a Java class. You can find a nice blog post about it here.
You are mocking a no-arg call to deleteDirectory, but the real deleteDirectory takes one argument of type File. Try this:
def exception = { File directoryToDelete -> throw new IOException() }
...
fileCleanUpService.metaClass.deleteDirectory = exception