0% found this document useful (0 votes)
36 views52 pages

ST Lab Manual

Uploaded by

ravikumark.it
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views52 pages

ST Lab Manual

Uploaded by

ravikumark.it
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 52

M.

KUMARASAMY COLLEGE OF ENGINEERING


(AUTONOMOUS),
THALAVAPALAYAM, KARUR

DEPARTMENT OF INFORMATION TECHNOLOGY

18ITC302JT-SOFTWARE TESTING LAB MANUAL


[REGULATION 2018]
SYLLABUS

18ITC302JT - SOFTWARE TESTING LABORATORY

List of Experiments:

1. Understand the Automation Testing Approach

2. Using Selenium IDE, Write a test suite containing minimum 4 test cases.

3. Conduct a test suite for any two web sites

4. Install Selenium server and demonstrate it using a script in Java/PHP

5. Write and test a program to login a specific web page

6. Write and test a program to update 10 student records into table into Excel file

7. Write and test a program to select the number of students who have scored more than 60 in any
one subject (or all subjects)

8. Write and test a program to provide total number of objects present / available on the page

9. Write and test a program to get the number of list items in a list / combo box

10. Write and test a program to count number of check boxes on the page checked and unchecked
count.
Exercise Number: 1
Date:

Title of the Exercise:


UNDERSTAND THE AUTOMATION TESTING APPROACH

Aim:

To understand the automation testing approach. (Theory concept)

Automation

Automation is making a process automatic eliminating the need for human intervention. It is a self-
controlling or self-moving process. Automation Software offers automation wizards and commands of its
own in addition to providing a task recording and re-play capabilities. Using these programs you can
record an IT or business task.

Benefits of Automation Fast

Reliable Repeatable
Programmable
Reusable
Makes Regression testing easy Enables
24*78 Testing
Robust verification.

Automation Test Workflow

Prepare Create Verify Integrate


&
Enhanc

Create Basic Test Enhance Basic Test Integrate Tests

Record user actions Add Synchronization


Pass Data
Insert check points Data drive the test
Confirm successful playback
Build integrated test sets
INTRODUCTION TO SELENIUM

1. History of Selenium

• In 2004 invented by Jason R. Huggins and team.

• Original name is JavaScript Functional Tester [JSFT]

• Open source browser based integration test framework built originally by Thought
works.

• 100% JavaScript and HTML

• Web testing tool

• That supports testing Web 2.0 applications

• Supports for Cross-Browser Testing(ON Multiple Browsers)

• And multiple Operating Systems

• Cross browser – IE 6/7, Firefox .8+, Opera, Safari 2.0+

2. What is Selenium?

• Acceptance Testing tool for web-apps

• Tests run directly in browser

• Selenium can be deployed on Windows, Linux, and Macintosh.

• Implemented entirely using browser technologies -

 JavaScript

 DHTML

 Frame

3. Selenium Components

– Selenium IDE

– Selenium Core

– Selenium RC
– Selenium Grid

Selenium IDE

• The Selenium-IDE (Integrated Development Environment) is the tool you use to


develop your Selenium test cases.

• It is Firefox plug-in
• Firefox extension which allows record/play testing paradigm
• Automates commands, but asserts must be entered by hand
• Creates the simplest possible Locator
• Based on Selenese
Overview of Selenium IDE:

A. Test Case Pane

B. Toolbar

C. Menu Bar

D. Log/Reference/UI-Element/Rollup Pane

A. Test Case Pane:

Your script is displayed in the test case pane.

It has two tabs.

one for displaying the command (source) and their parameters in a readable “table”
format

B. Toolbar: The toolbar contains buttons for controlling the execution of your test cases,
including a step feature for

C. Menu Bar:

File Menu: The File menu allows you to create, open and save test case and test
suite files.

Edit Menu: The Edit menu allows copy, paste, delete, undo and select all
operations for editing the commands in your test case.
Options Menu: The Options menu allows the changing of settings. You can set
the timeout value for certain commands, add user-defined user extensions to the
base set of Selenium commands, and specify the format (language) used when
saving your test cases.

D. Help Menu:

Introducing Selenium Commands

The command set is often called selenese. Selenium commands come in three “flavors”:
Actions, Accessory and Assertions.

a. Actions: user actions on application / Command the browser to do something.

Actions are commands that generally manipulate the state of the


