Unable to generate screenshot in Cucumber Extent Report - selenium-webdriver

I am unable to see screenshot getting captured in the Extent report for cucumber.
I have debuged and observed that the code gets executed, but the screenshot is not saved in the file of extent report or cucumber html report.
Screenshot Code
public void screenshot(Scenario scenario) {
if(scenario.isFailed()) {
byte[] screenshot=SeleniumUtils.captureScreenshot(); scenario.public void screenshot(Scenario scenario) { embed(screenshot, "image1/png"); }
Runner Class
#RunWith(Cucumber.class)
#CucumberOptions(
features = "src/test/java/features", tags = "#login_internal_user", glue
= {
"stepDefinitions"
}, plugin = {
"pretty",
"rerun:src/test/java/features/rerun.txt",
"com.aventstack.extentreports.cucumber.adapter.ExtentCucumberAdapter:",
"html:reports"
}
)
public class loginRunner {
#AfterClass
public static void teardown() {
SeleniumUtils.quitDriver();
}
}
Extent.properties file
# indicate which reporters to use
extent.reporter.logger.start=true
# point to any configuration used
extent.reporter.logger.config=src/main/resources/extent-config.xml
# specify output path
extent.reporter.logger.out=reports/importsTeam-reports
screenshot.dir=reports/importsTeam-reports/screenshots/
Maven Properties
Extent Cucumber Adapter version- 1.0.7
Cucumber Jars- 4.2.0

You should use the html reporter. your property file should include,
extent.reporter.html.start = true
extent.reporter.html.config = <html config xml>
extent.reporter.html.out = <output path>

Related

Unable to embed screenshot in Cucumber Report

I had built a Cucumber framework using Selenium and Java. And I have used maven cucumber reporting plugin for reporting. I am trying to showcase the failed screenshot in the report hence I have added hooks and added conditions to embed the screenshot in the report. Following code, I have written in #After cucumber hooks.
#After
public void tearDown(Scenario scenario) throws IOException {
File file;
if (scenario.isFailed()) {
String screenshotPath = System.getProperty("user.dir") + "\\target\\screenshots\\"+scenario.getName()+"\\";
file = new File(screenshotPath);
file.mkdir();
try {
final File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File(screenshotPath+"screenshot.png"));
} catch (final Exception e) {
e.printStackTrace();
}
String failedScreenShot = screenshotPath+"screenshot.png";
String url = "<img src="+failedScreenShot+" alt='failed screenshot'>";
scenario.embed(url.getBytes(),"png", "Click Here To See Screenshot");
}
}
When the scenario gets failed when executed I am getting error in the report as
Cucumber Report failed Screenshot
Can someone please help to achieve this. Thanks in advance.
From cucumber 6.0 onward, embed() has been removed Document/API. So instead of embed(). Use scenario.attach() which accept bytes, you also have to pass the media type and name. This name get displayed in the report.
#After
public void takeScreenShotsOnStepFailure(Scenario scenario) {
if (scenario.isFailed()) {
byte[] screenshot = testContext.getAppiumDriver().getScreenshotAs(OutputType.BYTES);
scenario.attach(screenshot, "image/png", scenario.getName());
}
}

In allure report unable to see logger statements put inside #Step annotated method

