How to access TestNg parameters from Cucumber Scenario file (Change the scenario Name Dynamically) - cucumber

Currently, I have implemented cucumber runner level parallel project using TestNG + Cucumber and it works really well. We are using allure for report generation but when I run the test cases in parallel imaging in 2 devices report will get replace since my scenario name is not unique in the feature file. Now I want to make my Scenario name dynamic so I want to put the Device name and Os version which I got from TestNg params function and access it in the Feature file. Here is my Runner file where I get parameters.
import org.testng.annotations.BeforeTest;
import cucumber.api.java.Before;
import org.testng.annotations.Parameters;
import cucumber.api.CucumberOptions;
import cucumber.api.testng.AbstractTestNGCucumberTests;
import device.Device;
import device.DeviceFactory;
import gherkin.formatter.model.Scenario;
#CucumberOptions(plugin = { "pretty", "html:target/html/", "json:target/cucumber.json", "junit:TEST-all.xml" }, glue = {
"steps" }, tags = { "#Mobile" })
public class ParallelRunner extends AbstractTestNGCucumberTests {
#BeforeTest
#Parameters({ "deviceName", "platformVersion" })
public void executeParallelTests(String deviceName, String platformVersion) {
Device device = new Device();
device.setDeviceName(deviceName);
device.setOsVersion(platformVersion);
DeviceFactory.setDevice(device);
}
}
I want to achieve something like this in my feature file
Feature: SignUp
#Android #IOS #Mobile
Scenario: SignUp ${"eviceName"} ${"platformVersion"}
Can anyone let me know how this can be achieved in cucumber?

Related

Screenshot with Cucumber Groovy

I am working with Cucumber features and Groovy as Step definitions in Katalon Studio. As we have each step in the step definition and wanted to take a screenshot when the step is failed (AShot or whatever). I would like to have the code to take the snap and would like to know do we need this in each step definition file.
I saw this similar question in this forum but does not have a clear answer.
Please note that I know the option in “Take Screenshot when execution failed” in settings.
Can you please provide the sample on this to proceed further?
Try adding the following to your #After test hook (or Test Listeners):
#After
public void TearDown(Scenario scenario) {
if (scenario.isFailed()) {
WebDriver driver = DriverFactory.getWebDriver()
byte[] screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES);
scenario.embed(screenshot, "image/png");
scenario.write(scenario.getName());
}
}
You will need some imports:
import cucumber.api.Scenario;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import com.kms.katalon.core.webui.driver.DriverFactory;
(Or just pres Ctrl + Shift + O to automatically import the missing classes.)
1st you are planning to run as a test suite of your test pack, you don't need to add any code to capture screenshots, Katalon will automatically do that.
for running as a test case use below example
public void catchNotyMessage(){
TestObject noty_warning = WebUI.modifyObjectProperty(findTestObject("DUMMY"), 'css', 'equals', 'div.noty_type_warning', true)
TestObject noty_error = WebUI.modifyObjectProperty(findTestObject("DUMMY"), 'css', 'equals', 'div.noty_type_error', true)
if (WebUI.verifyElementPresent(noty_error, 1, FailureHandling.OPTIONAL)){
this.takeWebElementScreenshot(noty_error)
}
else if (WebUI.verifyElementPresent(noty_warning, 1, FailureHandling.OPTIONAL)){
this.takeWebElementScreenshot(noty_warning)
}
}

How to create custom pages in Broadleaf Admin

I was trying to create custom pages on my broadleaf e-commerce admin side. I followed this tutorial. But when i try to access the page i get this strange error. . Here's code of my controller:
package com.community.admin.controller;
import org.broadleafcommerce.openadmin.web.controller.AdminAbstractController;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
#Controller
#RequestMapping("/" + ThemeController.SECTION_KEY)
#Secured("PERMISSION_OTHER_DEFAULT")
public class ThemeController extends AdminAbstractController {
protected static final String SECTION_KEY = "test";
#RequestMapping(value = "", method = RequestMethod.GET)
public String test(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
// This is expected by the modules/emptyContainer template, this is a custom template that gets included into the body
model.addAttribute("customView", "views/test");
// ensure navigation gets set up correctly
setModelAttributes(model, SECTION_KEY);
// gets the scaffolding set up to display the template from the customView attribute above
return "modules/emptyContainer";
}
}
Also as there's no Web-INF Folder as stated in tutorial so i added my html file in Resources > open_admin_styles > templates > views folder, where other html pages were present. Any help will be appreciated thanks
P.S: I get the AccessDeniedException. I executed these following queries for permissions:
INSERT INTO `blc_admin_module` (`ADMIN_MODULE_ID`, `DISPLAY_ORDER`, `ICON`, `MODULE_KEY`, `NAME`) VALUES (1, 7, 'icon-barcode', 'MyCustomModule', 'My Custom Module');
INSERT INTO `blc_admin_section` (`ADMIN_SECTION_ID`, `DISPLAY_ORDER`, `NAME`, `SECTION_KEY`, `URL`, `ADMIN_MODULE_ID`) VALUES (1, 1000, 'My Custom Section', 'MyCustomSection', '/test', 1);
INSERT INTO `blc_admin_sec_perm_xref` (`ADMIN_SECTION_ID`, `ADMIN_PERMISSION_ID`) VALUES (1, -1);
EDIT
Removing Security Annotation can solve the problem, regardless of the fact that i added all permissions in db as stated in documentation.
Spring Framework Security use "ROLE_" prefix, so you can't use #Secured("PERMISSION_OTHER_DEFAULT") because RoleVoter won't process it.
You have to change all Broadleaf permission name, add "ROLE_" prefix to make it work.
In this case you have to change "PERMISSION_OTHER_DEFAULT" in database to "ROLE_PERMISSION_OTHER_DEFAULT" and use in controller as:
#Controller
#RequestMapping("/" + ThemeController.SECTION_KEY)
#Secured("ROLE_PERMISSION_OTHER_DEFAULT")
public class ThemeController extends AdminAbstractController {
//something
}
Do the same with other permissions.
Here are some information: https://docs.spring.io/spring-security/site/docs/4.2.13.BUILD-SNAPSHOT/apidocs/org/springframework/security/access/vote/RoleVoter.html
The above answer resolves the security issues. But I don't find Web-INF Folder as well as Resources > open_admin_styles > templates > views folder too in admin on version 6.1.5-GA.
Then I add the test.html file on
admin > src > main > resources > community-demo-style.templates.admin
folder.
And also in MyController Class gives /test.html to model.addAtrribute() like model.addAttribute("customView", "/test.html");
It works fine for me.
MyController image

Custom update listener to set subtask's fix-version

I'm developing custom listener which will update subtask's fix version to same value as it's parent issue.
Currently we are using post-function in workflow in order to set subtask's fix version according to parent on subtask creation. This however doesn't cover cases when subtask already exists and parent's fix version gets updated. New value from parent task is not propagated to subtask.
I'm using script runner and I'm creating 'Custom lisatener', for my specific project and specified Event: 'Issue Updated'. I added script as following:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.SubTaskManager
import com.atlassian.jira.event.issue.AbstractIssueEventListener
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.project.version.Version
import org.apache.log4j.Logger
class CopyFixVersionFromParentToChild extends AbstractIssueEventListener {
Logger log = Logger.getLogger(CopyFixVersionFromParentToChild.class);
SubTaskManager subTaskManager = ComponentAccessor.getComponent(SubTaskManager.class)
IssueManager issueManager = ComponentAccessor.getComponent(IssueManager.class)
#Override
void issueUpdated(IssueEvent event) {
log.warn("\nIssue updated!!!\n")
try {
Issue updatedIssue = event.getIssue()
if (updatedIssue.issueTypeObject.name == "Parent issue type") {
Collection<Version> fixVersions = new ArrayList<Version>()
fixVersions = updatedIssue.getFixVersions()
Collection<Issue> subTasks = updatedIssue.getSubTaskObjects()
if (subTaskManager.subTasksEnabled && !subTasks.empty) {
subTasks.each {
if (it instanceof MutableIssue) {
((MutableIssue) it).setFixVersions(fixVersions)
issueManager.updateIssue(event.getUser(), it, EventDispatchOption.ISSUE_UPDATED, false)
}
}
}
}
} catch (ex) {
log.debug "Event: ${event.getEventTypeId()} fired for ${event.issue} and caught by script 'CopyVersionFromParentToChild'"
log.debug(ex.getMessage())
}
}
}
Problem is, that it doesn't work. I'm not sure whethe rit's problem that my script logic is encapsulated inside class. Do I have to register this in some specific way? Or am I using script runner completely wrong and I'm pasting this script to wrong section? I checked code against JIRA API and it looks like it should work, my IDE doesnt show any warnings/errors.
Also, could anyone give me hints on where to find logging output from custom scripts like this? Whatever message I put into logger, I seem to be unable to find anywhere in JIRA logs (although I'm aware that script might not work for now).
Any response is much appreciated guys, Thanks.
Martin
Well, I figure it out.
Method I posted, which implements listener as groovy class is used in different way than I expected. These kind of script files were used to be located in to specific path in JIRA installation and ScriptRunner would register them into JIRA as listeners.
In in order to create 'simple' listener script which reacts to issue updated event, I had to strip it down to this code
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.project.version.Version
IssueManager issueManager = ComponentAccessor.getComponent(IssueManager.class)
Issue updatedIssue = event.getIssue()
Collection<Version> fixVersions = new ArrayList<Version>()
fixVersions = updatedIssue.getFixVersions()
Collection<Issue> subTasks = updatedIssue.getSubTaskObjects()
subTasks.each {
if (it instanceof MutableIssue) {
((MutableIssue) it).setFixVersions(fixVersions)
issueManager.updateIssue(event.getUser(), it, EventDispatchOption.ISSUE_UPDATED, false)
}
}
You past this to script runner interface and it works :-). Hope this helps anyone who's learning ScriptRunner. Cheers.
Matthew

Neo4j Spatial: can't run spatial

I have been trying to work with Neo4j Spatial for my project, but I can't make it work.
With limited documentation and examples I figured out how to load OSM map to the database. But to check if it is loaded, I am trying to execute a spatial query.
While trying to run my code I get this error:
import.java:69: error: cannot access GremlinGroovyPipeline
.startIntersectSearch(layer, bbox)
^
class file for com.tinkerpop.gremlin.groovy.GremlinGroovyPipeline not found
I understand what's wrong (it can't find the required library), but I don't know how to fix it. The reason is when I run Neo4j Spatial tests, LayerTest.java and TestSpatial.java do include GeoPipeline library and it works perfectly fine. However, when I created my simple java file to test Neo4j, and trying to execute commands that depend GeoPipeline library I get the error above.
I read the instructions on github for Neo4j and saw this note:
Note: neo4j-spatial has a mandatory dependency on
GremlinGroovyPipeline from the com.tinkerpop.gremlin.groovy package.
The dependency in neo4j is type 'provided', so when using
neo4j-spatial in your own Java project, make sure to add the following
dependency to your pom.xml, too.
However, I am not using Maven to build my app. It is a simple java file, that I want to run to test if I get how everything works.
here is the code from my java file:
package org.neo4j.gis.spatial;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.geotools.data.DataStore;
import org.geotools.data.neo4j.Neo4jSpatialDataStore;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.neo4j.gis.spatial.osm.OSMDataset;
import org.neo4j.gis.spatial.osm.OSMDataset.Way;
import org.neo4j.gis.spatial.osm.OSMGeometryEncoder;
import org.neo4j.gis.spatial.osm.OSMImporter;
import org.neo4j.gis.spatial.osm.OSMLayer;
import org.neo4j.gis.spatial.osm.OSMRelation;
import org.neo4j.gis.spatial.pipes.osm.OSMGeoPipeline;
import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import com.vividsolutions.jts.geom.Envelope;
import com.vividsolutions.jts.geom.Geometry;
import org.neo4j.kernel.impl.batchinsert.BatchInserter;
import org.neo4j.kernel.impl.batchinsert.BatchInserterImpl;
import org.neo4j.kernel.EmbeddedGraphDatabase;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.gis.spatial.pipes.GeoPipeline;
class SpatialOsmImport {
public static void main(String[] args)
{
OSMImporter importer = new OSMImporter("ott.osm");
Map<String, String> config = new HashMap<String, String>();
config.put("neostore.nodestore.db.mapped_memory", "90M" );
config.put("dump_configuration", "true");
config.put("use_memory_mapped_buffers", "true");
BatchInserter batchInserter = new BatchInserterImpl("target/dependency", config);
importer.setCharset(Charset.forName("UTF-8"));
try{
importer.importFile(batchInserter, "ott.osm", false);
batchInserter.shutdown();
GraphDatabaseService db = new EmbeddedGraphDatabase("target/dependency");
importer.reIndex(db, 10000);
db.shutdown();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
GraphDatabaseService database = new EmbeddedGraphDatabase("target/dependency");
try{
SpatialDatabaseService spatialService = new SpatialDatabaseService(database);
Layer layer = spatialService.getLayer("layer_roads");
LayerIndexReader spatialIndex = layer.getIndex();
System.out.println("Have " + spatialIndex.count() + " geometries in " + spatialIndex.getBoundingBox());
Envelope bbox = new Envelope(-75.80, 45.19, -75.7, 45.23);
// Search searchQuery = new SearchIntersectWindow(bbox);
// spatialIndex.executeSearch(searchQuery);
// List<SpatialDatabaseRecord> results = searchQuery.getResults();
List<SpatialDatabaseRecord> results = GeoPipeline
.startIntersectSearch(layer, bbox)
.toSpatialDatabaseRecordList();
doGeometryTestsOnResults(bbox, results);
} finally {
database.shutdown();
}
}
private static void doGeometryTestsOnResults(Envelope bbox, List<SpatialDatabaseRecord> results) {
System.out.println("Found " + results.size() + " geometries in " + bbox);
Geometry geometry = results.get(0).getGeometry();
System.out.println("First geometry is " + geometry);
geometry.buffer(2);
}
}
It is very simple right now, but I can't make it work. How do I include com.tinkerpop.gremlin.groovy.GremlinGroovyPipeline in my app, so it works?
I run everything on Ubuntu 12.04 and java version "1.7.0_25", Java(TM) SE Runtime Environment (build 1.7.0_25-b15).
Any help is greatly appreciated.
the best way to get all the required dependencies in a place where you can include them in your classpath is to run
mvn dependency:copy-dependencies
in neo4j-spatial, and find the libs to include in target/deps, see http://maven.apache.org/plugins/maven-dependency-plugin/usage.html

How to Report Results to Sauce Labs using Geb/Spock?

I want to use the Sauce Labs Java REST API to send Pass/Fail status back to the Sauce Labs dashboard. I am using Geb+Spock, and my Gradle build creates a test results directory where results are output in XML. My problem is that the results XML file doesn't seem to be generated until after the Spock specification's cleanupSpec() exits. This causes my code to report the results of the previous test run, rather than the current one. Clearly not what I want!
Is there some way to get to the results from within cleanupSpec() without relying on the XML? Or a way to get the results to file earlier? Or some alternative that will be much better than either of those?
Some code:
In build.gradle, I specify the testResultsDir. This is where the XML file is written after the Spock specifications exit:
drivers.each { driver ->
task "${driver}Test"(type: Test) {
cleanTest
systemProperty "geb.env", driver
testResultsDir = file("$buildDir/test-results/${driver}")
systemProperty "proj.test.resultsDir", testResultsDir
}
}
Here is the setupSpec() and cleanupSpec() in my LoginSpec class:
class LoginSpec extends GebSpec {
#Shared def SauceREST client = new SauceREST("redactedName", "redactedKey")
#Shared def sauceJobID
#Shared def allSpecsPass = true
def setupSpec() {
sauceJobID = driver.getSessionId().toString()
}
def cleanupSpec() {
def String specResultsDir = System.getProperty("proj.test.resultsDir") ?: "./build/test-results"
def String specResultsFile = this.getClass().getName()
def String specResultsXML = "${specResultsDir}/TEST-${specResultsFile}.xml"
def testsuiteResults = new XmlSlurper().parse( new File( specResultsXML ))
// read error and failure counts from the XML
def errors = testsuiteResults.#errors.text()?.toInteger()
def failures = testsuiteResults.#failures.text()?.toInteger()
if ( (errors + failures) > 0 ) { allSpecsPass = false }
if ( allSpecsPass ) {
client.jobPassed(sauceJobID)
} else {
client.jobFailed(sauceJobID)
}
}
}
The rest of this class contains login specifications that do not interact with SauceLabs. When I read the XML, it turns out that it was written at the end of the previous LoginSpec run. I need a way to get to the values of the current run.
Thanks!
Test reports are generated after a Specification has finished execution and the generation is performed by the build system, so in your case by Gradle. Spock has no knowledge of that so you are unable to get that information from within the test.
You can on the other hand quite easily get that information from Gradle. Test task has two methods that might be of interest to you here: addTestListener() and afterSuite(). It seems that the cleaner solution here would be to use the first method, implement a test listener and put your logic in afterSuite() of the listener (and not the task configuration). You would probably need to put that listener implementation in buildSrc as it looks like you have a dependency on SauceREST and you would need to build and compile your listener class before being able to use it as an argument to addTestListener() in build.gradle of your project.
Following on from erdi's suggestion, I've created a Sauce Gradle helper library, which provides a Test Listener that parses the test XML output and invokes the Sauce REST API to set the pass/fail status.
The library can be included by adding the following to your build.gradle file:
import com.saucelabs.gradle.SauceListener
buildscript {
repositories {
mavenCentral()
maven {
url "https://repository-saucelabs.forge.cloudbees.com/release"
}
}
dependencies {
classpath group: 'com.saucelabs', name: 'saucerest', version: '1.0.2'
classpath group: 'com.saucelabs', name: 'sauce_java_common', version: '1.0.14'
classpath group: 'com.saucelabs.gradle', name: 'sauce-gradle-plugin', version: '0.0.1'
}
}
gradle.addListener(new SauceListener("YOUR_SAUCE_USERNAME", "YOUR_SAUCE_ACCESS_KEY"))
You will also need to output the Selenium session id for each test, so that the SauceListener can associate the Sauce Job with the pass/fail status. To do this, include the following output in the stdout:
SauceOnDemandSessionID=SELENIUM_SESSION_ID

Resources