application.

1. Click link- click / Clickandwait

2. Selecting items

b. Accessors: Accessors examine the state of the application and store the results in
variables, e.g. "storeTitle".

c. Assertions: For validating the application we are using Assertions

1. For verifying the web pages

2. For verifying the text

3. For verifying alerts

Assertions can be used in 3 modes:

 assert

 verify

 waitFor

Example: "assertText","verifyText"and "waitForText".

1. Commonly Used Selenium Commands


These are probably the most commonly used commands for building test. open -

opens a page using a URL.


click/clickAndWait - performs a click operation, and optionally waits for a new page to load.

verifyTitle/assertTitle - verifies an expected page title. verifyTextPresent-

verifies expected text is somewhere on the page.

verifyElementPresent -verifies an expected UI element, as defined by its HTML tag, is


present on the page.

verifyText - verifies expected text and it’s corresponding HTML tag are present on the page.

verifyTable - verifies a table’s expected contents.

Wait For Page To Load -pauses execution until an expected new page loads. Called automatically
when clickAndWait is used.

waitForElementPresent -pauses execution until an expected UI element, as defined by its


HTML tag, is present on the page.

Recording and Run settings

When Selenium-IDE is first opened, the record button is ON by default.


During recording, Selenium-IDE will automatically insert commands into your test case
based on your actions.
a. Remember Base URL MODE -
Using Base URL to Run Test Cases in Different Domains
b. Record Absolute recording mode – Run Test Cases in Particular Domain.

Running Test Cases

Run a Test Case Click the Run button to run the currently displayed test case.
Run a Test Suite Click the Run All button to run all the test cases in the currently loaded
test suite.
Stop and Start The Pause button can be used to stop the test case while it is running. The
icon of this button then changes to indicate the Resume button. To continue click Resume.
Stop in the Middle You can set a breakpoint in the test case to cause it to stop on a particular
command. This is useful for debugging your test case. To set a breakpoint, select a command,
right-click, and from the context menu select Toggle Breakpoint.
Start from the Middle You can tell the IDE to begin running from a specific command in
the middle of the test case. This also is used for debugging. To set a startpoint, select a
command, right-click, and from the context menu select Set/Clear Start Point.

Run Any Single Command Double-click any single command to run it by itself. This is useful
when writing a single command. It lets you immediately test a command you are constructing,
when you are not sure if it is correct. You can double-click it to see if it runs correctly. This is
also available from the context menu.

Test Suit
A test suite is a collection of tests. Often one will run all the tests in a test suite as one
continuous batch-job.
When using Selenium-IDE, test suites also can be defined using a simple HTML file. The
syntax again is simple. An HTML table defines a list of tests where each row defines the file
system path to each test.

Installing the IDE

Using Firefox, first, download the IDE from the SeleniumHQ downloads page Firefox will
protect you from installing addons from unfamiliar locations, so you will need to click
‘Allow’ to proceed with the installation, as shown in the following screenshot.
SOFTWARE TESTING LABORATORY

When downloading from Firefox, you’ll be presented with the following window.

Select Install Now. The Firefox Add-ons window pops up, first showing a progress bar,
and when the download is complete, displays the following.

Restart Firefox. After Firefox reboots you will find the Selenium-IDE listed under the

Software Testing-16IT711 Page 9


SOFTWARE TESTING LABORATORY

Firefox Tools menu.

Opening the IDE

To run the Selenium-IDE, simply select it from the Firefox Tools menu. It opens as
follows with an empty script-editing window and a menu for loading, or creating new test
cases.

Result:

Thus the automation testing approach is using selenium IDE is carried out.

Software Testing-16IT711 Page 10


SOFTWARE TESTING LABORATORY

Exercise Number: 2
Date:

Title of the Exercise:


USING SELENIUM IDE, WRITE A TEST SUITE CONTAINING MINIMUM 4 TEST
CASES.

Aim:

To write a test suite containing minimum four test cases using selenium IDE

TC’S #1: Manual Steps:

• Open (Example : Type www.google.com)


• Type “energy efficient” in the Google Search Input Box
• Click outside on an empty spot
• Click Search Button
• Verify the Text Present as “energy efficient”
• Assert the Title as “energy efficient - Google Search”
• Save the test case with .HTML
Extension. TC’S #2:

• Open (Example : Type www.google.com)


• Type “Selenium RC” in the Google Search Input Box
• Click outside on an empty spot
• Click Search Button
• Verify the Text Present as “Selenium RC”
• Assert the Title as “Selenium RC - Google Search”
• Save the test case with .HTML Extension.

Steps for creating test suite:

1. Create more Tc’s save each Test Case with <.html> extension.

2. Open Firefox

3. Open Tools  Selenium IDE

4. File  Open  new Test Suite

Software Testing-16IT711 Page 11


SOFTWARE TESTING LABORATORY

5. File Open  Add Test cases

6. Add more test cases

7. Save Suite with <.Html> extensions.

8. Run the test suite

Output:

Result:

Thus a test suite containing minimum four test cases using selenium IDE is created
successfully

Software Testing-16IT711 Page 12


SOFTWARE TESTING LABORATORY

Exercise Number: 3
Date:

Title of the Exercise:

CONDUCT A TEST SUITE FOR ANY TWO WEB SITES


Aim
:
To conduct a test suite for any two web sites.

TC’S #1: Manual Steps:

• Open (Example : Type www.redbus.com)


• Type “book ticket” in the Google Search Input Box
• Click outside on an empty spot
• Click Search Button
• Assert the Title as “book ticketsearch bus”
• Save the test case.

TC#2:

 Open Firefox Web Browser

 In the address bar, Type http://www.google.com

 In the search input button, Type "Java”

 Click on the "Web Search" submit button

 Wait for Search Results to come on "http:/search.google.com"

 Verify "Java” text is present anywhere in the search results: (Select and highlight
anywhere in the search results page)
 Save the test case

Software Testing-16IT711 Page 13


SOFTWARE TESTING LABORATORY

Output:

Software Testing-16IT711 Page 14


SOFTWARE TESTING LABORATORY

Result:

Thus a test suite for any two web sites is created successfully.

Software Testing-16IT711 Page 15


SOFTWARE TESTING LABORATORY

Exercise Number: 4
Date:

Title of the Exercise:

INSTALL SELENIUM SERVER AND DEMONSTRATE IT USING A SCRIPT IN


JAVA/PHP

Aim:

To Install Selenium server and to demonstrate it using a script in Java/PHP.

Procedure:

1. Go to URL – http://www.eclipse.org/downloads/
2. Select Eclipse IDE for Java Developers (Click on Windows 32 bit platform)
3. Click on OK button and save to a local drive (i.e. C: or D:, etc)
4. Unzip the downloaded zip file and rename that to Eclipse
5. Create one more folder “Eclipse-Workspace” (i.e. C:Eclipse-Workspace)in the
same drive where Eclipse is unzipped and renamed.
6. Create Eclipse desktop shortcut (go to C:Eclipse folder –> right click Eclipse.exe
and then click on “desktop create shortcut”) as demonstrated in the below
pictures.

1. Now we need to create a workspace folder –>


C:Eclipse- WorkspaceSeleniumTests

Software Testing-16IT711 Page 16


SOFTWARE TESTING LABORATORY

2. Double click on “Eclipse shortcut on Desktop”


3. This opens the Eclipse
4. Close Eclipse welcome screen
5. Click File menu –> Switch Worspace –> other
6. Now Select the C:Eclipse-WorkspaceSeleniumTests folder (These steps
are demonstrated in the following figure)

We have finished setting up the eclipse. Now, we need to download Selenium RC


server / client driver and configure that to Eclipse

1. Download Selenium server: http://seleniumhq.org/download/

2. Download Selenium Client driver for Java (from Selenium Client


Drivers section)
3. Create “Selenium” folder in C: drive and copy the Selenium-server.jar
as well as unzip the Selenium Client driver (C:Selenium)

Downloading and unzipping the files into a folder is done. We need to configure
the appropriate Selenium Client driver Jar file to the Eclipse.

1. Go to Eclipse –> Click File –> New –> Project (from various options need
to select just “project”)
2. In Select Wizard –> Click Java –> “Java Project” (demonstrated in the

Software Testing-16IT711 Page 17


SOFTWARE TESTING LABORATORY

below figure)

1. Give the project name (e.g. SugarCRMTests)


2. Click Finish – Click Yes
3. Now we are done with creation of project and need to configure
the Selenium Client driver to this Project
4. Right Click “SugarCRMTests” project

