Sunday, July 24, 2011

Selenium RC Setup On Eclipse

Selenium RC Setup On Eclipse

I. Following .jar files are required to setup the java and seleniumRC

1. JDK 1.5.19.jar

2. junit-4.7.jar

3. guava-r09.jar

4. selenium-java-client-driver.jar

5. selenium-server.jar
( Note:- guava-r09.jar - This jar file is required to test google / gmail )

II. Command to run Selenium Server in command prompt ( looks as below )
java -jar selenium-server.jar




( Note: command to run selenium server in command line using browser firefox )

java -jar selenium-server.jar -forcedBrowserMode firefox
III. Check for the java class path in the command line using the below command
   echo %CLASSPATH%
Find the below snapshot which shows the classpaths
IV. To Compile java program in command line
Javac testprogram.java



V. To Run java program in command line

Java testprogram.java
 Selenium Junit frame work setup in Eclipse
Setup jar files path in the Eclipse as below :

Select Project in Navigator window -> select properties -> select Java Build Path -> select Libraries
tab -> Select AddExternal JARs button.

Java Build Path, libraries should display following  jars

VII. To run Java program in eclipse using Junit Test :

Select the respective Program from Navigator window -> Run As -> Junit Test

( as shown below )




Xpath tips and tricks
–Strong attributes for use in xpath conditions
•Accessibility tags Type="button" Label="OK" etc…
•Text text()="Search‟
•Xpath that finds the button named search //*[@Type="button"]//*[text()="Search"]
. using regular expr
Ex:
//*[contains(@id,'Name of form')]//*[contains(@Property,'Value')]
//*[contains(text(),'Back')]



Working with selenium eclipse : sample program
Open eclipse

create a java project 


create class myfirstTest.java



Create a class for flight function

import FlightFunctions.*;

import junit.framework.*;

public class MyFirstTest extends Flight {
 Flight  ObjF = new Flight();
    public void setUp() throws Exception {
    ObjF.setUp();
                 
    }

    public void testGoogleSignUpErrors() throws Exception {
    ObjF.Login("test", "test");
    ObjF.IsPresent(XPath.roundTrip);
    //System.out.println("Login successfull"+ ObjF.IsPresent(XPath.tripType));
    if(ObjF.IsPresent(XPath.oneWay))
    {
    System.out.println("Login successfull");
    }
    else
    {
    System.out.println("Login Not successfull");
    }
    /* boolean CheckedPro=ObjF.IsRadioChecked(XPath.roundTrip);
    if (CheckedPro)
    {
    System.out.println("Verifying roundtrip"+CheckedPro);
    //verifyTrue(CheckedPro);
    }
    else
    {
        Assert.fail("Default Trip Type is not select");
    }*/
    FindFlight("oneway", "2", "Acapulco", "March", "23", "Paris", "March", "24", "economy","Blue Skies Airlines");
   
    ObjF.stop();
         
    }
 

    public static Test suite() {

                    return new TestSuite(MyFirstTest.class);

    }

    public static void main(String args[]) {

                    junit.textui.TestRunner.run(suite());

    }

}

create class flight.java


package FlightFunctions;

import com.thoughtworks.selenium.*;


public class Flight extends SeleneseTestCase {

public void setUp() throws Exception {

         setUp("http://newtours.demoaut.com", "*chrome");
         selenium.windowMaximize();
}
 public void Login(String Username , String Password)
 {
selenium.open("http://newtours.demoaut.com");//instantiate and start the browsers
      selenium.type(XPath.username, Username);
      System.out.println("Login Function");
     selenium.type(XPath.password, Password);
     selenium.click(XPath.login);
     selenium.waitForFrameToLoad(XPath.formFlight,"10000");
     selenium.click(XPath.oneWay);
 }
 public void stop(){
selenium.stop();
 }
public boolean IsPresent(String locator) throws Exception{

return selenium.isElementPresent(locator);

}
public boolean IsRadioChecked(String locator) throws Exception{

return selenium.isChecked(locator);

}
public void FindFlight(String tripType, String passenger, String fromPort, String fromMonth, String fromDay, String toPort, String toMonth, String toDay, String servClass, String airlines)
{
System.out.println("Verifying FindFlight function");
selenium.click(XPath.oneWay);
if (tripType.equals("oneway"))
{

selenium.click(XPath.oneWay);
System.out.println("Verifying FindFlight function"+tripType);
}
selenium.select(XPath.passengers, passenger);
selenium.select(XPath.departFrom, fromPort);
selenium.select(XPath.fromMonth, fromMonth);
selenium.select(XPath.fromDay, fromDay);
selenium.select(XPath.arrivalIn, toPort);
selenium.select(XPath.toMonth, toMonth);
selenium.select(XPath.toDay, toDay);
if (servClass.equals("economy"))
{
selenium.click(XPath.economy);
}
else if (servClass.equals("bussiness"))
{
selenium.click(XPath.bussiness);
}
else
{
selenium.click(XPath.first);
}
selenium.select(XPath.airlines, airlines);
selenium.click(XPath.continueButton);

}

}




create xpath.java

package FlightFunctions;


public class XPath {
public static final String  username="//input[@name='userName']";
public static final String  password="//input[@name='password']";
public static final String  login="//input[@name='login']";
public static final String roundTrip ="//input[@type='radio' and @value='roundtrip' ]";
public static final String oneWay ="//input[@type='radio' and @value='oneway']";
public static final String formFlight="//form[@name='findflight']";
public static final String passengers="//select[@name='passCount']";
public static final String departFrom="//select[@name='fromPort']";
public static final String fromMonth="//select[@name='fromMonth']";
public static final String fromDay="//select[@name='fromDay']";
public static final String arrivalIn="//select[@name='toPort']";
public static final String toMonth="//select[@name='toMonth']";
public static final String toDay="//select[@name='toDay']";
public static final String economy="//input[@type='radio' and @value='Economy']";
public static final String bussiness="//input[@type='radio' and @value='Bussiness']";
public static final String first="//input[@type='radio' and @value='First']";
public static final String continueButton="//input[@name='findFlights']";
public static final String airlines="//select[@name='airline']";
}
 run in junit 


