Pages

Friday, 5 October 2012

Way to handle frames in selenium. Handling new window in selenium.


Below command is used to handle frame application.

driver.switchTo().frame(driver.findElement(By.tagName("iframe"))); .

child window is opened from parent window.
to access child window elements we need to select child window then only we can click/access child window elements.

    final String thisWindow = driver.getWindowHandle();
        String newWindow = new WebDriverWait(driver, 10)
                .until(new ExpectedCondition<String>() {
                    @Override
                    public String apply(WebDriver d) {
                        Set<String> handles = d.getWindowHandles();
                        handles.remove(thisWindow);
                        return handles.size() > 0 ? handles.iterator().next()
                                : null;
                    }
                });
        //newWindow.wait(10000);
        driver.switchTo().window(newWindow);

Spring Https Custom Redirect Strategy

Here when user sends a request in https protocol to apache webserver. apache webserver will made a call to application server with http protocol.
apache server retries the http response from application server.
Apache server sends the http response to the user.

Here user is sending https request and getting the response in http.
Below steps are to handle the above issue. 
Defining LoginUrlAuthenticationEntryPoint: -
==============================

Here when user tries to acess any url customAuthenticationFilter will be called.
For log out also customLogoutFilter will be called.
 <http auto-config="false" entry-point-ref="LoginUrlAuthenticationEntryPoint" use-expressions="true">
        <intercept-url pattern="/login.html" filters="none" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
        <intercept-url pattern="/update/*" access="hasAnyRole('ROLE_ADMIN','ROLE_DEV')" />
        <custom-filter position="FORM_LOGIN_FILTER" ref="customAuthenticationFilter" />
        <custom-filter position="LOGOUT_FILTER" ref="customLogoutFilter"/>          
  </http>
====================================================

Defining customAuthenticationFilter:-
============================
 Depending upon the result of CustomAuthenticationFilter filter successHandler or failureHandler will be called.
<beans:bean id="customAuthenticationFilter" class="com.CustomAuthenticationFilter" >
         <beans:property name="authenticationManager" ref="authenticationManager" />
        <beans:property name="authenticationFailureHandler" ref="failureHandler" />
        <beans:property name="authenticationSuccessHandler" ref="successHandler" />      
</beans:bean>
 ======================================================

Defining successHandler: -
====================
In successHandler we are defining customRedirect startegy.

<beans:bean id="successHandler" class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler">
        <beans:property name="redirectStrategy" ref="customRedirect"/>
         <beans:property name="defaultTargetUrl" value="/" />
</beans:bean>
==============================

Creating the HttpsRedirectStratergy class:

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.web.DefaultRedirectStrategy;
import org.springframework.security.web.RedirectStrategy;

public class HttpsRedirectStratergy extends DefaultRedirectStrategy{

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Override
    public void sendRedirect(HttpServletRequest request, HttpServletResponse response,
            String redirectUrl) throws IOException {
        StringBuffer httpsRedirectUrl = new StringBuffer("https");
        logger.debug("In send Redirect");
        logger.debug("redirect URL : " + redirectUrl);
        if(!redirectUrl.startsWith("https") && redirectUrl.startsWith("http"))
        {
            httpsRedirectUrl.append(redirectUrl.substring(4,redirectUrl.length()));
        }
        logger.debug("Redirected after authentication to : " + httpsRedirectUrl.toString());
        //response.sendRedirect(redirectUrl);
        super.sendRedirect(request, response, httpsRedirectUrl.toString());
        logger.debug("End send Redirect");
       
    }
}
=====================

Thanks,
Sudharsana M

Thursday, 26 April 2012

Access or get HttpRequest or HttpSession out side container (in POJO)


We can use RequestContext class to share the Http Request object across all POJO classes. This is how it can be done.

Step 1: Setting the Http Request into RequestContext.
This is done by calling the static method newInstance(HttpServletRequest)on RequestContext  from your servlet.

Step 2: Fetch the Http Request from anywhere
Simply give a method call “getCurrentInstance().getRequest()” on RequestContext  in any POJO class. 
This will return Http Request object for current thread.

import java.io.Serializable;

import javax.servlet.http.HttpServletRequest;

public class RequestContext implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    private static ThreadLocal<RequestContext> instance = new ThreadLocal<RequestContext>() {

        protected RequestContext initialValue() {
            return null;
        }

    };

    private HttpServletRequest request;

    private RequestContext(HttpServletRequest request) {
        this.request = request;
    }
    /**
     * This method will returns RequestContext object
     * @return
     */
    public static RequestContext getCurrentInstance() {
        return instance.get();
    }

    public static RequestContext newInstance(HttpServletRequest request) {
        RequestContext context = new RequestContext(request);
        instance.set(context);
        return context;
    }
    /**
     * This method will removes RequestContext object from ThreadLocal for current thread
     *
     */
    public void release() {
        instance.remove();
    }

    public HttpServletRequest getRequest() {
        return request;
    }

}

JPA Entity creation using reverse engineering


Entity creation using reverse engineering
Steps:
Step 1: Create new JPA project.

 Step 2: Give the project name.
 Step 3: Click on Next 


 Step 4: Click on next.
Step 5: Select Platform type as “Generic 2.0”.
If you have created Userlibraries with JPA required then select type “User Library. And select user library.
 If you have not created user library for JPA libraries then select “Disable library configuration”.I am going with “Disable library configuration”. Click on Finish after selecting “Disable library configuration” type.
  Step 6: Project will be opened in JPA perspective.
Step 7: Right click on the project and select Entity from Tables.
After selecting entities from tables below option will come.

   Step 8: Click on add connections.
 Step 9:  Select Oracle connection profile and enter OracleConnection as the name in text field.
 Step 10: Click on Next.
 Step 11: Enter db details.
Step 12: After entering db details then click on Test Connection.

You will get Ping Succeeded success message. If not check db details and enter correctly. 
Step 13: Click on Finish and close. 
Step 14:Right click on the project and Select Entities from Tables.

 Step 15:Select Connection as Oracle from dropdown box. After selecting it all the tables will be    displayed.
Step 16: Select Tables for which you want to create entities.
Step 17: After selecting tables click on Next.
 
Step 18:Click on Next. 
Step 19 :
Enter key generator for primarykeys.
Select entity access for field or for property
Slect fetch type.
Select Collection Properties type what you reuired.
 Step 20: Enter the package name.
 Step 21: Click on Next.
 Step 22: Default class names will be created if you want to change Select the Table and change the name.
Step 23:
Click on Finish.
Entities will be created under specified package.
Thanks,
Sudharsan M