Thursday, September 6, 2012

Selenium RC Using Java - Post 1 of 10


HI All

I thought I should start a Selenium  blog posts series. In this series I will try to cover selenium related beginning thins to Advance features.  In this series I’ll do it using Java and I have listed the pre-requisites

to start up. Hope to cover whole series within 2 months. After that I’ll think of doing a series of selenium using C# and MStest.

Pre Conditions
Install Eclipse
Download TestNG latest jar  file
Download Selenium server latest jar file
Install Selenium IDE

Creating the Project in Eclips
Create New Java Project

















Create a new package for testscripts
 













Now in your source folder create a new folder name Lib

Then place your Selenium Standalone server executable jar file in to the Lib folder

And place your TEstNG executable jar file in to the same place

Eg - selenium-server-standalone-2.20.0

        testng-6.3.1

 
Go back to the project

Build path -- > Configure Build path

In Libraries tab add the External Jars (which are in the Lib folder)

 

Now go to the testscripts package and add new Class (main)

 




Now in your main class , Create code to start the Selenium Server

Selenium Sel = new DefaultSelenium("localhost", 4444, "*firefox", "http://localhost:8080/LogOn.aspx" );

Sel.start();
Start the Selenium Server by type this command   -  java –jar Selenium Standalone 2.20.0jar
 

Run the project and verify weather selenium browser start or not

Now we’ll start our test script

If you don’t have Selenium ID installed then open a firefox browser and get installed.

If it’s already installed then you can start the IDE by Tools à Selenium IDE

Once you start the IDE now type the URL in your browser

In my App it’s Http://ABC.Logon.aspx

In this scenario it’s navigating to a Login page

First I type Username , then password and Then Submit button .

All of my Actions will record in the Selenium IDE


 

















Now when you select each action you can find the command , target object and the values

 



 













Now you can create your script according to this

Following is my script for the Login

 
      Sel.open("http://ABC:8080/LogOn.aspx");

        Sel.type("id=body_UserNameTextBox","abc.admin@abc.com");

        Sel.type("id=body_PasswordTextBox", "Qwer1234");

        Sel.click("id=body_LogOnButton");

        Sel.waitForPageToLoad("30000");

 
Additionaly I have put a waitforpageload command because I need to wait until my page loads after I click logon button

Now run this file … it should  run

Now we need to make use of the TestNG which is a testing framework. We can make automation test cases now

Earlier we have mapped  TestNG jar file to the project

Now I can use @Test annotation to make my test case.

Then I can use methods instead of the main method, (no need of main method now )

Now I rename my main methof to testSucessLogin()

               

@Test

                public void testSucessLogin() {

        Sel.windowMaximize();

        Sel.open("http://localhost:8080/LogOn.aspx");

        Sel.type("id=body_UserNameTextBox","abc.admin@ABC.com");

        Sel.type("id=body_PasswordTextBox", "Qwer1234");

        Sel.click("id=body_LogOnButton");

        Sel.waitForPageToLoad("30000");

       
                }

Now I can create another method to verify the invalid login

public void testInvalidLogin() {

        Sel.windowMaximize();

        Sel.open("http://localhost:8080/LogOn.aspx");

        Sel.type("id=body_UserNameTextBox","abc.admin@ABC.com");

        Sel.type("id=body_PasswordTextBox", "Qwer12");

        Sel.click("id=body_LogOnButton");

        Sel.waitForPageToLoad("30000");

}

 
When I enter an invalid password system will give me an errormsg , so I need to catch it and verify it

Selec t and right click on error msg and then select the command verify textid

Now in Selenium IDE it display as this


 

 

You can write this assert as this - 





Assert.assertEquals(

Sel.getText("id=ErrorDetailLabel"), "Incorrect user name or password!");

 
Make sure you get the TestNG Assert when you selecting Assert from the list. (Becuase junit assert is  also there )

 





Now my Test method looks like this

@Test

               
public void testInvalidLogin() {
Selenium Sel = new DefaultSelenium("localhost", 4444, "*firefox", "http://localhost:8080/LogOn.aspx");

        Sel.start();

        Sel.windowMaximize();

        Sel.open("http://localhost:8080/LogOn.aspx");

        Sel.type("id=body_UserNameTextBox","abc.admin@ABC.com");

        Sel.type("id=body_PasswordTextBox", "Qwer12");

        Sel.click("id=body_LogOnButton");

        Sel.waitForPageToLoad("30000");

       Assert.assertEquals(Sel.getText("id=ErrorDetailLabel"), "Incorrect user name or password!");
   

                }

 

Now I am going to create a Test Base class. Concept of testbase class is to initialize my variables , create new objects and so

Create a new package -  Com.ABC.test.Util

Under that package create new Class   -  TestBase

In  this class create the Selenium new Object

public class TestBase {

                public Selenium Sel = new DefaultSelenium("localhost", 4444, "*firefox", "http://localhost:8080/LogOn.aspx");

}

Now you don’t need to create Selenium objects each time you create a tests.

Go to the TestLogin class and Extend the TestBase class

-    public class TestLogin extends TestBase

Now if you need to Start the selenium for this class you can create a constructor and start the selenium

//Constructor

public TestLogin(){

               

                Sel.start();

}

Now you can Run and check your tests :)
In my Next post i'll explain about TestNG XML and How you run your tests using TestNG xml .....


 

1 comment:

  1. CodingCub provides the best Software Testing Training in Chennai for people who are looking forward to achieving in the IT industry. We are the leading Software testing training institute in Chennai and have been the most preferred place for students to learn software testing.

    CodingCub has a unique style of grooming the students in the software testing. We have the most experienced tutors from the IT industry to knowledge you on the software testing and we proved you with the in and out knowledge on software testing. Testing has a huge variation and types and it depends on requirements that the type of testing you will do in the industry. CodingCub provides the best Software Testing Course in Chennai with a restriction to batch size to make sure that we can provide attention on each and every individual and help them to learn Software testing in-depth. We also conduct one-on-one on a regular basis to clarify the doubts and understand the pros and cons of the training improving on your training process.

    ReplyDelete