5. Click “Java Build Path”


6. Click Libraries tab
7. Click “Add External JARs” button
8. Select “Selenium Client Drivers” unzipped in C:Selenium folder(Selenium
Server JAR file should not be added)
9. Click OK
10. Referenced libraries –> contains both the Selenium Client driver jar filesas
shown in the below picture.

Software Testing-16IT711 Page 18


SOFTWARE TESTING LABORATORY

First script-Selenium RC-JUnit-Java

Selenium RC allows to write the automated tests in any language. Selenium Scripts can
be written in various ways:
1. Extending the Selenese Test Case
2. Extending the TestCase-Junit and defining an object to the DefaultSelenium
Class
Before you start with your script, the following are the steps that should be
performed.
Method1: Extending the Selenese Test Case

1. Install any IDE (Eclipse, IntelliJ, IDEA), create a project.


2. Download the selenium RC file from Selenium website.
3. Extract the selenium-remote-control-1.0.3.zip file and add the selenium- remote-
control-1.0.3\selenium-java-client-driver-1.0.1\selenium-java-clientdriver.jar and
selenium-remote-control-1.0.3\selenium-server- 1.0.3\selenium-server.jar file to
the build path.
4. Create a new package and a class in the newly created package.
5. Copy the below code:

Import com.thoughtworks.selenium.SeleneseTestCase;
public class SeleniumTest1 extends SeleneseTestCase { public
void setUp() throws Exception
{ setUp("http://www.hexbytes.com/", "*firefox");
}
public void testNew() throws
Exception{ selenium.open("/");

Software Testing-16IT711 Page 19


SOFTWARE TESTING LABORATORY

selenium.type("searchbox", "selenium rc"); selenium.click("css=input[type='submit']


[value='Search']"); selenium.waitForPageToLoad("30000");
assertTrue(selenium.isTextPresent("selenium rc"));
}
}
1. Start the selenium Server.

2. Click on the RunAs button and run the script as JUnit Test. If youdon’t find
the option Install the JUnit plug-in and run the script.

Method2: Extending the DefaultSelenium Class


Some times need the selenium scripts to contact the server running on a different port and
different machine. So configure the selenium scripts to handle such situations easily by
extending your selenium script from TestCase(Junit) and defining the selenium instance
as an object to DefaultSelenium class.
package test;
import junit.framework.*;
import com.thoughtworks.selenium.DefaultSelenium;
public class DefaultSelenium1 extends TestCase{ private
DefaultSelenium selenium;
public void testing()
{ selenium=new
DefaultSelenium("localhost",4444,"*iexplore","http://hexbytes.com");
selenium.start();
selenium.open("/");
selenium.type("searchbox", "selenium rc");
selenium.click("css=input[type='submit'][value='Search']");
selenium.waitForPageToLoad("30000");
Assert.assertTrue(selenium.isTextPresent("selenium rc"));
}
}

Software Testing-16IT711 Page 20


SOFTWARE TESTING LABORATORY

Output:

Result:

Thus the installation and demonstration of selenium server using a script in java/php
executed successfully.

Software Testing-16IT711 Page 21


SOFTWARE TESTING LABORATORY

Exercise Number: 5
Date:

Title of the Exercise:


WRITE AND TEST A PROGRAM TO LOGIN A SPECIFIC WEB PAGE

Aim:

To test a login page for a specific web page.

Procedure:

o Write the java script for creating a login page code in eclipse.

o Before running this code makes sure your selenium RC server is


running.

Program:

import com.thoughtworks.selenium.*;
@SuppressWarnings("deprecation")
public class login extends SeleneseTestCase {

public void setUp() throws Exception


{ setUp(
"http://php.thedemosite.co.uk/",firefox");

}
public void testloginlogout()

selenium.setSpeed("2000"); selenium.open("/login.php");
selenium.windowMaximize(); selenium.type("//input[@name='username']", "mytest");
selenium.type("//input[@name='password']", "mytest");
selenium.click("//input[@type='button']"); selenium.waitForPageToLoad("50000");

}
}

Software Testing-16IT711 Page 22


SOFTWARE TESTING LABORATORY

Software Testing-16IT711 Page 23


SOFTWARE TESTING LABORATORY

Output:

Result:

Thus a login page for specific web page is created and tested successfully.

Software Testing-16IT711 Page 24


SOFTWARE TESTING LABORATORY

Exercise Number: 6
Date:

Title of the Exercise:

WRITE AND TEST A PROGRAM TO UPDATE 10 STUDENT RECORDS INTO


TABLE INTO EXCEL FILE
Aim:

To test a program to update 10 student records into table into Excelfile.

Procedure:

