Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
30 views12 pages

STQA-Selenium Practicals

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 12

TYBSc Computer Science

STQA – Solved Selenium Practical


List By
Dr. Madhavi Vaidya. VES College
My YouTube Channel –
madhavi2303
Practical 1 -
Step 1
 Launch Firefox and Selenium IDE.
 Type the value for our Base URL:<<URL NAME>>
 Toggle the Record button on (if it is not yet toggled on by default).
Step 2
□ In Firefox, navigate to http://demo.guru99.com/test/newtours/. Firefox should take
you to the page similar to the one shown below.
Step 3
 Right-click on any blank space within the page, like on the Mercury Tours logo on the
upper left corner. This will bring up the Selenium IDE context menu. Note: Do not click on
any hyperlinked objects or images
 Select the "Show Available Commands" option.
 Then, select "assertTitle exact: Welcome: Mercury Tours." This is a command that
makes sure that the page title is correct.
Step 4
 In the "User Name" text box of Mercury Tours, type an invalid username,
"invalidUNN".
 In the "Password" text box, type an invalid password, "invalidPWD".
Step 5
 Click on the "Sign-In" button. Firefox should take you to this page.
Step 6
□ Toggle the record button off to stop recording.
Step 7
□ Now that we are done with our test script, we shall save it in a test case.
Step 8
 Choose your desired location, and then name the Test Case as "Invalid_login".
 Click the "Save" button.
Step 9
□ Notice that the file was saved as side extension.
Step 10
□ Go back to Selenium IDE and click the Playback button to execute the whole script.
Selenium IDE should be able to replicate everything properly.

Practical 2
Conduct a test suite for any two websites -
Create a script by Recording
Step 1- Launch Firefox and Selenium IDE
Type the value for any Base URL -http://demo.guru99.com/test/newtours/
Toggle the Record button on (if it’s not yet toggled on by default)
Step 2- In Firefox, navigate to http://demo.guru99.com/test/newtours/, firefox should take
you to the page which has been mentioned
Step 3-click on the various links or the pages on the given site
Step4 - you would see that the various commands shown in the IDE
Step 5- toggle the record button off to stop recording
Step 6- you can save the test suite (group of test cases) and give a name to it
TYBSc Computer Science
STQA – Solved Selenium Practical
List By
Dr. Madhavi Vaidya. VES College
My YouTube Channel –
madhavi2303
Step 7- Later go back to Selenium IDE,click the play button and execute the whole script or
run the recent Selenium IDE script.
Selenium IDE should be able to replicate everything flawlessly

Secondly, try the calculator.net to execute the operations such as Addition, Subtraction,
Multiplication and Division.
Practical 3 – Installation of Selenium server and demonstrate it using a script in Java.
Selenium installation is not only configuration and installation of single software but it is the
combination of few softwares configuration and installation. They are as below -
1. eclipse installation
2. Java configuration and setup
3. Selenium server standalone jar
4. geckodriver

Download Eclipse and install


download java and check its path in environment variables and check whether java is starting
or not from command prompt and once can check for java -version too

Download selenium server for java, download latest and stable version (list can be found
from the site)

Download geckodriver

Keep all software in one folder (in my case its selenium settings) so that we can get them in
place as and when required

Open eclipse, create a java project as we are going to configure for Selenium using Java code
for automation.

Give Project name as Automation and finish. Project gets created.

You will find that JRE system lib files have been created and installed automatically.

Go to JRE System library, right click and Go to Build Path(it would be under Properties for
the Project name), check and open the Libraries Tab(if doesn’t open automatically), click on
add External JARs

Following JARs are to be added under Ext JARs

Add here Selenium JAR file and Java JAR files from the said folder (selenium Settings)
a. Selenium has only one JAR file
b. But under Java,c:\ProgramFiles\Java\jre\lib , take all the JARs from here. Even go to each
and every folder like management,images,ext,deploy etc and choose all JARs one by one and
add through Add External JARs

Finally click on Apply and Finish.


TYBSc Computer Science
STQA – Solved Selenium Practical
List By
Dr. Madhavi Vaidya. VES College
My YouTube Channel –
madhavi2303
Once its done as above ,the process would run behind and you can see one more folder when
you click on the project name, would be seen as Referenced Libraries.
Under it, we can see Selenium standalone jar and other JARs situated.

Second step is very imp step-

Go to Run -- External Tools --External Tools Config,select newconfiguragtion under Main,


