December 5, 2010

Selenium Script Explanation

Selenium Script Explanation

Here we can see the code for simple login and logout functionality with comments and the explanation of code.

import com.thoughtworks.selenium.DefaultSelenium; //Selenium package

//Class name[class name should equal to program name]
public class Login {

 //creating a method
            public static void Login_results() throws Exception
                        /*Defining the selenium method if you want to declare Default selenium outside the method you need to declare as public
public static DefaultSelenium selenium=new DefaultSelenium("localhost",4444,"*iehta","http://");
4444 ---  serverport
Iehta ----- browserstart command(we can use *firefox also)
http:// ---browser URL(Here we can give full URL of site) */    
                     DefaultSelenium selenium=new DefaultSelenium("localhost", 4444, "*iehta", "http://");
//this command will start the selenium                
//to open the URL
//Maximizes the window
//to read user name
                        selenium.type("name=login[username]", "");
//to read password       
            selenium.type("name=login[password]", "mourya");
//click login button           "send2");
//waits until the page loads
//click the link sign out
              "link=Sign Out");
// print message
                        System.out.println("Login and Log out success");

            public static void main(String[] args) throws Exception{