 Write the java test code in eclipse. Since we are not using any Selenium object here
no need of selenium RC Server to run at the command prompt for this program.
 This program will create an Excel file Student.xls under the desktop.
 Since we are dealing with the excel, we need to add the external jar file jxl-2.6 in to
the java project.( download jxl-2.6 and Follow the same steps as we used to add
selenium server and selenium java driver to our java project refer page no 10 of this
manual.)
Program:

package Selenium;
import jxl.Cell;
import jxl.CellView;
import jxl.Sheet;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.format.UnderlineStyle;
import jxl.read.biff.BiffException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Locale;

Software Testing-16IT711 Page 25


SOFTWARE TESTING LABORATORY

import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.Number;

import
jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import org.testng.annotations.*;
public class Ex6 {
@BeforeClass
public void setUp() throws Exception {}
@Test
public void testImportexport1() throws Exception
{ student_excel_write test = new
student_excel_write();
test.setOutputFile("D:/exp6.xls");
test.write();
System.out.println("created ");
checkExcelSheet();
System.out.println("checked");
}

// first code
private WritableCellFormat timesBoldUnderline;
Software Testing-16IT711 Page 26
SOFTWARE TESTING LABORATORY
private WritableCellFormat times;

Software Testing-16IT711 Page 27


SOFTWARE TESTING LABORATORY

private String inputFile;


public void setOutputFile(String inputFile)
{ this.inputFile = inputFile;
}

public void write() throws IOException, WriteException


{ File file = new File(inputFile);
WorkbookSettings wbSettings = new WorkbookSettings();
wbSettings.setLocale(new Locale("en", "EN"));
WritableWorkbook workbook = Workbook.createWorkbook(file, wbSettings);
workbook.createSheet("Report", 0);
WritableSheet excelSheet = workbook.getSheet(0);
createLabel(excelSheet);
createContent(excelSheet);
workbook.write();
workbook.close();
}

private void createLabel(WritableSheet sheet)


throws WriteException {
// Lets create a times font
WritableFont times10pt = new WritableFont(WritableFont.TIMES, 10);
// Define the cell format
times = new WritableCellFormat(times10pt);

// Lets automatically wrap the cells


times.setWrap(true);
// Create a bold font with underlines
WritableFont times10ptBoldUnderline = new
WritableFont( WritableFont.TIMES, 10, WritableFont.BOLD, false,

Software Testing-16IT711 Page 28


SOFTWARE TESTING LABORATORY

UnderlineStyle.SINGLE);
timesBoldUnderline = new WritableCellFormat(times10ptBoldUnderline);
// Lets automatically wrap the cells
timesBoldUnderline.setWrap(true);
CellView cv = new CellView();
cv.setFormat(times);
cv.setFormat(timesBoldUnderline);

// Write a few headers


addCaption(sheet, 0, 0, "Student Name");

addCaption(sheet, 1, 0, "Subject 1");


addCaption(sheet, 2, 0, "subject 2");
addCaption(sheet, 3, 0, "subject 3");
}
private void createContent(WritableSheet sheet) throws WriteException,
RowsExceededException {
// Write a few number
for (int i = 1; i < 10; i++) {
// First column
addLabel(sheet, 0, i, "Student " + i);
// Second column
addNumber(sheet, 1, i, ((i*i)+10));
addNumber(sheet, 2, i, ((i*i)+4));
addNumber(sheet, 3, i, ((i*i)+3));
}
}
private void addCaption(WritableSheet sheet, int column, int row, String s)

Software Testing-16IT711 Page 29


SOFTWARE TESTING LABORATORY

throws RowsExceededException, WriteException


{ Label label;
label = new Label(column, row, s, timesBoldUnderline);
sheet.addCell(label);
}
private void addNumber(WritableSheet sheet, int column, int row,
Integer integer) throws WriteException, RowsExceededException
{ Number number;
number = new Number(column, row, integer, times);
sheet.addCell(number);
}
private void addLabel(WritableSheet sheet, int column, int row, String s)
throws WriteException, RowsExceededException {
Label label;
label = new Label(column, row, s, times);
sheet.addCell(label);
}
//
private void checkExcelSheet() throws Exception
// second code
{
FileInputStream fi = new FileInputStream("D:\\exp6.xls");
Workbook w = Workbook.getWorkbook(fi);
Sheet s = w.getSheet(0);
String a[][] = new String[s.getRows()][s.getColumns()];
FileOutputStream fo = new FileOutputStream("D://exp7.xls");
WritableWorkbook wwb = Workbook.createWorkbook(fo);

Software Testing-16IT711 Page 30


SOFTWARE TESTING LABORATORY

WritableSheet ws = wwb.createSheet("result1", 0);


for (int i = 0; i < s.getRows(); i++)
for (int j = 0; j < s.getColumns(); j++)
{

a[i][j] = s.getCell(j, i).getContents();


Label l2 = new Label(j, i, a[i][j]);
ws.addCell(l2);
Label l1 = new Label(6, 0, "Result");
ws.addCell(l1);
}

for (int i = 1; i < s.getRows(); i++)


{ for (int j = 2; j < s.getColumns(); j+
+)
{
a[i][j] = s.getCell(j, i).getContents();
int x=Integer.parseInt(a[i][j]);
if(x > 60)

{
Label l1 = new Label(6, i, "pass");
ws.addCell(l1);
}
else
{

Label l1 = new Label(6, i, "fail");


ws.addCell(l1);
break;
}}}
wwb.write();
Software Testing-16IT711 Page 31
SOFTWARE TESTING LABORATORY

Software Testing-16IT711 Page 32


SOFTWARE TESTING LABORATORY

wwb.close();
}}

Output:

Result:
Thus the program to update 10 student records into table into Excelfile is tested
successfully.

Software Testing-16IT711 Page 33


SOFTWARE TESTING LABORATORY

Exercise Number: 7
Date:

Title of the Exercise:

WRITE AND TEST A PROGRAM TO SELECT THE NUMBER OF STUDENTS


WHO HAVE SCORED MORE THAN 60 IN ANY ONE SUBJECT ( OR ALL
SUBJECTS )

Aim
:
To test the program to select the number of students who have scored more than
60 in any one subject ( or all subjects )

Procedure:

