Methods In Java - Tutorials For Selenium WebDriver

What Is Method?
In Selenium webdriver test suite for software web application, You need to perform some actions multiple time or in multiple test cases like Login in to Account of software web application, Place Order or any other action on software application. If you will write this kind of (same repeated) actions In all the test cases then It will becomes overhead for you and will Increase size of your code. Instead of that, If you create methods for this kind of operations and then call that method whenever required then It will be very easy to maintain your test cases of software web application.

Method Is group of statements which is created to perform some actions or operation when your java code call it from main method. Code written Inside methods can not be executed by It self. To execute that method code block, You need to call that method inside main method block. Let me give you simple example of how to create a method and how to call It from main method block.

Simple Method Example :
public class Methodexample {

 public static void main(String[] args) {
  Test1(); //Test1() method called inside main method.
  
 }
 
 public static void Test1(){ //Simple Method - Called from main method.
  System.out.println("Test1 Method Executed.");
 }
 
 public static void Test2(){ //Simple Method - Not called from main method.
  System.out.println("Test2 Method Executed.");
 }

}

If you will run above example then console output will looks like bellow.
Test1 Method Executed.
As per the console output, We can say that Test1() method is executed but Test2() method is not executed because It Is not called from main method.

Methods will have bellow given different components.

1. Visibility Of Method : You can set your method's visibility by providing access modifier keywords like public, private, protected at beginning of method. VIEW DETAILED EXAMPLE.
Example :
public static void Testing_Nomod(){ //public method
  //Block of code
}

2. Return Type Of Method : Any method can have return types like int, String, double etc.. based on returning value's data type. void means method is not returning any value.
VIEW EXAMPLE OF RETURN TYPE
Example :
public int Return_Type(){ //This method has int return type.
  int i=10;
  return i;
 }


3. Static or non static method : Method can be static or non static. If you wants to keep your method as static then you need to use static keyword with method. If you wants to create non static method then do not write static keyword with method. VIEW FULL DESCRIPTION ON STATIC AND NON STATIC.
Example :
public void My_Method1(){ //Non static Method
  //Block of code
 }
 
public static void My_Method2(){ //Static Method
  //Block of code
 }

4. Method Name : You can give any name to your method. Always use method name relative to its functional work.
Example :
public static void Login(String user, String Pass){ //Login is the method name.
  //Block of code
 }

5. Input Parameters To Method : We can pass Input parameters to any method. Need to write parameter with its data type.
VIEW EXAMPLE OF INPUT PARAMETERS
Example :
public static void Student_Details(int Rollno, String Name){ //Rollno and Name are input parameters preceded by their data types
  //Block of code
 }

5 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Must have notes for all students and people learning Java. You are doing such a wonderful service to Software community all over the world.
    Just a suggestion... could you please change the colour of comments from green to Dark Blue or Black or any other dark colour...
    Thanks

    ReplyDelete
  3. Method name usually start with Lowercase right? Only class name starts with upperCase.
    Correct me if i am wrong

    ReplyDelete