0% found this document useful (0 votes)
18 views27 pages

Practical 01:: Requirement: Aim: Code: Output

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 27

PRACTICAL 01:

Requirement:
Aim:
Code:
Output:

Practical 2

AIM​: Conduct a test suite for any two sites.

Test Suite 1

Code:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class Facebook {

public static void main(String[] args) {


System.setProperty("webdriver.chrome.driver", "C:\\janvi\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https:\\www.facebook.com");
WebElement username = driver.findElement(By.id("email"));
WebElement password = driver.findElement(By.id("pass"));
WebElement login = driver.findElement(By.id("loginbutton"));
username.sendKeys("Your mail");
password.sendKeys("Your Password");
login.click();

try
{
Thread.sleep(2000);
WebElement navBar = driver.findElement(By.id("js_u"));
System.out.println("Test Case Pass");
driver.close();

}
catch(Exception e)
{
System.out.print("Test Case Failed");
}

Output:

govind

Test Suite 2

Code:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Gmail {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","C:\\janvi\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://accounts.google.com/ServiceLogin?");
driver.findElement(By.id("identifierId")).sendKeys(“Your email");
driver.findElement(By.id("identifierNext")).click(); // click next
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
} driver.findElement(By.name("password")).sendKeys("Your
password"); driver.findElement(By.id("passwordNext")).click(); // click sign in
String title = driver.getTitle();
if(title.equals("Google Account"))
{
System.out.println("LOGIN SUCCESSFUL...");
}
else
{
System.out.println("LOGIN FAILED");
}
}
}

Output:
PRACTICAL 03:
Requirement: HTML file for LCM andGCD , Selenium Jar files, Eclipse
Aim: To find LCM and GCD
Code:
LCM code
lcm.html
<html>
<head>
<script type ="text/javascript">
function lcm()
{

var a,b;
a=parseInt(document.myform.txt1.value);
b=parseInt(document.myform.txt2.value);
x=a*b;
y=x/gcd(a,b);
document.myform.result.value=y;
}
function gcd(a,b){

while(a!=b)
{
if(a>b)
a=a-b;
else
b=b-a;
}
return a;
}
</script>
</head>
<body>
<h1 align ="Center">Program to calculate Lcm of 2 numbers</h1>
<hr color ="red">
<center>
Enter Two numbers:
<form name ="myform">
Number 1 :<input type="text" name="txt1" value =" ">
<br><br>
Number 2 :<input type="text" name="txt2" value =" ">
<br><br>
<input type="button" value ="get LCM"
onClick="lcm()"><br><br>
LCM is :<input type="text" name="result" value="">
</body>
</html>

Eclipse code for lcm


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

public static void main(String[] args) {


// TODO Auto-generated method stub
System.setProperty("webdriver.gecko.driver",
"C:\\\\Users\\\\User\\\\Downloads\\\\SeleniumIDE\\\\Mozilla
Driver\\\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("file:///D:/path/lcm.html");
driver.manage().window().maximize();
driver.findElement(By.name("txt1")).sendKeys("");
driver.findElement(By.name("txt2")).sendKeys("");
driver.findElement(By.name("Get LCM")).click();
String result =
driver.findElement(By.name("result")).getAttribute("Values");
System.out.println("Result is:"+result);
}

Output:

GCD Code:
gcd.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>
<h1 align ="Center">Program to calculate gcd of 2 numbers</h1>
<hr color ="red">
<center>
Enter Two numbers:
<form name ="myform">
Number 1 :<input type="text" name="n1" value =" ">
<br><br>
Number 2 :<input type="text" name="n2" value =" ">
<br><br>
<input type="button" value ="get GCD"
onClick="gcd()"><br><br>
GCD is :<input type="text" name="result" value="">
</body>
</html>

GCd.java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Gcd {

public static void main(String[] args) {


// TODO Auto-generated method stub
System.setProperty("webdriver.gecko.driver",
"C:\\\\Users\\\\User\\\\Downloads\\\\SeleniumIDE\\\\Mozilla
Driver\\\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("file:///D:/path/index1.html");
driver.manage().window().maximize();
driver.findElement(By.name("n1")).sendKeys("");
driver.findElement(By.name("n2")).sendKeys("");
driver.findElement(By.name("Get GCD")).click();
String result =
driver.findElement(By.name("result")).getAttribute("Values");
System.out.println("Result is:"+result);

Output:
PRACTICAL 04 A
Requirement ​: Selenium Jar files ,chrome/gecko Driver, Eclipse , a google
account

Aim ​: Write and test a program to login a specific web page.

Code​:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class gmail {

public static void main(String[] args) {


System.setProperty("webdriver.chrome.driver",
"D:\\Foram\\stqa\\SeleniumIDE\\chromedriver.exe" );
WebDriver driver=new ChromeDriver();
driver.get("https://accounts.google.com/ServiceLogin?");

driver.findElement(By.id("identifierId")).sendKeys("[email protected]
"); // enter username
driver.findElement(By.id("identifierNext")).click(); // click next
try
{
Thread.sleep(5000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
driver.findElement(By.name("password")).sendKeys("*******");
// enter password
driver.findElement(By.id("passwordNext")).click(); // click sign
in
String title = driver.getTitle();
System.out.println(title);
if(title.equals("Sign in – Google accounts"))
{
System.out.println("LOGIN SUCCESSFUL...");
}
else
{
System.out.println("LOGIN FAILED");
}

Output:
PRACTICAL 04 B:

Requirement ​: Selenium Jar files ,chrome/gecko Driver, Eclipse , a


facebook account

Aim ​: Write and test a program to login a facebook account.

Code​:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class facebook {

public static void main(String[] args) {


System.setProperty("webdriver.chrome.driver",
"D:\\Foram\\stqa\\SeleniumIDE\\chromedriver.exe" );
WebDriver driver=new ChromeDriver();
driver.get("https://www.facebook.com");

driver.findElement(By.id("email")).sendKeys("[email protected]");
driver.findElement(By.id("pass")).sendKeys("*******");
WebElement Login = driver.findElement(By.id("loginbutton"));

try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Login.click();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();}}}
Practical 05
Requirement : Excel file for Student Record, Jxl Jar Files,Testng (Testing Library
Aim : Write and test a program to update 10 student records into table
into Excel file.

Code :
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 org.testng.annotations.*;
public class StudentUpdate {
@BeforeClass //@BeforeClass runs once before the entire test.
public void setUp() throws Exception {}
@Test
public void testImportexport1() throws Exception {
FileInputStream fi = new
FileInputStream(&quot;C:\\Users\\Kewal\\Desktop\\Selenium
jars\\Students.xlsx&quot;);
Workbook w = Workbook.getWorkbook(fi);
Sheet s = w.getSheet(0);
String a[][] = new String[s.getRows()][s.getColumns()];
FileOutputStream fo = new
FileOutputStream(&quot;C:\\Users\\Kewal\\Desktop\\Selenium
jars\\Students.xlsx&quot;);
WritableWorkbook wwb = Workbook.createWorkbook(fo);
WritableSheet ws = wwb.createSheet(&quot;result1&quot;, 0);

for (int i = 0; i &lt; s.getRows(); i++)


for (int j = 0; j &lt; 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, &quot;Result&quot;);
ws.addCell(l1);
}
for (int i = 1; i &lt; s.getRows(); i++) {
for (int j = 2; j &lt; s.getColumns(); j++)
{
a[i][j] = s.getCell(j, i).getContents();
int x=Integer.parseInt(a[i][j]);
if(x &gt; 35)
{
Label l1 = new Label(6, i, &quot;pass&quot;);
ws.addCell(l1);
}
else
{
Label l1 = new Label(6, i, &quot;fail&quot;);
ws.addCell(l1);
break;
}
}
System.out.println(&quot;Records sucessfully updated &quot;);
}
wwb.write();
wwb.close();
}
}
Output:

PRACTICAL 06 : 6.A

Requirement : Excel file for Student Record, jxl jar files, Testng library
Aim : To find number of Students with scores more than 60

Code :

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 Result
{
public String inputFile;

public void setInputFile(String inputFile)


{
this.inputFile = inputFile;
}
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 columns and lines
for (int j = 0; j < sheet.getRows(); j++)
{
for (int i = 0; i < sheet.getColumns(); 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 more subjects is : "+count);
}

catch (BiffException e)
{
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException
{
Result test = new Result();
test.setInputFile("G:\\TY_Pract\\Students.xls");
test.read();
}
}

Output :
PRACTICAL 06 : 6.B

Requirement : Excel file for Student Record, jxl jar files, Testng library
Aim : To find number of Students with scores less than 60

Code :

import jxl.*;
import jxl.read.*;
import jxl.write.*;
import java.io.*;
import org.testng.annotations.Test;

public class StudentScores {


@Test
public void testImportexport1() throws Exception
{
FileInputStream fi = new
FileInputStream("G:\\TY_Pract\\Students.xls");

Workbook w = Workbook.getWorkbook(fi);
Sheet s = w.getSheet(0);
String a[][] = new String[s.getRows()][s.getColumns()];

FileOutputStream fo = new
FileOutputStream("G:\\TY_Pract\\Scores.xls");

WritableWorkbook wwb = Workbook.createWorkbook(fo);


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 < 60) {
c++;
break;
}
}

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


Label l2 = new Label(j, i-c, a[i][j]);
ws.addCell(l2);
}
}
System.out.println("Total number of students who scored less
than 60 : "+c);
wwb.write();
wwb.close();
}
}

Output :
PRACTICAL 07:
Requirement:​Selenium Jar files ,chrome/gecko Driver, Eclipse

Aim: Write and test program to provide total number of object


present/available on the page.

Code:
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class TestLink {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","C:\\Users\\shaht\\Download
s\\SeleniumIDE\\SeleniumIDE\\chrome driver\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("https://www.gmail.com");
List <WebElement> mylist= driver.findElements(By.xpath("//a"));
System.out.println("Number of links =" +mylist.size());
}
}

Output:

PRACTICAL 09:
Requirement:​Selenium Jar files ,chrome/gecko Driver, Eclipse

Aim: Write and test a program to count the number of check boxes /radio
buttons on the page checked and unchecked count.

Code:
Html:
<html>
<head><title>testSTQAprac</title></head>
<body>
<h2>Text Input</h2>
<form>
First name:<br><input type="text" id="fname" name="fname" ><br>
Last name:<br><input type="text" id="lname" name="lname" >
<h2>Select Gender</h2>
<input type="radio" value="Male" id="male" name="gender" checked
>Male</radio><br>
<input type="radio" value="Female" id="female"
name="gender">Female</radio><br>
<input type="radio" value="other" id="other"
name="gender">other</radio>
<h2>Select Languages Known</h2>
<input type="checkbox" id="java" name="java" value="Java"
>Java<checkbox><br>
<input type="checkbox" id="PHP" name="PHP" value="PHP"
checked>PHP<checkbox><br>
<input type="checkbox" id="C#" name="C#" value="C#"
>C#<checkbox><br>
<input type="checkbox" id="Python" name="Python" value="Python"
checked>Python<checkbox><br>
<input type="checkbox" id="Ruby" name="Ruby" value="Ruby"
>Ruby<checkbox><br>
<br><input type="submit">
</form>
</body>
</html>

Java:
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class CheckRadio {


public static void main(String[] args) {

System.setProperty("webdriver.chrome.driver","S://SeleniumIDE//c
hromeDriver//chromedriver.exe");
WebDriver driver=new ChromeDriver();

driver.get("S:\\programming\\Eclipse\\STQAcheckbox.html");
driver.manage().window().maximize();
int chck=0,uchk=0;
List <WebElement>
mylist=driver.findElements(By.xpath("//input[@type =
'checkbox']"));
for(WebElement we : mylist){
if(we.isSelected()) {
chck+=1;
}
else {
uchk+=1;
}
}
int rad=0,urad=0;
List <WebElement>
list=driver.findElements(By.xpath("//input[@type = 'radio']"));
for(WebElement we : list){
if(we.isSelected()) {
rad+=1;
}
else {
urad+=1;
}
}
System.out.println("Number of Checked boxes : " +
chck);
System.out.println("Number of Unchecked boxes : " +
uchk);
System.out.println("Number of Checked radio : " +
rad);
System.out.println("Number of Unchecked radio : " +
urad);
}
}

Output:
​PRACTICAL : 08-A

Requirement : Selenium Jar files ,chrome/gecko Driver, Eclipse

Aim : Write and test a program to get the number of items in a


list/combo-box.

Code: Java

import java.util.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
public class TestComboBox {

public static void main(String[] args) {

System.setProperty("webdriver.chrome.driver","C:\\Users\\RD\\eclipse-work
space\\SeleniumIDE\\SeleniumIDE\\Mozilla Driver\\chromedriver.exe");
WebDriver driver= new ChromeDriver();
driver.get("https://www.facebook.com/r.php");
driver.manage().window().maximize();;

Select se = new
Select(driver.findElement(By.xpath("//Select[@id='month']")));
List <WebElement> mylist = se.getOptions();
mylist.size();
System.out.println("Number of items: " +mylist.size());
}

Output :

Practical-8B
Html Code:

<html>
<body>
<form>
<label for="country">Choose your country:</label>
<select name="country" id="country">
<option value="India">India</option>
<option value="USA">USA</option>
<option value="Japan">Japan</option>
<option value="Australia">Australia</option>
</select>
</form>
</body>
</html>

Code:

package pkg;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

public class HtmlCombo {


public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","C:\\eclipse-20200928T1511
20Z-001\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("file:\\D:\\Stqa Prac\\Combo.html");
Select selectDropdown = new
Select(driver.findElement(By.id("country")));
List<WebElement> ls = selectDropdown.getOptions();
int dropdownCount = ls.size();
System.out.println("Total Number of item count in
dropdown list = " + dropdownCount);
System.out.println("Elements present in the List are: ");
for(WebElement we : ls ){
String elements =we.getText();
System.out.println(elements);
}
}
}

Output:

You might also like