Is there a way to use node js with karate ZIP edition [duplicate] - karate

I'd like to use the 'faker' library to generate fake data in JSON file as below.
In karate-config.js, I do the following:
var faker = require('faker');
In sample.json:
{
'firstName': '#(faker.name.firstName)'
'city' : '#(faker.address.city)'
}
But I'm getting error like 'unable to find 'require' keyword in 'karate-config.js'
Please help on this.

Karate does not support NPM or the require keyword. For complex custom logic, the recommendation is to use Java interop.
My suggestion is you should find a Java library that does what "faker" does and integrate it.

First add the below dependency in your pom.xml
<dependency>
<groupId>com.github.javafaker</groupId>
<artifactId>javafaker</artifactId>
<version>1.0.2</version>
</dependency>
For the latest version of the dependency click here
Use the below code in karate-config.js:
config.faker = Java.type('com.github.javafaker.Faker');
In the feature file use the below code:
* def fakerObj = new faker()
* def fName = fakerObj.name().firstName()
* def lName = fakerObj.name().lastName()
* def mailId = fName+'.'+lName+'#test.com'
You could use the same in JSON body as follows:
"emailAddress":"#(mailId)",
"firstName":"#(fName)",
"lastName":"#(lName)",
"address":{
line1:"#(fakerObj.address().streetAddress())"}
Please click here for the class and methods of faker package

As far as I know, karate only supports java based dependencies. So try to find a Java equivalent for your JS library.

thanks for your response and suggestion, tried below and working fine.
in karate-config.js:
var faker = Java.type('.FakerClass');
......
config.faker = faker;
in sample.json:
{ 'name': '#(faker.address.city)' }

Related

Feature with tag still being run when configured not to

