Is it possible to pass Java-Enum as argument from cucumber feature file - selenium-webdriver

I am currently using selenium with Java,And want to implement cucumber to make test script more readable.
Currently facing issue while passing argument to java method where Enum is expected as parameter.
I would also like to know if there are any other known limitations of cucumber-java before migrating current framework.

The answer is: Yes
You can use all kind of different types in your scenario: primitive types, own classes (POJOs), enums, ...
Scenario :
Feature: Setup Enum and Print value
In order to manage my Enum
As a System Admin
I want to get the Enum
Scenario: Verify Enum Print
When I supply enum value "GET"
Step definition code :
import cucumber.api.java.en.When;
public class EnumTest {
#When("^I supply enum value \"([^\"]*)\"$")
public void i_supply_enum_value(TestEnum arg1) throws Throwable {
testMyEnum(arg1);
}
public enum TestEnum {
GET,
POST,
PATCH
}
protected void testMyEnum(TestEnum testEnumValue) {
switch (testEnumValue) {
case GET:
System.out.println("Enum Value GET");
break;
case POST:
System.out.println("Enum Value POST");
break;
default:
System.out.println("Enum Value PATCH");
break;
}
}
}
Let me know how you are doing. I could try to help you.

This youtube lecture of about 11 minutes gives a good way of doing it.
https://www.youtube.com/watch?v=_N_ca6lStrU
For example,
// enum, obviously in a separate file,
public enum MessageBarButtonType {
Speak, Clear, Delete, Share
}
// method for parameter type. if you want to use a different method name, you could do #ParameterType(name="newMethodName", value="Speak|Clear|Delete|Share") according to the video.
#ParameterType("Speak|Clear|Delete|Share")
public MessageBarButtonType MessageBarButtonType(String buttonType) {
return MessageBarButtonType.valueOf(buttonType);
}
// use like this. the name inside {} should match the name of method, though I just used the type name.
#Then("Select message bar {MessageBarButtonType} button")
public void select_message_bar_button(MessageBarButtonType buttonType) {
...
}

First register a transformer based on an ObjectMapper, then you can just use enums as would be expected.
private final ObjectMapper objectMapper = new ObjectMapper().registerModule(new JavaTimeModule());
#DefaultParameterTransformer
#DefaultDataTableEntryTransformer
#DefaultDataTableCellTransformer
public Object defaultTransformer(Object fromValue, Type toValueType) {
JavaType javaType = objectMapper.constructType(toValueType);
return objectMapper.convertValue(fromValue, javaType);
}
Scenario: No.6 Parameter scenario enum
Given the professor level is ASSOCIATE
#Given("the professor level is {}")
public void theProfessorLevelIs(ProfLevels level) {
System.out.println(level);
System.out.println("");
}
public enum ProfLevels {
ASSISTANT, ASSOCIATE, PROFESSOR
}
Source

This is no more supported in latest io.cucumber maven group
https://github.com/cucumber/cucumber-jvm/issues/1393

Related

Hashset to string [duplicate]

