TMT-Testing-Utilities-1.0.0.jar

TMT Testing Utilities is a Java based library of methods and utilities for automation testing that makes it easy to use Selenium WebDriver .

TMT Testing Utilities is to make life easy for automation testers. This means that you will need to include the original library, like the selenium-server.

These utilities work best with Selenium 4 and above. It is advisable to use the latest stable version of Selenium 4.

With time, more utilities, features and methods will be added to enhance the automation testing experience.

Inherit TMT Testing Utilities Object

To use the TMT Testing Utilities you will first of all need to inherit the Utilities object u.

To inherit the object you can simply implement the built in interface Utilities

Using the Utilities object u you will be able to invoke any method from the TMT Testing Utilities library and perform the desired action.

public class className implements Utilities {
    u.method();
}

 


Browser

Open Browser

Selenium
WebDriver driver = new ChromeDriver();
WebDriver driver = new FireFoxDriver();
WebDriver driver = new SafariDriver();
WebDriver driver = new EdgeDriver();
TMT Testing Utilities

With TMT Testing Utilities you do not need to specifically create an object for individual browser driver.

You can simply invoke the method setBrowser(String) and pass the browser name as a string variable.

setBrowser(String) methos accepts "chrome", "firefox", "safari" and "edge" as input parameter to launch the respective browser.

public class className implements Utilities {
    u.setBrowser("chrome");
}

Close Browser

Once all your tests have executed, you can breakdown or close the browser with closeBrowser() method.

closeBrowser() doesn't accept any parameter or input.

public class className implements Utilities {
    u.setBrowser("chrome");
    u.closeBrowser();
}

 


Open website/link/URL in browser

After launching the browser the first thing you would want to do is to open your website.

With TMT Testing Utilities you can simply invoke the go(String) method to open your website in the browser.

public class className implements Utilities {
    u.setBrowser("chrome");
    u.go("https://www.theminitools.com");
    u.closeBrowser();
}

You can also pass the URL as a String variable as parameter in the go(String) method.
For example:

public class className implements Utilities {
    u.setBrowser("chrome");
    String url = "https://www.theminitools.com"";
    u.go(url);
    u.closeBrowser();
}

You can also navigate to a particular link/URL with goTo(String) method.

public class className implements Utilities {
    u.setBrowser("chrome");
    u.go("https://www.theminitools.com");
    u.goTo("https://www.theminitools.com/tools/TMT-Testing-Utilities.html");
    u.closeBrowser();
}

 


Find an element

Finding WebElements is a major part of interaction with the Web page/application in Automation Testing.

Selenium WebDriver comes with some many different type of locators with each having their separate Find By method.

TMT Testing Utilities makes it easier ti find elements with a common method for all the locators of Selenium.

You can find a WebElement with the find(String, String) method of TMT Testing Utilities library.

find(String, String) method has a return type WebElement . This means that the method returns a WebElement value which can be stored in a WebElement type variable.

find(String, String) method accepts two parameters of String type.

  1. Locator Type : This parameter accepts
    • "xpath" to find element by XPATH.
    • "css" to find element by cssSelector.
    • "id" to find element by id.
    • "tag" to find element by tagName.
    • "class" to find element by className.
    • "link" to find element by linkText.
    • "partialLink" to find element by partialLinkText.
    • "name" to find element by name.
  2. The second parameter will be the value of the locator type to identify and locate the particular WebElement.
public class className implements Utilities {
    u.setBrowser("chrome");
    u.go("https://www.theminitools.com/tools/test-case-manager.html");
    u.find("xpath","//input[@onclick='newRow();']");
    u.find("css","input[name='TCID']");
    u.find("id","TCID");
    u.find("tag","a");
    u.find("class","history");
    u.find("link","Sitemap");
    u.find("partialLink","Mini Tools");
    u.find("name","TCID");
    u.closeBrowser();
}

find(String, String) has as return type WebElement. So, you can store the located element in a variable of WebElement type.

public class className implements Utilities {
    u.setBrowser("chrome");
    u.go("https://www.theminitools.com/tools/test-case-manager.html");
    WebElement xpathElement = u.find("xpath","//input[@onclick='newRow();']");
    WebElement cssElement = u.find("css","input[name='TCID']");
    WebElement idElement = u.find("id","TCID");
    WebElement tagElement = u.find("tag","a");
    WebElement classElement = u.find("class","history");
    WebElement linkElement = u.find("link","Sitemap");
    WebElement partialLinkElement = u.find("partialLink","Mini Tools");
    WebElement nameElement = u.find("name","TCID");
    u.closeBrowser();
}

 


Find all matching elements

There can be some use cases where you might want to find all the elements in a web-page having similar locator.

For example, you might want to find all the elements from a list (<li>) or you would want to find all the links within the web-page.

You can do this with the findAll(String, String) method of TMT Testing Utilities .

Syntax of findAll(String, String) is similar to that of find(String, String) .

This method has a return type of List<WebElement>

public class className implements Utilities {
    u.setBrowser("chrome");
    u.go("https://www.theminitools.com/tools/test-case-manager.html");
    u.findAll("xpath","//input[@onclick='newRow();']");
    List<WebElement> cssElements = u.findAll("css","input[name='TCID']");
    u.findAll("id","testCase");
    List<WebElement> tagElements = u.findAll("tag","a");
    u.findAll("class","r");
    List<WebElement> linkElements = u.findAll("link","Sitemap");
    u.findAll("partialLink","Mini Tools");
    List<WebElement> nameElements = u.findAll("name","TCID");
    u.closeBrowser();
}

 


SendKeys() | Type into any element

To type into an element you need to find the element with a locator and then use the sendKeys() method to mimic the key press events in Selenium.

TMT Testing Utilities comes with the input(String, String, String) method to make this task simpler.

public class className implements Utilities {
    u.setBrowser("chrome");
    u.go("https://www.theminitools.com/tools/test-case-manager.html");
    u.input("id", "product", "Sample Product");
    u.closeBrowser();
}

If you have already found the WebElement and stored it in a variable with the find(String, String) method, then you can simply pass that WebElement instead of the Locator Type and it's value. Here is the sample Code for this,

public class className implements Utilities {
    u.setBrowser("chrome");
    u.go("https://www.theminitools.com/tools/test-case-manager.html");
    WebElement idElement = u.find("id","product");
    u.input(idElement, "Sample Product");
    u.closeBrowser();
}

 


Click an element

You can click a link or a button or you can select a check-box or a radio button with click(String, String) method.

public class className implements Utilities {
    u.setBrowser("chrome");
    u.go("https://www.theminitools.com/tools/test-case-manager.html");
    u.click("link", "Sitemap");
    u.closeBrowser();
}

If you have already found the WebElement and stored it in a variable with the find(String, String) method, then you can simply pass that WebElement instead of the Locator Type and it's value. Here is the sample Code for this,

public class className implements Utilities {
    u.setBrowser("chrome");
    u.go("https://www.theminitools.com/tools/test-case-manager.html");
    WebElement linkElement = u.find("link","Sitemap");
    u.click(linkElement);
    u.closeBrowser();
}

 


Select from a Drop Down list

No let's select a value from a drop down list from a web element of <select> type.

