Best SeLion code snippet using com.paypal.selion.platform.html.PageValidationException
Source:BasicPageImpl.java
...26import com.paypal.selion.internal.utils.RegexUtils;27import com.paypal.selion.platform.grid.Grid;28import com.paypal.selion.platform.html.AbstractElement;29import com.paypal.selion.platform.html.Container;30import com.paypal.selion.platform.html.PageValidationException;31import com.paypal.selion.platform.html.ParentTraits;32import com.paypal.selion.platform.html.UndefinedElementException;33import com.paypal.selion.platform.html.support.HtmlElementUtils;34/**35 * A Base class from which all page classes should be derived.36 * 37 * It contains the code to initialize pages, load values to the "ObjectMap", and interact in various ways with the38 * page(s).39 */40public abstract class BasicPageImpl extends AbstractPage implements ParentTraits {41 private static final String NESTED_CONTAINER_ERR_MSG = "No support for defining a Container within a Container.";42 /** The SELION_GUI_COMPONENTS. */43 // TODO This should probably be determined via reflection instead44 private static final String SELION_COMPONENTS = "com.paypal.selion.platform.html";45 /**46 * Instantiates a new base page impl.47 */48 protected BasicPageImpl() {49 super();50 }51 /*52 * (non-Javadoc)53 * 54 * @see com.paypal.selion.platform.html.WebPage#initPage(java.lang.String, java.lang.String)55 */56 /**57 * Load object map. This method takes a HashMap<String, String> and uses it to populate the objectMap This is58 * intended to allow for the use of programmatically generated locators in addition to the excel file format IDs and59 * Locators60 * 61 * @param sourceMap62 * the source map63 */64 // TODO: So what happens if the sourceMap object is null or is empty ? Do we still assume that the page has been65 // initialized ?66 // Come back to this logic.67 protected void loadObjectMap(HashMap<String, String> sourceMap) {68 if (sourceMap == null) {69 return;70 }71 if (sourceMap.containsKey("pageTitle")) {72 pageTitle = sourceMap.get("pageTitle");73 }74 if (objectMap == null) {75 objectMap = new HashMap<String, String>();76 }77 objectMap.putAll(sourceMap);78 pageInitialized = true;79 }80 /*81 * (non-Javadoc)82 * 83 * Return the actual page title for this page84 */85 public String getActualPageTitle() {86 return Grid.driver().getTitle();87 }88 /*89 * (non-Javadoc)90 * 91 * @see com.paypal.selion.platform.html.WebPage#getExpectedPageTitle()92 */93 public String getExpectedPageTitle() {94 return getPage().pageTitle;95 }96 /**97 * Validates whether the actual current page title equals to expected page title.98 * 99 * @return true if the actual page title is equal to any of the titles represented by this page object otherwise100 * returns false101 */102 public boolean hasExpectedPageTitle() {103 List<String> pageTitles = Arrays.asList(getPage().pageTitle.split("\\|"));104 for (String title : pageTitles) {105 if (RegexUtils.wildCardMatch(getPage().getActualPageTitle(), title)) {106 return true;107 }108 }109 return false;110 }111 /**112 * Require extended class to provide this implementation.113 * 114 * @return the page115 */116 public abstract BasicPageImpl getPage();117 /**118 * This method is responsible for automatically initializing the PayPal HTML Objects with their corresponding key119 * values obtained from the hash map.120 * 121 * @param whichClass122 * Indicate for what object you want the initialization to be done for e.g., the GUI Page class name such123 * as PayPalLoginPage, PayPalAddBankPage, etc124 * @param objectMap125 * Pass the {@link Map} that contains the key, value pairs read from the yaml file or excel sheet126 */127 public void initializeHtmlObjects(Object whichClass, Map<String, String> objectMap) {128 ArrayList<Field> fields = new ArrayList<Field>();129 Class<?> incomingClass = whichClass.getClass();130 // If the class type is a container then adding the fields related to the container.131 if (incomingClass.getSuperclass().equals(Container.class)) {132 fields.addAll(Arrays.asList(incomingClass.getDeclaredFields()));133 } else {134 // This definitely a page object and so proceeding with loading all the fields135 Class<?> tempIncomingClass = incomingClass;136 do {137 fields.addAll(Arrays.asList(tempIncomingClass.getDeclaredFields()));138 tempIncomingClass = tempIncomingClass.getSuperclass();139 } while (tempIncomingClass != null);140 }141 String errorDesc = " while initializaing HTML fields from the object map. Root cause:";142 try {143 for (Field field : fields) {144 // proceed further only if the data member and the key in the .xls file match with each other145 // below condition checks for this one to one mapping presence146 if (objectMap.containsKey(field.getName())) {147 field.setAccessible(true);148 if (isContainerWithinContainer(field, incomingClass)) {149 throw new UnsupportedOperationException(NESTED_CONTAINER_ERR_MSG);150 }151 String packageName = field.getType().getPackage().getName();152 // We need to perform initialization only for the paypal html objects and153 // We need to skip for any other objects such String, custom Classes etc.154 if (packageName.equals(SELION_COMPONENTS)) {155 // Checking if the superClass/Parent is also a container. If so its not allowed.156 Class<?> dataMemberClass = Class.forName(field.getType().getName());157 Class<?> parameterTypes[] = new Class[3];158 parameterTypes[0] = String.class;159 parameterTypes[1] = String.class;160 parameterTypes[2] = ParentTraits.class;161 Constructor<?> constructor = dataMemberClass.getDeclaredConstructor(parameterTypes);162 String locatorValue = objectMap.get(field.getName());163 if (locatorValue == null) {164 continue;165 }166 Object[] constructorArgList = new Object[3];167 constructorArgList[0] = new String(locatorValue);168 constructorArgList[1] = new String(field.getName());169 constructorArgList[2] = whichClass;170 Object retobj = constructor.newInstance(constructorArgList);171 field.set(whichClass, retobj);172 } else if (field.getType().getSuperclass().equals(Container.class)) {173 Class<?> dataMemberClass = Class.forName(field.getType().getName());174 Class<?> parameterTypes[] = new Class[3];175 parameterTypes[0] = field.getType().getDeclaringClass();176 parameterTypes[1] = String.class;177 parameterTypes[2] = String.class;178 Constructor<?> constructor = dataMemberClass.getDeclaredConstructor(parameterTypes);179 String locatorValue = objectMap.get(field.getName());180 if (locatorValue == null) {181 continue;182 }183 Object[] constructorArgList = new Object[3];184 constructorArgList[0] = whichClass;185 constructorArgList[1] = new String(locatorValue);186 constructorArgList[2] = new String(field.getName());187 Object retobj = constructor.newInstance(constructorArgList);188 // Associating a parent type here itself! Kind of an hack189 Container createdContainer = (Container) retobj;190 createdContainer.setParentForContainer((ParentTraits) whichClass);191 field.set(whichClass, retobj);192 // Calling it recursively to load the elements in the container193 initializeHtmlObjects(retobj, objectContainerMap.get(field.getName()));194 }195 }196 }197 } catch (ClassNotFoundException exception) {198 throw new RuntimeException("Class not found" + errorDesc + exception, exception);199 } catch (IllegalArgumentException exception) {200 throw new RuntimeException("An illegal argument was encountered" + errorDesc + exception, exception);201 } catch (InstantiationException exception) {202 throw new RuntimeException("Could not instantantiate object" + errorDesc + exception, exception);203 } catch (IllegalAccessException exception) {204 throw new RuntimeException("Could not access data member" + errorDesc + exception, exception);205 } catch (InvocationTargetException exception) {206 throw new RuntimeException("Invocation error occured" + errorDesc + exception, exception);207 } catch (SecurityException exception) {208 throw new RuntimeException("Security error occured" + errorDesc + exception, exception);209 } catch (NoSuchMethodException exception) {210 throw new RuntimeException("Method specified not found" + errorDesc + exception, exception);211 }212 }213 private boolean isContainerWithinContainer(Field field, Class<?> incomingClass) {214 return (field.getType().getSuperclass().equals(Container.class) && incomingClass.getSuperclass().equals(215 Container.class));216 }217 /*218 * (non-Javadoc)219 * 220 * @see com.paypal.selion.platform.html.ParentType#locateChildElements(java.lang.String)221 */222 public List<WebElement> locateChildElements(String locator) {223 return HtmlElementUtils.locateElements(locator);224 }225 /*226 * (non-Javadoc)227 * 228 * @see com.paypal.selion.platform.html.ParentType#locateChildElement(java.lang.String)229 */230 public RemoteWebElement locateChildElement(String locator) {231 return HtmlElementUtils.locateElement(locator);232 }233 /*234 * (non-Javadoc)235 * 236 * @see com.paypal.selion.platform.html.ParentType#getCurrentPage()237 */238 public BasicPageImpl getCurrentPage() {239 return this;240 }241 /**242 * Perform page validations against list of elements defined in the YAML file.243 */244 public void validatePage() {245 // Call getPage to make sure the page is initialized.246 getPage();247 if (pageValidators.size() == 0) {248 if (!hasExpectedPageTitle()) {249 throw new PageValidationException(getClass().getSimpleName() + " isn't loaded in the browser, "250 + getExpectedPageTitle() + " didn't match.");251 }252 } else {253 for (String elementName : pageValidators) {254 // We can set the action we want to check for, by putting a dot at the end of the elementName.255 // Following by isPresent, isVisible or isEnabled, default behaviour is isPresent256 String action = "";257 int indexOf = elementName.indexOf(".");258 if (indexOf != -1) {259 action = elementName.substring(indexOf + 1, elementName.length());260 elementName = elementName.substring(0, indexOf);261 }262 verifyElementByAction(elementName, action);263 }264 }265 }266 /**267 * Get the AbstractElement by the key that is defined in the Yaml files.268 * 269 * @param elementName270 * The element name271 * @return instance of {@link AbstractElement}272 */273 private AbstractElement getAbstractElementThroughReflection(String elementName) {274 Field field = null;275 Class<?> currentClass = getClass();276 do {277 try {278 field = currentClass.getDeclaredField(elementName);279 field.setAccessible(true);280 return (AbstractElement) field.get(this);281 } catch (Exception e) {282 // NOSONAR283 }284 } while ((currentClass = currentClass.getSuperclass()) != null);285 throw new UndefinedElementException("Element with name " + elementName + " doesn't exist.");286 }287 /**288 * Verify if the element is availible based on a certain action289 * 290 * @param elementName291 * @param action292 */293 private void verifyElementByAction(String elementName, String action) {294 AbstractElement element = getAbstractElementThroughReflection(elementName);295 switch (action) {296 case "isPresent":297 if (!element.isElementPresent()) {298 throw new PageValidationException(getClass().getSimpleName() + " isn't loaded in the browser, "299 + elementName + " isn't present.");300 }301 break;302 case "isVisible":303 if (!element.isElementPresent() && !element.isVisible()) {304 throw new PageValidationException(getClass().getSimpleName() + " isn't loaded in the browser, "305 + elementName + " isn't visible.");306 }307 break;308 case "isEnabled":309 if (!element.isElementPresent() && !element.isEnabled()) {310 throw new PageValidationException(getClass().getSimpleName() + " isn't loaded in the browser, "311 + elementName + " isn't enabled.");312 }313 break;314 default:315 if (!HtmlElementUtils.isElementPresent(element.getLocator())) {316 throw new PageValidationException(getClass().getSimpleName() + " isn't loaded in the browser, "317 + elementName + " isn't present.");318 }319 break;320 }321 }322 /**323 * Verify's if the current page is opened in the browser. It does this based on the pageValidators.324 * 325 * @return boolean if page is opened.326 */327 public boolean isCurrentPageInBrowser() {328 try {329 validatePage();330 return true;...
PageValidationException
Using AI Code Generation
1import org.testng.annotations.Test;2import org.testng.Assert;3import com.paypal.selion.platform.html.PageValidationException;4import com.paypal.selion.platform.html.PageValidationException.ErrorType;5public class PageValidationExceptionTest {6 public void testPageValidationException() {7 PageValidationException pageValidationException = new PageValidationException(ErrorType.MISSING_ELEMENT, "Error Message");8 Assert.assertEquals(pageValidationException.getErrorType(), ErrorType.MISSING_ELEMENT);9 Assert.assertEquals(pageValidationException.getMessage(), "Error Message");10 }11}12import org.testng.annotations.Test;13import org.testng.Assert;14import com.paypal.selion.platform.html.PageValidationException;15import com.paypal.selion.platform.html.PageValidationException.ErrorType;16public class PageValidationExceptionTest {17 public void testPageValidationException() {18 PageValidationException pageValidationException = new PageValidationException(ErrorType.MISSING_ELEMENT, "Error Message");19 throw pageValidationException;20 }21}22import org.testng.annotations.Test;23import org.testng.Assert;24import com.paypal.selion.platform.html.PageValidationException;25import com.paypal.selion.platform.html.PageValidationException.ErrorType;26public class PageValidationExceptionTest {27 public void testPageValidationException() {28 PageValidationException pageValidationException = new PageValidationException(ErrorType.MISSING_ELEMENT, "Error Message");29 throw pageValidationException;30 }31}32import org.testng.annotations.Test;33import org.testng.Assert;34import com
PageValidationException
Using AI Code Generation
1public class PageValidationException extends Exception {2 private static final long serialVersionUID = 1L;3 public PageValidationException(String message) {4 super(message);5 }6 public PageValidationException(String message, Throwable cause) {7 super(message, cause);8 }9}10public class PageValidationException extends Exception {11 private static final long serialVersionUID = 1L;12 public PageValidationException(String message) {13 super(message);14 }15 public PageValidationException(String message, Throwable cause) {16 super(message, cause);17 }18}19public class PageValidationException extends Exception {20 private static final long serialVersionUID = 1L;21 public PageValidationException(String message) {22 super(message);23 }24 public PageValidationException(String message, Throwable cause) {25 super(message, cause);26 }27}28public class PageValidationException extends Exception {29 private static final long serialVersionUID = 1L;
PageValidationException
Using AI Code Generation
1PageValidationException ex = PageValidationException.getBuilder().build();2PageValidationException ex = PageValidationException.getBuilder().withMessage("Invalid Page").build();3PageValidationException ex = PageValidationException.getBuilder().withMessage("Invalid Page").withCause(new Exception()).build();4PageValidationException ex = PageValidationException.getBuilder().withCause(new Exception()).build();5PageValidationException ex = PageValidationException.getBuilder().build();6PageValidationException ex = PageValidationException.getBuilder().withMessage("Invalid Page").build();7PageValidationException ex = PageValidationException.getBuilder().withMessage("Invalid Page").withCause(new Exception()).build();8PageValidationException ex = PageValidationException.getBuilder().withCause(new Exception()).build();9PageValidationException ex = PageValidationException.getBuilder().build();10PageValidationException ex = PageValidationException.getBuilder().withMessage("Invalid Page").build();11PageValidationException ex = PageValidationException.getBuilder().withMessage("Invalid Page").withCause(new Exception()).build();12PageValidationException ex = PageValidationException.getBuilder().withCause(new Exception()).build();13PageValidationException ex = PageValidationException.getBuilder().build();14PageValidationException ex = PageValidationException.getBuilder().withMessage("Invalid Page").build();15PageValidationException ex = PageValidationException.getBuilder().withMessage("Invalid Page").withCause(new Exception()).build();16PageValidationException ex = PageValidationException.getBuilder().withCause(new Exception()).build();17PageValidationException ex = PageValidationException.getBuilder().build();18PageValidationException ex = PageValidationException.getBuilder().withMessage("Invalid Page").build();19PageValidationException ex = PageValidationException.getBuilder().withMessage("Invalid Page").withCause(new Exception()).build();20PageValidationException ex = PageValidationException.getBuilder().withCause(new Exception()).build();21PageValidationException ex = PageValidationException.getBuilder().build();22PageValidationException ex = PageValidationException.getBuilder().withMessage("Invalid Page").build();
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!