Working with Testng

go to help and Install new software 
go to the website 
download testng.jar
Update junit to testng
 create flight.java


package FlightFunctions;
import java.util.*;

import org.openqa.selenium.server.RemoteControlConfiguration;
import org.openqa.selenium.server.SeleniumServer;
import org.testng.ITestContext;
import org.testng.Reporter;
import org.testng.annotations.*;

import com.thoughtworks.selenium.*;


public class Flight extends SeleneseTestCase {
    SeleniumServer server;
     HttpCommandProcessor proc;
    //StartandTearDown testobj = new StartandTearDown();

    @Test(description="Launches the Flight site")
    public void launchSite(){
      selenium.open("http://www.newtours.demoaut.com/");
      selenium.waitForPageToLoad("30000");
      assertEquals(selenium.getTitle(), "Welcome: Mercury Tours");
      Reporter.log(selenium.getTitle());
  
    }
    @Parameters ({"username","password"})
    @Test(description="Enters Login to Flight")
      public void login(String username ,String password) {
      selenium.type(XPath.username, username);
      selenium.type(XPath.password, password);
      selenium.click(XPath.login);
      selenium.waitForPageToLoad("30000");
      Reporter.log("test"+(selenium.isTextPresent("Flight Details")));
      assertTrue(selenium.isTextPresent("Flight Details"));
      String Items[]= selenium.getSelectOptions(XPath.departFrom);
      String ActualItems[]={"Acapulco","Frankfurt","London","New York","Paris","Portland","San Francisco","Seattle","Sydney","Zurich"};
      assertTrue(Arrays.equals(Items, ActualItems));
     for (int i =0; i <Items.length; i++ )
     {
         Reporter.log("Select items "+ Items[i] );
         System.out.println("Console " +Items[i]);
     }
     String ButtonType = selenium.getAttribute(XPath.continueButton+"@type");
     Reporter.log("Button type "+ selenium.getAttribute(XPath.continueButton+"@name"));
     if(ButtonType.equals("image"))
     {
         assertTrue(true);
     }
     else
     {
         assertTrue(false);
     }
        
    }
  
     @BeforeSuite(alwaysRun = true)
        // @Parameters({"selenium.host","selenium.port","selenium.browser","selenium.url"})
         public void setupBeforeSuite(ITestContext context) {
           String seleniumHost = context.getCurrentXmlTest().getParameter("selenium.host");
           String seleniumPort = context.getCurrentXmlTest().getParameter("selenium.port");
           String seleniumBrowser = context.getCurrentXmlTest().getParameter("selenium.browser");
           String seleniumUrl = context.getCurrentXmlTest().getParameter("selenium.url");
         /*  String seleniumHost ="localhost";
           String seleniumPort = "3737";
           String seleniumBrowser = "*firefox";
           String seleniumUrl = "http://demo.opensourcecms.com/wordpress/";*/
           RemoteControlConfiguration rcc = new RemoteControlConfiguration();
           rcc.setSingleWindow(true);
           rcc.setPort(Integer.parseInt(seleniumPort));
         
           try {
             server = new SeleniumServer(false, rcc);
             server.boot();
           } catch (Exception e) {
             throw new IllegalStateException("Can't start selenium server", e);
           }
         
               proc = new HttpCommandProcessor(seleniumHost, Integer.parseInt(seleniumPort),
               seleniumBrowser, seleniumUrl);
           selenium = new DefaultSelenium(proc);
           selenium.start();
         }
         @AfterSuite(alwaysRun = true)
         public void setupAfterSuite() {
           selenium.stop();
           server.stop();
         }
  

}
In xpath.java


package FlightFunctions;


public class XPath {
    public static final String  username="//input[@name='userName']";
    public static final String  password="//input[@name='password']";
    public static final String  login="//input[@name='login']";
    public static final String roundTrip ="//input[@type='radio' and @value='roundtrip' ]";
    public static final String oneWay ="//input[@type='radio' and @value='oneway']";
    public static final String formFlight="//form[@name='findflight']";
    public static final String passengers="//select[@name='passCount']";
    public static final String departFrom="//select[@name='fromPort']";
    public static final String fromMonth="//select[@name='fromMonth']";
    public static final String fromDay="//select[@name='fromDay']";
    public static final String arrivalIn="//select[@name='toPort']";
    public static final String toMonth="//select[@name='toMonth']";
    public static final String toDay="//select[@name='toDay']";
    public static final String economy="//input[@type='radio' and @value='Economy']";
    public static final String bussiness="//input[@type='radio' and @value='Bussiness']";
    public static final String first="//input[@type='radio' and @value='First']";
    public static final String continueButton="//input[@name='findFlights']";
    public static final String airlines="//select[@name='airline']";
}



Testng xml



<suite name="Demo Flight.info - Flight Test" verbose="10">
  <parameter name="selenium.host" value="localhost" />
  <parameter name="selenium.port" value="3737" />
  <parameter name="selenium.browser" value="*firefox" />
    <parameter name="selenium.url" value="http://www.newtours.demoaut.com/" />
  <parameter name="username" value="test" />
  <parameter name="password" value="test" />

  <test name="Write new post" preserve-order="true">
    <classes>
 
      <class name="FlightFunctions.Flight"/>
 
    </classes>
  </test>
</suite>


Advantage of Testng

when running in testng server will start and stop automatically


Result can be understandable more flexibly