Let's look at a few ways in which you can select a value from a drop down element.

  1. Select by index

    selectIndex(String, String, int) public class className implements Utilities {
        u.setBrowser("chrome");
        u.go("https://www.theminitools.com/tools/test-case-manager.html");
        u.selectIndex("id", "testType", 2);
        u.closeBrowser();
    }

    If you have already found the WebElement and stored it in a variable with the selectIndex(WebElement, int) method, then you can simply pass that WebElement instead of the Locator Type and it's value. Here is the sample Code for this,

    public class className implements Utilities {
        u.setBrowser("chrome");
        u.go("https://www.theminitools.com/tools/test-case-manager.html");
        WebElement element = u.find("id","testType");
        u.selectIndex(element, 2);
        u.closeBrowser();
    }
  2. Select by text

    selectText(String, String, String) public class className implements Utilities {
        u.setBrowser("chrome");
        u.go("https://www.theminitools.com/tools/test-case-manager.html");
        u.selectText("id", "testType", "Security");
        u.closeBrowser();
    }

    If you have already found the WebElement and stored it in a variable with the selectText(WebElement, String) method, then you can simply pass that WebElement instead of the Locator Type and it's value. Here is the sample Code for this,

    public class className implements Utilities {
        u.setBrowser("chrome");
        u.go("https://www.theminitools.com/tools/test-case-manager.html");
        WebElement element = u.find("id","testType");
        u.selectText(element, "Security");
        u.closeBrowser();
    }
  3. Select by value

    selectValue(String, String, String) public class className implements Utilities {
        u.setBrowser("chrome");
        u.go("https://www.theminitools.com/tools/test-case-manager.html");
        u.selectValue("id", "testType", "Performance");
        u.closeBrowser();
    }

    If you have already found the WebElement and stored it in a variable with the selectValue(WebElement, String) method, then you can simply pass that WebElement instead of the Locator Type and it's value. Here is the sample Code for this,

    public class className implements Utilities {
        u.setBrowser("chrome");
        u.go("https://www.theminitools.com/tools/test-case-manager.html");
        WebElement element = u.find("id","testType");
        u.selectValue(element, "Performance");
        u.closeBrowser();
    }

 


Get all the values in a drop down list

For some use case you might want to fetch all the values listed in a drop dwon field (<select>). You can do this with the getOptions(String, String) method. You do not need to find the <select> first and then iterate through to get all the options. Just this one line code [ getOptions(String, String) ] will do the job for you. This method has a return type of List<WebElement>.

public class className implements Utilities {
    u.setBrowser("chrome");
    u.go("https://www.theminitools.com/tools/test-case-manager.html");
    u.getOptions("id", "testType");
    u.closeBrowser();
}

If you have already found the WebElement and stored it in a variable with the getOptions(WebElement) method, then you can simply pass that WebElement instead of the Locator Type and it's value. Here is the sample Code for this,

public class className implements Utilities {
    u.setBrowser("chrome");
    u.go("https://www.theminitools.com/tools/test-case-manager.html");
    WebElement element = u.find("id","testType");
    u.getOptions(element);
    u.closeBrowser();
}

 


getText()

To get the content of any element or div from a web-page you don't need to first find the element and then append the .getText() method of Selenium.

You can simply use getText(String, String) from the TMT Testing Utilities library.

getText(String, String) returns a String value. You can store this in a variable and verify with an expected value for your test.

public class className implements Utilities {
    u.setBrowser("chrome");
    u.go("https://www.theminitools.com/tools/test-case-manager.html");
    System.out.println(u.getText("xpath", "//table[@id='cases']//h2"));
    u.closeBrowser();
}

If you have already found the WebElement and stored it in a variable with the find(String, String) method, then you can simply pass that WebElement instead of the Locator Type and it's value. Here is the sample Code for this,

public class className implements Utilities {
    u.setBrowser("chrome");
    u.go("https://www.theminitools.com/tools/test-case-manager.html");
    WebElement text = u.find("xpath","//table[@id='cases']//h2");
    System.out.println(u.getText(text));
    u.closeBrowser();
}

 


Get attribute value from an element

In a certain use case you might want to fetch the value of a particualr attribute of a WebElement. You can do this with the attribute(String, String, String) method.

attribute(String, String, String) accepts three parameters,

  1. Locator Type: This can be any of "xpath" "css" "id" "tag" "class" "link" "partialLink" "name"
  2. The value of the Locator. For example: value of an XPATH woulc be "//a[@class='link']"
  3. Name of the arrtibute whose value you want to fetch.

