December 22, 2010

How to declare IE,FF and Chrome in Selenium

December 22, 2010 0
How to declare IE,FF and Chrome in Selenium
Selenium supported browsers include:



  *firefox
  *mock
  *firefoxproxy
  *pifirefox
  *chrome
  *iexploreproxy
  *iexplore
  *firefox3
  *safariproxy
  *googlechrome
  *konqueror
  *firefox2
  *safari
  *piiexplore
  *firefoxchrome
  *opera
  *iehta
  *custom
 on session null


December 15, 2010

Write results to excel sheet

December 15, 2010 60
After successfully executing scripts, every one want to write results to excel sheet..here is the way to write results to excel sheet....

Below is the sample script to write results to excel sheet...


package test;

import java.io.FileInputStream;
import java.io.FileOutputStream;

import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import com.thoughtworks.selenium.*;

import org.openqa.selenium.server.*;
import org.testng.annotations.*;

public class Importexport1 {
public Selenium selenium;
public SeleniumServer seleniumserver;

@BeforeClass
public void setUp() throws Exception {
RemoteControlConfiguration rc = new RemoteControlConfiguration();
seleniumserver = new SeleniumServer(rc);
selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://");
seleniumserver.start();
selenium.start();
}

@Test
public void testImportexport1() throws Exception {

// Read data from excel sheet
FileInputStream fi = new FileInputStream(
"F:\\Framework\\testdata\\Login1_Credentials.xls");
Workbook w = Workbook.getWorkbook(fi);
Sheet s = w.getSheet(0);
String a[][] = new String[s.getRows()][s.getColumns()];
// Write the input data into another excel file
FileOutputStream fo = new FileOutputStream(
"F:\\Framework\\Results\\LoginResult1.xls");
WritableWorkbook wwb = Workbook.createWorkbook(fo);
WritableSheet ws = wwb.createSheet("loginresult1", 0);

selenium.open("http://www.gmail.com");
selenium.windowMaximize();

System.out.println("s.getRows() = " + s.getRows());

for (int i = 0; i < s.getRows(); i++) {
System.out.println("s.getColumns = " + s.getColumns());
for (int j = 0; j < s.getColumns(); j++) {
a[i][j] = s.getCell(j, i).getContents();
Label l = new Label(j, i, a[i][j]);
Label l1 = new Label(2, 0, "Result");
ws.addCell(l);
ws.addCell(l1);
}
}

for (int i = 1; i < s.getRows(); i++) {
selenium.type("Email", s.getCell(0, i).getContents());
selenium.type("Passwd", s.getCell(1, i).getContents());
selenium.click("signIn");
selenium.waitForPageToLoad("30000");

boolean aa = selenium.isTextPresent("The username or password you entered is incorrect. [?]");
System.out.println("the value of aa is::" + aa);
if (aa)

{

Label l3 = new Label(2, i, "fail");

ws.addCell(l3);
System.out.println("Login Failure");
Thread.sleep(10000);

} else {

Label l2 = new Label(2, i, "pass");

ws.addCell(l2);
selenium.click("link=Sign out");
Thread.sleep(10000);
}
}
wwb.write();
wwb.close();
}

@AfterClass
public void tearDown() throws Exception {
selenium.stop();
seleniumserver.stop();

}
}




Your input data should be like this....















Your out put excel should be like this











Hope this post is helpful....

Test Suite using JUnit....

December 15, 2010 5
Test Suite using JUnit....
Most testing frameworks have the concept of grouping a set of text fixtures into a ’suite’ so that you can execute a number of related tests together.

Creating test suites with Selenium doesn’t appear to be obvious at first. Here’s a simple guide on how to create and execute suites of functional tests:

Here i am using Junit scripts and Test suite()


First create two scripts....Ex:Google1 and Google2

Google1.java

------------------------------------------------------------
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.server.*;

import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.SeleneseTestCase;
import com.thoughtworks.selenium.Selenium;

public class Google1 extends SeleneseTestCase{

private Selenium selenium;
private SeleniumServer seleniumServer;

@Before
public void setUp() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://");
seleniumServer = new SeleniumServer();
seleniumServer.start();
selenium.start();
}

@After
public void tearDown() throws Exception {
selenium.stop();
seleniumServer.stop();
}

@Test
public void testText1() throws Exception {
selenium.open("http://www.yahoo.com");
selenium.windowMaximize();
}
}

------------------------------------------------------------


Google2.java

------------------------------------------------------------
package defaultpackage;
import org.openqa.selenium.server.SeleniumServer;

import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.SeleneseTestCase;
import com.thoughtworks.selenium.Selenium;

public class Google2 extends SeleneseTestCase{

private Selenium selenium;
private SeleniumServer seleniumServer;


public void setUp() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*iehta", "http://");
seleniumServer = new SeleniumServer();
seleniumServer.start();
selenium.start();
}


public void tearDown() throws Exception {
selenium.stop();
seleniumServer.stop();
}


public void testText2() throws Exception {
selenium.open("http://www.google.com");
selenium.windowMaximize();
}
}

------------------------------------------------------------


Here is the test suite....TestSuite1.java
------------------------------------------------------------

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;

public class TestSuite1 extends TestCase {

public static Test suite()
{
TestSuite nag = new TestSuite();

nag.addTestSuite( Google1.class);
nag.addTestSuite( Google2.class);
return nag;
}

public static void main(String arg[])
{
TestRunner.run(suite());

}
}
------------------------------------------------------------



Run the TestSuite1.java script ...it will automatically run the Google1 and Google2....

Even though first test case is failed it will run the second one....we can change the order of the test cases...

December 13, 2010

Sample script using JUnit

December 13, 2010 0
Sample script using JUnit
Here is the sample script which will open a page...

This script was written using JUnit...


import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.server.SeleniumServer;

import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.SeleneseTestCase;
import com.thoughtworks.selenium.Selenium;

public class Google1 extends SeleneseTestCase{

private Selenium selenium;
private SeleniumServer seleniumServer;

@Before
public void setUp() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*iehta", "http://");
seleniumServer = new SeleniumServer();
seleniumServer.start();
selenium.start();
}



@Test
public void testText1() throws Exception {
selenium.open("http://www.yahoo.com");
selenium.windowMaximize();
}
@After
public void tearDown() throws Exception {
selenium.stop();
seleniumServer.stop();
}
}

Selenium IDE PPT

December 13, 2010 1
Selenium IDE PPT
Here is the good PPT about selenium

Please click the below link...



Presentation Transcript

Selenium Tutorial :

Selenium Tutorial


What is Selenium? :

What is Selenium? • Javascript framework that runs in your webbrowser • Works anywhere Javascript is supported • Hooks for many other languages • Java, Ruby, Python • Can simulate a user navigating through pages and then assert for specific marks on the pages • All you need to really know is HTML to start using it right away


Where to get it? :

Where to get it? • You can use Selenium-Core and customize everything • But it is easier to just get a Firefox plug-in “Selenium-IDE” that helps you “record” test cases • You can record how an app is being used and then play back those recordings followed by asserts • Get everything at: www.openqa.org/selenium/


Selenium IDE :

Selenium IDE The list of actions in the actual test case to execute The root of web application you want to test The log of the events that were executed, including any errors or warning that may have occurred


Selenium IDE :

Selenium IDE Execution Commands Record test actions Try the test in the Web based TestRunner Specify commands, including asserts Reference of the currently selected command 


Test Creation Demo :

Test Creation Demo • Create test case to log into the gallery • Create test case to log out of the gallery


Slide 7:

Start Pixory Connect to the Server Go to the Login Screen Hit the Record Button


Slide 8:

Type in Username and Password Hit record again to stop Hit submit IDE should update


Slide 9:

Add assertTextPresent and type the text to search for Hit play to make sure your test is successful


Creating a Test Suite :

Creating a Test Suite A Test Suite in Selenium is just an HTML file that contains a table of links to tests Demo Test Suite
Demo Test Suite
TestLogin
TestLogout


