need to convert PageManager into Generic - java

In Selenium I have created a page object manager, so when a new page is created then the class needs to be added in Page Object Manager class, so that object of the page will be created only if calling Page is null else already created object will be returned.
Example:
public class PageObjectManager {
private DatePicker datePicker;
public DatePicker getDatePicker(){
return (datePicker == null) ? datePicker = new DatePicker() : datePicker;
}
}
But if there are more Pages then we have to create private fields and getter for each. So how to overcome with this.
Can I use Generic here like
public class PageObjectManager<T> {
private Class<T> pageClass;
public PageObjectManager(Class<T> pageClass) {
this.pageClass= pageClass;
}
}
Thanks

Related

can't find the way to bind a vaadin combobox

my problem is that I can't find the way to bind a combobox, my goal is to autocomplete a combobox after clicking on a table (Grid):
The combobox data is taken from the BD Acception table, but the table data is taken from the WordAcception class
These are the errors it shows me:
[enter image description here][1]
[enter image description here][2]
WORDACCEPTION.java
public class WordAcception implements Serializable, Cloneable {
private String idacception = "";
public String getIdacception() {
return idacception;
}
public void setIdacception(String idacception) {
this.idacception = idacception;
}
}
VISTA.java
private ComboBox<Acception> WordAcceptioncombo = new ComboBox<>("idacception");
WordAcceptioncombo.setItemLabelGenerator(Acception::getIdAcception);
WordAcceptioncombo.setItems(AcceptionPersistence.getInstance().findAllIdAcception());
add(WordAcceptioncombo);
wordacceptionGrid.asSingleSelect().addValueChangeListener(event ->
formword.setWordAcception(wordacceptionGrid.asSingleSelect().getValue()));
WORDACCEPTIONFORM.java
binder.bind(WordAcceptioncombo, Acception::getIdAcception,
Acception::setIdAcception);
public void setWordAcception(WordAcception wordAcception) {
if(wordAcception != null) {
System.out.println("setWordAcception= "+wordAcception.getIdacception());
WordAcceptioncombo.setValue(wordAcception.getIdacception());
}
binder.setBean(wordAcception);
if (binder.getBean() == null) {
setVisible(false);
} else {
setVisible(true);
idacception.focus();
}
}
Aception.java
public class Acception implements Serializable, Cloneable {
private String idAcception = "";
public String getIdAcception() {
return idAcception;
}
public void setIdAcception(String idAcception) {
this.idAcception = idAcception;
}
}
/////////////UPDATE///////////
The combobox is completed thanks to the Acception class, for that reason I had to create an Acception instance in the WordAcception class in order to obtain that instance of the class (this has hidden the problems from me, but the combobox still does not autofill)
ACCEPTION.JAVA
public class Acception implements Serializable, Cloneable {
private Clase clase;
public Clase getClase() {
return clase;
}
public void setClase(Clase clase) {
this.clase = clase;
}
WORDACCEPTIONFORM.JAVA
binder.bind(WordAcceptioncombo, WordAcception::getAcception,
WordAcception::setAcception);
BUT I DON'T NOTICE ANY CHANGE, THE COMBOBOX STILL NOT FILLED
You usually get that kind of is not applicable for the arguments error from .bind when you are using a getter and setter for a value that is a different type than your field. In your case WordAcception.idAcception is type String but your ComboBox is set to take in items of type Acception.
If you want a ComboBox that gives you the option of selecting which particular id belongs to this specific WordAcception and for changing that id (without changing the WordAcception instance), the type of the ComboBox should be String. If you want a ComboBox that selects a specific Acception for you, you need to use a getter and setter for a field of that type.
Unrelated to the error, I'm not quite certain if there is supposed to be some connection between Acception and WordAcception, but based on the examples currently there is none. Did you mean for one of them to extend the other?

wicket models is not updated

I have a Link in the constructor of the class DetailsPanel and I want it to save a change in the form of the class DetailsPanel when I click on it and go to the next page.
When I use for example if(propertyModel.getSomething() != null) { // change something } in the constructor Details() the change is made.
When I do the same in the constructor DetailsPanel() that is if(info.getSomething() != null) { // change something } the change is not made because the model is not updated when I click the link. It just goes to the next page and when I go back the change does not remain..
I have tested the code and the value of "something" passes in both cases.
I am just trying to understand how the wicket model works here...
public class DetailsPanel extends Panel {
private static final class Details extends BootstrapForm<InfoModel> {
public Details(String id, final CompoundPropertyModel<InfoModel> propertyModel) {
// form with various components..
}
}
public DetailsPanel(String id, final InfoModel info) {
// my link is here
}
}
public class InfoModel implements IClusterable {
// private fields with getters and setters eg.
private int something;
}

Unchecked cast warning with abstract method providing specific return value

I'm writing selenium tests for an app that has very standard pages that can easily be modeled by a generic structure as the base for the pages, with only a few base types (mostly list pages containing a list of records, and edit pages where one can edit one record). To model this I have these two classes:
public abstract class AbstractListPage<E extends EditPage> extends AbstractSelfOpeningPage implements ListPage {
// Provides the specific object for the edit page when it's opened
protected abstract E editPageHook();
public E getEditPage() {
return editPageHook();
}
public E openEditPage(String key, boolean search) {
//Do page opening stuff like clicking buttons
// Return new object for the page that has been opened
return getEditPage();
}
}
// Implementation class
public class DossiersListPage extends AbstractListPage<DossierPage> {
#Override
protected DossierPage<DossiersListPage> editPageHook() {
return new DossierPage<>(driver, this);
}
}
// Usage in test, this shows an unchecked cast warning
DossierPage<DossiersListPage> dossierPage = new DossiersListPage(driver).openPage().openEditPage("3905");
I would like to know if there is a good way to fix this, and what am I missing? I'm not having any issues currently, but the warning all over my test code is making me feel a bit iffy.
The reason for the generics here is so I can model elements on my page that return the page they belong to in a fluent way:
public abstract class AbstractPageElement<P extends Page> implements PageElement<P> {
#Override
public P click() throws TimeoutException {
// Do click
return page;
}
}
// DossierPage
public class DossierPage<L extends ListPage> extends AbstractEditPage<L> {
public OrderDate<DossierPage<L>> orderDate = new OrderDate<>(driver, this);
public OrderType<DossierPage<L>> orderType = new OrderType<>(driver, this);
public Status<DossierPage<L>> status = new Status<>(driver, this);
}
// Test
dossierPage.orderDate.click()
.orderType.click()
.status.click();
I could reverse-engineer the problem. The DossierPage must look something like this:
public class DossierPage<E extends AbstractListPage> extends EditPage {
//...
}
So now we're getting the problem. You can solve it by specifying more type arguments:
public class DossiersListPage extends
AbstractListPage<DossierPage<DossiersListPage>> { // this is the tricky part
#Override
protected DossierPage<DossiersListPage> editPageHook() {
return new DossierPage<>();
}
//...
}
Just add the following line above the line giving the warning:
#SuppressWarnings("unchecked")

Using GWT-Editors without UiBinder

I wanted to improve the code of my gwt-project by using editors to bind the POJOs, which I used to manually parse to the widgets back and forth. However I find the documentation confusing, mostly because it references ui binder, another feature I haven't figured out yet.
Does it make sense to use editors without ui binder? My ParentDTO contains a number of childDTOs. The following snippet shows how I am trying to nest some ChildEditors extended by TextArea into my ParentEditor (tried to strip it down to the essentials):
public class MyEditorPage {
// editors
class ParentDTOEditor implements Editor<ParentDTO> {
Integer dataBaseId;
List<ChildDTOEditor> childs;
public void attach(RootPanel rootPanel) {
for (ChildDTOEditor widget : childs) {
rootPanel.add(widget);
}
}
}
class ChildDTOEditor implements Editor<ChildDTO> extends TextArea {}
// driver
interface Driver extends SimpleBeanEditorDriver<ParentDTO, ParentDTOEditor> {}
Driver driver = GWT.create(Driver.class);
// load set widgets to the root panel
public void loadPage(RootPanel rootPanel) {
// get pojos from server
myService.getSuff(...
...
public void onSuccess(ParentDTO result) {
ParentDTOEditor editor = new ParentDTOEditor();
driver.initialize(editor);
driver.edit(result);
editor.attach(rootPanel);
}
}
// save
public void save() {
ParentDTO dto = driver.flush();
... // call myService.saveStuff(dto,...
}
}
Do I even need separate editors or just a parent editor of type ListEditor directly holds the child dtos?
Does it make sense to use editors without ui binder?
Yes, the features are independent and each can be used without the other.
Do I even need separate editors or just a parent editor of type ListEditor directly holds the child dtos?
You could create a ListEditor implementation direcly, but GWT can do that for you if you tell it how to create and destroy an Editor<ChildDTO> by extending EditorSource<ChildDTOEditor> and using ListEditor.of(new YourChildDTOEditorSourceImpl())
There's a decent example here but I'll call out a minimum implementation here.
public class FooEditor extends Composite implements Editor<Foo> {
// Implement one of uibinder+fields, fields, methods, or LeafValueEditor.set/getValue()
public FooEditor() {
initWidget(/* root widget or call to uiBinder.createAndBindUi(this) */)
}
}
public class FooListEditor extends Composite implements IsEditor<ListEditor<Foo, FooEditor>> {
private class FooEditorSource extends EditorSource<FooEditor> {
#Override
public FooEditor create(int index) {
FooEditor subEditor = new FooEditor();
// any additional per-item config can go here, e.g wiring up delete handler
listPanel.insert(subEditor, index);
return subEditor;
}
#Override
public void dispose(FooEditor subEditor) {
subEditor.removeFromParent();
}
#Override
public void setIndex(FooEditor subEditor, int index) {
listPanel.insert(subEditor, index);
}
}
// FlowPanel or anything else you want to use to hold the sub-editors.
// Instantiated explicitly or through uibinder.
FlowPanel listPanel = new FlowPanel();
// Let GWT handle the ListEdiotr implementation
ListEditor<Foo, FooEditor> editor = ListEditor.of(new FooEditorSource());
public FooListEditor() {
initWidget(listPanel /* or uiBinder.createAndBindUi(this) */);
}
#Override
public ListEditor<Foo, FooEditor> asEditor() {
return editor;
}
}
Now you can create an editor driver and use it as you would a normal editor, but pass it a list instead.
interface Driver extends SimpleBeanEditorDriver<List<Foo>, ListEditor<Foo, FooEditor>> {}
Driver driver = GWT.create(Driver.class);
FooListEditor fooListEditor = new FooListEditor();
/* snip */
driver.initialize(FooListEditor);
driver.edit(someListOfFoo);

Set the list of acceptable values in a GWT ValueListBox based on an EnumSet?

Given that I have this entity as part of an editor chain:
public class Commission implements Serializable
{
private EnumSet<CommissionType> commissionTypes;
private CommissionType type; // must exist in commissionTypes
private String value;
public Commission()
{
}
}
and this editor for it:
public class CommissionEditor extends Composite implements Editor<Commission>
{
private static CommissionEditorUiBinder uiBinder = GWT.create(CommissionEditorUiBinder.class);
interface CommissionEditorUiBinder extends UiBinder<Widget, CommissionEditor>
{
}
#UiField(provided = true)
ValueListBox<CommissionType> type = new ValueListBox<CommissionType>(new AbstractRenderer<CommissionType>()
{
#Override
public String render(CommissionType object)
{
return object == null ? "" : object.toString();
}
});
#UiField
TextBox value;
public CommissionEditor()
{
type.setAcceptableValues(Arrays.asList(CommissionType.values()));
initWidget(uiBinder.createAndBindUi(this));
}
}
At the moment the ValueListBox renders all possible options for CommissionType, like this:
The EnumSet could contain between 1 and 4 of the possible options, depending on the particular entity. Is there a way to make the ValueListBox only render the options in the EnumSet and then save the value in commissionType?
Bear in mind that I want to set the value of commissionType as well.
There are two ways to solve it:
1.) If you have a direct access to the CommissionEditor then create a setter in it call it when you edit the entity:
public void setAcceptableValues(List<CommissionType> values) {
type.setAcceptableValues(values);
}
And call it like this when you call driver.edit(entity);:
commissionEditor.setAcceptableValues(commission.getCommissionTypes());
2.) Instead of extending the Editor interface you can extend the ValueAwareEditor and in the setValue() method call setAcceptableValues with the corresponding values.
Approach 2 is probably the cleaner approach.

Categories

Resources