I am doing POC on allure reporting.I created a Gradle project that has a test class with two #Test annotated methods.The same test class has #Step annotated method that has logger statements.This #Step annotated method is called from one of the #Test annotated methods.Upon running the tests,I do not see the logger info(present under #Step method) in allure result.
I have added needed dependencies and plugin in build.gradle.
content of build.gradle file.
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java Library project to get you started.
* For more details take a look at the Java Libraries chapter in the Gradle
* user guide available at https://docs.gradle.org/4.8.1/userguide/java_library_plugin.html
*/
plugins {
// Apply the java-library plugin to add support for Java Library
id 'java-library'
id "io.qameta.allure" version "2.8.1"
}
dependencies {
// This dependency is exported to consumers, that is to say found on their compile classpath.
api 'org.apache.commons:commons-math3:3.6.1'
implementation 'io.qameta.allure:allure-testng:2.12.0'
implementation 'org.testng:testng:6.9.9'
compile('org.aspectj:aspectjweaver:1.9.4')
compile 'org.slf4j:slf4j-api:1.7.25'
compile 'org.apache.logging.log4j:log4j-slf4j-impl:2.0.2'
compile 'org.apache.logging.log4j:log4j-web:2.0.2'
// This dependency is used internally, and not exposed to consumers on their own compile classpath.
implementation 'com.google.guava:guava:23.0'
// Use JUnit test framework
testImplementation 'junit:junit:4.12'
}
// In this section you declare where to find the dependencies of your project
repositories {
// Use jcenter for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
allure {
version = '2.8.1'
autoconfigure = true
aspectjweaver = true
useTestNG {
version = '2.0-BETA20'
}
}
my sample test code:
public class SampleTestForAllureReport {
public static Logger logger = LoggerFactory.getLogger(SampleTestForAllureReport.class);
#Severity(SeverityLevel.BLOCKER)
#Description("This is a sample test for printing welcome")
#Test()
public void welcomeEvent()
{
loggingSteps();
System.out.println("WELCOME");
}
#Test()
public void exitEvent()
{
System.out.println("EXIT");
}
#Step("logger for welcome test")
public void loggingSteps()
{
logger.info("log for step1");
}
}
allure result.

cucumber feature file not picking up Step Definition in Java class but there are no error in the console after running Junit

Project Structure-image Structure I am writing some cucumber tests for my java project. When running cucumber class there are no error occurred in the console but as per the step definition script i have given the script to invoke browser so as per my assumption the step definition class or the glue is not called by the cucumber class.
Can you please check and do let me know why it is not invoked.
Code:
#RunWith(Cucumber.class)
#CucumberOptions(
features={"F:/Selinium/practise-cucumber/practise1/features/login.feature"},
glue={"F:/Selinium/practise-cucumber/practise1/src/Stepdefinition/loginmethod.java"})
public class Runcucumber {
}
Step Definition code:
public class loginmethod {
public WebDriver driver ;
#Given("^User is on Home Page$")
public void user_is_on_home_page() throws Throwable{
System.out.println("homepagre");
System.setProperty("webdriver.chrome.driver",
"F:/Selinium/practise-cucumber/practise1/driver/chromedriver1.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com/");
System.out.println("lUNCHED homepagre");
}
Browser should be invoked
Console output
Structure
Can you please change your Runcucumber class name to RuncucumberTest then try again. It happened me one time.
#RunWith(Cucumber.class)
#CucumberOptions(
features={"F:/Selinium/practise-cucumber/practise1/features/login.feature"},
glue={"F:/Selinium/practise-cucumber/practise1/src/Stepdefinition/loginmethod.java"})
public class RuncucumberTest {
}

How to use Cucumber ReTry Runner in Combination of cucumber-jvm-parallel-plugin

I am using cucumber-jvm-parallel-plugin, for generating dynamic runners on run-time for my Java-cucumber based tests.
<artifactId>cucumber-jvm-parallel-plugin</artifactId>
<version>4.2.0</version>
For standalone tests we have something called ReTry Runner which can be triggered after finishing of the current Cucumber tests.
#RunWith(Cucumber.class)
#CucumberOptions(
plugin = {"listners.ExtListner",
"html:target/cucumber-html-report",
"json:target/cucumber.json"}
,features = {"#target/rerun.txt"}
, format = {"pretty", "html:target/cucumber-html-report-retry"}
, glue = {"stepDefs"}
,strict = false
)
public class IOSReTryRunner {
private static boolean dunit = false;
#BeforeClass
public static void setup() throws IOException, AWTException {
// Initiates the extent report and generates the output in the output/Run_<unique timestamp>/report.html file by default.
ExtListner.initiateExtentCucumberFormatter();
// Loads the extent config xml to customize on the report.
ExtListner.loadConfig(new File("src/main/resources/features/extent-config.xml"));
// User can add the system information as follows
ExtListner.addSystemInfo("Browser Name", "<Name>");
// Also you can add system information using a hash map
Map systemInfo = new HashMap();
systemInfo.put("Cucumber version", "v1.2.3");
systemInfo.put("Extent Cucumber Reporter version", "v1.1.1");
ExtListner.addSystemInfo(systemInfo);
}
#AfterClass
public static void teardown() throws IOException, NoSuchFieldException {
System.out.println("Ran the tearDown.");
// WebDriverFactory.getInstance().closeAppiumDriver();
}
}
Is there any way that I could use the same mechanism for Auto-generated runners using above referred cucumber-jvm-parallel-plugin?
The cucumber-jvm-parallel-plugin is a code generator. You can provide it with a custom virtual marker template through the customVmTemplate in the maven configuration.
For a reference of what is supported check the build in templates:
cucumber-junit-runner.java.vm
cucumber-testng-runner.java.vm

