What is the equivalent of karate.get() in Java? - karate

I have created a Java object in karate scripts using Java interop and have used it as required.
Now, I want to retrieve that same instance in another Java class. Is there a way to do that?
In karate, we have an equivalent method like karate.get('objectName'). Is there any equivalent API available in Karate lib?

Please assume that the answer is no. I'd like to mention that the whole point of creating Karate was to avoid using Java for API test automation. I say this because I suspect based on your questions that you have made the same mistake I've seen a few other teams make - which is attempting to make Karate work like the Seleniums and Rest Assureds of the world. That always ends badly. And what's worse is that these teams blame Karate for that and go around telling everyone. I'd rather have that not happen - so if this is a "blocker" please consider using some other tool.
If you are already calling Java, you have plenty of ways to pass objects around right? Just use that and don't over-complicate your tests. Or you can use the "singleton" pattern, nothing to do with Karate, you can look it up.

Related

Add Karate custom actions [duplicate]

This question already has an answer here:
Do Karate support .avro file as an input request?
(1 answer)
Closed 1 year ago.
Beyond the interfaces that you offer out of the box, we have some other interfaces which need to be integrated within E2E tests. Those are TCP based protocols.
Is there any chance that you will make karate extendable any time soon so that we can make such calls in actions like statements and not via JS/Java API?
We would need something like
... REST API Test
And send customProtocol message(attr1=1,attr2=2)
Then expect customProtocol message(res=OK)
... GUI Test
Karate is already "extendable" via Java interop. If you use Java interop, you will need one extra line to "import" the API you need. And yes, you will not get the "natural language" look that you seem to be after, but in my honest opinion, all of that is very over rated.
So if the extra line and using an API that looks like Java in a feature file is a deal-breaker for you, you may need to look for some other framework.
Please take a look at this example, where you can get close to a custom DSL using the JS or Java-interop approach: https://twitter.com/getkarate/status/1144458169822806016
I also think that for your TCP based protocol, this answer directly answers your question: https://stackoverflow.com/a/62826394/143475

how can we call specific scenario with #tag in feature files using the Java API?

Yes, we can "select" a single Scenario by appending a "tag selector" at the end of the feature-file you are calling.
question
However, do we support calling specific scenario with #tag in feature files using the Java API?
Map<String, Object> result = Runner.runFeature(getClass(), "from-java.feature", args, true);
It seems "Runner.runFeature" only supports feature files without #tag
You are the first person ever to ask for this and I just made this commit.
I leave it to you to test and if it doesn't work you can contribute code. Please refer to the Developer Guide if needed.
My honest opinion based on your questions is you are trying to use Karate for things it is not designed for. Karate is a testing framework.
So maybe you should look at the new Java API because it makes some of the features available in different ways: https://github.com/intuit/karate/wiki/1.0-upgrade-guide#java-api

Karate Java API for Match methods

Background:
I noticed this interesting post on Twitter about a Java API for Karate: https://twitter.com/ptrthomas/status/1344290316212342784
I don't have a twitter account so unable to reply - hope you don't mind me asking here instead.
I have used Karate (with the parallel runner, feature files and all) in one work project and it is a joy to use - and a few folks in the workplace are very impressed with the complex JSON assertion. We are achieving a level of JSON response field coverage for that project that is not possible with other frameworks (IMO).
I've now been tasked with improving an older test project which uses JUnit + RESTAssured, but has quite limited field assertions. I am unable to fully convert it to raw Karate (too many tests, not enough time), but I'm keen to explore the possibility of supplementing what is there already with the Karate Java API mentioned in above twitter post.
For example - for an existing test which gets a Response using RESTAssured, it would be nice for me to somehow use the 'contains' method (or any of the methods in Match class) in Karate to assert that response, as it is the best JSON assertion tool I have come across.
Questions:
Off the bat - is this a practice you would recommend for my use case? Let me know if you think this is an anti-pattern. I ask this because the example here (https://github.com/ptrthomas/karate-showcase) is not using RESTAssured to get the response and my use case is a bit different.
If its worth exploring - I assume this stuff will be available in v1.0.0? (I see public methods such as that are on the develop branch, but couldn't see it on the v1.0.0 github thread).
but I'm keen to explore the possibility of supplementing what is there already with the Karate Java API mentioned in above twitter post.
That's very interesting and you have got me also thinking about this potentially interesting way to introduce Karate to legacy projects. I do get this question on how to magically migrate existing RA tests a lot.
I would recommend it. You can expect a 0.9.9.RC3 next week that should have all the assertions you want. And the API has been simplified so you don't need to call isTrue() etc.
We plan to have better examples (maybe you can help) but for now, use this as a starting point (look at the last test / very end of the file): MatchTest.java
If you want to get started now (which I recommend and you can provide more feedback) do take a look at the developer guide: https://github.com/intuit/karate/wiki/Developer-Guide

How do I access/read the Phonegap API?

I know there is this: http://docs.phonegap.com/en/2.1.0/index.html but it doesn't really help.
I am trying to learn about the appView variable (I think it's a variable). I would've said it was a class but it starts with a lower case letter :/
The reason I am trying to learn that is because I am trying to understand the appView.addJavascriptInterface(Object, String) method.
My main goal is to send a variable from a java file to a javascript file. Tutorials online seem to be using the method stated above. Because the method takes in an object, the tutorials seem to be creating another class. I want to simplify my code as much as possible so I was seeing if there are any other options.
You will want to write a Plugin. We've already gone through the pain of the JS to Java and back to JS communication. If you purely use addJavaScriptInterface you will run into some edge cases where it doesn't work and we already guard against.
In appView.addJavascriptInterface(Object, String) method Object refers to Java object from which you want to transfer data from Java to java script.
You can't achieve functionality without creating new class.
Apart from Plugin usage above mentioned way only we can achieve communication between java and javascript in phonegap apps.

Is mocking functionality available for Microsoft Fakes?

With the inclusion of Microsoft Fakes in Visual Studio 11, I thought it would be worth to look into again, since I did not find Moles mature enough last i checked. The documentation is still sparse, but I cannot find any reference to mocks, i.e. the ability to set up and verify expectations on the faked objects.
Does anyone know if this is currently included (as a feature in the library) or will be?
It is not included currently, but we are considering something in this area.
Although they are not included, you can use stubs to hack in the same behavior. In your stub you can capture whether the method was called and the parameters that were called. Of course, by the time you start doing that you're almost hand rolling your own mocks.
In the meantime I'd suggest Moq or RhinoMocks. I find the syntax simpler and I'm not a fan of the generated code that Microsoft Fakes uses anyhow. Try renaming a method on one of your fakes using a refactoring tool. It's not possible because your fake is a generated class, not an instance of the interface you are stubbing.