give java.exe path under Location,which can be browsed from File System
Under Working Directory,select the location of Selenium Standalone server

Under Arguments, give


-jar <<take the selenium standalone server file's name>>presenting software's dir and copy.
so it would look like as,
-jar selenium-server-standalone-3.12.0.jar

OR
If port for the selenium server found already running busy then give the command as below -
-jar selenium-server-standalone-3.12.0.jar -port 1234 //-port 1234 is to be given if port 4444
is busy

Finally click on Apply and Run

Under console you would see -


By default, Selenium standalong server runs on port 4444 and you would get the message as
below -
OUTPUT is seen as below-
07:52:35.259 INFO [GridLauncherV3.launch] - Selenium build info: version: '3.12.0',
revision: '7c6e0b3'
07:52:35.261 INFO [GridLauncherV3$1.launch] - Launching a standalone Selenium Server
on port 4444
2018-07-09 07:52:35.366:INFO::main: Logging initialized @678ms to
org.seleniumhq.jetty9.util.log.StdErrLog
07:52:36.837 INFO [SeleniumServer.boot] - Selenium Server is up and running on port 4444
package package1;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Sample {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.gecko.driver", "E:\\
seleniumsettings\\gecko\\geckodriver.exe"); WebDriver
driver = new FirefoxDriver();
driver.get("https://www.gmail.com"); //new FirefoxDriver();
}
}
Practical 4 – Testing a Facebook page
TYBSc Computer Science
STQA – Solved Selenium Practical
List By
Dr. Madhavi Vaidya. VES College
My YouTube Channel –
madhavi2303
package package4;

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.support.ui.Select;

public class sample4 {

public static void main(String[] args) {


// TODO Auto-generated method stub
System.setProperty("webdriver.gecko.driver", "E:\\Selenium\\gecko\\geckodriver.exe");
System.setProperty("webdriver.Firefox.driver", "E:\\Selenium\\Firefox.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.facebook.com/");
//driver.findElement(By.id("")).sendKeys("yourEmailID");
driver.findElement(By.xpath("//*[@id=\"u_0_n\"]")).sendKeys("Test Selenium");
driver.findElement(By.xpath("//*[@id=\"u_0_l\"]")).sendKeys("Madhavi");

driver.findElement(By.xpath("//*[@id=\"u_0_q\"]")).sendKeys("9869026553");
driver.findElement(By.xpath("//*[@id=\"u_0_x\"]")).sendKeys("passwd");
//Select sel1=new
Select(driver.findElement(By.xpath("//*[@id=\"day\"]")));
//sel1.selectByIndex("3");
//driver.findElement(By.id("SignIn")).click();

driver.findElement(By.xpath("//*[@id=\"u_0_15\"]")).click();
Select dropdown = new
Select(driver.findElement(By.id("day")));

dropdown.selectByIndex(2);

driver.findElement(By.xpath("//*[@id=\"month\"]")).sendKeys("July");

Select dropdown1 = new Select(driver.findElement(By.id("year")));


dropdown1.selectByValue("2019");
//driver.findElement(By.xpath("//*[@id=\"year\"]")).sendKeys("1970");
//following working code for radio buttons , try both one by one by commenting
driver.findElement(By.xpath("//*[contains(text(),'Female')]")).click();//for
female
driver.findElement(By.xpath("//*[contains(text(),'Male')]")).click();//for male
driver.findElement(By.xpath("//*[@id=\"u_0_15\"]")).click();
}}
Practical 5-
Write and test a program to select the number of students’ records into table into excel
file ( I have taken two programs here..)
TYBSc Computer Science
STQA – Solved Selenium Practical
List By
Dr. Madhavi Vaidya. VES College
My YouTube Channel –
madhavi2303
1.
package package4;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import jxl.*;
import jxl.read.*;
import jxl.write.*;
import java.io.*;
public class updatestudrecords {

@Test
public void testImportexport1() throws Exception {
FileInputStream fi = new FileInputStream("E:\\selenium\\Student.xls");
Workbook w = Workbook.getWorkbook(fi);
Sheet s = w.getSheet(0);
String a[][] = new String[s.getRows()][s.getColumns()];
FileOutputStream fo = new FileOutputStream("E:\\selenium\\myBook2res.xls");
WritableWorkbook wwb = Workbook.createWorkbook(fo);
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(); //first col of the specific row
Label l2 = new Label(j, i, a[i][j]);
ws.addCell(l2); //adding a new cell
Label l1 = new Label(6, 0, "Result"); //new label named Result would be created
6th col
ws.addCell(l1);
}
for (int i = 1; i < s.getRows(); i++) {
for (int j = 2; j < s.getColumns(); j++) //to check each row/col
{
a[i][j] = s.getCell(j, i).getContents();
int x=Integer.parseInt(a[i][j]);
if(x > 35) //if mks greater than 35 then pass , would be checked in each col
{
Label l1 = new Label(6, i, "pass"); //6th col would get pass/fail according to marks
ws.addCell(l1);
}
else
{
Label l1 = new Label(6, i, "fail");
ws.addCell(l1);
break; } }}
wwb.write();
wwb.close(); } }
TYBSc Computer Science
STQA – Solved Selenium Practical
List By
Dr. Madhavi Vaidya. VES College
My YouTube Channel –
madhavi2303

2. Read from excel sheet and show it in Webform -using testng


package package4;

import java.io.FileReader;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import com.opencsv.CSVReader;

public class csvread2 {


//Provide CSV file path. It Is In E: Drive.
String CSV_PATH="E:\\Selenium\\test1.csv";
WebDriver driver;

@BeforeTest
public void setup() throws Exception {
System.setProperty("webdriver.gecko.driver",
"E:\\Selenium\\gecko\\geckodriver.exe");
//WebDriver driver = new FirefoxDriver();
TYBSc Computer Science
STQA – Solved Selenium Practical
List By
Dr. Madhavi Vaidya. VES College
My YouTube Channel –
madhavi2303
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.get("http://only-testing-blog.blogspot.in/2014/05/form.html");
}

@Test
public void csvDataRead() throws IOException{

CSVReader reader = new CSVReader(new FileReader(CSV_PATH));


String [] csvCell;
//while loop will be executed till the last line In CSV.
while ((csvCell = reader.readNext()) != null)
{
String FName = csvCell[0];
String LName = csvCell[1];
String Email = csvCell[2];
String Mob = csvCell[3];
String company = csvCell[4];
driver.findElement(By.xpath("//input[@name='FirstName']")).sendKeys(FName);
driver.findElement(By.xpath("//input[@name='LastName']")).sendKeys(LName);
driver.findElement(By.xpath("//input[@name='EmailID']")).sendKeys(Email);
driver.findElement(By.xpath("//input[@name='MobNo']")).sendKeys(Mob);
driver.findElement(By.xpath("//input[@name='Company']")).sendKeys(company);
driver.findElement(By.xpath("//input[@value='Submit']")).click();
// driver.switchTo().alert().accept();} }}}
TYBSc Computer Science
STQA – Solved Selenium Practical
List By
Dr. Madhavi Vaidya. VES College
My YouTube Channel –
madhavi2303
Practical 6- To select the number of students who have scored more than 60 in any
subject
//will show the records in output file where the marks>=60 in any two col/either of col except
middle one
package package4;
import jxl.*;
import jxl.write.*;
import java.io.*;
import org.testng.annotations.Test;

public class countstuds {


@Test
public void testImportexport1() throws Exception {

FileInputStream fi = new FileInputStream("C:\\Users\\HP\\Desktop\\Student.xls");

Workbook w = Workbook.getWorkbook(fi);
Sheet s = w.getSheet(0);
String a[][] = new String[s.getRows()][s.getColumns()];
FileOutputStream fout=new FileOutputStream("C:\\Users\\HP\\Desktop\\new1.xls");

WritableWorkbook wwb = Workbook.createWorkbook(fout);


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

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


for (int j = 0; j <s.getColumns(); j++)
{
System.out.println(s.getColumns());
if(i >= 1)
{ String b= new String();
b=s.getCell(3,i).getContents();
int x= Integer.parseInt(b);
if( x < 60)
{ c++;
System.out.println("less than 60");
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();
fi.close();
TYBSc Computer Science
STQA – Solved Selenium Practical
List By
Dr. Madhavi Vaidya. VES College
My YouTube Channel –
madhavi2303
w.close();
}}

Practical No -7 – To find total No. of objects present


//Find all links
package package1;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Sample_linkcount {

public static void main(String[] args) {


System.setProperty("webdriver.gecko.driver", "D:\\Madhavi\\Selenium
Pract for Ty18-19\\selenium settings\\gecko\\geckodriver.exe");

WebDriver driver = new FirefoxDriver();

driver.get("http://toolsqa.wpengine.com/");

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

System.out.println(links.size());

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


{
TYBSc Computer Science
STQA – Solved Selenium Practical
List By
Dr. Madhavi Vaidya. VES College
My YouTube Channel –
madhavi2303
System.out.println(links.get(i).getText());}}}

Practical No.8 – to find number of List/combo box


package package1;
import java.util.List;
import org.apache.bcel.generic.Select;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Count_Dropdown {

public static void main(String[] args) {

//Define the Webdriver for Browser i.e. Firefox


System.setProperty("webdriver.gecko.driver", "E:\\
seleniumsettings\\gecko\\geckodriver.exe"); WebDriver
driver = new FirefoxDriver();

//Open the URL (Website)


driver.get("http://housejoy.in/");

//Assign and Select the dropdown list element


Select selectDropdown = new Select(driver.findElement(By.id("cityName")));

//Get all the option from dropdown list and assign into List
List<WebElement> listOptionDropdown = selectDropdown.getOptions();

// Count the item dropdown list and assign into integer variable
int dropdownCount = listOptionDropdown.size();

//Print the total count of dropdown list using integer variable


System.out.println("Total Number of item count in dropdown list = " + dropdownCount);

}}

Practical No.9 – to find no. of checkboxes


package package1;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;
TYBSc Computer Science
STQA – Solved Selenium Practical
List By
Dr. Madhavi Vaidya. VES College
My YouTube Channel –
madhavi2303
import org.openqa.selenium.firefox.FirefoxDriver;

import com.sun.xml.internal.bind.v2.schemagen.xmlschema.List;

public class sample8_chkbox {

public static void main(String[] args) {

System.setProperty("webdriver.gecko.driver", "D:\\Madhavi\\Selenium Pract for Ty18- 19\\


selenium settings\\gecko\\geckodriver.exe");

WebDriver driver = new FirefoxDriver();

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

driver.get("https://www.ironspider.ca/forms/checkradio.htm");

//List<WebElement> checkBoxes = driver.findElements((By.name("vehicle")));


java.util.List<WebElement> checkBoxes = driver.findElements((By.name("color")));
for(int i=0; i<checkBoxes.size(); i=i+1)
{
checkBoxes.get(i).click();

}
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++;
}}
System.out.println("number of selected checkbox: "+checkedCount);
System.out.println("number of unselected checkbox: "+uncheckedCount);
}}
Practical No.10 - Jmeter installation and execution
https://jmeter.apache.org/download_jmeter.cgi - to download apache-jmeter.zip
Steps for installation –
In JMeter there are number of listeners available in order to monitor and analyse the results in
different parameters and statistics. We need to add an appropriate listener to analyse results
which is difficult part of the performance testing. In the below example we will add 'Graph
Result' listener. Graph Results listener presents data, average, median, deviation and
throughput in a graphical format.
TYBSc Computer Science
STQA – Solved Selenium Practical
List By
Dr. Madhavi Vaidya. VES College
My YouTube Channel –
madhavi2303
Please do not use listeners during the load test as it consume loads of resources and produce
unreliable load test results. You can use when developing and debugging tests.
JMeter Graph Results Listener Example:
Step 1: Right Click Test Plan>Add>Threads (Users) > Thread Group.
Set Values as Number of Threads as 1000, Ramp-Up Period as 1 and Loop Count value as 1.
Step 2: Thread Group > Add > Sampler > HTTP request
Set Server Name as: amazon.com and Set Path as: /
Step 3: Thread Group > Add > Listener > Graph Results
Step 4: Save the Test Plan and Run
Graph Result shows the graph plotted. The x-axis contains the nth sampler and y-axis is the
response time. The data, average, median, deviation and throughput are displayed as legends.
Green - Current Throughput Rate
Black - Current Sampler
Blue - Current Average of all Samples
Red - Current Standard deviation
Throughput is measured in requests/transaction per second/minute/hour the server
handled. i.e.. Throughput = (number of requests) / (total time). The time is calculated from
the start of the first sample to the end of the last sample.
Average - Average response time between request sent and getting reply response from
server.
No of Samples - Total number of requests sent to server during the load test.
Deviation - Shows how much the response time varies. Deviation value should always be as
low as possible. Higher values generally means system is under stress.

You might also like