Skip to content

Conversation

TikhomirovSergey
Copy link
Contributor

@TikhomirovSergey TikhomirovSergey commented Apr 5, 2015

  • Ability to use not only Webdriver to populate page object fields via method
public static <T> T initElements(SearchContext context, Class<T> pageClassToProxy).

This change was done in order to simplify page object instantiation and population when there is the necessity to implement something that describes widget or repeatable element set nested in a root element. So if there is a class with constructors like follows:

public PageObject(Search context)

//or

public PageObject(WebElement rootElement)

and there is no necessity to implement customized decorating engines then it is possible to populete its WebElement-fields this way (only one step):

PageObject po = PageFactory.initElements(driver, PageObject,class) //then PageObject.class has 
//the default constructor or constructor with a SearchContext- or WebDriver- parameter


//or 
PageObject po = PageFactory.initElements(element, PageObject,class) //then PageObject.class has 
//the default constructor or constructor with a SearchContext- or WebElement- parameter

//and so on... 
  • the additional checking of field modifiers before it will be populated.

Field that is going to be filled should not be static and final. I found these issues some time ago. I tried to implement page object with final fields that weren't annotated by @findby's. Also I found that it tries to set proxy-WebElement as a value of a static WebElement-field. In both cases I caught IllegalModifierException. Actually I think that both issues are minor but it would be good if whey were fixed.


This change is Reviewable

@andreastt andreastt added Z-awaiting review Archived: use GitHub review assignments C-java Java Bindings labels Apr 5, 2015
@opensource21
Copy link

Hi I made some similar change at selophane. How ever the last days I found a use case where it's necessary to have the webdriver. So I changed it back. The background is, that if you create something like a button it would be great if there was a wait till a click on the button is finished. Therefor you need a WebDriver.
You can see my current solution in a pull request

@TikhomirovSergey
Copy link
Contributor Author

@opensource21
There is nothing prevents to use Webdriver if this change will be accepted. Actually it is possible to use WebElement for the subelements populating now:

//excessive actions
WidgetObject wo = new WidgetObject(/*refToElement*/);
DefaultFieldDecorator dfd = new DefaultFieldDecorator(
new DefaultElementLocatorFactory(refToElement)); 
//excessive actions are finished
PageFactory.initElements(dfd, wo);

This should be usefull for cases and apps/services like... Google (just for example). The search form is the page by itself and the widget inside some element at Drive or GMail page. So if it is not necessary to made up and implement users customized decorator then this widget object will be created and populated in one action.

@opensource21
Copy link

@TikhomirovSergey Yes you are right! Sorry for the trouble and thanks for the hints.

@ddavison
Copy link
Member

there appears to be an issue with this travis build.

no ruby changes were detected, yet still failed. @lukeis? any idea

@titusfortner
Copy link
Member

I'm not sure why it ran in the first place, but it looks like I might have changed something in the specs. I'll take a look.

@TikhomirovSergey TikhomirovSergey force-pushed the page_factory_enhancements branch from 4a314b8 to c8a8e13 Compare September 30, 2015 16:24
- Ability to use not only Webdriver to populate page object fields via
method
public static <T> T initElements(SearchContext context, Class<T>
pageClassToProxy).
This change was done in order to simplify page object instantiation and
population when there is the necessity to implement something that describes
widget or repeatable element set nested in a root element.
- the additional checking of field modifiers before it will
be populated. Field that is going to be filled should not  be static and
final. I found these issues some time ago. I tried to implement page
object with final fields that weren't annotated by @findby's. Also I found
that it tries to set proxy-WebElement as a value of a static WebElement-field.
Actually I think that both issues are minor but it would be good if whey
were fixed.
@TikhomirovSergey TikhomirovSergey force-pushed the page_factory_enhancements branch from fb266f6 to d079a12 Compare October 5, 2015 14:22
…page_factory_enhancements

Conflicts:
	java/client/src/org/openqa/selenium/support/pagefactory/internal/LocatingElementHandler.java
Also:
- PageFactory: some redundant code was removed
@TikhomirovSergey
Copy link
Contributor Author

TikhomirovSergey commented Oct 26, 2017

@mykola-mokhnach @shs96c
It seems that PR has been actualized.
Could you rewiew this PR?

Also I don't know why tests are not passed

if (!parameterClazz.isAssignableFrom(context.getClass()))
continue;
c.setAccessible(true);
return (T) c.newInstance(context);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pageClassToProxy.cast should address unchecked class cast warning

} catch (NoSuchMethodException e) {
return pageClassToProxy.newInstance();
Constructor<?>[] availableConstructors = pageClassToProxy.getDeclaredConstructors();
for (Constructor<?> c: availableConstructors){

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a space is missing


Class<?>[] parameterTypes = c.getParameterTypes();
if (parameterTypes.length != 1)
continue;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is a good practice to always enclose condition block into curly brackets, even if there is only one operator

public Object invoke(Object object, Method method, Object[] objects) throws Throwable {

if ("toString".equals(method.getName())) {
return "Proxy element for: " + locator.toString();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

String.format might be a better choice here

- some code style issues were fixed out
@TikhomirovSergey
Copy link
Contributor Author

TikhomirovSergey commented Nov 1, 2017

@mykola-mokhnach @shs96c I made the update. Please look at this once again.

Field[] fields = proxyIn.getDeclaredFields();
for (Field field : fields) {
int modifiers = field.getModifiers();
if (Modifier.isFinal(modifiers) || Modifier.isStatic(modifiers))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

curly brackets would be good here as well

continue;
}
c.setAccessible(true);
return (T) c.newInstance(context);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did pageClassToProxy.cast fail?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. I can improve it later.

- some code style issues were fixed out
@CLAassistant
Copy link

CLAassistant commented Nov 23, 2019

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@diemol
Copy link
Member

diemol commented May 19, 2020

Apologies for the poor handling of this PR.

In general, we are discouraging the use of PageFactory, but in any case, lots of people use, so making these types of changes would break backward compatibility. Closing this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-needs decision TLC needs to discuss and agree C-java Java Bindings Z-awaiting review Archived: use GitHub review assignments

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants