0% found this document useful (0 votes)
33 views54 pages

Stqa

Jjjjjj

Uploaded by

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

Stqa

Jjjjjj

Uploaded by

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

INDEX

Sr.No Date Practical Page.No Signature

1 04/08/2024 Install Selenium IDE and create a test suite containing a 2


minimum of 4 test cases for different web page formats
(e.g., HTML, XML, JSON, etc.).

2 11/08/2024 Conduct a test suite for two different websites using 13


Selenium IDE. Perform various actions like clicking links,
filling forms, and verifying content.

3 18/08/2024 Install Selenium Server (Selenium RC) and demonstrate its 24


usage by executing a script in Java or PHP to automate
browser actions.

4 25/08/2024 Write a program using Selenium WebDriver to update 10 31


student records in an Excel file. Perform data
manipulation and verification.

5 01/09/2024 Write a program using Selenium WebDriver to select the 36


number of students who have scored more than 60 in any
one subject (or all subjects). Perform data extraction and
analysis.

6 15/09/2024 Write a program using Selenium WebDriver to provide the 41


total number of objects present or available on a web
page. Perform object identification and counting.

7 22/09/2024 Write a program using Selenium WebDriver to get the 45


number of items in a list or combo box on a web page.
Perform element identification and counting.

8 29/09/2024 Write a program using Selenium WebDriver to count the 50


number of checkboxes on a web page, including checked
and unchecked counts. Perform checkbox identification
and counting.

Page 1 of 54
Practical No.1
AIM: Install Selenium IDE. Write a test suite containing minimum 4 test cases for
different formats.

REQUIREMENTS:

1) Selenium IDE extension on a browser.


2) NetBeans IDE for creating test case files.
STEPS:

1. Just google “Selenium IDE chrome” and click on the first link. Add the “Selenium IDE” extension
to your browser.
Before:

After:

2. For testing, we need a test case suite, so we create a WebApplication in NetBeans IDE. File >
New Project > Java Web > Web Application > Add GlassFish Server > Finish.
Open NetBeans IDE:

Page 2 of 54
3. In index.html, we write the following code for “ARITHMETIC OPERATIONS” test case suite. : ---
index.html---

<html>

Page 3 of 54
<head>
<title>prac1</title> <script
language="javascript">
function addition()
{
var num1=parseInt(document.arithmetic.n1.value);
var num2=parseInt(document.arithmetic.n2.value);
var result=num1+num2;
document.arithmetic.res.value=result;
}
function subtraction()
{
var num1=parseInt(document.arithmetic.n1.value);
var num2=parseInt(document.arithmetic.n2.value);
var result=num1-num2;
document.arithmetic.res.value=result;

}
function multiplication()
{
var num1=parseInt(document.arithmetic.n1.value);
var num2=parseInt(document.arithmetic.n2.value);
var result=num1*num2;
document.arithmetic.res.value=result;

}
function division()
{
var num1=parseInt(document.arithmetic.n1.value);
var num2=parseInt(document.arithmetic.n2.value);

Page 4 of 54
var result=num1/num2;
document.arithmetic.res.value=result;

}
</script>
</head>
<body>
<h1 align="center"> Arithmetic Operations </h1>
<form name="arithmetic">
<table border="1" align="center">
<tr>
<td>Number 1: </td>
<td><input type="text" name="n1" size="20"></td>
</tr>
<tr>
<td>Number 2: </td>
<td><input type="text" name="n2" size="20"></td>
</tr>
<tr>
<td colspan=2>
<input type="button" name="add" value="Add"
onclick="javascript:addition();">&nbsp; <input
type="button" name="sub" value="Subtract"
onclick="javascript:subtraction();">&nbsp; <input
type="button" name="mul" value="Multiply"
onclick="javascript:multiplication();">&nbsp; <input
type="button" name="div" value="Divide"
onclick="javascript:division();">&nbsp;

</td>
</tr>

Page 5 of 54
<tr>
<td colspan="2">Result is: <input type="text" name="res" size="20" value=""></td>

</tr>
</table>
</form> </body> </html>

4. Create a JSP file “newjsp.jsp” with a link to the created HTML file.

---newjsp.jsp---

<%@page contentType="text/html" pageEncoding="UTF-8"%>


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title> </head>
<body align="center">
<h1>Test Cases IDE</h1>
<a href="index.html">Arithmetic Operations</a>
</body>
</html>

5. Run this JSP file from NetBeans. It will then open up in a browser.
• Then from the browser, open the link:

• Now you’ll be redirected to ARITHMETIC OPERATIONS page. Copy the URL of the page and
open “Selenium IDE” extension:

Page 6 of 54
• Now, Record a new test project in a new project:

Name your project:

• Paste that URL here and click Start Recording:

Page 7 of 54
• A new tab will be opened, maximize that tab(this tab is your working tab for recording):

Click on Stop Recording:

• Just as you stop your recording, you’ll be prompted to Name your new test, we’ll make our new
test as “addition”:

Page 8 of 54
• Save the project after each test case:

A file with “.SIDE” extension will be saved(SIDE abbrev to Selenium IDE):

• Now, we’ll Start Recording our first test case(addition):

• We’ll be prompted to paste our URL again, then click Start Recording:

Page 9 of 54
NOTE THAT each clicks are recorded:

• Now we’ll Stop Recording from the Selenium IDE:

• We’ll then Run current test(addition):

Page 10 of 54
The test will run automatically and the completion will be shown step-by-step in the “Log” area in
the Selenium IDE:

• NOTE1: You can set the “Test execution speed” too:

Page 11 of 54
NOTE2: Paste here the URL again if the Glassfish Error occurs between any test case:

• Now create 3 more test cases(subtraction, multiplication, division). Then “Run all tests”:

6) Finish!

Page 12 of 54
Practical No. 2
AIM: Conduct a test case suite for any two websites.
REQUIREMENTS:

1) Selenium IDE extension on a browser. 2) A stable Internet connection.


STEPS:

1) Just google “Selenium IDE chrome” and click on the first link. Add the “Selenium IDE”
extension to your browser.

2) For testing, we’ll choose “redbus.in” as our first website. We’ll be checking some Selenium
IDE Commands like assertText, verifyTitle, storeText, echo. Detailed info for several
commands are provided at https://ui.vision/docs/selenium-ide .
• Now google “redbus.in”.
• Copy the URL of the page and open “Selenium IDE” extension:

• Now, Record a new test project in a new project:

Page 13 of 54
Name your project:

Paste that URL here and click Start Recording:

A new tab will be opened, maximize that tab(this tab is your working tab for recording):

Now we’ll make use of command verifyTitle.

Page 14 of 54
(This command verifies title of the webpage and keeps on continuing the execution.) So anywhere on the
window, do Right Click> select Selenium IDE> click Verify Title:

• NOTE THAT each command is recorded:

• Now after filling the FROM, TO, ONWARD DATE, we proceed further by clicking on
Search Buses: (make sure not to do unnecessary clicks)

Now we’ll make use of command assertText.


(This command verifies particular text of the webpage and stops the execution of testcase if the match
isn’t found.)

Page 15 of 54
So, we want to make sure that our automated testcase chooses our specified Bus Provider.

To ensure that, here we do Right Click over the name of the bus provider> then select Selenium IDE>
and click Assert Text :

• Now we’ll make use of command storeText.


(This command stores the text value of page element into variable for future use.) Here we’ll store the
price of the ticket.

So, we select the price> right click over it> select Selenium IDE> click Store Text :

Now Selenium IDE will prompt us to enter the name of the variable in which that price will be stored:

Page 16 of 54
• After clicking OK, we can see how the target is automatically mapped to the css attribute of the
page, and our price is stored:

• Now we’ll make use of command echo.


(This command is used to display text(comments, notes) and/or the stored value of variables in the log
area of Selenium IDE.)

Here we’ll display the price of the ticket from the variable “price” that we stored during the use of
storeText command.

To create a new command, we just click onto the next line and enter our command:

Page 17 of 54
• We’ll enter the Command name echo, & Value ${price} because we are calling the created variable
“price” from this command:

• Now we’ll Run current test:

Page 18 of 54
• After the test gets completed, we can see the echo: 350 in the log area of Selenium IDE showing
the output of the echo command.
Here we can see how 350 is stored in variable “price” by storeText command, and that variable is then
called by the echo command, which is then returning the value fetched from the page:

3) Finish!

(Assert and verify commands are both useful for verifying condition match or not. The difference is that
verify command will verify the condition and if it’s not match, it will give error message in Log area and
the macro(testcase) continues to run. With the assert command, if the condition does not match then
it will stop remaining macro(testcase) execution in the selenium IDE software testing tool.)

Page 19 of 54
Practical 2 – Part ||
Conduct a test suite for two different websites using Selenium IDE. Perform various
actions like clicking links, filling forms, and verifying content.

A:

- Open the Selenium IDE


- Open Existing or New Project
- Set the Base Url or Playback Base Url to “https://www.programiz.com/”

- Now add this following commands in the test case:


1. Open “/rust” directory

2. Verify Title “Learn Rust Programming - For Beginners”

Page 20 of 54
3. Click event to a target specified “linkText = Rust Hello World”

4. Close event to exit the window

B:

- Change Playback Base Url to “https://practicetestautomation.com/”

Page 21 of 54
- Add this following commands in test:

1. Open “practice-test-login” directory

2. Type Target: id = username , Value: student


3. Type Target: id = password , Value: Password123

4. Click Target: id = submit

5. Verify Text Target: css = .post-title , Value: Logged In Successfully

6. Click Target: linkText = Log out

Page 22 of 54
7. Close

Result:

Page 23 of 54
Practical No. 3
AIM: Install Selenium Server(Selenium RC) and demonstrate it using a script in Java.
PRE-REQUISITES(*here are w.r.t. Windows 10(64 bit), so choose accordingly w.r.t. your specs):

1) To Download “JDK”:
• Visit
https://www.oracle.com/technetwork/java/javase/downloads/jdk12downloads52
95953.html

• Download this file “jdk-12.0.2_windows-x64_bin.exe” and install it.


2) To Download “Eclipse IDE”:
• Visit
https://www.eclipse.org/downloads/download.php?file=/oomph/epp/201906/R
/ecli pse-inst-win64.exe
• Click “Download”.
• Installation:
Open application.(click OK if errors occur like “could not find java.dll” & “could
not find Java Runtime Environment SE”)
Select “Eclipse IDE for Java Developers”.
It will automatically locate the JDK. Choose path, and click “Install”.
• After installation, click “Launch” or open Eclipse from START menu in Windows.
3) To Download “Selenium Server Driver and Client Driver(JAR files)”:
a) For “Selenium Server Driver”:

• Visit https://www.seleniumhq.org/download/
• Under section “Selenium Standalone Server”, click download version “3.141.59”
• You’ll get the executable jar file(selenium-server-standalone-3.141.59)
b) For “Selenium Client Driver”:

• Visit https://www.seleniumhq.org/download/
• Under section “Selenium Client & WebDriver Language Bindings”, download the
“3.141.59” version of Java.

• Extract the file and you’ll see two jar files. From them, we’ll be using this
executable jar file(client-combined-3.141.59) 4) To Download “Gecko Driver”:

• Visit https://github.com/mozilla/geckodriver/releases
• Under section “Assets”, download “geckodriver-v0.24.0-win64.zip” file.
• You’ll get the application file “geckodriver”.

STEPS: 1) Open Eclipse. Select your workspace directory. Click Launch:

Page 24 of 54
2) Create a Project(File > New > Java Project ):

3) Name the project as “gcd” > click Finish > click Don’t Create module:

4) Close the “Welcome” tab.


5) Create a Package(right-click on Project Name > New > Package > Name it > Finish):

Page 25 of 54
6) Create a Class(right-click on Project Name > New > Class > Name it > Finish):

Page 26 of 54
7) Adding “Selenium Server Driver and Client Driver(JAR files)” in Eclipse IDE: • right-click
on Project Name > Build Path > Configure Build Path…

• now go under:Java Build Path > Libraries > Classpathclick


> Add External JARs…

• Browse and add JAR files > click Apply and Close :

Page 27 of 54
8) Creating a link for HTML file(wherein calculation part is present):
(NOTE that this file will be run by the ‘script in JAVA’(which we’ll create later)) •
Create a Notepad file with the following code and save it as “gcdhtml.html”:
---(gcdhtml.html)---
<html>
<head>

<script type="text/javascript"> function


