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

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

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()]
}
}

Karate - Referencing variable within schema, js function

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

Reading parameters from TestNG file

I successfully implemented several tests within TestNG framework, where parameters are being read from xml file.
Here is the example block that is executed as first:
#Parameters({ "country" })
#BeforeSuite(alwaysRun = true)
public void prepareRequest(String country, ITestContext cnt) {
LoginInfoRequestParm loginParms = new LoginInfoRequestParm(country);
Headers reqHeaders = new Headers();
reqHeaders.setHeaders(loginParms);
}
The problem/question is, why does it work only if the ITestContext is specified? Once it is removed, the overall suite is broken and it will never come to the specified method prepareRequest(). I was not able to debug it, because I cannt set breakpoint before the method to be able to see what is going on in TestNG itself.
Thank you for your explanation.
To get out of this situation, try something like this
String myPar = context.getCurrentXmlTest().getParameter("country");
if (myPar == null) {
myPar = "INDIA";
}
now myPar can be used, only thing here is if you run class for debug or any other purpose then we are using INDIA. if we run from testng.xml file then it will take values from that file.

Running Kotlin HTML Builder in the Browser

I am a Java developer that is very new to Kotlin. I love the language though, and I like how easily web applications can be done with it. The problem is that I cannot figure out how to run Kotlin HTML builder files in the browser, so that I can create a basic web page skeleton in kotlin. I can output it in the IDE, but it is silly how hard it seems to be to get it to run in my browser. This may be a dumb question and I'm missing something very obvious, but I can't seem to find the answer online.
Keep in mind that I'm not using the Intelli-J IDE. Would love to, but can't afford to pay out the nose just to do web development in Kotlin. Been using Eclipse.
Thanks in advance.
When you use Kotlin html builders kotlinx.html or any other of that sort, you need to, well, build them in order to get HTML for the browser.
There are no such thing as "Kotlin builder files". Those constructs are plain Kotlin code, so you write them inside your (server?) codebase, compile them and then invoke them to generate HTML responses. This also means you need a (normal Java) router framework, like Spark for example.
To sum up, html-builders are a way to generate HTML strings, so they do not include a way to ship the HTML elsewhere.
Kotlinx itself doesn't have any utilities to send the result to the user's browser. It's just a regular Kotlin code which can create HTML string. You need a way to send it to the user. There are some.
The simplest one is plain old Java servlets. Anybody still using them?
#WebServlet(urlPatterns = arrayOf("/servlet"), loadOnStartup = 1)
class KotlinxHtmlServlet : HttpServlet() {
override fun doGet(request: HttpServletRequest?, response: HttpServletResponse?) {
response!!.setContentType("text/html")
response!!.writer.appendHTML(true).html {
head {
title = "Hello from kotlinx.html + Servlets"
}
body {
h1 { +"Kotlin is awesome" }
p {
+"Read more about "
a("http://kotlinlang.org") {
target = ATarget.blank
+"it"
}
}
}
}
}
}
Spring Boot is very popular today. However, this #Controller will work in vanilla Spring too:
#Controller
class KotlinxHtmlController {
#ResponseBody
#RequestMapping(path = arrayOf("controller"), method = arrayOf(RequestMethod.GET))
fun doGet(): String {
return createHTML(true).html {
head {
title = "Hello from kotlinx.html + Servlets"
}
body {
h1 { +"Kotlin is awesome" }
p {
+"Read more about "
a("http://kotlinlang.org") {
target = ATarget.blank
+"it"
}
}
}
}
}
}
SparkJava is one of the plenty of young Java micro-frameworks. Note, that in case of SparkJava you can just write routes inside your main:
fun main(args: Array<String>): Unit {
get("spark", { request: Request, response: Response ->
createHTML(true).html {
head {
title = "Hello from kotlinx.html + Servlets"
}
body {
h1 { +"Kotlin is awesome" }
p {
+"Read more about "
a("http://kotlinlang.org") {
target = ATarget.blank
+"it"
}
}
}
}
})
}
I'm leaving dependency management, running the app and guessing the correct URLs to access generated pages to you. All of the above examples will result in this HTML:
<html>
<head title="Hello from kotlinx.html + Servlets"></head>
<body>
<h1>Kotlin is awesome</h1>
<p>Read more about it</p>
</body>
</html>
You can also try Dropwizard or Ninja frameworks.
Also, you can take a look at Kara – a web frameworks especially designed for Kotlin – but it is still in alpha stage.
I may be missing something here, but if using kotlinx.html javascript version, the resultant js code does perform as a DOM builder ... can add more if this is what is required.

Is it possible to skip tests if an element doesn't exists?

I'm writing a test script for a website. The website has tabs (navigation link).
Let's say, the element of that tab is id=email.
If that doesn't exist, is it possible to skip the whole test. All test cases are based on that tab (id=email).
Right now, I have:
if($this->isElementPresent("id=email") == true) {
perform these steps
}
And all the test scripts are like that, so it's just opening the browser and closing it without testing anything. It's passing them all. Is it possible to skip tests if that element doesn't exists?
I would configure the test to use the same setting to see if the fields exist or not, instead of skipping tests. Mock your configuration, and set to disabled, then the tests should look for the absence of the fields, and test accordingly. Then, set the configuration to be enabled, and test that the field is there and test accordingly.
When the field is set to be disabled, you can also use the $this->markTestSkipped(). It is documented in the PHPUnit help Chapter 9. Incomplete and Skipped Tests.
Sample:
public function testEmailIdAbsent()
{
if($this->MockConfiguration['Email'] == 'disabled') // Or however your configuration looks
{
$this->assertFalse($Foo->IsElementPresent("id=email", "Email ID is present when disabled.");
...
}
}
public function testEmailIdPresent()
{
if($this->MockConfiguration['Email'] == 'enabled') // Or however your configuration looks
{
$this->assertTrue($Foo->IsElementPresent("id=email", "Email ID is not present when enabled.");
...
}
}
public function testEmailId()
{
if($this->MockConfiguration['Email'] == 'disabled') // Or however your configuration looks
{
$this->markTestSkipped('Email configuration is disabled.');
}
}