Bootstrapping NancyFX with RavenDB

I am trying to add bootstrap NancyFX with RavenDB and I am running into the following error trying to run the application...
"Unable to resolve type: Nancy.IResponseFormatter"
Environment:
ASP.Net
Nancy
Nancy.Hosting.Aspnet
RavenDB
VS2010 DevelopmentServer
In lieu of pasting all of the code, here is a link to the site that I used as an example. By example, I mean I copied it verbatim to see if I could get it to work.
http://stuff-for-geeks.com/category/NancyFx.aspx
I have actually seen this code run in a demo before, but I for some reason can not get it to run at all. It fails at start up. It is almost as if Nancy is not using my BootStrapper.
More of the Stack Trace:
[TypeInitializationException: The type initializer for 'Nancy.Hosting.Aspnet.NancyHttpRequestHandler' threw an exception.]
Nancy.Hosting.Aspnet.NancyHttpRequestHandler..ctor() +0
[TargetInvocationException: Exception has been thrown by the target of an invocation.]
Any help would be really appreciated.
That code is based on an older version of Nancy. You should be looking at using the IResponseFormatterFactory instead. The custom module builder, that is defined in the blog post, is based on an old copy of the DefaultNancyModuleBuilder and if you have a look at the current version https://github.com/NancyFx/Nancy/blob/master/src/Nancy/Routing/DefaultNancyModuleBuilder.cs you should be able to make the necessary adjustments
Here is the code for the RavenAwareModuleBuilder class under discussion:
Edit 1
The code below has been updated for Nancy Release 0.12. Note the new NegotiationContext lines in BuildModule method.
public class RavenAwareModuleBuilder : INancyModuleBuilder
{
private readonly IViewFactory viewFactory;
private readonly IResponseFormatterFactory responseFormatterFactory;
private readonly IModelBinderLocator modelBinderLocator;
private readonly IModelValidatorLocator validatorLocator;
private readonly IRavenSessionProvider ravenSessionProvider;
public RavenAwareModuleBuilder(IViewFactory viewFactory, IResponseFormatterFactory responseFormatterFactory, IModelBinderLocator modelBinderLocator, IModelValidatorLocator validatorLocator, IRavenSessionProvider ravenSessionProvider)
{
this.viewFactory = viewFactory;
this.responseFormatterFactory = responseFormatterFactory;
this.modelBinderLocator = modelBinderLocator;
this.validatorLocator = validatorLocator;
this.ravenSessionProvider = ravenSessionProvider;
}
public NancyModule BuildModule(NancyModule module, NancyContext context)
{
context.NegotiationContext = new NegotiationContext
{
ModuleName = module.GetModuleName(),
ModulePath = module.ModulePath,
};
module.Context = context;
module.Response = this.responseFormatterFactory.Create(context);
module.ViewFactory = this.viewFactory;
module.ModelBinderLocator = this.modelBinderLocator;
module.ValidatorLocator = this.validatorLocator;
context.Items.Add(
"IDocumentSession",
ravenSessionProvider.GetSession()
);
module.After.AddItemToStartOfPipeline(ctx =>
{
var session = ctx.Items["IDocumentSession"] as IDocumentSession;
if (session != null) session.Dispose();
});
return module;
}
}

Resources