Selenium Java
Selenium Java
Open source.
Supports various programming languages such as Java, Jscript, OOPs, Python , c#, Perl, Ruby, PhP
It came in 2004.
Selenium 1:
Selenium 2 :
Selenium IDE => Record and play back. Its not an independent tool. Its firebox plugin.
Selenium RC :
Selenium Web Driver: It’s a programming interface to create and execute automation tc.
If it is another browser, we need to download the browser driver and set the path.
Demerits:
Selenium Grid:
Its only for test execution. It did not support test automation design.
It’s used for parallel execution (multiple browsers, operating environments and multiple machines)
Selenium gird 2 supports, selenium RC test and Selenium web driver tests.
*** Selenium Web Driver, Java Programming, testing frame work (junit or testNG – exe batches,
generate detailed reports, grouping the TC).
Supporting environments:
Application environment: CUI based applications Command user interface applications. (Selenium does
not support CUI based applications)
Windows based applications (1 tire application and 2 tire). Selenium does not support windows-based
applications.
Mobile applications – Selenium supports – Native (Selenium does not support), Hybrid (Selenium does
not support), web apps (Selenium supports)
Selenium License: Open source. But it has license. All selenium projects are released under Apache 2
license. (anyone can download and use all selenium projects with free of cost, anyone can modify the
source code, distribute the source code anyone, selling modified source code is not allowed)
J-Unit: Unit test frame work. it can be used as a test runner in selenium.
Mozilla Plug Ins: Fire bug and Fire path. – to spy elements in Firebox.
IE and Chrome – we have built in developers’ tools F12.
Eclipse IDE for code development. – create and execute any programs.
Configure selenium:
Advantages of selenium:
Open source.
Disadvantages of Selenium:
Test planning, test design, generating basic test, enhancing test, running and debugging test,
Doing POC.
Web Driver:
Inserting verification points – using java conditional statements. Also using testNG assertion methods we
can do verification.
Parametrization: replacing constant values with parameter. Looping statement to parametrize values.
Test Run:
Analyzing Test reports and reporting defects: Using TestNG, we can generate detailed test steps.
Selenium 5:
Java for Selenium: Selenium written in Java. Why? Java is platform independent language. More
compatibility. Knowledge sharing is quite easy.
Java topics: Core Java, java fundamental and OOPS. For Test Automation using selenium – Core Java
1) Data Types is Java: It is a classification of the type of data, that a variable or object can hold in
computer programming. Integer, characters, Constant, string, float, Boolean , logical
2) Two types of data types - Primitive Data types. Reference Data types
3) Java Modifiers- Modifiers are used to access level for classes, variables, methods etc…
4) Variables in Java: Variable is temp memory used in function. It’s a name location to store temporary
data. Variable stored in primary memory – RAM
Conditional Statements:
7) Looping statements:
1. Finite Loop – For loop(Scalar variable ), Enhanced For Loop(arrays) 2. Infinite Loop – Do
While Loop(Scalar variable ), While Loop(Scalar variable )
8) Arrays in Java: Array is an object and contains a similar type of values. Java array is Index based. Each
item in an array called as array element. The length of an array is established when the array is created,
and the array length is fixed.
9) Java Methods – also known as functions(Build in and user defined). In object OOPS we use build in
and user defined .
User Defined Methods: 1. Method with Returning value, 2. Method without returning value.
10) Regular Expressions: Formula for matching patterns. A regular exp defines search pattern for strings.
Java provides java.util.Regex Package(Build in Packages)
11) IO Operations and File Handling in Java: The Java.IO Package contains predefined classes to
perform input and output operations.
1. Reading data using input devices. 2. Display output on the console. 3. Read and write text
files and other files.
Using java class, we can handle text files (Flat files – Note Pad). To work with excel we need to
download excel JAR from external.
12) Exception handling in Java – The exception handling in Java is to handle the run time errors.
13) OOPS –
1. Inheritance- reusing
2. Polymorphism
3. Abstraction
4. Encapsulation.
1. Inheritance: is the Mechanism in which one object acquires all the properties and behaviors of
parent object. Using inheritance, we can create classes that are build in upon existing classes. When we
inherit from any existing class, we can reuse methods and fields(variables) from parent class and we can
add new methods and fields also.
Types of Inheritance: Java supports Single inheritance, Multi-level inheritance, Hierarchal inheritance.
2. Polymorphism: Performing tasks in different ways. It derived from two Greek words. Poly means –
Many, Morphs – means ways(forms).
We can achieve polymorphism though method over loading and method Riding.
3.Abstraction: is a process of hiding the implementation details and showing functionality to the user.
It shows important things to the user and hide internal details. Abstraction focus on what the object
does instead of how it does. Abstraction can be achieved in two ways. 1. Using Abstract class. (0 to
100%) 2. Interface (100%)
4. Encapsulation: it is a process of wrapping code and data together into a single unit. Ex: Capsule –
Mixed of several medicines. It provides control over the data.
Java hierarchies:
1. Java Packages. (Group of similar types of classes, Interfaces and sub packages). Types – 1. Build
In Packages, Ex – IO Packages, Util Package, SQL Package and etc..2. User defined Packages.
2. Interface in Java – It is a collection of abstract methods. It is not a class but like writing a class.
But both are diff concepts.
Java Project
>>Java Packages
>> Java Class (Program)/Java Interface.
Session 6:
Introduction to Java:
1. Introducing Java
Object Orientated Programming language, it can be extendable, Platform Independent, Simple
and easy to learn, Securable
It can be complied and interpreted. (Writer, Compiler, executing)
Usage of Java:
App development (Desktop App, Web app)
Mobile app development.
Embedded system development.
Smart cards development.
Games development.
Test Automation is secondary purpose.
Java Syntax rule:
>>Case sensitive language. Java keywords are case sensitive. But data and message is not
case sensitive.
>> First letter of a class name should be upper case.
>> Method name should start with lower case.
>>Java Program file name should match with class name.
>>Java program execution starts from Main method (Build in).1. Build in method, 2. User
defined method. Main method is mandatory for every java program.
>> Every statement/states should end with semi colon symbol.
>>Code blocks enclosed with {conditional, method, loop}(Curly Phrases)
2. Java Environment Setup
Steps:
Down load JDK and install
Set environment path(Path variable). System properties – Advanced Setting –
Environment variables – Path – JDK BIN Path
To check version cmd – Java or Java -version or Javac
Installation – eclipse.org
Step 4: Create multiple java class under Package (Create Java Class)
In Comp-Program – writing a program – Compile the program (converting high level lang into
machine lang)– execute the program
Step 2: Compiling the program – go to cmd - change to java program file directory. (changing the
directory in cmd ex. Cd desktop)
Edit the Java program file. We need to create a Javac - class file. Then run
//test program
package seleniumtest;
import java.io.console;
import java.lang.*;
//method declaration/creation
int result=a*b*c;
return result;
System.out.println(money);//100
System.out.println(c);
//condition block
If (a>b){
System.out.println(“a is biggest”)
Else{
System.out.println(“B is biggest”)
//loop block
for(int d=1;d<=10;d++){
System.out.println(d);
Int x=objTest.multiply(1,2,3);
System.out.println(x);
=> Comments in Java: comments are English word for code documentation.
Purpose of comments:
Syntax : // - for single line disbale, /* for multiple line disable ….*/
Java Data Types:
Data type is a classification of the type of data, that variable or constant or object can hold in computer
programing.
** Java supports explicit declaration of data types. – it means we need to specify the data type before
declaring the variable.
Syntax :
Datatype variablename;
Datatype v1,v2,v3;
Example:
Int a;
Char b=’c’;
Int a,b,c;
Two type of datatypes in Java: Primitive and Non-Primitive (Reference data types)
Non primitive or reference data types – in java are objects and arrays.
Session 8:
1. Access Modifiers: we use access modifier to define access control for classes, method and
variables.
Types : 4 types.
Private – it is accessible with in class. Ex: Private int a=100;
Default – it is accessible with in Package. If we did not specify any modifier, it can be treated as
default. Ex: class sample {
…
}
Protected: it is accessible with in Package. Outside of the package through inheritance only.
Ex: protected class sample{
….
}
Public : it is accessible in everywhere.
Ex: public class sample{
….
}
2. Non-Access Modifiers. We have many non-access modifiers. But in general, we use three.
1. Static: it is used to create class, methods and variables.
Ex: Static int a=10; // static variable
Static void int add(){ // static methods
..
}
2. Final : it is to finalizing the classes, methods and variables.
Ex: final int a =100; // variable
3. Abstract: (in complete) it is to create abstract classes, methods.
Ex: abstract class sample{
..
}
Java variables: a named memory location to store that temporary data in a program.
Syntax :
datatype variablename;
ex: int a;
datatype variablename=value;
datatype var1,var2,var3;
datatype var1=value;var2=value;
Int a=10, b;
b=a; //reading
Types of variables:
Operators:
Java has many operators. But for test automation we use four operators.
1. Arithmetic
2. Relational – used for comparison
3. Assignment
4. Logical
Arithmetic Operator:
*=> multiplication
/=> division
++=> increment
--=> decrement
== equal to
!=
>
>=
<
<=
Logical operators:
Assignment Operators:
Int a=10;Result = 10
a+=10; result : a = 20
a-=10; result : 10
Conditional statements:
3 types of conditions.
compound condition or multi – if (a>b) && (a>c){} ; if (a>b)||(a>c) ** one else is enough
if(a>c){
if(a>d){
If (condition){
//statement
Else if (condition){
//statement
Else{ ///optional
//statement.
}
Syntax : if (condition){
If (condition){
//statements
Syntax:
Switch (expression){
Case value:
Statements;
Break;
Case value:
Statements;
Break;
Default
Statements;
Loop statements for repetitive execution. For loop structures in Java.(It repeats a block of
statement for specified number of times)
1. For Loop
2. While loop
3. Do while loop
4. Enhanced for loop
Statements
Statements..
}
Program : print 1 to 10 numbers. Print 10 to 1 numbers, Print 1 to 10 numbers except 7.
Syntax:
Initialization
While ( condition){
Statements
..
Do Loop : it repeats a block of statement while condition is true. It executes a block of statement once
before checking the condition.
Syntax:
Initialization
Do
Statements;
} while(condition);
For(declaration: expression/array){
Statements;
How to handle data in Java – numbers – integers-byte, short, int, long and floating/decimal values-float,
double.
How to handle strings – in Java String is Object. String may have alphabets, numbers and special chars.
System.out.println("Selenium"); // Selenium)
System.out.println("123Selenium");
System.out.println("Selenium * &123");
System.out.println("1234");
Create Strings:
String myTool="Selenium"; //strinvariable
String [] myTools= {"UFT", "Selenium", "RFT", "LOadrunner"}; // array of
strings
System.out.println(myTool);
for (String tool:myTools) { //to get string array use enhanced array
System.out.println(tool);
}
Operations on strings:
String concatenation: +
String str1 = "Selenium";
String str2 = "Testing";
System.out.println(str1+" "+str2);
System.out.println("Selenium" + 1 + 1 ); //Selenium11
System.out.println(1+1+"Selenium");//2selenium
String Comparison:
1. 2 way comparison
2. 3 way comparison (0, > 0, <0)
String comparison using (== relational operator) double equal to operator. This supports 2 way
comparison. Result will be Boolean result(true/false)
System.out.println(str3.equals(str4));//false
System.out.println(str4.equals(str5));// true
String comparison using compareTo() method. 3 way comparison. (0, < 0, > 0)
Arrays: in java array is object. It is an object that holds a fixed number of values of a single data type.
Declaration of arrays:
1st Method:
Arrayname[index]=value;
3rd Method:
Types of arrays:
For (….){
For(…){ }
Disadvantage:
1. IO Operation.
2. File handling in Java.
3. Exception handling in Java.
IO Operations:
Using Java Util Scanner is the easier way to reading data. Also it includes many methods to check input is
valid to read.
package testexamples;
import java.util.*;
public class IOoperations {
public static void main(String [] args) {
Scanner scanobj = new Scanner(System.in); // system.in is a input stream
……
Examples.
1. Create a folder
2. package testexamples;
3. import java.io.File;
4. public class Filehandling {
5. public static void main(String [] args) {
6. File fileobject=new File("D://testexamples");
7. fileobject.mkdir();
8. }
9.
10. }
Check the existence of folder:
package testexamples;
import java.io.File;
public class Filehandling {
public static void main(String [] args) {
File fileobject=new File("D://testexamples");
fileobject.mkdir();
boolean blnstatus=fileobject.exists();
if (blnstatus==true) {
System.out.println("folder is exist");
}
else {
System.out.println("flder is not exist");
}
}
newfileobject.createNewFile();
}
Types of Methods:
1. Build in Method or predefined methods.
Read only, Limited,
Ex: import java.io.console;
Import java.util.*;
To use build in methods we need to import packages or classes.
Java.lang package is automatically imported in every java program.
Using import keyword we can import java classes and packages.
Categories of Build in methods:
String Methods.
Array methods.
Number methods
Character method.
Example:
package testexamples;
Method body;
}
Call Method
Datatype variablename = methodname(values);
Or
System.out.println(methodname(values));
Calling method:
Classname onjname = new classname();
Objname.method(parameter);
Usage of methods.
1. Internal methods. (creating and calling methods with in same class)
2. External methods. (calling methods from other classes)
External Methods:
1. By invoking object.
//create method
public int add(int a, int b) {
int c = a+b;
return c;
}
public static void main(String [] args) {
//create object for internal method
externalmethodsam objadd = new externalmethodsam();
int resultc=objadd.add(1, 2);
System.out.println(resultc);
externalmethods objmul = new externalmethods();
int exres= objmul.multiply(1, 2, 3);
System.out.println(exres);
}
String methods:
1. compareTo() method(it compares two strings and supports three way comparison)
result : zero, greater than zero and less than zero.
Str1.compareTo(str2);
@Test : running a test. All methods should attach with the class.
@Before : its like pre requestee of the TC(Temp variable, default values, invoking test environment)
@after: its like end of TC. Releasing memory, temp var, generating result)
@beforeclass: this instruct the Junit fw tells that, the method attached to it run only once before any
other method in the class is executed. We can use this annotation like the task that can be done only
once for all test such as connecting db, intestinal computational task.
Get() – Loads a new web page in the current browser with the help of http protocol.
getTitle()-fetches the title of the current opened web page for assertion.
getWindowHandle()-returns a unique browser window to identify the elements and work with them.
Quit()-quits the web driver instance and close all bowser windows.
Selenium Tutorials:
Assertion:
Verify – when verify fails, the test will continue execution, logging the failure
xpath – absolute xpath – capturing the elements with static html attributes- begin with single slash (/). I
can select an elemnt from root node.