I have a main feature file where I have included a "setup" feature file that should add some test data. This setup feature file has an annotation that I have called #ignore. However, following the instructions in this Can't be enable to #ignore annotation for the features SO answer, but I am still seeing the setup feature file being run outside of the main test feature.
Main feature file, unsubscribe_user.feature:
Feature: Unsubscribe User
Background:
* def props = read('properties/user-properties.json')
* url urlBase
* configure headers = props.headers
* def authoriZation = call read('classpath:basic-auth.js') { username: 'admin', password: 'admin' }
* def testDataSetup = call read('classpath:com/meanwhileinhell/app/karate/feature/mockserver/testDataSetup.feature') { data1: #(props.data1), data2: #(props.data2) }
Scenario: Unsubscribe user
...
...
Scenario: Remove test data
* def testDataTearDown = call read('classpath:com/meanwhileinhell/app/karate/feature/mockserver/testDataTearDown.feature') { data1: #(props.data1), data2: #(props.data2) }
...
testDataSetup.feature file
#ignore
Feature: Add data to REST Mock Server
Background:
* url mockServerUrlBase
Scenario: Add data
* print 'Adding test data'
Given path 'mapping'
And request { data1: '#(data1)', data2: '#(data2)' }
When method post
Then status 201
Now from my Java runner class, I have added #KarateOptions(tags = "~#ignore").
import org.junit.runner.RunWith;
import com.intuit.karate.KarateOptions;
import com.intuit.karate.junit4.Karate;
import cucumber.api.CucumberOptions;
#RunWith(Karate.class)
#CucumberOptions(features = "classpath:com/meanwhileinhell/app/karate/feature/unsubscribe_user.feature")
#KarateOptions(tags = "~#ignore")
public class KarateTestUnSubscribeUserRunner {
}
However, I can still see my print statement in my setup class being called, and two POSTs being performed. I have also tried running my suite with the following cmd options, but again, still see the feature file run twice.
./gradlew clean test -Dkarate.env=local -Dkarate.options="--tags ~#ignore" --debug
I am following this wrong somewhere? Is there something I can add to my karate-config.js file? I am using Karate version 0.9.0.
Annotations only work on the "top level" feature. Not on "called" features.
If your problem is that the features are being run even when not expected, you must be missing something, or some Java class is running without knowing it. So please follow this process and we can fix it: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue
EDIT: I think I got it - please don't mix CucumberOptions, in fact we deprecated it, use only KarateOptions. Even that is not recommended in 0.9.5 onwards and you should move to JUnit 5.
Read the docs: https://github.com/intuit/karate#karate-options

Cannot resolve method 'format' in Cucumber reporting

Trying to run a cucumber scenario with reporting to a JSON file, but for some reason the 'format' isn't working. It states 'cannot resolve method 'format'. I'm following a tutorial and can't understand why this isn't working. Any help would be appreciated, thanks
working with selenium/java/intelliJ/testNG
Replace format with plugin as format option was deprecated from V. 1.2.0 onwards on 30-October-2014. Example below -
#RunWith(Cucumber.class)
#CucumberOptions(features = "classpath:features/functional/",
glue = {"com.jacksparrow.automation.steps_definitions.functional" },
plugin = { "pretty","json:target/cucumber-json/cucumber.json",
"junit:target/cucumber-reports/Cucumber.xml", "html:target/cucumber-reports"},
tags = { "#BAMS_Submitted_State_Guest_User" },
strict = false,
dryRun = false,
monochrome = true)

How to set a cookie in Geb / Selenium with PhamtomJS

How do you set cookie in Geb ? I'm running into the following error with the given example:
org.openqa.selenium.InvalidCookieDomainException: {"errorMessage":"Can only set Cookies for the current domain" ....
.. Ive also tried explicitly setting the cookie domino using the Cookie Builder though that only cause another exception : org.openqa.selenium.UnableToSetCookieException: {"errorMessage":"Unable to set Cookie"}
Note that I used to have a baseURL in the GebConfig.groovy file .. but I have removed it as well .. Other then PhantomJS driver config, there are no settings in the config file.
I'm on OSX and using PhantomJS latest version (1.3.0 jar, and 2.1.1 driver OSX).
Note the example DOES work using the Chrome Webdriver for some reason.
import geb.spock.GebSpec
import org.openqa.selenium.Cookie
class SetCookieIT extends GebSpec {
def "Cookie example"() {
given:
def options = driver.manage()
when:
go "https://www.wikipedia.org/"
then:
!options.getCookieNamed("my-geb-cookie")
when:
options.addCookie(new Cookie("my-geb-cookie", "foobar"))
go "https://www.wikipedia.org/"
then:
title == "Wikipedia"
options.getCookieNamed("my-geb-cookie").value == "foobar"
}
}
Wikipedia is not spelt with an "ie" in the domain name and "org.com" also looks very strange. Maybe next time you want to provide an example which is actually executeable and does something meaningful. :-7
For me this works nicely:
package de.scrum_master.stackoverflow
import geb.spock.GebReportingSpec
import org.openqa.selenium.Cookie
class SetCookieIT extends GebReportingSpec {
def "Cookie example"() {
given:
def options = driver.manage()
when:
go "https://www.wikipedia.org/"
then:
!options.getCookieNamed("my-geb-cookie")
when:
options.addCookie(new Cookie("my-geb-cookie", "foobar"))
go "https://www.wikipedia.org/"
then:
title == "Wikipedia"
options.getCookieNamed("my-geb-cookie").value == "foobar"
}
}
If you have any further problems, please update your question and provide an SSCCE reproducing the actual problem.
Update after the question was modified: The problem with PhantomJS is that it refuses to create cookies if you do not explicitly specify the domain. This works:
options.addCookie(new Cookie("my-geb-cookie", "foobar", ".wikipedia.org", "/", null))

Ensure Ruby version in Nix Dev Environment when using latest version

Currently, the default and latest Ruby in Nix is 2.2.2-p0. When I run nix-env -qaP ruby it returns a list, which says that this ruby version is accessed via nixpkgs.ruby. I expect that this Ruby link will change to stay up-to-date with the latest supported ruby version. There is no optional nixpkgs.ruby_2_2_2 for me to use to ensure my ruby version.
Looking at the .nix definition file at https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/interpreters/ruby/ruby-2.2.2.nix, however, I see that they specify the revision in that script.
So I'm wondering, is there some way for me to specify the revision of the Nix package that I want when I'm listing it in the buildInputs of my Nix expression for creating the development environment (which will be accessed via nix-shell .)? Or is there something else that I might do that would enable me to ensure that ruby 2.2.2-p0 is used for the installation, and not just the latest Ruby, which might change?
Current script:
let
pkgs = import <nixpkgs> {};
in with pkgs; {
rubyEnv = stdenv.mkDerivation rec {
name = "ruby-env";
version = "0.1";
src = ./.;
buildInputs = [
stdenv
ruby
bundler_HEAD
];
};
}
I didn't see this covered in the documentation at http://nixos.org/nix/manual/#chap-writing-nix-expressions
There is no optional nixpkgs.ruby_2_2_2 for me to use to ensure my
ruby version.
Actually there is a ruby_2_2_2 in nixpkgs:
$ nix-env -qaP ruby
nixos.ruby_1_8 ruby-1.8.7-p374
nixos.ruby_1_9 ruby-1.9.3-p551
nixos.ruby_2_0 ruby-2.0.0-p645
nixos.ruby_2_1_0 ruby-2.1.0-p0
nixos.ruby_2_1_1 ruby-2.1.1-p0
nixos.ruby_2_1_2 ruby-2.1.2-p353
nixos.ruby_2_1_3 ruby-2.1.3-p0
nixos.ruby_2_1 ruby-2.1.6-p0
nixos.ruby_2_2_0 ruby-2.2.0-p0
nixos.ruby ruby-2.2.2-p0
nixos.bundler_HEAD ruby-2.2.2-p0-bundler-2015-01-11
By looking at the definition of ruby package in the index, you can see that the current default ruby is just an alias to ruby 2.2:
ruby = ruby_2_2;
that is in turn an alias to ruby 2.2.2:
ruby_2_2 = ruby_2_2_2;
To override the ruby package to a specific ruby version in a nix expression, overridePackages can be used:
let
nixpkgs = import <nixpkgs> {};
pkgs = nixpkgs.overridePackages (self: super: {
ruby = nixpkgs.ruby_2_2_2;
});
in with pkgs;
{
rubyEnv = stdenv.mkDerivation rec {
name = "ruby-env";
version = "0.1";
src = ./.;
buildInputs = [
stdenv
ruby
bundler
];
};
}

Jython multipart post with commons http-client/core

Hello im trying to send a multipart post request and using jython with commons httpclient 4.3.1.
http_client = DefaultHttpClient()
http_post = HttpPost(url)
bin = FileBody(File(file_name), ContentType.APPLICATION_XML)
me = MultipartEntity()
me.addPart('datei', bin)
http_post.setEntity(me)
print "Executing Post Request:", http_post.getRequestLine()
http_response = http_client.execute(http_post)
result_entity = http_response.getEntity()
return EntityUtils.toString(result_entity)
In my opinion the return value should a str with the content, but it is:
"type 'org.apache.http.conn.BasicManagedEntity'"
Where is my mistake?
I do not know the exact answer to your question. And today I have been trying to make a multipart post work from my jython script for a skiuli project I am working on.
Here is how I was able to do it.
First get ez_setup.py from: http://peak.telecommunity.com/dist/ez_setup.py
Then run jython ez_setup.py from the cmd prompt
Second get poster from: http://atlee.ca/software/poster/dist/0.8.1/
Unzip the file
Then run jython setup.py install
Last you can follow this sample code or see more on the atlee site:
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers
import urllib2
register_openers()
upload_url = "http://your.website.com/upload-multipart.php"
#this will upload a file and a json object
#in php the file is in $_FILES and the json is in $_POST
params = {'file': open("test.txt", "rb"), 'jsonData': '[{"foo":"bar", "foo1":"bar1"}]'}
datagen, headers = multipart_encode(params)
request = urllib2.Request(upload_url, datagen, headers)
result = urllib2.urlopen(request)
print result.read()
I hope this helps someone out there. This example will also work for post multipart in python.