Using variables inside gherkin syntax - java

I'm trying to write my step definitions for my feature file, and I'm having abit of an issue.
Part of my scenario is
Then the "CUSTOMER_ORDER_PAGE" is displayed
The step for this is
#Then("^the \"([^\"]*)\" is displayed$")
public void the_is_displayed(String arg1) {
assertEquals(chromeDriver.getCurrentUrl(), arg1)
}
the CUSTOMER_ORDER_PAGE is actually a String in a separate class which i want to be called into the assert equals, but it keeps passing through the variable name instead.
Does anyone know a way of taking the string passed through the gherkin syntax and then using the value assigned to that variable inside the steps definition?
p.s ignore the chromeDriver stuff, it's just finding the current URL.

Related

Get console parameters in ISuite class (TestNG)

I´m writting a new listener to open and close the database connection at start and end of the suite. I defined several parameters in the testng.xml file to store the database credentials, for example:
<parameter name="DB.user" value="user"/>
Now, I´m trying to get this parameters in the listener:
public class DatabaseListener implements ISuiteListener {
#Override
public void onStart(ISuite suite) {
System.out.println( suite.getXmlSuite().getParameter("DB.user"));
}
This works fine, but I also need this parameters will be updated if I overwrite the value by command line:
-DDB.user=test
I´m always getting the value defined in the testng.xml (user) instead the value I set in console (test).
For example, in a test I do the next to get the runtime value:
#BeforeClass()
#Parameters({"DB.user"})
public void beforeClass(String DBUser)
How can I do this in the listener?
Thanks and regards.
TestNG is capable of updating <parameters> by using their names and then overriding their values with the values provided via JVM arguments (-D). But that happens at a much later stage and well before an actual #Test method is invoked.
But its not true when TestNG is invoking suite level listeners, which is what you are doing. At this point, TestNG is still just constructing the suite file and the above mentioned parameter value resolution is yet to happen. That explains why you always see the value that is present in the suite file.
One way of dealing with this would be to do the following:
public class DatabaseListener implements ISuiteListener {
#Override
public void onStart(ISuite suite) {
String fromSuite = suite.getXmlSuite().getParameter("DB.user");
String parameter = System.getProperty("DB.user", fromSuite);
System.out.println( parameter);
}
As you can see, you are explicitly querying from the System properties and at the same time defining a default value which comes from your suite. Kind of works the same way as TestNG resolves parameters (but at a later stage)

Java Cucumber custom object type arguments

I'm new to cucumber and i need to use it to implement tests for my java app. Among my tests, i need to test a POST request webservice having an entity body (a custom object), so i thought i should send this body from my feature scenario to its implementation. I looked around and i saw that this is how you send a list of a custom object :
Scenario: Some scenario
Given something
|filed1 |filed2 |filed3 |
|value1 |value2 |value3 |
|value1 |value2 |value3 |
......
And its implementation:
#Given("^something:$")
public void something(List<MyObject> arg1) {
......... Using the list
}
It actualy gives the list even without a mapper, but as i said in my case for the post request body, i only need to send one object not a list, something like that: (don't know how to right the feature yet)
#Given("^something:$")
public void something(MyObject arg1) {
.........Using the object
}
I tried multiple ways but can't seem to find the right one, can anyone give me a hand or a hint. And also tell me if i'm thinking with the right logic cauze if something works doesn't mean it's a good practice. Thanx in advance

How to get default application name in cli?

I want to create a framework that shows the application name on statup. Targeting command line interface applications.
Question: how can I get such an application name in a generic way?
Eg spring offers a property, but which is not set by default:
#Value("${spring.application.name}")
private String appname;
And I don't want to set that property explicit. Looking for some kind of "default application name".
In a Java EE container there is also the following option:
String myApplicationName = (String) initialContext.lookup("java:app/AppName");
But how about CLI apps? How can I get some kind of generic application name?
The closest you can get, if I interpreted correctly your question, is to:
find which class is running public static void main(String [] args) method
get its simpleName
store aforementioned name into a system property
and in order to do so, you have two options:
call Thread.currentThread().getStackTrace(), and inspect its tail element. But this has to be executed in the main thread as well, otherwise you wont retrieve the correct StackTraceElement;
call Thread:getAllStackTraces(), and parse the entire map to identify the main Thread, get the corresponding value, and pick its last StackTraceElement
Once you have StackTraceElement, you can call StackTraceElement:getClassName() which will return something like
scala.tools.nsc.MainGenericRunner
Split the string, save it into a system property, and you're good to go.
Hope it will help you.

refractoring java bytecode

I am trying to replace a certain class file with my own in an obfuscated jar. The original class file has methods named "new" and "null" so a quick decompile + compile doesn't work.
I tried compiling and using jbe to add new methods named "new" that relayed everything to "new_symbol" functions (with new_symbol beeing the decompiled version of the original "new" function).
This did not work. ("code segment has wrong length in class file")
Does anyone know of a way to refractor method names in class files? And if that isn't possible, a way to reliably create those "proxy functions"?
From google I learned that there are about 1000+ different backend library's but only jbe as fronted for bytecode editing?
EDIT:
Let me try to illustrate it.
Let's say that there is a jar file with a class that provides a function that logs everything you give it to a database.
I'd like to replace that class file with my own, and it should not only log everything to a database, but also print whatever data it gets to the command line.
The problem is, that class file was obfuscated and the obfuscator gave it public method names like "new" or "null". If you try:
public class replacement{
public void new (string data){
...
}
}
And compile that, you get compilation errors.
My idea was to create this :
public class replacement{
public void newsymbol (string data){
...
}
}
And use a bytecode editor to create a function named "new" that calls "newsymbol" with the same arguments. (but I get "code segment wrong length" and other errors going down this route.
My question therefore could be better frased as "give me a way to intercept calls to a class file who's public methods are named "new" "null" "weird_unicode_symbols""....
Scala allows you to use identifiers in names if you surround them by `.
class f{
def `new`():Int = {
return 3
}
}
jd-gui output
import scala.reflect.ScalaSignature;
#ScalaSignature(bytes=/* snip */)
public class f
{
public int jdMethod_new()
{
return 3;
}
}
I assume that jdMethod_ is prefixed in order to make the identifier valid. There is no jdMethod_ when looking at the class file using a hex editor.
However, this does have a flaw when you need to use public fields; scalac never generates public fields, it always makes them private and creates accessors.
So, what turned out to be the best solution for me was to use a hex editor (as suggested by user60561).
Apparantly, the name of every function and field is only saved once in the class file so if you use names with the same amount of bytes you can hexedit your way to victory.
For me it came down to replacing "new" by "abc" and every strange unicode character with a two-char sequence.
Thanks for all the suggestions.

Can I access property keys with Selenium on a wicket generated html page?

I am trying to automate frontend tests with Selenium for a wicket based web application.
Therefore I have:
- Different languages
- language property files (submit.signup.form=Submit) and wicket messages () using them
- HTML pages which are generated by wicket (input type:button and value:Submit)
If I go ahead and automate a test case with that, it will work properly.
The problems start when somebody decides to change the property file to f.i. submit.signup.form=Send.
If that happens I will have to adjust all Selenium tests to check for the correct label again to make a test successful (this is not really applicalbe for that example but for error messages it will be a problem)
Now the question:
Is there a way to make wicket to put the property key onto/into the generated html files?
Desired benefit:
I can use Java and make Selenium take the property ke and check the property file for the text. That way a change of a label in the property file would not effect the Selenium tests at all and would make it much more easy to handle.
I am grateful for any answer. :)
Best regards
By default, Wicket starts in development mode. In development mode you should see the wicket tags, you should take a look in to IDebugSettings
, however you will not see the properties gathered from the java code, but you can add the key as attribute, for example
new Label(getString("propertieKey")).add(new AttributeAppender("key","propertieKey"))
It's quite easy to do actually.
Put in your application init method:
getResourceSettings().getStringResourceLoaders().add(0, new NoResourceLoader());
Implement NoResourceLoader:
public class NoResourceLoader implements IStringResourceLoader {
#Override
public String loadStringResource(Class<?> clazz, String key, Locale locale, String style, String variation) {
if ("noProperties".equals(style)) {
return key;
}
return null;
}
#Override
public String loadStringResource(Component component, String key, Locale locale, String style, String variation) {
if ("noProperties".equals(style)) {
return key;
}
return null;
}
}
This resource loader just returns the key if the style is set to noProperties. As it returns null, the localizer will try the next resourceloader for any other invocation.
In order to set style to "noProperties" I'd suggest adding a parameter check to your pages' constructor that would set the style on the session object when you call your application with the parameter.
public BasePage(PageParameters pp) {
String style = pp.get("st").toOptionalString();
if (style != null) {
getSession().setStyle("noProperties");
}
It would be enough to call your first url with this parameter set, then you should walk through the whole session with property keys instead of values in the html. I'd also disable this check when the app is running in production.

Categories

Resources