Executing the Test Suite :

Executing the Test Suite • Selenium Core is a collection of Javascript and HTML with iFrames • Due to security concerns Core must be deployed within the same server as the application being hosted • The simplest way to run Pixory is to just run the Java application and let it use its own server • Problems using Core with Pixory • Selenium IDE is a plug-in for Firefox and thus can go around these restrictions


Running the Test Suite :

Running the Test Suite • We basically want to execute the test suite using the Selenium IDE plug-in TestRunner.html chrome://selenium-ide/content/selenium/TestRunner.html? baseURL=&test=file:///&auto=true chrome://selenium-ide/content/selenium/TestRunner.html? baseURL=http://localhost:8081&test=file:///Users/ms333/ projects/classes/running/v_and_v/hw3/selenium/test/ TestSuite.html&auto=true


Test Suite :

Test Suite


Test Suite :

Test Suite Execution Control Test Cases Steps of the test case Application being tested


Test Runner Control :

Test Runner Control


Test Runner Control :

Test Runner Control Pause/Play Execution Step through Execution Control Speed of Execution Summary of the Test View the log of the current execution View the DOM of the current Page being tested Highlight Elements in the Execution Run All Tests Run Selected Test


TestRunner Demo :

TestRunner Demo Execute Tests created inside the Firefox TestRunner.  

December 9, 2010

Check boxes count, Checked and Uncecked

December 09, 2010 3
Check boxes count, Checked and Uncecked
This is the script ...which will count number of check boxes in a page and number of checked check boxes and unchecked check boxes




package test;

import com.thoughtworks.selenium.*;

import org.openqa.selenium.server.*;
import org.testng.annotations.*;

public class Checkboxcount {
public Selenium selenium;
public SeleniumServer seleniumserver;

@BeforeClass
public void setUp() throws Exception {
RemoteControlConfiguration rc = new RemoteControlConfiguration();
seleniumserver = new SeleniumServer(rc);
selenium = new DefaultSelenium("localhost", 4444, "*firefox","http://");
seleniumserver.start();
selenium.start();
}

@Test
public void testDefaultTNG() throws Exception {

selenium.open("http://browsershots.org/");
selenium.windowMaximize();

//Count of check boxes
Number c  =selenium.getXpathCount("//input[@type['checkbox']]");
System.out.println("Count of check boxes " +c);
//Number of check boxes checked
Number d = selenium.getXpathCount("//input[@type='checkbox' and @checked]");
System.out.println("Count of Checked check boxes " +d);
//Number of check boxes unchecked
Number e = selenium.getXpathCount("//input[@type='checkbox' and not(@checked)]");
System.out.println("Count of Checked check boxes " +e);
}

@AfterClass
public void tearDown() throws Exception {
selenium.stop();
seleniumserver.stop();

}
}


If you uncheck some check boxes and try the above script it will not work....bcos if you check and uncheck this wouldn't change any thing in their html....


December 8, 2010

Verify value in drop down list.

December 08, 2010 0
Verify value in drop down list.
How to verify value in drop down list.


Its difficult to find element with some specific attributes.
Take an example How to find some value is in drop down list or not? 
To find element drop down is easy but to find a drop down having some specific value in their list is difficult.


Go to www.google.com  and click Advanced Search, you will see there are some drop down boxes.

How to verify a specific value "50 results" is in drop down list.




Here is the script for finding the value is in drop down or not





package test;


import com.thoughtworks.selenium.*;


import org.openqa.selenium.server.*;
import org.testng.annotations.*;




public class Dropdownall {
public Selenium selenium;
public SeleniumServer seleniumserver;


  @BeforeClass
  public void setUp() throws Exception {
RemoteControlConfiguration rc = new RemoteControlConfiguration();
seleniumserver = new SeleniumServer(rc);
selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "http://");
seleniumserver.start();
selenium.start();
}


