Selenium with JAVA...

In last post, we learnt that Selenium is an excellent automation tool used for functional and regression testing. Selenium offers us great flexibility to work with many programming languages like Java, Python, Ruby, C# etc.  but as we all know Java is the most popular and widely used programming language used in the industry, we will start our selenium journey with writing Java scripts.

To write selenium scripts in Java, it is imperative to learn some basic Java concepts and syntax to efficiently write our script. Java is basically Object Oriented Programming (OOP) language. Here, we manipulate objects with data and methods to get desired output or results. so, we need to learn some basic OOPs concepts first. Some core OOPs concepts are: 





  1. Class

  2. Object

  3. Abstraction

  4. Encapsulation

  5. Inheritance

  6. Polymorphism



  1. Class: Basically, class is a group of similar entities. In terms of coding, it is a logical component not physical one. For example, if you have a class called 'Bank', it could have objects like SBI, Axis, HSBC etc. It's properties(data) can be customer id, account number, customer name etc. and it can have methods or functions like deposit, withdraw, Open FD etc.

  2. Object: It is defined as an instance of the class and there can be multiple instances of the class just like we saw above, for 'Bank' class, there can be multiple objects. object can contain both data and methods, we perform different methods on our objects. In general, we can say that class is a blueprint for our object. We always create objects in the main method of our program.( Hang on for time being, you will get more clear idea once we start actual coding in eclipse.)

Sample program for Class and Object:

public class Bank {

//defining instance variables (data)

int accountNumber;

String customerName;

//defining one function called info to retrieve account number and customer name

void info(){ // void is a return type here as we are only printing and not returning any       value, info is a method name

accountNumber=123;

customerName="Raj"

System.out.println("Account Number is: "+accountNumber+" Customer Name is: "+customerName);

}

//defining one main method where we create objects of the class

public static void main(String args[]){

Bank SBI = new Bank(); // object creation syntax

SBI.info(); // calling info method for SBI object

}

}

Here, one must note few points:

  1. Java always follows camel casing to declare variables.(first letter in small followed by first capital letter)

  2. System.out.println is a print statement in Java.

  3. It is always preferable to create objects in main method and we call methods on our object from main method.

Bravo ! our first Java sample program is ready, just run it in eclipse and observe the result !!

In next post, we will see more about Java in good detail and will explore new topics like constructor besides other OOPs concepts.























Comments

  1. Great work bro!keep it up!

    ReplyDelete
  2. Informative!!
    Thanks for sharing

    ReplyDelete
  3. Great work.. Keep up your hard work....👍

    ReplyDelete
  4. Great work !! I really appreciate your effort and knowledge...
    Keep it up ...

    ReplyDelete
  5. […] that is used to initialize objects of the class,for more info on class and object, please visit Selenium with JAVA… . Specifically, it is one special type of method to pass the value to the object at the time of […]

    ReplyDelete

Post a Comment

More posts on selenium...

Selenium: BDD Cucumber- Cucumber Options

Selenium: Automate Web Application Login

Selenium: All about XPath Locator in WebDriver

Selenium/Java-OOPs-Encapsulation

Selenium: BDD- The Disruptive Framework

Selenium: Waits

Selenium: Frameworks

Selenium/Java-Interface

Selenium: Capturing the screenshot