gcd()
{
var x,y;
x=parseInt(document.myform.n1.value);
y=parseInt(document.myform.n2.value);
while(x!=y)
{
if(x>y){x=x-y;}
else{y=y-x;}
}
document.myform.result.value=x;
}
</script>
</head>

<body>
<center>
<h1>---Program to calculate GCD of two numbers---</h1>
<hr color="red">
<form name="myform">
Enter Number 1: <input type="text" name="n1" value=""> <br> <br>
Enter Number 2: <input type="text" name="n2" value=""> <br> <br>
<input type="button" name="btn" value="Get GCD" onClick="gcd()"><br><br>
GCD: <input type="text" name="result" value="">
</form>
</center>
</body>

</html>

• Close the file. Then right-click > Open with > Firefox Browser

Page 28 of 54
• Copy URL from the webpage:

9) Creating the script in JAVA :


(NOTE that this script will be run by Eclipse IDE)
(In simple words, it’s like we are
-ordering Eclipse to run a script or to do a job
-of opening the HTML file
-and putting the values in the textboxes with the help of Selenium Drivers -and to
show the result.
-Hence automating the work in browser)

• Now we’ll put the path of “geckodriver” in a String driverPath


• And we’ll paste the copied URL in the .get() method of the WebDriver class
---(Test.java)---
package gcdpackage;
import org.openqa.selenium.By; import
org.openqa.selenium.WebDriver; import
org.openqa.selenium.firefox.FirefoxDriver; import
org.openqa.selenium.firefox.FirefoxOptions; import
org.openqa.selenium.firefox.FirefoxProfile;

public class Test {


static String driverPath = "C:\\Users\\Usman\\503\\geckodriver.exe";
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver",driverPath);
//DesiredCapabilities capabilities = DesiredCapabilities.firefox();
//capabilities.setCapability("marionette",true);
//ProfilesIni allProfiles = new ProfilesIni();
FirefoxProfile fp = new FirefoxProfile();
fp.setPreference(FirefoxProfile.PORT_PREFERENCE,"7055");
FirefoxOptions options = new FirefoxOptions();
options.setProfile(fp);
WebDriver driver=new FirefoxDriver(options);//(capabilities);
//WebDriver driver=new ChromeDriver();
driver.get("file:///D:/Usman/College/503%20pracs/gcdhtml.html");
driver.manage().window().maximize();
driver.findElement(By.name("n1")).sendKeys("36");
driver.findElement(By.name("n2")).sendKeys("6");
driver.findElement(By.name("btn")).click();
String
result=driver.findElement(By.name("result")).getAttribute("name=result");

Page 29 of 54
System.out.println("GCD="+result);
}
}
10) Run the file from Eclipse IDE:
• OUTPUT:

11) Finish!

What is Gecko Driver:

-a WebBrowser engine inbuilt within Mozilla Firefox browser.

-acts as a proxy between Web Driver enabled clients(Eclipse, NetBeans, etc.) and Mozilla Firefox
browser or simply acts as a link between Selenium Web Driver tests and Mozilla Firefox browser.

What is Selenium Remote Control (RC):

-a test tool that allows you to write automated web application UI tests in any programming
language against any HTTP website using any mainstream JavaScript-enabled browser.

-Selenium RC comes in two parts.

1) A server which automatically launches and kills browsers, and acts as a HTTP proxy for web
requests from them.
2) Client libraries for your favourite computer language.

Page 30 of 54
PRACTICAL No. 4
AIM: Write a program using Selenium WebDriver to update 10 student records in an
Excel file. Perform data manipulation and verification.
PRE-REQUISITES:

1) Check that you have Eclipse IDE. 2)


To Download “JXL.JAR”:

• Visit http://www.java2s.com/Code/Jar/j/Downloadjxl26jar.htm
• Download this file: “jxl/jxl-2.6.jar.zip( 603 k)” and extract it.(you’ll get the .jar file)
STEPS:

1) Open Eclipse. Select your workspace directory. Click Launch:

2) Create a Project(File > New > Java Project


):

Page 31 of 54
3) Name the project as “p5” > click Finish > click Don’t Create module:

4) Close the “Welcome” tab.


5) Create a Package(right-click on Project Name > New > Package > Name it > Finish):