attribute(String, String, String) returns a String value. You can store this in a variable and verify with an expected value for your test.

public class className implements Utilities {
    u.setBrowser("chrome");
    u.go("https://www.theminitools.com/tools/test-case-manager.html");
    System.out.println(u.attribute("xpath", "//input[@id='report']", "value"));
    u.closeBrowser();
}

If you have already found the WebElement and stored it in a variable with the find(String, String) method, then you can simply pass that WebElement instead of the Locator Type and it's value. Here is the sample Code for this,

public class className implements Utilities {
    u.setBrowser("chrome");
    u.go("https://www.theminitools.com/tools/test-case-manager.html");
    WebElement att = u.find("xpath", "//input[@id='report']");
    System.out.println(u.attribute(att, "value"));
    u.closeBrowser();
}

 


Alert

Using the alert(String) method you can easily handle the alert box appearing in the browser. This method accepts the message that the alert box is supposed to display as a String input. It then verifies that the actual message displaying in the alert matched and clicks the "Ok" button to accept and close the alert box.

The alert(String) method returns a String value that helps the tester to undestand whether the alert box had the correct message and was indeed accepted and closed.

public class className implements Utilities {
    u.setBrowser("chrome");
    u.go("https://www.example.com");
    System.out.println(u.alert("Expected message goes here..."));
    u.closeBrowser();
}

 


Confirmation box

To handle a confirmation box you can use the alert(String, Boolean) method of TMT Testing Utilities . This method accepts 2 inputs,

  1. Message that will display in the confirmation box. This will be a String input.
  2. A Boolean value.
    • true to accept or click "Ok / Yes".
    • falue to dismiss or click "Cancel / No".

The alert(String, Boolean) method returns a String value that helps the tester to undestand whether the alert box had the correct message and was indeed accepted or dismissed and closed.

public class className implements Utilities {
    u.setBrowser("chrome");
    u.go("https://www.example.com");
    System.out.println(u.alert("Expected message goes here...", true));
    u.closeBrowser();
}

 


Prompt

Sometimes websites show up a prompt box to accept some accept input inform of a text based input from a user and then click on a button, usually labelled "Ok" or "Accept" to submit and close the prompt. It can be a custom CAPTCHA or any other input. Such a propmt can be automated or handled with prompt(String) method of TMT Testing Utilities .

public class className implements Utilities {
    u.setBrowser("chrome");
    u.go("https://www.example.com");
    u.prompt("Your input here...");
    u.closeBrowser();
}

 


Drag and Drop

With several interactive web applications there can be uses cases where you might want to drag a WebElement from one place and drop it to another (simply put, move it from one place to another).

