com.mycompany.selenium.factory.Page.java Source code

Java tutorial

Introduction

Here is the source code for com.mycompany.selenium.factory.Page.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.selenium.factory;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.apache.commons.lang3.reflect.MethodUtils;

/**
 *
 * @author sidochenko
 */
public abstract class Page {

    public abstract String getTitle();

    public void setValue(String field, String value) throws Exception {
        this.getClass().getDeclaredField(field).set(this, value);
    }

    public void takeAction(String action, Object... param) throws Throwable {
        action = action.replaceAll(" ", "_");
        try {
            MethodUtils.invokeMethod(this, action, param);
        } catch (NoSuchMethodException e) {
            StringBuilder sb = new StringBuilder();

            sb.append("There is no \"").append(action).append("\" action ").append("in ").append(this.getTitle())
                    .append(" page object").append("\n");
            sb.append("Possible actions are:").append("\n");
            Class tClass = this.getClass();
            Method[] methods = tClass.getDeclaredMethods();
            for (int i = 0; i < methods.length; i++) {
                sb.append("\t\"").append(this.getTitle()).append("\"->\"").append(methods[i].getName())
                        .append("\" with ").append(methods[i].getGenericParameterTypes().length)
                        .append(" input parameters").append("\n");
            }
            throw new NoSuchMethodException(sb.toString());
        } catch (InvocationTargetException ex) {
            throw ex.getCause();
        }
    }
}