6) Create a Class(right-click on Project Name > New > Class > Name it > Finish):

Page 32 of 54
7) Adding “JXL(JAR file)” in Eclipse IDE: right-click on Project Name > Build Path > Configure
Build Path…

• now go under:Java Build Path > Libraries > Classpathclick


> Add External JARs…

• Browse andadd JAR file >click Apply and Close :

Page 33 of 54
8) Creating the script in JAVA:
(NOTE that this script will be run by Eclipse IDE)
(In simple words, it’s like we are
-ordering Eclipse to run a script or to do a job
-of creating and opening .xls file
-and putting the values in the cells with the help of jxl.jar -and to
show the result.
-Hence automating the work in a local system(PC)).

---(Excelwriter.java)---
package excelwrite;

import jxl.*; //used for WorkbookSettings,Workbook import jxl.write.*; //used for


WriteException,WritableWorkbook,WritableSheet,Label
import jxl.write.Number; //used for Number import java.io.*; //used for
IOException,File import java.util.Locale; //used for Locale

public class Excelwriter {


public static void main(String[] args) throws IOException,WriteException {
// TODO Auto-generated method stub
int r=0,c=0;
String header[]={"Student
Name","Subject1","Subject2","Subject3","Total"};
String
sname[]={"Carls","James","Paul","Philip","Smith","Thomson","Rhodey","Stark","Gary"
,"AnneMarie"};
int marks[]={50,45,60,55,70,45,67,78,89,90,30};

File file = new File("student.xls");


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);
//creating header row for(r=0;r<1;r++)
{
for(c=0;c<header.length;c++) {
Label l=new Label(c,r,header[c]);
excelSheet.addCell(l);
}
}
//filling name in column1
for(r=1;r<=sname.length;r++) {
for(c=0;c<1;c++) {
Label l=new Label(c,r,sname[r-1]);
excelSheet.addCell(l);
}
}
//filling name in column2,3,4
for(r=1;r<=sname.length;r++) {
for(c=1;c<4;c++) {

Page 34 of 54
Number num = new Number(c, r, marks[r-1]);
excelSheet.addCell(num);
}
}
//filling name in total
for(r=1;r<=sname.length;r++) {
for(c=4;c<5;c++) {
int total=marks[r-1]+marks[r-1]+marks[r-1];
Number num = new Number(c, r, total); excelSheet.addCell(num);
}
}
workbook.write();
workbook.close();
System.out.println("Excel File Created!!!!!");
}
}
9) Run the file from Eclipse IDE:
• OUTPUT:

10) Finish!

What is JXL.JAR:

-Java libraries are distributed as a “.jar” file

-jxl.jar is the library of JExcelApi , which is open source java API to read, write, and modify Excel
spreadsheets dynamically. It contains all the compiled *.class files, associated metadata and
resources that are used by the Java Excel API internally .

Page 35 of 54
PRACTICAL No. 5
AIM: Write and test a program to select the number of students from Excel file(table) who
have scored 60 or more in any one subject(or all subjects). Perform data extraction and
analysis.
PRE-REQUISITES:

1) Check that you have Eclipse IDE.


2) Check that you have JXL.JAR.
3) Check that you’ve the Excel file(“student.xls”) that we’ll be working on for reading data.

STEPS:

1) Open Eclipse. Select your workspace directory. Click Launch:

2) Create a Project(File > New > Java Project):

Page 36 of 54
3) Name the project as “p6” > click Finish > click Don’t Create module:

4) Close the “Welcome” tab.


5) Create a Package(right-click on Project Name > New > Package > Name it > Finish):

6) Create a Class(right-click on Project Name > New > Class > Name it > Finish):

Page 37 of 54
7) Adding “JXL(JAR file)” in Eclipse IDE:

• right-click on Project Name > Build Path > Configure Build Path…

• now go under: Java Build Path > Libraries > Classpath > click Add External JARs…

• Browse and add JAR file > click Apply and Close :

8) Creating the script in JAVA:

Page 38 of 54
(NOTE that this script will be run by Eclipse IDE)
(In simple words, it’s like we are
-ordering Eclipse to run a script or to do a job
-of opening .xls file
-and fetching the marks count>=60 from the cells with the help of jxl.jar
and to show the result.
-Hence automating the work in a local system(PC)).

---(Excelreader.java)---
package excelread;
import java.io.File;
import
java.io.IOException;
import jxl.Cell; import
jxl.CellType; import
jxl.Sheet; import
jxl.Workbook;
import jxl.read.biff.BiffException;
public class Excelreader {
private String inputFile;
public void setInputFile(String inputFile) {this.inputFile = inputFile;}

public void read() throws IOException {


File inputWorkbook = new File(inputFile);
Workbook w;
boolean flag=false;
int count=0;
try {
w = Workbook.getWorkbook(inputWorkbook);
// Get the first sheet
Sheet sheet = w.getSheet(0);
// Loop over first 10 column and lines
for (int j = 0; j < sheet.getRows(); j++) {
for (int i = 0; i < sheet.getColumns()-1; i++) {
Cell cell = sheet.getCell(i, j);
if (cell.getType() == CellType.NUMBER) {

if(Integer.parseInt(cell.getContents())>=60){
flag = true;
if(flag == true){
count++;
flag=false;
}
break;
}
}
}
}
System.out.println("Total number of students who scored more
than 60 in one or more subjects: " +count);
}
catch (BiffException e) {e.printStackTrace();}

Page 39 of 54
}

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


Excelreader test = new Excelreader();

test.setInputFile("C:\\Users\\Usman\\eclipseworkspace\\p5\\student.xls");
test.read();
}
}

9) Run the file from Eclipse IDE:


• OUTPUT:

10) Finish!

Page 40 of 54
PRACTICAL No. 6
AIM: Write and test a program to provide total number of objects present/available on
webpage. Perform element identification and counting.

PRE-REQUISITES:
1) Check that you have JDK.
2) Check that you have Eclipse IDE.
3) Check that you have Selenium Server Driver and Client Driver(JAR files).
4) Check that you have Gecko Driver.
5) Check that you have a stable Internet connection.

STEPS:

1) Open Eclipse. Select your workspace directory. Click Launch:

2) Create a Project(File > New > Java Project ):

Page 41 of 54
3) Name the project as “p8” > click Finish > click Don’t Create module:

4) Close the “Welcome” tab.


5) Create a Package(right-click on Project Name > New > Package > Name it > Finish):

6) Create a Class(right-click on Project Name > New > Class > Name it > Finish):

Page 42 of 54
7) Adding “Selenium Server Driver and Client Driver(JAR files)” in Eclipse IDE: • right-click
on Project Name > Build Path > Configure Build Path…

• now go under: Java Build Path > Libraries > Classpath > click Add External JARs…

• Browse and add JAR files > click Apply and Close :

8) Creating the script in JAVA:


(NOTE that this script will be run by Eclipse IDE)

Page 43 of 54
(In simple words, it’s like we are
-ordering Eclipse to run a script or to do a job
-of opening the browser, visiting the URL
-and finding the <a> tag WebElements with the help of Selenium Drivers
and to show the result.
-Hence automating the work in browser)

• Now we’ll put the path of “geckodriver” in a String driverPath


---(FindAllLinks.java)---
package p8;
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement; import
org.openqa.selenium.firefox.FirefoxDriver; import
org.openqa.selenium.firefox.*; import
org.openqa.selenium.firefox.FirefoxOptions; import
org.openqa.selenium.firefox.FirefoxProfile; import
org.openqa.selenium.firefox.internal.ProfilesIni;

public class FindAllLinks {


static String driverPath = "C:\\Users\\Usman\\503\\geckodriver.exe";
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver",driverPath);
//NOTE THAT: following commented lines are required for old machines
//DesiredCapabilities capabilities = DesiredCapabilities.firefox();
//capabilities.setCapability("marionette",true);
//ProfilesIni allProfiles = new ProfilesIni();
//FirefoxProfile fp = new FirefoxProfile();
//fp.setPreference(FirefoxProfile.PORT_PREFERENCE,"7055");
//FirefoxOptions options = new FirefoxOptions();
//options.setProfile(fp);
WebDriver driver = new FirefoxDriver();
String appUrl ="https://www.google.co.in/";
driver.get(appUrl);

java.util.List<WebElement> links =
driver.findElements(By.tagName("a"));

for (int i = 1; i<links.size(); i=i+1)


{
System.out.println(links.get(i).getText());
}
System.out.println("Total No. of Links: "+links.size());
//driver.quit();
}
}
9) Run the file from Eclipse IDE: OUTPUT:

Page 44 of 54
PRACTICAL No. 7
AIM: Write and test a program using Selenium WebDriver to get the number of items in
a list/combo box. Perform element identification and counting.
PRE-REQUISITES:

1) Check that you have JDK.


2) Check that you have Eclipse IDE.
3) Check that you have Selenium Server Driver and Client Driver(JAR files).
4) Check that you have Gecko Driver.
5) Check that you have a stable Internet connection.

STEPS:

1) Open Eclipse. Select your workspace directory. Click Launch:

2) Create a Project(File > New > Java Project):

Page 45 of 54
3) Name the project as “p9” > click Finish > click Don’t Create module:

4) Close the “Welcome” tab.


5) Create a Package(right-click on Project Name > New > Package > Name it > Finish):

6) Create a Class(right-click on Project Name > New > Class > Name it > Finish):

7) Adding “Selenium Server Driver and Client Driver(JAR files)” in Eclipse IDE: • right-click
on Project Name > Build Path > Configure Build Path…

Page 46 of 54
• now go under: Java Build Path > Libraries > Classpath > click Add External JARs…

• Browse and add JAR files > click Apply and Close :

8) Create HTML file in a notepad > Save it > Open it in browser > Copy the URL:
(NOTE THAT: as this is our LOCAL FILE, this file will be opened thru STATIC URL in script)
---(combobox.html)---

Page 47 of 54
<select id="continents">
<option value="Asia">Asia</option>
<option value="Europe">Europe</option>
<option value="Africa">Africa</option>
</select>

9) Creating the script in JAVA:


(NOTE that this script will be run by Eclipse IDE)
(In simple words, it’s like we are
-ordering Eclipse to run a script or to do a job
-of opening the browser, visiting the URL
-and finding the WebElement By “ID” with the help of “Select” class and Selenium Drivers
and to show the result.
-Hence automating the work in browser)

• Now we’ll put the path of our LOCAL FILE(combobox.html) in a string •


Also put the path of “geckodriver” in a String driverPath
---(ComboBox.java)---
package p9;
import java.util.List; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver; import
org.openqa.selenium.firefox.*; import
org.openqa.selenium.firefox.FirefoxOptions; import
org.openqa.selenium.firefox.FirefoxProfile; import
org.openqa.selenium.firefox.internal.ProfilesIni; import
org.openqa.selenium.support.ui.Select;

public class ComboBox { static String driverPath =


"C:\\Users\\Usman\\503\\geckodriver.exe";
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver",driverPath);
//NOTE THAT: following commented lines are required for old machines
//DesiredCapabilities capabilities = DesiredCapabilities.firefox();
//capabilities.setCapability("marionette",true);
//ProfilesIni allProfiles = new ProfilesIni();
//FirefoxProfile fp = new FirefoxProfile();
//fp.setPreference(FirefoxProfile.PORT_PREFERENCE,"7055");
//FirefoxOptions options = new FirefoxOptions();
//options.setProfile(fp);
WebDriver driver = new FirefoxDriver();
String appUrl ="https://www.toolsqa.com/automation-practice-form/"; //DYNAMIC
URL(WEBSITE)
//String appUrl = "

Page 48 of 54
file:///D:/Usman/College/503%20pracs/combobox.html"; //STATIC URL(LOCAL FILE)
driver.get(appUrl);

Select oSelect = new


Select(driver.findElement(By.id("continents")));
//Select oSelect = new
Select(driver.findElement(By.tagName("select"))); //this works too

List<WebElement> oSize = oSelect.getOptions();


int iListSize = oSize.size();

for(int i =0; i < iListSize ; i++)


{
// Storing the value of the option
String sValue = oSelect.getOptions().get(i).getText();
// Printing the stored value
System.out.println(sValue);
}
System.out.println("Total No. Items in Dropdown: "+iListSize);
//driver.quit();
}
}
10) Run the file from Eclipse IDE:

• OUTPUT:

Figure 1: by using the DYNAMIC URL(website)

Figure 2: by using the STATIC URL(local file)

Page 49 of 54
PRACTICAL No. 8
AIM: Write and test a program to count the number of check boxes on the page,
including checked and unchecked counts. Perform checkbox
identification and counting.

PRE-REQUISITES:

1) Check that you have JDK.


2) Check that you have Eclipse IDE.
3) Check that you have Selenium Server Driver and Client Driver(JAR files).
4) Check that you have Gecko Driver.
5) Check that you have a stable Internet connection.

STEPS:

1) Open Eclipse. Select your workspace directory. Click Launch:

2) Create a Project(File > New > Java Project):

Page 50 of 54
3) Name the project as “p10” > click Finish > click Don’t Create module:

4) Close the “Welcome” tab.


5) Create a Package(right-click on Project Name > New > Package > Name it > Finish):

6) Create a Class(right-click on Project Name > New > Class > Name it > Finish):

Page 51 of 54
7) Adding “Selenium Server Driver and Client Driver(JAR files)” in Eclipse IDE: • right-click
on Project Name > Build Path > Configure Build Path…

• now go under: Java Build Path > Libraries > Classpath > click Add External JARs…

• Browse and add JAR files > click Apply and Close :

8) Create HTML file in a notepad > Save it > Open it in browser > Copy the URL:

Page 52 of 54
(NOTE THAT: as this is our LOCAL FILE, this file will be opened thru STATIC URL in script)

---(multicheckbox.html)---
<input type="checkbox" value="A">A<br>
<input type="checkbox" value="B" CHECKED>B<br>
<input type="checkbox" value="C">C<br>
<input type="checkbox" value="D" CHECKED>D<br>
<input type="checkbox" value="E">E<br>

9) Creating the script in JAVA:


(NOTE that this script will be run by Eclipse IDE)
(In simple words, it’s like we are
-ordering Eclipse to run a script or to do a job
-of opening the browser, visiting the URL
-and finding the WebElement By “xpath” with the help of Selenium Drivers
-and to show the result.
-Hence automating the work in browser)

• Now we’ll put the path of our LOCAL FILE(multicheckbox.html) in a string •


Also put the path of “geckodriver” in a String driverPath
---(MultiCheckBox.java)---
package p10; import
java.util.List; import
org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import
org.openqa.selenium.WebElement; import
org.openqa.selenium.firefox.FirefoxDriver; import
org.openqa.selenium.firefox.*; import
org.openqa.selenium.firefox.FirefoxOptions; import
org.openqa.selenium.firefox.FirefoxProfile; import
org.openqa.selenium.firefox.internal.ProfilesIni;

public class MultiCheckBox {


static String driverPath = "C:\\Users\\Usman\\503\\geckodriver.exe";
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver",driverPath);
//DesiredCapabilities capabilities = DesiredCapabilities.firefox();
//capabilities.setCapability("marionette",true);
//ProfilesIni allProfiles = new ProfilesIni();
//FirefoxProfile fp = new FirefoxProfile();
//fp.setPreference(FirefoxProfile.PORT_PREFERENCE,"7055");
//FirefoxOptions options = new FirefoxOptions();
//options.setProfile(fp);
WebDriver driver = new FirefoxDriver();

Page 53 of 54
String appUrl = "https://www.toolsqa.com/automation-practice-form/";
//DYNAMIC URL(WEBSITE)
//String appUrl =
"file:///D:/Usman/College/503%20pracs/multicheckbox.html"; //STATIC URL(LOCAL
FILE)
driver.get(appUrl);

List<WebElement> checkBoxes =
driver.findElements(By.xpath("//input[@type='checkbox']"));
int checkedCount=0, uncheckedCount=0;
for(int i=0; i<checkBoxes.size(); i++){
System.out.println(i+" checkbox is selected
"+checkBoxes.get(i).isSelected());
if(checkBoxes.get(i).isSelected())
{checkedCount++;} else {uncheckedCount++;}
}
//driver.quit();
System.out.println("No. of selected checkbox: "+checkedCount);
System.out.println("No. of unselected checkbox: "+uncheckedCount);
}
}

10) Run the file from Eclipse IDE:


• OUTPUT:

Figure 1: by using the DYNAMIC URL(website)

Figure 2: by using the STATIC URL(local file)

Page 54 of 54

You might also like