@Test
public void testDropdownall()throws Exception {

selenium.open("http://www.google.com");
selenium.windowMaximize();
selenium.click("link=Advanced Search");
selenium.waitForPageToLoad("50000");

//Verifying Element is Present or not here i used xpath to find that

if(selenium.isElementPresent("//select[@name='num']/option[contains(text(),'50 results')]"))
{
System.out.println("ys");
}


}
   
 @AfterClass
 public void tearDown()throws Exception {
selenium.stop();
seleniumserver.stop();
 
 }
 }



if the value 50results are in drop down it will display yes or else u will not get any message..



//select[@name='num']/option[contains(text(),'50 results')]





Here name=num is the name of the dropdown u can see it in HTML code...
We used contains (text(),50 results') .....

Select,identify selected and print values in dropdown

December 08, 2010 55
Select,identify selected and print values in dropdown
Here is the post which will explain you 

How to select a value from drop down
Identify the selected value in drop down.
Display the values in drop down
Verifying Element is Present or not here i used xpath to find that


package test;


import com.thoughtworks.selenium.*;


import org.openqa.selenium.server.*;
import org.testng.annotations.*;




public class Dropdownselectvalue {
public Selenium selenium;
public SeleniumServer seleniumserver;


  @BeforeClass
  public void setUp() throws Exception {
RemoteControlConfiguration rc = new RemoteControlConfiguration();
seleniumserver = new SeleniumServer(rc);
selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "http://");
seleniumserver.start();
selenium.start();
}


@Test
public void testDropdownselectvalue()throws Exception {

selenium.open("http://www.google.com");
selenium.windowMaximize();
selenium.click("link=Advanced Search");
selenium.waitForPageToLoad("50000");
// Print all the available options from the results dropdown in the
String[] options = selenium.getSelectOptions("name=num");
// The above command returns a array of strings(options)
    for (int i = 0; i < options.length; i++) {
System.out.println("Option: " + i + " is " + options[i]);
}
//select value from dropdown
    selenium.select("num", "label=30 results");
    //Print the selected value from dropdown
String s=selenium.getSelectedValue("name=num");
System.out.println("selected value is "+s);

//Verifying Element is Present or not here i used xpath to find that if(selenium.isElementPresent("//select[@name='num']/option[contains(text(),'50 results')]")) { System.out.println("ys"); }


}
   
 @AfterClass
 public void tearDown()throws Exception {
selenium.stop();
seleniumserver.stop();
 
 }
 }


.

December 5, 2010

Selenium Script Explanation

December 05, 2010 6
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://");
localhost—serverHost
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                
selenium.start();
//to open the URL
                        selenium.open("http://test2.xyzt.com");
//Maximizes the window
                        selenium.windowMaximize();
//to read user name
                        selenium.type("name=login[username]", "dnr5dnr@gmail.com");
//to read password       
            selenium.type("name=login[password]", "mourya");
//click login button                    
selenium.click("send2");
//waits until the page loads
                        selenium.waitForPageToLoad("50000");
//click the link sign out
                        selenium.click("link=Sign Out");
// print message
                        System.out.println("Login and Log out success");

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

}





Tools for identifying the element names and IDs

December 05, 2010 0
Tools for identifying the element names and IDs
Tools for identifying the element names and IDs


We have to use some tools to identify the element locators.
  1. Debugbar
  2. Firebug
DEBUGBAR:
Debug bar is an Internet Explorer plug-in.

We can use this tool for identifying the element names and Ids.
View source code


You can download Debug bar by using the link http://www.debugbar.com/?langage=en


FIREBUG:
Firebug integrates with Firefox to put a wealth of development tools at your fingertips while you browse. You can edit, debug, and monitor CSS, HTML, and JavaScript live in any web page...

1. Inspect HTML and modify style and layout in real-time
2. Accurately analyze network usage and performance

You can download fire bug using the link  https://addons.mozilla.org/en-US/firefox/addon/1843/ 

Selenium Commands

December 05, 2010 3
Selenium Commands

Element Locator

