Methods and Return Type Of Method -For Selenium WebDriver
What Is Method?In Selenium webdriver test suite, You need to perform some actions multiple time or in multiple test cases like Login in to Account, Place Order or any other action. 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.
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. Here is an 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
}
kinds of return types with methods.
1. Void (Void means returning nothing)
2. Any data types (when your method is returning some values like Integer, String, double, etc.., You need to provide return type with method according to returning values )
data types int, String, double etc Data types- If method is returning any value then you need to provide return type with method like int, String, Double etc.
void - If method is not returning any value then you can give void as return type. void means method returning nothing.
Let me give you simple example of int, double and void return type.
package Test_Package1;
public class Return_Types {
static int c;
static double d;
public static void main(String[] args) {
Mul(2,3);
Div(7,3);
System.out.println("Value of c Is "+c);
System.out.println("Value of d Is "+d);
Message();
}
//This method is returning integer value. It's return type is int.
public static int Mul(int a, int b){
c=a*b;
return c;
}
//This method is returning double value. It's return type is double.
public static double Div(double a, double b){
d=a/b;
return d;
}
//This method is returning nothing so there is used void return type.
public static void Message(){
System.out.println("Test Message");
}
}
If you see in above example, I have used three methods (Mul, Div and Message) and called all three methods in main method to execute them.
Method Mul is returning multiplied integer value in variable c so I have used int data type as return type with Mul method.
Method Div is returning division of two values in variable d so I have used double data type as return type with Div method.
Method Message is returning nothing so I need to use void return type with It.
No comments:
Post a Comment