MyEducationPath  

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Learn Java Basics for Selenium
#1
Java Basics for Selenium
Below are some basics of JAVA useful for Selenium scripts:

Class
Method
Object
Sample program
Variables

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 on Software Testing? Explore the blog post on Best Selenium Training to become a pro in Selenium.

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.

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.

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

Sample program:
IF CONDITIONs:
if(Boolean-Expression){
//Executes if Boolean expression is true
}
else{
//Executes if Boolean expression is false
}

For Loop in Java
The for loop is the type of looping construct. It also works as while loop construct but it provide that the initialization, condition and the increment is same written in the for construct. All the statements which has to be executed is written in the for 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”);
}
}
}

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 an 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 through out 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);
}
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)