USCS503 – Software Testing and Quality Assurance
Practical – 3 :
Install Selenium server and demonstrate it using a script in Java
Documentation :
Refer https://www.seleniumhq.org/docs/03_webdriver.jsp page and answer
the following questions
1. Write a note Selenium WebDriver API
2. List out the Selenium WebDriver API commands and operations and
explain their working (up to “By CSS”)
Software Needed :
1. XAMPP
2. Mozilla Firefox 38.0.5 (If higher version is installed in the system, it has
to uninstalled and the given version should be installed)
3. Java NetBeans IDE 7.0.1
4. JDK 1.8.0
5. Selenium-java-client-driver-1.0.1.jar
6. Selenium-server-standalone-3.13.0.jar - Save this file in a folder. That
folder has to be added to the path. To add a folder to the path, follow
the steps below.
a. Control Panel System and Security System
b. Advanced System Settings Environment Variables
c. Select Path variable Edit Add the absolute path of the folder
in the Path value OK
7. Before executing the java programs, Selenium server should be started
using the steps below
a. Go to Command Prompt
b. Go to the folder where you have saved the jar file
c. Execute the command
java –jar selenium-server-standalone-3.13.0.jar
d. Server will be started in a port normally in 4444.
e. Don’t close the window. Minimize it and start Java Programming
8. For all the practicals from practical 3, we need to create a package and
inside the package, we may create java class for the each practical.
9. Before coding, we need to add the jar files to the library using the
following steps
Prepared by : Prof. Jayshree Ravi, Mithibai College
Steps to create a java application and java class
1. Create a new java application
2. Give an appropriate name to the application
3. Right click on the project name and select Properties option
4. Select Libraries option and set the java platform to JDK 1.8
5. Select Add Jar/Folder option and add the two jar files mentioned
above.
6. Return to Projects
7. Create a Java class and paste the java code with respect to each
practical
Prerequisite Knowledge :
1. HTML, CSS, JavaScript, Core Java
Practical – 3 :
Install Selenium server and demonstrate it using a script in Java
Sample code:
1. Refer https://www.seleniumhq.org/docs/03_webdriver.jsp#setting-up-
webdriver-project
2. Under the topic “Introducing the Selenium-WebDriver API by
Example”, java code for accessing google webpage is given.
3. Create a new java class and paste the code.
4. Run the code. It should open firefox window and google site should be
opened with cheese as the search term
5. If this works fine, then the steps followed are right
Gcd.html : (Save in any folder)
<html>
<head>
<script type="text/javascript">
function gcd()
{
var x,y;
x=parseInt(document.myform.n1.value);
y=parseInt(document.myform.n2.value);
Prepared by : Prof. Jayshree Ravi, Mithibai College
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 two 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" name="btn" value="Get GCD" onClick="gcd()"> <br>
<br>
GCD is : <input type="text" name="result" value="">
</form>
</body>
</html>
Create a java class and paste the code. Run the code
GT.java :
package gt;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class GT {
public static void main(String[] args) {
WebDriver driver=new FirefoxDriver();
driver.get("http://localhost:1111//sw//gcd.html");
driver.manage().window().maximize();
driver.findElement(By.name("n1")).sendKeys("14");
driver.findElement(By.name("n2")).sendKeys("28");
Prepared by : Prof. Jayshree Ravi, Mithibai College
driver.findElement(By.name("btn")).click();
String result =
driver.findElement(By.name("result")).getAttribute("value");
System.out.println("GCD is " + result);
}
}
Practical – 4
Write and test a program to login a specific web page
Login.html:
<html>
<head>
<title>
Login page
</title>
</head>
<body>
<form id='login' action='user.php' method='post' accept-charset='UTF-8'>
UserName*:
<input type='text' name='username' id='username' maxlength="50" />
Password*:
<input type='password' name='password' id='password' maxlength="50" />
<input type='submit' name='button' value='Submit' />
</form>
</body>
</html>
User.php :
<?php
$u = $_POST['username'];
$p = $_POST['password'];
if(($u=="test")&&($p=="test"))
{
echo "Login Successful";
Prepared by : Prof. Jayshree Ravi, Mithibai College
}
else
{
echo "Invalid user";
}
Prepared by : Prof. Jayshree Ravi, Mithibai College
Gt.java:
Create a new java class test
package gt;
import com.thoughtworks.selenium.webdriven.commands.Click;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class test {
public static void main(String[] args) {
WebDriver driver=new FirefoxDriver();
driver.get("http://localhost:1111//sw//login.html");
driver.manage().window().maximize();
driver.findElement(By.name("username")).sendKeys("test");
driver.findElement(By.name("password")).sendKeys("test");
driver.findElement(By.name("button")).click();
}
}
Prepared by : Prof. Jayshree Ravi, Mithibai College