Blog

Learn Java Basics For Selenium

  • (4.0)
  •   |   772 Ratings

Learn Java Basics for Selenium

Java Basics for Selenium

Below are some basics of JAVA useful for Selenium scripts:

  1. Class
  2. Method
  3. Object
  4. Sample program
  5. Variables
  6. Creating Objects
  7. If conditions
  8. For loops
  9. Extending class
  10. Arrays

Learn Java Basics for Selenium

1. Class:

A class is nothing but a blueprint or a template for creating different objects which defines its properties and behaviors. Java class objects exhibit the properties and behaviors defined by its class. A class can contain fields and methods to describe the behavior of an object.

Desired to gain proficiency in Software Testing? Explore the blog post on Selenium Training to become a pro in Selenium.

2. Method:

Methods are nothing but members of a class that provide a service for an object or perform some business logic. Java fields and member functions names are case sensitive. Current states of a class’s corresponding object are stored in the object’s instance variables. Methods define the operations that can be performed in java programming.

3. Objects:

An object is an instance of a class created using a new operator. The new operator returns a reference to a new instance of a class. This reference can be assigned to a reference variable of the class. The process of creating objects from a class is called instantiation. An object encapsulates state and behavior.

Related Page: How To Install IE Developer Tool - SELENIUM

An object reference provides a handle to an object that is created and stored in memory. In Java, objects can only be manipulated via references, which can be stored in variables

4. Sample program:

IF CONDITIONs:

if(Boolean-Expression){
//Executes if Boolean expression is true
}
else{
//Executes if Boolean expression is false
}

Frequently Asked Selenium Interview Questions

5. For Loop in Java

The for loop is the type of looping construct. It also works as while loop construct but it provides that the initialization, condition and the increment are same written in the for construct. All the statements which have to be executed are written in the for a block.

for( initialization; termination; increment)
{
statements;
}
package com.devmanuals2;
public class ForExample {
public static void main(String[] args) {
int i;
for(i=1;i<=5;i++){
System.out.println(i+” Times”);
}
}
}

Checkout Selenium Tutorial

6. Variables:

A variable is a container that stores a meaningful value that can be used throughout a program. For example, in a program that calculates tax on items, you can have a few variables – one variable that stores the regular price of an item and another variable that stores the total price of an item after the tax is calculated on it. Variables store this information in a computer’s memory and the value of a variable can change all throughout a program.

  • Declaring and defining variables
  • Before using variables, you must declare the variables name and type. See the following example for variables declaration:

int num; //represents that num is a variable that can store value of int type.
String name; //represents that name is a variable that can store string value.
boolean bol; //represents that bol is a variable that can take boolean value (true/false);
You can assign a value to a variable at the declaration time by using an assignment operator ( = ).
int num = 1000; // This line declares num as an int variable which holds value “1000”.
boolean bol = true; // This line declares bol as boolean variable which is set to the value “true“.

Example program:

class PrintText
{
public static void main(String[] args){
//declare some variables
byte aByte = -10;
int aNumber = 10;
char aChar = ‘b’;
boolean isBoolean = true;
//print variables alone
System.out.println(aByte);
System.out.println(aNumber);
//print variables with text
System.out.println(“aChar = ” + aChar);
System.out.println(“Is the isBoolean variable a boolean variable? ” + isBoolean);
}
}

Related Page: XPATH Usage In Selenium

7. Extends:

You extend a class by using the extends keyword in a class declaration,

The Parent class
public class Parent {
}
The Child class
public class Child extends Parent {
}

In Java, there’s the very important “extend” keyword. A class declaration can use the extend keyword on another class, like this: class C extends B { … }.

When a class C extends class B, C automatically has all variables and

methods defined in class B. (think of it as a internal copying mechanism)

If class C defines a variable or method that has the same name in class B, class C’s definition overrides B’s.

class B {
int x;
void setIt (int n) { x=n;}
void increase () { x=x+1;}
void triple () {x=x*3;};
int returnIt () {return x;}
}
class C extends B {
void triple () {x=x+3;} // override existing method
void quadruple () {x=x*4;} // new method
}
public class GetRich {
public static void main(String[] args) {
B b = new B();
b.setIt(2);
b.increase();
b.triple();
System.out.println( b.returnIt() ); // prints 9
C c = new C();
c.setIt(2);
c.increase();
c.triple();
System.out.println( c.returnIt() ); // prints 6
}
}

In the above code, class C inherits all class B’s variables and methods. Class C overrides the “triple” method and added a “quadruple” method.

Other classes can extend Class C, as to inherit all members and methods of class C (which includes those in B). In this way, a tree hierarchy is formed.

When class C extends B, we say that C is a “subclass” of B, and B is the “superclass” of C. This is called inheritance because C inherited from B. Two or more classes can inherit from the same parent class. However, a class can only have one parent.

In Java, EVERY class is a subclass of java.lang.Object (or subclass of its subclasses). Every class in Java is a node on this giant tree.

Explore Selenium Sample Resumes! Download & Edit, Get Noticed by Top Employers!Download Now!

8. Arrays:

An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed. You’ve seen an example of arrays already, in the main method of the “Hello World!” application. This section discusses arrays in greater detail.

Each item in an array is called an element, and each element is accessed by its numerical index. As shown in the above illustration, numbering begins with 0. The 9th element, for example, would therefore be accessed at index 8.

An array is a very common type of data structure wherein all elements must be of the same data type.

Once defined, the size of an array is fixed and cannot increase to accommodate more elements.

The first element of an array starts with zero.


Popular Courses in 2018

Get Updates on Tech posts, Interview & Certification questions and training schedules