How to use initElements method of org.openqa.selenium.support.PageFactory class

Best Selenium code snippet using org.openqa.selenium.support.PageFactory.initElements

Run Selenium automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

copy
1package com.orasi.core.interfaces.impl.internal;
2
3import java.lang.reflect.Constructor;
4import java.lang.reflect.InvocationTargetException;
5
6import org.openqa.selenium.WebDriver;
7import org.openqa.selenium.support.PageFactory;
8import org.openqa.selenium.support.pagefactory.FieldDecorator;
9
10import com.orasi.exception.automation.PageInitialization;
11import com.orasi.utils.OrasiDriver;
12import com.orasi.utils.TestReporter;
13
14/**
15 * Element factory for wrapped elements. Similar to {@link org.openqa.selenium.support.PageFactory}
16 */
17public class ElementFactory {
18
19    /**
20     *  See {@link org.openqa.selenium.support.PageFactory#initElements(org.openqa.selenium.WebDriver driver, Class)}
21     */
22    public static <T> T initElements(OrasiDriver driver, Class<T> pageClassToProxy) {
23		TestReporter.logTrace("Entering ElementFactory#initElements");
24		TestReporter.logTrace("Creating Page Object");
25        T page = instantiatePage(driver, pageClassToProxy);
26		TestReporter.logTrace("Successfully created Page Object");
27        final OrasiDriver driverRef = driver;
28		TestReporter.logTrace("Initialize Page Elements");
29        PageFactory.initElements(new ElementDecorator(new CustomElementLocatorFactory(driverRef)), page);
30		TestReporter.logTrace("Successfully created Page Elements");
31		TestReporter.logTrace("Exiting ElementFactory#initElements");
32        return page;
33    }
34
35    /**
36     *  See {@link org.openqa.selenium.support.PageFactory#initElements(org.openqa.selenium.support.pagefactory.FieldDecorator, Object)}
37     */
38    public static void initElements(OrasiDriver driver, Object page) {
39		TestReporter.logTrace("Entering ElementFactory#initElements");
40        final OrasiDriver driverRef = driver;
41		TestReporter.logTrace("Initialize Page Elements");
42        PageFactory.initElements(new ElementDecorator(new CustomElementLocatorFactory(driverRef), driverRef), page);
43		TestReporter.logTrace("Successfully created Page Elements");
44		TestReporter.logTrace("Exiting ElementFactory#initElements");
45    }
46
47    /**
48     * see {@link org.openqa.selenium.support.PageFactory#initElements(org.openqa.selenium.support.pagefactory.ElementLocatorFactory, Object)}
49     */
50    public static void initElements(CustomElementLocatorFactory factory, Object page) {
51		TestReporter.logTrace("Entering ElementFactory#initElements");
52        final CustomElementLocatorFactory factoryRef = factory;
53		TestReporter.logTrace("Initialize Page Elements");
54        PageFactory.initElements(new ElementDecorator(factoryRef), page);
55		TestReporter.logTrace("Successfully created Page Elements");
56		TestReporter.logTrace("Exiting ElementFactory#initElements");
57    }
58
59    /**
60     * see {@link org.openqa.selenium.support.PageFactory#initElements(org.openqa.selenium.support.pagefactory.ElementLocatorFactory, Object)}
61     */
62    public static void initElements(FieldDecorator decorator, Object page) {
63		TestReporter.logTrace("Entering ElementFactory#initElements");
64		TestReporter.logTrace("Initialize Page Elements");
65        PageFactory.initElements(decorator, page);
66		TestReporter.logTrace("Successfully created Page Elements");
67		TestReporter.logTrace("Exiting ElementFactory#initElements");
68    }
69
70    /**
71     * Copy of {@link org.openqa.selenium.support.PageFactory#instantiatePage(org.openqa.selenium.WebDriver, Class)}
72     */
73    private static <T> T instantiatePage(WebDriver driver, Class<T> pageClassToProxy) {
74		TestReporter.logTrace("Entering ElementFactory#instantiatePage");
75        try {
76            try {
77            	TestReporter.logTrace("Create Constructor of Page object");
78                Constructor<T> constructor = pageClassToProxy.getConstructor(WebDriver.class);
79            	TestReporter.logTrace("Successfully created Constructor");
80
81            	TestReporter.logTrace("Create new instance of Page object");
82            	T instance = constructor.newInstance(driver);
83            	TestReporter.logTrace("Successfully created new Page instance");
84        		TestReporter.logTrace("Exiting ElementFactory#instantiatePage");
85                return instance;
86            } catch (NoSuchMethodException e) {
87        	try{            		
88        		TestReporter.logTrace("Entering ElementFactory#instantiatePage");
89        		return pageClassToProxy.newInstance();
90        	}catch(InstantiationException ie){ 
91        		throw new PageInitialization("Failed to create instance of: " + pageClassToProxy.getName(), driver);
92        	}
93            }
94        } catch (InstantiationException e) {
95            throw new PageInitialization("Failed to create instance of: " + pageClassToProxy.getName(), driver);
96        } catch (IllegalAccessException e) {
97            throw new RuntimeException(e);
98        } catch (InvocationTargetException e) {
99            throw new RuntimeException(e);
100        }
101    }
102}
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Most used method in PageFactory

Trigger initElements code on LambdaTest Cloud Grid

Execute automation tests with initElements on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie