I have Karate scenario defined as below
Feature: Random Users
Background:
* url 'https://askuser.me'
#get-user
Scenario: Get Random User data
Given path 'api'
When method get
Then status 200
* string json = response
* def Util = Java.type('com.example.mobiletest.utils.TestUtils')
* def SaveResponse = Util.writeToJSONFile(json,'randomuser.json')
And is Corresponding Runner class defined as below:
public class RandomUserRunner {
#Karate.Test
public Karate testRandomUserRunner(){
return Karate.run("RandomUser").relativeTo(getClass());
}
}
I want to execute testRandomUSerRunner() programatically from other java function, how do I do that (reason this is, karate scenario fetches response and saves in json file, other method in java want to reuses these steps)
I tried to call as below but it didnt work:
RandomUserRunner runner = new RandomUserRunner();
runner.testRandomUserRunner();
Anyhelp or pointers would be really appreciated.
First - a disclaimer. Karate is not designed for this. It looks like you are already using some Java utils from Karate, so I personally think trying to call Karate from Java is wrong. The JUnit classes exist to take care of reporting, and here also - the parallel Runner is recommended: https://stackoverflow.com/a/65578167/143475
That said, see if the Runner.runFeature() API meets your use case. You will be able to access variables created by the Feature also.
Refer: https://github.com/karatelabs/karate#invoking-feature-files-using-the-java-api
Related
I have developed UI page which has href to external link. Now this external link is built (with db based key values for http request parameters) in a class which extends GenericLink of Tapestry. I am overriding public ILink getLink(IRequestCycle cycle) and public String getHref() methods for that. The developed task is working as expected.
Now I want to write JUnit tests for this class, but I read there is other mechanism for Unit testing in Tapestry but this is again not clear to me w.r.t. GenericLinks.
Any leads to any artical will really help in order to test GenericLink class.
I'm trying to figure out how I can automatically update test case results for test cases in Rally through Cucumber automation scripts. I want to be able to run my test scripts, which will then update the test case results in Rally automatically to Pass or Fail.
Is there any way to do that with Cucumber? I'm using Cucumber along with TestNG and Rest Assured.
If you are using TestNG's QAF extension for BDD it provides a way to integrate your test results with test management tool by providing TestCaseResultUpdator. In your test case or scenario you need to provide test case id from test management tool and call api to update test result for that test case. QAF supports gherkin but gherking doesn't support custom meta-data. You can use BDD2 which is super set of gherkin and your scenario may look like below:
#smoke #RallyId:TC-12345
Scenario: A scenario
Given step represents a precondition to an event
When step represents the occurrence of the event
Then step represents the outcome of the event
In above example assume that RallyId represents test case id in test management tool. You can use it while implementing result updator.
package example;
...
public class RallyResultUpdator implements TestCaseResultUpdator{
#Override
public String getToolName() {
return "Rally";
}
/**
* This method will be called by result updator after completion of each testcase/scenario.
* #param params
* tescase/scenario meta-data including method parameters if any
* #param result
* test case result
* #param details
* run details
* #return
*/
#Override
public boolean updateResult(Map<String, ? extends Object> metadata,
TestCaseRunResult result, String details) {
String tcid = metadata.get("RallyId");
// Provide test management tool specific implemeneation/method calls
return true;
}
}
Register your updator as below:
result.updator=example.RallyResultUpdator
Result updator will automatically called by qaf when testcase completed and will run in separate thread so your test execution need not to wait.
I am using cucumber-java in groovy code. I prefer cucumber-java to cucumber-groovy because I can run the tests like plain old good JUnit tests. However, the step definition templates spitted out by cucumber are in java style. Instead, I would like to have a groovy style. For example, in java style, you will get something like
#When("^an HTTP GET request is sent to obtain config.xml of \"([^\"]*)\"$")
public void an_HTTP_GET_request_is_sent_to_obtain_config_xml_of(String arg1) throws Throwable {
// Express the Regexp above with the code you wish you had
throw new PendingException();
}
Since I am using groovy, I would like to get something like
#When(/^an HTTP GET request is sent to obtain config.xml of "([^"]*)"$/)
void 'an HTTP GET request is sent to obtain config.xml of'(String arg1) {
// Express the Regexp above with the code you wish you had
throw new PendingException();
}
I am thinking to implement such a feature. Basically, my idea is to add a new field, maybe called templateLanguage, in cucumber.api.CucumberOptions. When this new field is equal to groovy, then the groovy-style templates will be spitted. This will probably involve an if statement in cucumber.runtime.java.JavaSnippet.template(), such as
if( runtimeOptions.getTemplateLanguage().toLowerCase().equals('groovy') ) {...}
However, my question is: how can I get a reference of the runtimeOptions that is passed in like
#CucumberOptions(
format = ["pretty", "html:build/cucumber"],
features="src/test/resources/cucumber_features/api/job_view.feature",
glue=['com.yahoo.adcd.jenkins.tests.smoke.api.cucumber.job.view'],
strict = true
)
Thank you very much!
In a case like this, you would need to write your own boot class since there is no dependency injection for RuntimeOptions. A good starting location is to look at cucumber.api.cli.Main. You would need to create your own class that extends RuntimeOptions, then add in your logic there.
This solution, however, will not allow you run the app using the CucumberOptions annotation anymore. If you do prefer using the annotation though, you would need to also implement your own custom annotation and override the RuntimeOptionsFactory to use your annotation, and then use that factory in your new main class to create the runtime dynamically.
I'm looking for the best way to test a class which internally makes HTTP requests to a pre-defined URL. Generally, the class in question looks more or less like this :
public class ServiceAccess {
private static final String SERVICE_URL = "http://someservice.com/";
public ServiceAccess(String username) throws IOException,
UserNotFoundException, MalformedURLException {
URL url = new URL(SERVICE_URL + username);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
if(conn.getResponseCode() == HTTP_NOT_FOUND) {
throw new UserNotFoundException("user not found : " + username);
}
// and some more checks
}
}
I would like to test that the class properly reacts to the HTTP server's responses, including response codes, header fields, and such. I found the mockwebserver library that looks just like something I need. However, in order to use it, I would need to somehow change the URL that the class connects to.
The only sensible option that I see is to pass the URL in the constructor : however, it seems to me that this does not play too well in terms of design, since requiring the client to pass an URL to such a class looks fishy. Furthermore, I have not seen any other web service access libraries (Twitter4J, RestFB) that would require their clients to pass the URL in order to actually use them.
I'm not a Java whiz, but I'd like to get it as right as possible. All answers welcome.
What is fishy about passing the URL? Not sure I get that.
Generally for things like this, don't you want the URL to be a property? I would think in the same way that the database url for your instance is going to be constructed of properties, you would want to do the same here. In which case, in your test you just override the property/ies.
The other interesting thing about these kinds of tests is I think it's a really good idea to have tests of the actual protocol (which is what you are doing with the mock) and also the actual service and then run the service tests on a schedule, just as a way to make sure that the downstream services you are consuming are still there and honoring their end of the contract. Was reading the excellent Continuous Delivery book from Addison Wesley, contemplating making this part of a pipeline today.
if you have written your tests first, you would have never written such code :)
your class violates single responsibility rule. refactor this class. extract part responsible for networking (in your code - getting connection). then ServiceAccess should use that class. then you can easily test ServiceAccess in unit tests. unit testing networking code is pointless - guys from oracle have already done that. all you can test is that you have provided correct parameters and that's the role of integration tests
Iff you can't change the code, you could use PowerMock to mock HttpURLConnection.
I am trying to encode input form data here. There are two options and I have tried both of them:
Use URLencoder.encode(inputString) method which does not work on GWT client side (My code resides in client module) Results in error 'Did you forget to inherit required module?'
URL.encodeQueryString(inputString) which works well, But when I run relevant test cases using JUnit, all I get is unsatisfiedlinkederror
Are there any alternatives for encoding method or is there any work around for above mentioned methods?
For your second option :
GWT uses modules and needs to be compiled, which is different than running a simple JUnit test. Take a look at http://www.gwtproject.org/doc/latest/DevGuideTesting.html, they explain how to setup JUnit test.
Just use the URL class and its methods:
URL.encode(String decodedURL)
URL.encodeQueryString(String decodedURLComponent)
Do not forget to inherit the required module <inherits name="com.google.gwt.http.HTTP"/>.
For URL building, I use the "UrlBuilder": com.google.gwt.http.client.UrlBuilder
UrlBuilder u = new UrlBuilder();
u.setProtocol("https");
u.setHost("www.mysite.com:8080");
u.setPath("/myServletPath");
u.setParameter("username", nameField.getValue());
u.setParameter("someAttribute", "itsValue");
u.buildString();
This code will result in:
https://www.mysite.com:8080/myServlet?username=GWT%20User&someAttribute=itsValue