Skip to content

Modeling

Software Engineer in Test uses java Class primarily for representing actual web pages in their automation scripts. This practice of representing the web pages or any entity in Java code is known as Modeling.

In this code note, we will understand following terminology:

  • Understanding Class
  • Understanding Object



› Class

Class is used for the following purpose in Java programming :

  • Code organization for related data
  • Code organization for related methods
  • Code representation or modeling of a data entity

Let's represent following actual web page we use on Trello in our Java program.

Trello Page

We can represent Trello pages in out Java code by implementing the followings:

  • List of web element locations that users would interact on the page as a variables
  • List of user's actions on the page as a method

Trello Page Representation
public class Trello {
    // ----  Field Variables ( list of variables with initial data ) ---- //
    final String url = "https://trello.com";
    final By loc_login_lnk   = linkText("Log in");
    final By loc_email_input = css("input#user");
    final By loc_pass_input  = css("input#password");
    final By loc_login_bttn  = css("input#login");


    // ----   Behaviors ( list of methods )  ------------------ //
    // these method represents what the user can do 
    // on these Trello web pages
    // notice that data from the Field are used inside these class 
    public void visitTrello() {
        visit(url);
        pause(2);
    }

    public void visitLogin() {
        click(loc_login_lnk);
        pause(2);
    }

    public void doSignIn(String user, String pass) {
        type(loc_email_input, user);
        type(loc_pass_input, pass);
        click(loc_login_bttn);
        pause(3);
    }
}//end::class



› Object

An object is a complete copy of all the code you have in a Class you created. The only way to use the code you wrote in any Class is to create an object from it. Through the object, we can use the code we grouped in our Class.

The syntax for creating an Object from the Trello class

Trello pages = new Trello(); 

Here is the breakdown of the above code:

Code Description
Trello pages a variable that holds the object notice the data type of the variable pages
= assignment operator that assigns the data to the variable
new Trello(); code that creates an object (complete code of the code) from the Trello class.

Let's see an example of where we are using the codes we wrote in the Trello class.

Trello object example
public class Tests {
    @Test
    public void verifyUserLogin() {
        // -- Test Data
        Trello page = new Trello();  // an object from Trello class
        String username = "[email protected]";
        String password = "Pass123!";

        // -- Test Steps ---//
        startAutomation();
        page.visitTrello();       
        page.visitLogin();
        page.doSignIn(username, password);
        stopAutomation();
    }
}//end::class

In the above code, here is what we are doing:

  • Creating an object from the Trello class
  • The variable page is holding the object.
  • We use the methods we wrote in Class through the object.