top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Methods In Java How we used as an SDET

Lets learn about methods in  java which are very useful for testers especially in frameworks like cucumber BDD/TestNG.


Method: Block of code that performs a specific task or action(like addition, subtraction, multiplication, just printing numbers)


How we declare Methods

[Access Modifier] [Return Type] [Method Name] ([Parameter List])

{

[Method Body] //Contains the code to be executed

}

Access Modifier -> Specifies the visibility of method like public or private.

Return Type ->Every method has a return type in java which tells what kind of data it is going to return. If a method does not return anything its return type is void.

Method Name -> We can give any name for method but better to give a meaningful name what exactly that method do.

Parameter list -> Method may or may not take the input that we will provide in Parameter list.

Method Body -> Method body contains actual code or logic.


Lets look into example of methods in Java


This code demonstrates the use of methods with different return types (integer, String, boolean, and array), input parameters, and method calls in a Java program. It's a good example of how methods can be organized to perform specific tasks and enhance code readability and reusability.





Code Explanation

calRectangleArea Method:

  • Purpose: Calculates the area of a rectangle.

  • Parameters: Takes 2 integers, length and breadth, as input parameters.

  • Return Type: Returns an area of integer.

welcomeMessage Method:

  • Purpose: Generate a welcome message.

  • Parameters: Takes a String as an input parameter.

  • Return Type: Returns a String that has a welcome message.

evenOrNot Method:

  • Purpose: Checks if a given number is even.

  • Parameters: Takes an integer number as an input parameter.

  • Return Type: Returns a boolean indicating whether the number is even(true) or not (false).

first10EvenNumbers Method:

  • Purpose: Generates first 10 even numbers.

  • Parameters: No input parameters.

  • Return Type: Returns an array of integers.

substractNumbers Method:

  • Purpose: Subtracts two numbers and prints the result.

  • Parameters: Takes two integers, num1 and num2, as input parameters.

  • Return Type: No return type(void).

main Method:

  • Purpose: This is the place where execution starts in java.


Lets now try these methods in Cucumber BDD framework.


Below is our simple framework


Lets start with Feature File


This is a feature file written in Gherkin syntax, used in Cucumber BDD which is in a human-readable format.

Feature: Contains a brief description.

This feature file contains only one scenario where a user logs into the application with valid username and password.

This feature file follows the behavior-driven development (BDD) approach, allowing both technical and non-technical stakeholders to understand and collaborate. See this feature file is in plain English where anyone can understand by reading it.


Each step in the feature file is associated with a step definition in the step definition file (LoginSteps class). The step definitions contains the actual actions to be taken during the execution of the scenario.


Feature File


This class contains step definitions that map to the Gherkin steps in a Cucumber feature file for testing login functionality.

Step Defiinition


In Above Code: An instance of the LoginPage class is created for interaction with the login page.

  • Each step definition calls methods from the LoginPage class, separating the test logic from the pages.

  • Assertions are used to validate the expected result.

  • Annotations (@Given, @When, @And, @Then) are used to describe the behavior for each step


This code represents a Page Object for a login page with WebElement declarations, methods to interact with page elements, and a method to close the WebDriver instance after the test execution.




Code Explanation: The above code represents a page object for a login page in a Cucumber BDD framework.

  • First created instance of WebDriver as a ChromeDriver

  • LoginPage() constructor initializes the page elements using the Selenium's PageFactory class.

  • WebElement are declared using the @FindBy annotation. These fields represent various elements on the login page, such as textboxes and buttons.

  • Several methods are defined in the LoginPage class to perform actions on the login page.

  • loginUrl: Navigates to the login page URL.

  • enterUsername: Enters an email into the email textbox.

  • getPasswordTextBox: Returns the passwordTextBox WebElement.We can also directly send the password same as like email, but just wanted to show you how to return the WebElement from Page Object.

  • clickOnLoginButton: Clicks the login button.

  • isAccountPage: Checks if the account page is displayed.

  • The @After annotated method (teardown) is used to close the WebDriver after the test execution.


If i miss anything with code you can visit the code in git repo https://github.com/SudhaTelluri/Methods.git


Advantages of using Methods

  • Code reusability: In our framework we have created LoginPage right we can use this page methods in almost every other scenarios.

  • Testing: we can test the methods individually so that it would be easy to identify errors and fixing them.

  • Organization is easy: In our framework we created different folders for pages (which contains methods), step definitions where we call those methods, features where we execute the code. these all connections happens because of using the methods.

  • Scalability: By writing the code in methods we can easily add new functionalities or features by just adding another method. We can also edit the methods which already coded.

Lets see very good real time example about the usage of methods and what exactly Scalability and Organization of Data means.

  • The driverFactory method initializes the WebDriver with specific chrome options, maximizes the window, and navigates to the specified URL.




  • In the above example

  • The driverFactory method enhances code reusability. It allows you to set up the browser configuration in one place and call the method whenever needed.

  • It's particularly useful when you want to perform browser setup in multiple test cases or scenarios.

  • listOfPages() method promotes better code organization and reusability. The method can be called whenever needed, reducing code duplication.

Tip: If you have any complex logic that includes lot of code try to break the code into small methods which could be easy to understand and easy to reuse.


Thank You, Sudha.



49 views0 comments
bottom of page