junit test case for login form

8,407 views
Skip to first unread message

Ravi

unread,
Mar 14, 2011, 8:24:02 AM3/14/11
to Selenium Users
here is the following code for login page. i m new to junit env. can u
help me out to write a junit test case for the below login form using
springs.. i m not able to understand wat to pass as an argument
during the invocation of validate method.


package net.roseindia.web;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import org.springframework.validation.ValidationUtils;

import net.roseindia.web.*;




public class LoginValidator implements Validator {
@Override
public boolean supports(Class clazz)
{
return Login.class.isAssignableFrom(clazz);
}



public void validate(Object obj, Errors errors)

{
Login login = (Login) obj;
if (login.getUsername() == null ||
login.getUsername().length() == 0) {
errors.rejectValue("username", "error.empty.field",
"Please Enter User Name");
}
else if (!login.getUsername().equals("admin")) {
errors.rejectValue("username", "unknown.user", "Unknown
User");
}
if (login.getPassword() == null ||
login.getPassword().length() == 0) {
errors.rejectValue("password", "error.empty.field",
"Please Enter Password");
}
else if (!login.getPassword().equals("admin")) {
errors.rejectValue("password", "wrong.password", "Wrong
Password");
}
}
}


BillR

unread,
Mar 14, 2011, 1:00:10 PM3/14/11
to Selenium Users
It looks like you are checking login values yourself, rather than
testing whether they work on a web page.
> }- Hide quoted text -
>
> - Show quoted text -

Ravi kumar

unread,
Mar 15, 2011, 1:03:02 AM3/15/11
to seleniu...@googlegroups.com, BillR
then can u give me a code for login page and appropriate test class....coz i m new to this enev...


--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en.




--

RAVI KUMAR....

 9963989909

Nisum Technologies India  pvt Limited.




.

image002.gif

BillR

unread,
Mar 15, 2011, 4:34:06 PM3/15/11
to Selenium Users
It depends on the login page. Here's a simple one:

selenium.open("/somedir/");
selenium.type("username", "someacct");
selenium.type("pword", "somepwd");
selenium.click("submit");
selenium.waitForPageToLoad("10000");
> *
>
> RAVI KUMAR....
>  9963989909
>
> Nisum Technologies India  pvt Limited.
>
> .
> *
>
>  image002.gif
> 43KViewDownload- Hide quoted text -

Mohammed Sikander

unread,
Mar 15, 2011, 11:59:10 PM3/15/11
to seleniu...@googlegroups.com
Hi here is the JUnit Test Case for login page, create a junit test case in Eclipse just copy and paste the foolowing test case and execute. 

package actItTimeSmokeTest;

import static org.junit.Assert.assertEquals;
import java.io.IOException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.server.RemoteControlConfiguration;
import org.openqa.selenium.server.SeleniumServer;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;

public class JUnitTestCase_01{
SeleniumServer seleniumserver;
private Selenium selenium;
@Before
public void setUp() throws Exception{
RemoteControlConfiguration rc = new RemoteControlConfiguration();
rc.setSingleWindow(true);
seleniumserver = new SeleniumServer(rc);
selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "http://demo.actitime.com");
seleniumserver.start();
selenium.start();
}

@Test
public void verifyTasksTab() throws IOException, InterruptedException{
selenium.open("/login.do");
selenium.setSpeed("5000");
selenium.type("username", "user");
selenium.type("pwd", "user");
selenium.click("loginButton");
selenium.waitForPageToLoad("30000");
}
@After
public void tearDown() throws Exception {
selenium.stop();
seleniumserver.stop();
}
}

If you need step by step instruction refer these links - create junit test case and run junit test case

carol

unread,
Mar 29, 2011, 1:04:21 AM3/29/11
to Selenium Users
Hi , I have a simple servlet file.I want to build a Junit test case.
When i create a junit test file using ecplise i will get some
methods.But i dont know what exactly code should be there in that.
This is my servlet file
package com.glan.login;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class LoginSuccess extends HttpServlet {

protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/
html;charset=UTF-8");
String uname=request.getParameter("user");
String upass=request.getParameter("pass");
if(uname.equals("admin")&& upass.equals("admin")){

request.getRequestDispatcher("loginSuccess.jsp").forward(request,
response);
}
else{
request.setAttribute("errormessage", "Sorry !!!
Invalid Credentials.");

request.getRequestDispatcher("index.jsp").forward(request, response);
}
}

protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
System.out.println("**************** Servlet called.");
processRequest(request, response);
}

public String getServletInfo() {
return "Short description";
}

}

This is test file genetated.

package com.glan.login;


import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;

/**
* @author
*
*/
public class LoginSuccessTest {

private LoginSuccess success;
/**
* @throws java.lang.Exception
*/
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}

/**
* @throws java.lang.Exception
*/
@AfterClass
public static void tearDownAfterClass() throws Exception {
}

/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
success=new LoginSuccess();
}

/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
}

}

please help me with this test case file
Reply all
Reply to author
Forward
0 new messages