dragAndDrop() method of TMT Testing Utilities allows you to do this. This method can help you move the elements in four different ways,

  1. You can drag a WebElement from it's current location to another location by providing the x and y axis pixel points on the screen.

    Syntax for this method is
    dragAndDrop(String, String, int, int)

    The accepted parameters for this method are Locator Type, Locator value, x-axis pixel position, y-axis pixel position in that order.

  2. If you have already found the variable and stored in in a variable of type WebElement, you can simply pass that WebElement variable as the first parameter with destination x and y axis position.

    Syntax for this method is
    dragAndDrop(WebElement, int, int)

    The accepted parameters for this method are WebElement, x-axis pixel position, y-axis pixel position in that order.

  3. More often than none, instead of moving the WebElement from it's current position to a pixel location, you would come across use cases where you might need to move the WebElement from one (it's source) WebElement to another (it's destination) WebElement.

    Syntax for this method is
    dragAndDrop(String, String, String, String)

    The accepted parameters for this method are Source WebElement Locator Type, Source WebElement Locator value, Destination WebElement Locator Type, Destination WebElement Locator value in that order.

  4. It may be possible that you have already located the source and destination WebElement and stored them in their respective variables. In such a case instead of passing the Locator Type and the Locator value of source and destination WebElements, you can simply pas the variables in the dragAndDrop().

    Syntax for this method is
    dragAndDrop(WebElement, WebElement)

    The accepted parameters for this method are Source WebElement, Destination WebElement in that order.

 


Get the domain of the current web-page

For a certain use case you might want to get the domain name from the URL of the current web-page. You can do this with the

getDomain() method of TMT Testing Utilities .

This method returns the domain as a String

public class className implements Utilities {
    u.setBrowser("chrome");
    u.go("https://www.theminitools.com/tools/test-case-manager.html");
    System.out.println(u.getDomain(text));
    u.closeBrowser();
}

 


Find all the links in a web-page

Want to do a link test for all the links in your web-page? You will first need to find all the links from the page. For this you can use the getAllLinks() method of TMT Testing Utilities .

getAllLinks() will check every element that has a src or a href attribute and then get the value of that attribute of the tag.

<a> and <link> tags have the attribute href.

<img> and <script> tags have the attribute src.

getAllLinks() finds the values of all the src and href attributes in the web-page and returns them as a List<String>.

public class className implements Utilities {
    u.setBrowser("chrome");
    u.go("https://www.theminitools.com/sitemap.html");
    System.out.println(u.getAllLinks());
    u.closeBrowser();
}

 


Find all images in the Web-page

To get the URL of all the images in the web-page use getImages() method of TMT Testing Utilities .

getImages() has a return type of List<String>. This means that it will fetch all the URLs from the src of images and add them to a list of String type.

public class className implements Utilities {
    u.setBrowser("chrome");
    u.go("https://www.theminitools.com/sitemap.html");
    System.out.println(u.getImages());
    u.closeBrowser();
}

 


Find all hyperlinks in the web-page

With getLinks() method you can fetch all the hyperlinks (<a>) with the web-page.

getLinks() has a return type of List<String>. This means that it will fetch all the URLs from the href arrtibute of hyperlinks and add them to a list of String type.

public class className implements Utilities {
    u.setBrowser("chrome");
    u.go("https://www.theminitools.com/sitemap.html");
    System.out.println(u.getLinks());
    u.closeBrowser();
}

 


Get the HTML Dom | Page Source

getSource() with get the whole HTML source of the web-page as a String.

public class className implements Utilities {
    u.setBrowser("chrome");
    u.go("https://www.theminitools.com/sitemap.html");
    System.out.println(u.getSource());
    u.closeBrowser();
}

 


Get Tag

For a certain use case you might want to get the tag name of a WebElement.

To do this you can use the getTag(String, String) method of TMT Testing Utilities . getTag() returns the name of the tag as a String.

public class className implements Utilities {
    u.setBrowser("chrome");
    u.go("https://www.theminitools.com/sitemap.html");
    System.out.println(u.getTag(String, String));
    u.closeBrowser();
}

If you already have the WebElement stored in a variable, you can simply pass that as an argument/parameter in the method instead of the locator type and it's value.

getTag(WebElement) public class className implements Utilities {
    u.setBrowser("chrome");
    u.go("https://www.theminitools.com/sitemap.html");
    WebElement tag = u.find("xpath", "//input[@id='report']");
    System.out.println(u.getTag(tag));
    u.closeBrowser();
}

 


Get title of current web-page

getTitle() method when invoked will return the title (content of <title></title> tag) as a String.

public class className implements Utilities {
    u.setBrowser("chrome");
    u.go("https://www.theminitools.com/sitemap.html");
    System.out.println(u.getTitle());
    u.closeBrowser();
}

 


Frames

To access any element inside an iframe you will first need to switch to that perticular frame. There are 4 ways by which you can switch to a frame,

  1. By ndex of the frame:
    iframe(int)
  2. By name of the frame:
    iframe(String)
  3. By locator:
    iframe(String, String)
  4. By WebElement:
    iframe(WebElement)

 


Switch to the default / original frame

With the help of exitIframe() method of TMT Testing Utilities you can switch back to the first or the original (default) frame. It doesn't matter whether you are inside a nested frame. This method will return to the initial index.

 


Back

Just call / invoke the back() method to nagivate to the previous page. This is equivalent of clicking the go back button of the browser.

public class className implements Utilities {
    u.setBrowser("chrome");
    u.go("https://www.theminitools.com/tools/test-case-manager.html");
    u.goTo("https://www.theminitools.com/");
    u.back();
    u.closeBrowser();
}

 


Forward

After navigating to the previous page, if you want to navigate back forward, simply invoke the forward() method.

public class className implements Utilities {
    u.setBrowser("chrome");
    u.go("https://www.theminitools.com/tools/test-case-manager.html");
    ugoTo("https://www.theminitools.com/");
    u.back();
    u.forward();
    u.closeBrowser();
}

 


Refresh or Reload the web-page | F5 key

You want to refresh or reload your current page? Just invoke the f5() method of TMT Testing Utilities .

public class className implements Utilities {
    u.setBrowser("chrome");
    u.go("https://www.theminitools.com/tools/test-case-manager.html");
    u.f5();
    u.closeBrowser();
}

 


Full Screen view

Want to operate the website / application in a fullscreen view? Just invoke fullScreen() method of TMT Testing Utilities .

public class className implements Utilities {
    u.setBrowser("chrome");
    u.go("https://www.theminitools.com/tools/test-case-manager.html");
    u.fullScreen();
    u.wait(5);
    u.closeBrowser();
}

 


Maximize browser window

Simply call maximize() method to maximize the browser window.

 


Minimize browser window

Simply call minimize() method to maximize the browser window.

 


Open a new Tab in browser

Sometimes, while testing you might need to open a URL in a new tab and leave the current tab as is so that you can return later to contoniue further actions.

newTab() method of TMT Testing Utilities allows you to open a new tab. Once the new tab is opened you can simply use goTo() method to open the new link/URL.

 


Switch to Tab

In case you have multiple tabs open in your browser you can switch from one tab to another with the help of the tab index using tab(int) method of TMT Testing Utilities .

Open a new browser window

For a certain use case, you might want to open a completely new browser window rather than a new tab. You can do this with newWindow() method of TMT Testing Utilities .

 


Get Height of abrowser window

With the help of height() method of TMT Testing Utilities you can get the height of the display area of the browser window.

height() method returns an integer value.

 


Get Height of an element

You want to verify the height of a WebElement? Pass the Locator Type and the value of the locator in height(String, String) method of TMT Testing Utilities to find the element and return it's height as an integer.

If you have already found the WebElement with find(String, String) method and stored it in a variable, you can simply pass that variable in height(WebElement) method to get the height of the element.

 


Set position of element

setPosition(int, int)

 


Get position of element

position()

 


position(String, String)

 


position(WebElement)

 


positionX()

 


positionX(String, String)

 


positionX(WebElement)

 


positionY()

 


positionY(String, String)

 


positionY(WebElement)

 


Print current web-page

print(String)

 


Resize and element

resize(int, int)

 


resize(String, String, int, int)

 


resize(WebElement, int, int)

 


Take a screenshot

screenshot(String)

 


size()

 


size(String, String)

 


size(WebElement)

 


snapElement(String, String, String)

 


snapElement(String, WebElement)

 


Add a wait | Pause in code execution

wait(int)

 


width()

 


width(String, String)

 


width(WebElement)

 

 

Cookie

cookie()

 


Add Cookie

addCookie(String, String)

 


addCookie(String, String, String)

 


Delete Cookie

deletCookie(Cookie)

 


deletCookieByName(String)

 


deletCookies()

 


Get Cookie from the web-page

getCookie(String)

 


Write data in a file

writeFile(String, String)

 


Read File

readFile(String)

The Mini Tools