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
I have a problem with Karate afterFeature configuration.
I want to call a clean up step from a separate feature file after every scenario. Hence, I configured a afterFeature js function, that should call that cleanup feature using karates call function.
The callonce works fine in prior step, but I have problem with the afterFeature.
This is the code how I configure afterFeature:
* def result = callonce read('../callOnceCreateCompanyForBrowse.feature')
* def id = result.response.data.createCompanyEvent.id
* configure afterFeature = function(){ karate.call('../../deleteCompanyEvent.feature', {id : id}); }
Suggestion, instead of relative paths (which is hard to get right) use the classpath: prefix.
* configure afterFeature = function(){ karate.call('classpath:com/myco/deleteCompanyEvent.feature', { id: id }) }
EDIT: looks like the solution was using a runner instead of running the feature directly.
Suppose following configuration:
build.dependencies.gradle:
ext {
libraries = [:]
}
libraries += [
library : [group: 'com.example', name: 'library', version: '1.1.1']
]
build.gradle.kts:
apply(from = "build.dependencies.gradle")
dependencies {
implementation(libraries["library"]) // does not work
}
Is there a way to get values provided by Groovy script in build.gradle.kts?
It doesn’t work because Kotlin is statically/strongly typed language unlike Groovy. libraries is not defined on any object from Gradle’s API.
You can access it like so:
dependencies {
implementation((project.extra["libraries"] as LinkedHashMap<*, *>)["library"]!!)
}
println(project.extra["libraries"])
project.extra[“libraries”] returns an Object so we need to cast it correctly in order to get the next value. It is also marked as #Nullable so hence the !! operator.
—
A better way to manage dependency versions is to leverage Java Platform plugin.
I have Spring RESTfull application and I want to generate an API for it. I use a gradle configuration from there https://github.com/Casturan/swagger-gradle-example/blob/master/build.gradle to generate code. But there is problem it uses models defined in definitions: while I want it to use my models from shared module. I found that I need to use importMapping but when I try to apply this command in my build.gradle I am getting an error:
importMappings = [
'board_container': 'board_container=com.workingbit.share.domain.impl.BoardContainer'
]
> Could not set unknown property 'importMappings' for task ':myproject:generateApi' of type org.gradle.api.DefaultTask.
So question how to use importMapping and how to connect it with my model in yaml?
I haven't tried it but looking at the code of CodegenConfigurator modifying the generateApi task like this in the build.gradle might work:
task generateApi {
inputs.file("$projectDir/$swaggerSourceFile")
outputs.dir("$projectDir/$swaggerTargetFolder")
doLast{
def config = new CodegenConfigurator()
config.setInputSpec("file:///$projectDir/$swaggerSourceFile")
config.setOutputDir("$projectDir")
config.setLang('spring')
config.setAdditionalProperties([
'interfaceOnly' : 'true',
'apiPackage' : 'com.dturan.api',
'modelPackage' : 'com.dturan.model',
'sourceFolder' : swaggerTargetFolder
])
//Add this line
config.addImportMapping("board_container", "com.workingbit.share.domain.impl.BoardContainer")
new DefaultGenerator().opts(config.toClientOptInput()).generate()
}
}
The CodegenConfigurator is being called at the second line in the build.gradle you linked to in your question and it has a few methods to configure importMappings. You can have a look here (if this doesn't work try with the setImportMappings).
I am new to use ruby language and having an issue to switch the context to webview. found solution in Java that I need to put in ruby, please could you help understand how this works in Ruby?
Set<String> contextNames = driver.getContextHandles();
for (String context : contextNames) {
if (context.contains(contextName)) {
driver.context(context);
}
}
Using appium_lib, I use set_context for change context between 'NATIVE_APP' or 'WEBVIEW'. doc: http://www.rubydoc.info/gems/appium_lib/8.0.1/Appium%2FDevice%3Aset_context
And for list contexts I use: available_contexts. doc: http://www.rubydoc.info/gems/appium_lib/8.0.1/Appium/Device#available_contexts-instance_method
We created a full appium suite and use the methods below to switch the context for us:
def set_native_context
$driver.set_context($driver.available_contexts.first)
#context = 'Native_App'
end
def set_webview_context
$driver.set_context($driver.available_contexts.last)
#context = 'WebView'
end
I am not able to make plugin work with angular project template .GitHub shows only code in native and XML .Sample plugin code works but unfortunately no angular support or help given. I am not able show on angular template.
relevant code i am using
detail.component.ts
registerElement("AutoComplete", () => require("nativescript-autocomplete").AutoComplete);
public list :Array = ['1','2','3','4','567'] ;
public itemTapped(args){
console.log("tapped");
}
detail.component.html
<AutoComplete items=""{{list}}"" itemTap="itemTapped($event)"> </AutoComplete>
i am getting exception on console while page loads and autocompletion doesnt work
this.items.forEach is not a function inside plugin code .that line is with definition of AutoComplete.prototype.itemsUpdate inside autocomplete.android.js plugin source
Debugging into plugin source it breaks at initialization time :
'AutoComplete.prototype.itemsUpdate = function (items) {
var arr = Array.create(java.lang.String, this.items.length);
this.items.forEach(function (item, index) {
arr[index] = item;
});
var ad = new android.widget.ArrayAdapter(app.android.context, android.R.layout.simple_list_item_1, arr);
this._android.setAdapter(ad);
};'
In detail.component.html
<AutoComplete [items]="list" (itemTap)="itemTapped($event)"> </AutoComplete>
in details.component.ts add
public list:any= ['1','2','3','4','567'] ;
itemTapped(ev){
//console.log(ev); your code
}
Issue in npm version. Clone the repository.
Replace all the files in node_modules/nativescript-autocomplete ,expect screenshot, demo folders and git related files. And try the solution