Karate - Referencing variable within schema, js function - karate

I have .js file whose content is as below... where companyAssociations is list of object for which i have defined schema on top in var...
function() {
var companyAssociationsSchema = '{"companyId":"#string", "displayName":"#string", "associationType":"#string", "companyType":"#string" }';
return {
"StationSchema":{
"stationId":"#string",
"companyAssociations":"#[] "+ companyAssociationsSchema,
"primaryBroadcastLanguage":"#string"
}
}
}
and here is how i read/load schema in my feature file * def getStationResponse = call read(path to above file) and than wherever i need to check for above schema i refer to it as getStationResponse.StationSchema
above works in karate 0.9.6 but i am having hard time getting same thing working in karate 1.1.0
Any help would be appreciated

Related

Kotlin Room DB Export schemas

class RoomSchemaArgProvider(
#get:InputDirectory
#get:PathSensitive(PathSensitivity.RELATIVE)
val schemaDir: File
) : CommandLineArgumentProvider {
override fun asArguments(): Iterable<String> {
// Note: If you're using KSP, you should change the line below to return
// listOf("room.schemaLocation=${schemaDir.path}")
return listOf("-Aroom.schemaLocation=${schemaDir.path}")
}
}
I need to Export old db schema in json. I wanted to use the above code if any one used this as per https://developer.android.com/training/data-storage/room/migrating-db-versions#export-schemas please help me with the same.
i tried to use as per this https://developer.android.com/training/data-storage/room/migrating-db-versions#export-schemas
problem faced during testing migration. hence i need this solution.
java.io.FileNotFoundException: Cannot find the schema file in the assets folder. Make sure to include the exported json schemas in your test assert inputs. See https://developer.android.com/training/data-storage/room/migrating-db-versions#export-schema for details. Missing file: com.sboxnw.freeplay.data.database.SugarBoxDatabase/2.json
at androidx.room.testing.MigrationTestHelper.loadSchema(MigrationTestHelper.java:484)
at androidx.room.testing.MigrationTestHelper.createDatabase(MigrationTestHelper.java:238)
at com.sboxnw.freeplay.DownloadMigrationTest.testAllMigrations(DownloadMigrationTest.kt:72)
I need to Export all old db json schema for migration testing .
It is failing due to src path not define correctly. You can add source path directly in defaultConfig of build.gradle
javaCompileOptions {
annotationProcessorOptions {
arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
}
}

Getting "not defined" error while calling a feature file from another feature file (Karate framework)

Whenever I try to call the feature file(one.feature) from another feature file(starter.feature), I am not able to access the info(caller) in specific scenario
But It is working fine and I am able to access info(caller),
when I run one.feature file standalone.
when I run one.feature file through Junit test case.
CODE WORK-FLOW :
In one.feature :
In the scenario(Scenario: matching two Strings) we are trying to match two Strings and configuring afterScenario JS function to get the info of the scenario and If the info contains any errors, The function will call the other scenario(Scenario: getting caller details) with info details assigned in the variable caller
starter.feature :
Feature: call another feature
Scenario: calling feature file
* def callFeat = call read('one.feature')
one.feature :
Feature: get details
Background:
* configure afterScenario =
"""
function() {
var info = karate.info;
if(info.errorMessage){
karate.call('one.feature#CallCaller', { caller : info });
}
}
"""
#CreateErrorMsg
Scenario: matching two Strings
* def Text1 = 'Hey'
* def Text2 = 'Hello'
* match Text1 == Text2
#CallCaller #ignore
Scenario: getting caller details
* print caller
Can't you use karate.log from the JS function rather than calling another scenario to just print the info? Also, note that karate.info has been deprecated from 1.0 version of Karate and replaced by karate.scenario - https://github.com/karatelabs/karate/wiki/1.0-upgrade-guide#karateinfo-deprecated
* configure afterScenario =
"""
function() {
var info = karate.info;
if(info.errorMessage){
karate.log(info.scenarioName);
}
}
"""

javascript function is not recognized as javascript code

I have the following javascript file MyService.js:
function(config) {
config.MyService = function(request) {
return call('classpath:path/to/my.feature#tag', request);
};
return config;
}
I load this js from my karate-config.js in order to reuse it from my feature files.
config = karate.callSingle('classpath:path/to/MyService.js', config);
It works as expected and I can call my.feature from any feature file. For example:
Given def res = call MyService myRequest
The problem appears when I try to add a new level to MyService.js function:
function(config) {
config.ApiOauthService = {
myCall : function(request) {
return call('classpath:path/to/my.feature#tag', request);
}
};
return config;
}
When I add the following code to my feature file:
Given def myCall = call MyService.myCall myRequest
I get the following error: "not a js function or feature file"
Do anybody know where is the problem? Is it possible to do what I am trying to do?
Thanks
Yes in Karate 0.9.3 onwards we have limited JS function nesting to only the top-level. You can find a full explanation in this comment.
So if you need JS function "name spacing" you can easily achieve this as per the documentation: https://github.com/intuit/karate#multiple-functions-in-one-file
And if you need more complex nesting, switch to using Java code where you can nest functions at any level.

How to call a particular javascript function from .js file in karate feature file

Suppose I have saved followings functions in a Utility js file.
function getCurrentDate(){
return 'date';
}
function getMonth(){
return 'Oct';
}
Please help me how any of these methods can be accessed in feature file.
I tried following code but it is not working.
* def fun = call read('Utility.js')
* def result = getData()
or
* def result = fun.getData()
In Karate, a JS file can contain only one function and it does not need a name, take a closer look at the examples.
I don't really recommend combining multiple functions into one file, it just makes things much harder to maintain. But if you really insist, here's how:
function() {
return {
getCurrentDate: function(){ return 'date' },
getMonth: function(){ return 'month' }
}
}
EDIT: a much better answer is here: https://stackoverflow.com/a/49384760/143475
* def data = karate.read('classpath:<path>/getRandomString.js')(<inputToTheFunctionIfAny>);
The above code works.

Serenity+jbehave : How to pass test data from external resources

I am using Jbehave and serenity in my BDDs.My requirement is "Passing test data from excel sheet".How do I get the test data from excel in my Given when and then?
I tried with injecting the test data to a test step,
withTestDataFrom( filePath ).run( testSteps ).givenStatement( #param1,#param2 );
But that wont satisfies my requirement. Is there any other way to do it?
you can use Apache POI just like java code. refe this link and this.
you can also try the below code for any other external input cases.
you can use the properties file like this.
you can also use the JBehave table parameter just like this.
This worked for me:
put the pipe ("|") delimited rows into src\test\resources\data\data.table
in build.gradle, put
sourceSets {
main {
java {
srcDirs = ['src/main/java']
}
}
test {
java {
srcDirs = ['src/test/java']
}
resources {
srcDirs = ['src/test/resources']
}
}
}
in the .story file, write:
Examples:
data\data.table