Element Locators tell Selenium which HTML element a command refers to.
The format of a locator is:  locatorType=argument
Example: - (“link=click”)
Selenium supports the following strategies for locating elements:
id=id
Select the element with the specified @id attribute
name=name
Select the first element with the specified @name attribute.
·         username
·         name=username
xpath=xpathExpression
Locate an element using an XPath expression.
·         xpath=//img[@alt='The image alt text']
·         xpath=//table[@id='table1']//tr[4]/td[2]
link=textPattern
Select the link (anchor) element which contains text matching the specified pattern.
·         link=The link text


  Important Selenium Commands


Command
Usage
Description
start()
selenium.start();
Launches the browser with a new Selenium session
stop()
selenium.stop();
Ends the current Selenium testing session (normally killing the browser)
 click()
selenium.click("link=Home");
selenium.click("name=send");
Clicks on a link, button, checkbox or radio button. If the click action causes a new page to load (like a link usually does), call waitForPageToLoad.
doubleClick()

Double clicks on a link, button, checkbox or radio button. If the double click action causes a new page to load (like a link usually does), call waitForPageToLoad.
type()
Type(
   
string locator,
   
string value
);

selenium.type("name=login[username]", ndasam);
Sets the value of an input field, as though you typed it in. Can also be used to set the value of combo boxes, check boxes, etc. In these cases, value should be the value of the option selected, not the visible text.

check()
Check(
   
string locator
);
selenium.check ("guest”);
Check a toggle-button (checkbox/radio)
 uncheck()
Uncheck(
   
string locator
);
Uncheck a toggle-button (checkbox/radio)
 select()
Select(
   
string selectLocator,
   
string optionLocator
);
selenium.select("payware_expiration_yr", "label=2010”);

Select an option from a drop-down using an option locator.





December 3, 2010

Some helpful docs for testers

December 03, 2010 4
Some helpful docs for testers

Sample bug report

December 03, 2010 1
Sample bug report
Below sample bug/defect report will give you exact idea of how to report a bug in bug tracking tool.
Here is the example scenario that caused a bug:
Lets assume in your application under test you want to create a new user with user information, for that you need to logon into the application and navigate to USERS menu > New User, then enter all the details in the ‘User form’ like, First Name, Last Name, Age, Address, Phone etc. Once you enter all these information, you need to click on ‘SAVE’ button in order to save the user. Now you can see a success message saying, “New User has been created successfully”.
But when you entered into your application by logging in and navigated to USERS menu > New user, entered all the required information to create new user and clicked on SAVE button. BANG! The application crashed and you got one error page on screen. (Capture this error message window and save as a Microsoft paint file)
Now this is the bug scenario and you would like to report this as a BUG in your bug-tracking tool.
How will you report this bug effectively?
Here is the sample bug report for above mentioned example:
(Note that some ‘bug report’ fields might differ depending on your bug tracking system)
SAMPLE BUG REPORT:
Bug Name: Application crash on clicking the SAVE button while creating a new user.
Bug ID: (It will be automatically created by the BUG Tracking tool once you save this bug)
Area Path: USERS menu > New Users
Build Number: Version Number 5.0.1
Severity: HIGH (High/Medium/Low) or 1
Priority: HIGH (High/Medium/Low) or 1
Assigned to: Developer-X
Reported By: Your Name
Reported On: Date
Reason: Defect
Status: New/Open/Active (Depends on the Tool you are using)
Environment: Windows 2003/SQL Server 2005
Description:
Application crash on clicking the SAVE button while creating a new
user, hence unable to create a new user in the application.
Steps To Reproduce:
1) Logon into the application
2) Navigate to the Users Menu > New User
3) Filled all the user information fields
4) Clicked on ‘Save’ button
5) Seen an error page “ORA1090 Exception: Insert values Error…”
6) See the attached logs for more information (Attach more logs related to bug..IF any)
7) And also see the attached screenshot of the error page.
Expected result: On clicking SAVE button, should be prompted to a success message “New User has been created successfully”.
(Attach ‘application crash’ screen shot.. IF any)
Save the defect/bug in the BUG TRACKING TOOL.  You will get a bug id, which you can use for further bug reference.
Default ‘New bug’ mail will go to respective developer and the default module owner (Team leader or manager) for further action.