 Write the java test code in eclipse. Since we are not using any Selenium object here no
need of selenium RC Server to run at the command prompt for this program.
 This program reads an excel file so Create an Excel file Student.xls under the desktop
as follows.
Since we are dealing with the excel, we need to add the external jar file jxl-2.6 in to the
java project.( download jxl-2.6 and Follow the same steps as we used to add selenium
server and selenium java driver to our java project refer page no 10 of this manual.

Program:

package Selenium;
import jxl.Cell;
import jxl.CellView;
import jxl.Sheet;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.format.UnderlineStyle;
import jxl.read.biff.BiffException;
import java.io.File;

Software Testing-16IT711 Page 34


SOFTWARE TESTING LABORATORY
import java.io.FileInputStream;

Software Testing-16IT711 Page 35


SOFTWARE TESTING LABORATORY

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Locale;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.Number;
import
jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import org.testng.annotations.*;
public class Ex7 {
@BeforeClass
public void setUp() throws Exception {}
@Test
public void testImportexport1() throws Exception
{ student_excel_write test = new student_excel_write();
test.setOutputFile("D:/exp6.xls");
test.write();
System.out.println("created ");
checkExcelSheet();
System.out.println("checked");
}
// first code
Software Testing-16IT711 Page 36
SOFTWARE TESTING LABORATORY

Software Testing-16IT711 Page 37


SOFTWARE TESTING LABORATORY

private WritableCellFormat timesBoldUnderline;


private WritableCellFormat times;
private String inputFile;
public void setOutputFile(String inputFile)
{ this.inputFile = inputFile;
}
public void write() throws IOException, WriteException
{ File file = new File(inputFile);
WorkbookSettings wbSettings = new WorkbookSettings();
wbSettings.setLocale(new Locale("en", "EN"));
WritableWorkbook workbook = Workbook.createWorkbook(file, wbSettings);
workbook.createSheet("Report", 0);
WritableSheet excelSheet = workbook.getSheet(0);
createLabel(excelSheet);
createContent(excelSheet);
workbook.write();
workbook.close();
}
private void createLabel(WritableSheet sheet) throws WriteException {
// Lets create a times font
WritableFont times10pt = new WritableFont(WritableFont.TIMES, 10);
// Define the cell format

times = new WritableCellFormat(times10pt);


// Lets automatically wrap the cells
times.setWrap(true);
// Create a bold font with underlines
WritableFont times10ptBoldUnderline = new WritableFont(

Software Testing-16IT711 Page 38


SOFTWARE TESTING LABORATORY

WritableFont.TIMES, 10, WritableFont.BOLD, false,


UnderlineStyle.SINGLE);
timesBoldUnderline = new WritableCellFormat(times10ptBoldUnderline);
// Lets automatically wrap the cells
timesBoldUnderline.setWrap(true);
CellView cv = new CellView();
cv.setFormat(times);
cv.setFormat(timesBoldUnderline);
// Write a few headers
addCaption(sheet, 0, 0, "Student Name");

addCaption(sheet, 1, 0, "Subject 1");


addCaption(sheet, 2, 0, "subject 2");
addCaption(sheet, 3, 0, "subject 3");
}
private void createContent(WritableSheet sheet) throws WriteException,
RowsExceededException {
// Write a few number
for (int i = 1; i < 10; i++) {
// First column
addLabel(sheet, 0, i, "Student " + i);
// Second column
addNumber(sheet, 1, i, ((i*i)+100));
addNumber(sheet, 2, i, ((i*i)+4));
addNumber(sheet, 3, i, ((i*i)+3));
}
}
private void addCaption(WritableSheet sheet, int column, int row, String s)

Software Testing-16IT711 Page 39


SOFTWARE TESTING LABORATORY

throws RowsExceededException, WriteException


{ Label label;
label = new Label(column, row, s, timesBoldUnderline);
sheet.addCell(label);
}
private void addNumber(WritableSheet sheet, int column, int row,
Integer integer) throws WriteException, RowsExceededException
{ Number number;
number = new Number(column, row, integer, times);
sheet.addCell(number);
}
private void addLabel(WritableSheet sheet, int column, int row, String s)
throws WriteException, RowsExceededException {
Label label;
label = new Label(column, row, s, times);
sheet.addCell(label);
}
//
private void checkExcelSheet() throws Exception
// second code
{
FileInputStream fi = new FileInputStream("D:\\exp6.xls");
Workbook w = Workbook.getWorkbook(fi);
Sheet s = w.getSheet(0);
String a[][] = new String[s.getRows()][s.getColumns()];
FileOutputStream fo = new FileOutputStream("D://exp7Result.xls");
WritableWorkbook wwb = Workbook.createWorkbook(fo);

Software Testing-16IT711 Page 40


SOFTWARE TESTING LABORATORY

WritableSheet ws = wwb.createSheet("result", 0);


int c=0;
for (int i = 0; i < s.getRows(); i++)
{ for (int j = 0; j < s.getColumns(); j+
+)
{
if(i >= 1)
{
String b= new String();
b=s.getCell(3,i).getContents();
int x= Integer.parseInt(b);
if( x < 35)
{ c+
+;
break;
}
}

a[i][j] = s.getCell(j, i).getContents();


Label l2 = new Label(j, i-c, a[i][j]);
ws.addCell(l2);
}
}
wwb.write();

wwb.close();
}
}

Software Testing-16IT711 Page 41


SOFTWARE TESTING LABORATORY
}

Software Testing-16IT711 Page 42


SOFTWARE TESTING LABORATORY

Output:

Result:

Thus the program to select the number of students who have scored more
than 60 in any one subject ( or all subjects ) created successfully.

Software Testing-16IT711 Page 43


SOFTWARE TESTING LABORATORY

Exercise Number: 8
Date:

Title of the Exercise:

WRITE AND TEST A PROGRAM TO PROVIDE TOTAL NUMBER OF OBJECTS


PRESENT / AVAILABLE ON THE PAGE

Aim:

To test a program to provide total number of objects present / available on the page.

Procedure:

 Write this java test code in eclipse.

 Before running this code makes sure your selenium RC server is running.
Program:

package Selenium;
import com.thoughtworks.selenium.*;
import org.openqa.selenium.server.*;
import org.testng.annotations.*;
public class Ex8 {
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://www.google.co.in/");
selenium.windowMaximize();

Software Testing-16IT711 Page 44


SOFTWARE TESTING LABORATORY

String lc[]=selenium.getAllLinks();
System.out.println("TOTAL NO OF LINKS="+lc.length);
String bc[]=selenium.getAllButtons();
System.out.println("TOTAL NO OF BUTTONS="+bc.length);
String fc[]=selenium.getAllFields();
System.out.println("TOTAL NO OF INPUT FIELDS="+fc.length);
}
@AfterClass
public void tearDown() throws Exception {
selenium.stop();
seleniumserver.stop();
}
}

Output:

Result:

Thus a program to provide total number of objects present / available on the page
executed successfully.

Software Testing-16IT711 Page 45


SOFTWARE TESTING LABORATORY

Exercise Number: 9
Date:

Title of the Exercise:

WRITE AND TEST A PROGRAM TO GET THE NUMBER OF LIST ITEMS IN A

LIST / COMBO BOX


Aim:

To test a program to get the number of list items in a list / combo box.

Procedure:

 Write this java test code in eclipse.


 Before running this code makes sure your selenium RC server is running.

 Write Combocount.html as shown below and save under desktop.

<html>
<body>
<select>
<option>Volvo</option>
<option>Express</option>
<option>Mercedes</option>
<option>RajaHamsa</option>
</select>
</body>
</html>

Write the below code in eclipse


Make sure that the path of your combocount.html file must be correct for
your system , in this example it is
file:///C:/Users/Savitha/Desktop/combocount.html
Program:

import com.thoughtworks.selenium.*;

Software Testing-16IT711 Page 46


SOFTWARE TESTING LABORATORY

public class Comboitemcount extends SeleneseTestCase {

public void setUp() throws Exception {


selenium = new DefaultSelenium("localhost", 4444, "*chrome",
"file:///C:/Users/Savitha/Desktop/combocount.html");
selenium.start();}

public void
testloginlogout()
{ selenium.setSpeed("1
00 0");
selenium.open("file:///C:/Users/Savitha/Desktop/combocoun
t.html");
selenium.waitForPageToLoad("30000");
selenium.windowMaximize();
String[] selectelements = new
String[1000]; selectelements=
selenium.getSelectOptions("//select");
System.out.println("The number of options present in the given select box is "

+selectelements.length);

}
}

Software Testing-16IT711 Page 47


SOFTWARE TESTING LABORATORY

Output:

Result:

Thus the program to get the number of list items in a list / combo box executed
successfully.

Software Testing-16IT711 Page 48


SOFTWARE TESTING LABORATORY

Software Testing-16IT711 Page 49


SOFTWARE TESTING LABORATORY

Exercise Number: 10
Date:

Title of the Exercise:

WRITE AND TEST A PROGRAM TO COUNT NUMBER OF CHECK BOXES ON THE


PAGE CHECKED AND UNCHECKED COUNT.

Aim:

To test a program to count number of items present on a desktop.

Procedure:

 Write this java test code in eclipse. Since we are not using any Selenium object
here no need of selenium RC Server to run at the command prompt for this
program.
 Write a visual basic script with the following code and save as count.vbs
under the desktop.

Set fso = createobject("Scripting.FileSystemObject")


DesktopPath =
CreateObject("WScript.Shell").SpecialFolders("Desktop") 'Files
count

msgbox "The number of files on the desktop is : "


&fso.GetFolder(DesktopPath).Files.Count 'Folders count
msgbox "The number of folders on the desktop is : "
&fso.GetFolder(DesktopPath).SubFolders.Count
Program:

In eclipse write the following


code import java.io.*;

public class

desktop_item_count {

Software Testing-16IT711 Page 50


SOFTWARE TESTING LABORATORY

public static void

main(String[] args) { try

Runtime.getRuntime().exec("wscript C:/Users/Savitha/Desktop/count.vbs");

} catch

(IOException e) {

System.exit(0);

Note: Don’t forget to change the desktop file path for your system

Output:

Software Testing-16IT711 Page 51


SOFTWARE TESTING LABORATORY

Result:

Thus a program to count number of check boxes on the page checked and unchecked
count executed successfully.

Software Testing-16IT711 Page 52

You might also like