I have a class defined as follows:
public class Person {
private String name;
// constructor and getter/setter omitted
}
I tried to print an instance of my class:
System.out.println(myPerson);
but I got the following output: com.foo.Person#2f92e0f4.
A similar thing happened when I tried to print an array of Person objects:
Person[] people = //...
System.out.println(people);
I got the output: [Lcom.foo.Person;#28a418fc
What does this output mean? How do I change this output so it contains the name of my person? And how do I print collections of my objects?
Note: this is intended as a canonical Q&A about this subject.
Background
All Java objects have a toString() method, which is invoked when you try to print the object.
System.out.println(myObject); // invokes myObject.toString()
This method is defined in the Object class (the superclass of all Java objects). The Object.toString() method returns a fairly ugly looking string, composed of the name of the class, an # symbol and the hashcode of the object in hexadecimal. The code for this looks like:
// Code of Object.toString()
public String toString() {
return getClass().getName() + "#" + Integer.toHexString(hashCode());
}
A result such as com.foo.MyType#2f92e0f4 can therefore be explained as:
com.foo.MyType - the name of the class, i.e. the class is MyType in the package com.foo.
# - joins the string together
2f92e0f4 the hashcode of the object.
The name of array classes look a little different, which is explained well in the Javadocs for Class.getName(). For instance, [Ljava.lang.String means:
[ - an single-dimensional array (as opposed to [[ or [[[ etc.)
L - the array contains a class or interface
java.lang.String - the type of objects in the array
Customizing the Output
To print something different when you call System.out.println(myObject), you must override the toString() method in your own class. Here's a simple example:
public class Person {
private String name;
// constructors and other methods omitted
#Override
public String toString() {
return name;
}
}
Now if we print a Person, we see their name rather than com.foo.Person#12345678.
Bear in mind that toString() is just one way for an object to be converted to a string. Typically this output should fully describe your object in a clear and concise manner. A better toString() for our Person class might be:
#Override
public String toString() {
return getClass().getSimpleName() + "[name=" + name + "]";
}
Which would print, e.g., Person[name=Henry]. That's a really useful piece of data for debugging/testing.
If you want to focus on just one aspect of your object or include a lot of jazzy formatting, you might be better to define a separate method instead, e.g. String toElegantReport() {...}.
Auto-generating the Output
Many IDEs offer support for auto-generating a toString() method, based on the fields in the class. See docs for Eclipse and IntelliJ, for example.
Several popular Java libraries offer this feature as well. Some examples include:
ToStringBuilder from Apache Commons Lang
MoreObjects.ToStringHelper from Google Guava
#ToString annotation from Project Lombok
Printing groups of objects
So you've created a nice toString() for your class. What happens if that class is placed into an array or a collection?
Arrays
If you have an array of objects, you can call Arrays.toString() to produce a simple representation of the contents of the array. For instance, consider this array of Person objects:
Person[] people = { new Person("Fred"), new Person("Mike") };
System.out.println(Arrays.toString(people));
// Prints: [Fred, Mike]
Note: this is a call to a static method called toString() in the Arrays class, which is different to what we've been discussing above.
If you have a multi-dimensional array, you can use Arrays.deepToString() to achieve the same sort of output.
Collections
Most collections will produce a pretty output based on calling .toString() on every element.
List<Person> people = new ArrayList<>();
people.add(new Person("Alice"));
people.add(new Person("Bob"));
System.out.println(people);
// Prints [Alice, Bob]
So you just need to ensure your list elements define a nice toString() as discussed above.
I think apache provides a better util class which provides a function to get the string
ReflectionToStringBuilder.toString(object)
Every class in Java has the toString() method in it by default, which is called if you pass some object of that class to System.out.println(). By default, this call returns the className#hashcode of that object.
{
SomeClass sc = new SomeClass();
// Class # followed by hashcode of object in Hexadecimal
System.out.println(sc);
}
You can override the toString method of a class to get different output. See this example
class A {
String s = "I am just a object";
#Override
public String toString()
{
return s;
}
}
class B {
public static void main(String args[])
{
A obj = new A();
System.out.println(obj);
}
}
In Eclipse,
Go to your class,
Right click->source->Generate toString();
It will override the toString() method and will print the object of that class.
I prefer to use a utility function which uses GSON to de-serialize the Java object into JSON string.
/**
* This class provides basic/common functionalities to be applied on Java Objects.
*/
public final class ObjectUtils {
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
private ObjectUtils() {
throw new UnsupportedOperationException("Instantiation of this class is not permitted in case you are using reflection.");
}
/**
* This method is responsible for de-serializing the Java Object into Json String.
*
* #param object Object to be de-serialized.
* #return String
*/
public static String deserializeObjectToString(final Object object) {
return GSON.toJson(object);
}
}
In intellij you can auto generate toString method by pressing alt+inset and then selecting toString() here is an out put for a test class:
public class test {
int a;
char b;
String c;
Test2 test2;
#Override
public String toString() {
return "test{" +
"a=" + a +
", b=" + b +
", c='" + c + '\'' +
", test2=" + test2 +
'}';
}
}
As you can see, it generates a String by concatenating, several attributes of the class, for primitives it will print their values and for reference types it will use their class type (in this case to string method of Test2).
By default, every Object in Java has the toString() method which outputs the ObjectType#HashCode.
If you want more meaningfull information then you need to override the toString() method in your class.
public class Person {
private String name;
// constructor and getter/setter omitted
// overridding toString() to print name
public String toString(){
return name;
}
}
Now when you print the person object using System.out.prtinln(personObj); it will print the name of the person instead of the classname and hashcode.
In your second case when you are trying to print the array, it prints [Lcom.foo.Person;#28a418fc the Array type and it's hashcode.
If you want to print the person names, there are many ways.
You could write your own function that iterates each person and prints
void printPersonArray(Person[] persons){
for(Person person: persons){
System.out.println(person);
}
}
You could print it using Arrays.toString(). This seems the simplest to me.
System.out.println(Arrays.toString(persons));
System.out.println(Arrays.deepToString(persons)); // for nested arrays
You could print it the java 8 way (using streams and method reference).
Arrays.stream(persons).forEach(System.out::println);
There might be other ways as well. Hope this helps. :)
If you Directly print any object of Person It will the ClassName#HashCode to the Code.
in your case com.foo.Person#2f92e0f4 is getting printed . Where Person is a class to which object belongs and 2f92e0f4 is hashCode of the Object.
public class Person {
private String name;
public Person(String name){
this.name = name;
}
// getter/setter omitted
#override
public String toString(){
return name;
}
}
Now if you try to Use the object of Person then it will print the name
Class Test
{
public static void main(String... args){
Person obj = new Person("YourName");
System.out.println(obj.toString());
}
}
If you look at the Object class (Parent class of all classes in Java) the toString() method implementation is
public String toString() {
return getClass().getName() + "#" + Integer.toHexString(hashCode());
}
whenever you print any object in Java then toString() will be call. Now it's up to you if you override toString() then your method will call other Object class method call.
Using Lombok #Data annotation on class will provide getter, setter, toString and hashcode. Using Lombok is better as it handles boilerplate code.
For a "deep" toString() there is an alternative to the JSON based answers (Jackson, GSON, etc.): ReflectionToStringBuilder from the Apache Commons Lang 3 library, with RecursiveToStringStyle or MultilineRecursiveToStringStyle. Code example:
System.out.println("My object: " +
ReflectionToStringBuilder.toString(theObject, new RecursiveToStringStyle()));
Output examples:
// RecursiveToStringStyle
Person#7f54[name=Stephen,age=29,smoker=false,job=Job#43cd2[title=Manager]]
// MultilineRecursiveToStringStyle
Person#7f54[
name=Stephen,
age=29,
smoker=false,
job=Job#43cd2[
title=Manager
]
]
I managed to get this done using Jackson in Spring 5. Depending on the object it might not work in all cases.
import com.fasterxml.jackson.databind.ObjectMapper;
ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.writeValueAsString(yourObject));
the output would look like
{
"id" : 1,
"fieldOne" : "string"
}
Here are more examples using Jackson
If you use GSON instead It might look like
Gson gson = new Gson();
System.out.println(gson.toJson(yourObject));
If you are using project Lombok you could use the #ToString annotation and generate a standard toString() method without adding boilerplate.
import lombok.ToString;
#ToString
public class LoginDto {
private String user;
private String pass;
}
...
System.out.println(loginDto.toString());
// LoginDto(user=x#xxx.x, pass=xxxxx)

How to find a specific field from a genetic List object in wpf

I am trying to create a generic class for finding duplicate values for different class.
I am casting each list to an object and passing it as parameter like this
List<StudentModel1> a = new List<StudentModel1>();
List<StudentModel2> b = new List<StudentModel2>();
List<StudentModel3> c = new List<StudentModel3>();
List<object> obj = a.Cast<object>().ToList();//like this
public bool Duplicate(List<object> obj, string Fieldname, string Name)
{
if (obj.Any(x => x.Fieldname.Contains(Name))) { return true; } else { return false; }
}/// something like this;
here i am passing fieldname propery ,string name and object for finding duplicate and return a bool.How to access field name in linq.
please help how to acheive this.
thanks.
If I understand your question correctly, you have 3 different versions of a class: StudentModel1, StudentModel2 and StudentModel3 and you want to be able to compare lists of them. You are casting them to Object so that you can pass any any version of that class to your Duplicate method.
Assuming the above is correct, what you need is inheritance. If that's not something your familiar with you should definitely read up on it.
Consider the following:
class StudentModelBase
{
public string SomeProperty { get; set; }
}
class StudentModel1 : StudentModelBase
{
}
class StudentModel2 : StudentModelBase
{
}
class StudentModel3 : StudentModelBase
{
}
If your Duplicate method should be able to handle any of the "StudentModel" classes, then that means the information needed to tell if there are duplicates must be common to all three versions of that class. The properties and fields which store that information should be moved into StudentModelBase. Then, instead of casting to Object you should cast to StudentModelBase.
Your cast becomes:
List<StudentModelBase> base = a.Cast<StudentModelBase>().ToList();
And your Duplicate method becomes something like the following:
public bool Duplicate(List<StudentModelBase> obj, string Name)
Notice I left out Fieldname because if this is implemented correctly you shouldn't need to pass that as a parameter. You should just be able to access the need members of StudentModelBase like any normal variable, since they should be the same for all versions of "StudentModel".

Save any new/changed entity respectively in Entity Framework

first, I was only trying to update a modified model. Lets say, we talk about "Article" as a model.
The following method is implemented in a class called "Articles":
public static void SaveArticle(Article article)
{
if (article.Id == 0)
{
webEntities.Articles.Add(article);
}
else
{
webEntities.Articles.Attach(article);
webEntities.Entry(article).State = EntityState.Modified;
}
webEntities.SaveChanges();
}
So whenever I want to save an modified article in a controller action, I just have to call "Articles.SaveArticle(myArticle);", which works as expected.
So far so good but this means I would need to implement this redundantly for every model/entity.
Then I thought about something like a template-pattern. I.e. a class called "Entity" where "Article" inherits from "Entity".
Furthermore, a class called "Entities" contains a static method like this:
public static void SaveEntity(Entity entity)
{
if (Entity.Id == 0) // <-- Problem 1
{
webEntities.Entities.Add(entity); // <-- Problem 2
}
else
{
webEntities.Entities.Attach(entity); // <-- Problem 3
webEntities.Entry(entity).State = EntityState.Modified; // <-- Problem 4
}
webEntities.SaveChanges();
}
So I would not need to implement it redundantly but I don't know how to solve the problems mentioned in the code above.
Do I think too complicated or what would be a best practice to my problem?
Thanks in advance!
Kind regards
Use generics.
public static void Save<T>(T entity)
where T : class
{
webEntities.Set<T>().AddOrUpdate(entity);
webEntities.SaveChanges();
}
AddOrUpdate is an extension method in System.Data.Entity.Migrations.

How to share an Array between all Classes in an application?

I want to share an Array which all classes can "get" and "change" data inside that array. Something like a Global array or Multi Access array. How this is possible with ActionScript 3.0 ?
There are a couple of ways to solve this. One is to use a global variable (as suggested in unkiwii's answer) but that's not a very common approach in ActionScript. More common approaches are:
Class variable (static variable)
Create a class called DataModel or similar, and define an array variable on that class as static:
public class DataModel {
public static var myArray : Array = [];
}
You can then access this from any part in your application using DataModel.myArray. This is rarely a great solution because (like global variables) there is no way for one part of your application to know when the content of the array is modified by another part of the application. This means that even if your data entry GUI adds an object to the array, your data list GUI will not know to show the new data, unless you implement some other way of telling it to redraw.
Singleton wrapping array
Another way is to create a class called ArraySingleton, which wraps the actual array and provides access methods to it, and an instance of which can be accessed using the very common singleton pattern of keeping the single instance in a static variable.
public class ArraySingleton {
private var _array : Array;
private static var _instance : ArraySingleton;
public static function get INSTANCE() : ArraySingleton {
if (!_instance)
_instance = new ArraySingleton();
return _instance;
}
public function ArraySingleton() {
_array = [];
}
public function get length() : uint {
return _array.length;
}
public function push(object : *) : void {
_array.push(object);
}
public function itemAt(idx : uint) : * {
return _array[idx];
}
}
This class wraps an array, and a single instance can be accessed through ArraySingleton.INSTANCE. This means that you can do:
var arr : ArraySingleton = ArraySingleton.INSTANCE;
arr.push('a');
arr.push('b');
trace(arr.length); // traces '2'
trace(arr.itemAt(0)); // trace 'a'
The great benefit of this is that you can dispatch events when items are added or when the array is modified in any other way, so that all parts of your application can be notified of such changes. You will likely want to expand on the example above by implementing more array-like interfaces, like pop(), shift(), unshift() et c.
Dependency injection
A common pattern in large-scale application development is called dependency injection, and basically means that by marking your class in some way (AS3 meta-data is often used) you can signal that the framework should "inject" a reference into that class. That way, the class doesn't need to care about where the reference is coming from, but the framework will make sure that it's there.
A very popular DI framework for AS3 is Robotlegs.
NOTE: I discourage the use of Global Variables!
But here is your answer
You can go to your default package and create a file with the same name of your global variable and set the global variable public:
//File: GlobalArray.as
package {
public var GlobalArray:Array = [];
}
And that's it! You have a global variable. You can acces from your code (from anywhere) like this:
function DoSomething() {
GlobalArray.push(new Object());
GlobalArray.pop();
for each (var object:* in GlobalArray) {
//...
}
}
As this question was linked recently I would add something also. I was proposed to use singleton ages ago and resigned on using it as soon as I realized how namespaces and references work and that having everything based on global variables is bad idea.
Aternative
Note this is just a showcase and I do not advice you to use such approach all over the place.
As for alternative to singleton you could have:
public class Global {
public static const myArray:Alternative = new Alternative();
}
and use it almost like singleton:
var ga:Alternative = Global.myArray;
ga.e.addEventListener(GDataEvent.NEW_DATA, onNewData);
ga.e.addEventListener(GDataEvent.DATA_CHANGE, onDataChange);
ga.push(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "ten");
trace(ga[5]); // 5
And your Alternative.as would look similar to singleton one:
package adnss.projects.tchqs
{
import flash.utils.Proxy;
import flash.utils.flash_proxy;
public class Alternative extends Proxy
{
private var _data:Array = [];
private var _events:AltEventDisp = new AltEventDisp();
private var _dispatching:Boolean = false;
public var blockCircularChange:Boolean = true;
public function Alternative() {}
override flash_proxy function getProperty(id:*):* {var i:int = id;
return _data[i += (i < 0) ? _data.length : 0];
//return _data[id]; //version without anal item access - var i:int could be removed.
}
override flash_proxy function setProperty(id:*, value:*):void { var i:int = id;
if (_dispatching) { throw new Error("You cannot set data while DATA_CHANGE event is dipatching"); return; }
i += (i < 0) ? _data.length : 0;
if (i > 9 ) { throw new Error ("You can override only first 10 items without using push."); return;}
_data[i] = value;
if (blockCircularChange) _dispatching = true;
_events.dispatchEvent(new GDataEvent(GDataEvent.DATA_CHANGE, i));
_dispatching = false;
}
public function push(...rest) {
var c:uint = -_data.length + _data.push.apply(null, rest);
_events.dispatchEvent(new GDataEvent(GDataEvent.NEW_DATA, _data.length - c, c));
}
public function get length():uint { return _data.length; }
public function get e():AltEventDisp { return _events; }
public function toString():String { return String(_data); }
}
}
import flash.events.EventDispatcher;
/**
* Dispatched after data at existing index is replaced.
* #eventType adnss.projects.tchqs.GDataEvent
*/
[Event(name = "dataChange", type = "adnss.projects.tchqs.GDataEvent")]
/**
* Dispatched after new data is pushed intwo array.
* #eventType adnss.projects.tchqs.GDataEvent
*/
[Event(name = "newData", type = "adnss.projects.tchqs.GDataEvent")]
class AltEventDisp extends EventDispatcher { }
The only difference form Singleton is that you can actually have multiple instances of this class so you can reuse it like this:
public class Global {
public static const myArray:Alternative = new Alternative();
public static const myArray2:Alternative = new Alternative();
}
to have two separated global arrays or even us it as instance variable at the same time.
Note
Wrapping array like this an using methods like myArray.get(x) or myArray[x] is obviously slower than accessing raw array (see all additional steps we are taking at setProperty).
public static const staticArray:Array = [1,2,3];
On the other hand you don't have any control over this. And the content of the array can be changed form anywhere.
Caution about events
I would have to add that if you want to involve events in accessing data that way you should be careful. As with every sharp blade it's easy to get cut.
For example consider what happens when you do this this:
private function onDataChange(e:GDataEvent):void {
trace("dataChanged at:", e.id, "to", Global.myArray[e.id]);
Global.myArray[e.id]++;
trace("new onDataChange is called before function exits");
}
The function is called after data in array was changed and inside that function you changing the data again. Basically it's similar to doing something like this:
function f(x:Number) {
f(++x);
}
You can see what happens in such case if you toggle myArray.blockCircularChange. Sometimes you would intentionally want to have such recursion but it is likely that you will do it "by accident". Unfortunately flash will suddenly stop such events dispatching without even telling you why and this could be confusing.
Download full example here
Why using global variables is bad in most scenarios?
I guess there is many info about that all over the internet but to be complete I will add simple example.
Consider you have in your app some view where you display some text, or graphics, or most likely game content. Say you have chess game. Mayby you have separated logic and graphics in two classes but you want both to operate on the same pawns. So you create your Global.pawns variable and use that in both Grahpics and Logic class.
Everything is randy-dandy and works flawlessly. Now You come with the great idea - add option for user to play two matches at once or even more. All you have to do is to create another instance of your match... right?
Well you are doomed at this point because, every single instance of your class will use the same Global.pawns array. You not only have this variable global but also you have limited yourself to use only single instance of each class that use this variable :/
So before you use any global variables, just think twice if the thing you want to store in it is really global and universal across your entire app.

Silverlight 4 - Type.GetType() not working across assemblies

Basically the problem is that I'd like to invoke a method in an unreferenced assembly, but can't seem to find the right call for instantiating the class. I've tried things like a simple Type t = Type.GetType("MyApp.Helper") which returns null, and Assembly.LoadFrom("MyApp.Helper") which throws a security exception.
In the example below, two projects/assemblies (Helper.dll and Menu.dll) are compiled separately into a common 'libs' folder, but do not reference each other. Main.dll references both, and the references are set to 'Copy local' in VS. So when the app runs, the Main.xap should contain all three assemblies and they should be loaded into the same application domain. Or so goes my understanding. Is this an impossible quest? I see lots of comments regarding plug-ins but so far I haven't seen examples for this specific design. For example, I suppose I could do something like Jeff Prosise describes here, but I'd rather have everything in one package.
Here's a sketch of my code:
In one project/assembly, I have a worker class:
namespace MyApp.Helper {
public class Helper {
public void ShowHelp() {
Console.Write("Help!");
}
}
}
In another project/assembly, I have a menu class which tries to invoke the helper:
namespace MyApp.Menu {
public class Selector {
public void InvokeSelection(string className, string functionName) {
// fails: t will be null
Type t = Type.GetType(className);
// fails: t will be null
t = Type.GetType(string.Format("{0}.{1}, {0}, Version=1.0.0.0, Culture=\"\", PublicTokenKey=null", "MyApp.Helper", "Helper"));
// however, this works (reference to main assembly?)
t = Type.GetType(string.Format("{0}.{1}, {0}, Version=1.0.0.0, Culture=\"\", PublicTokenKey=null", "MyApp.Main", "Worker"));
// and, I'd like to do something like the following
// t.InvokeMember(functionName, ...);
}
}
}
Finally, I have the main app assembly:
namespace MyApp.Main {
public class Main {
public static void Main() {
MyApp.Menu.Selector sel = new Menu.Selector();
sel.InvokeSelection("MyApp.Help.Helper", "ShowHelp"); // fails
sel.InvokeSelection("MyApp.Main.Main", "Worker"); // works in some cases
}
public void Worker() {
Console.Write("Work!");
}
}
}
Thanks for any ideas!
-Chris.
You need to get the Assembly object, then call its GetType method.
However, I don't see why you're using Reflection at all.
You can call the method normally from your main project.
First, you should note that since it's SL, you can't invoke private/protected/internal members.
Second, try this:
public void InvokeSelection(string className, string functionName) {
var asm = Assembly.Load("MyApp.Helper, Version=1.0.0.0, Culture=\"\", PublicTokenKey=null"); // double check this is correct!
Type t = asm .GetType(className);
// and, I'd like to do something like the following
// t.InvokeMember(functionName, ...);
}

Resources