TMT-Testing-Utilities-1.0.0.jar
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
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
You can simply invoke the method
public class className implements Utilities {
u.setBrowser("chrome");
}
Close Browser
Once all your tests have executed, you can breakdown or close the browser with
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
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
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
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.
You can find a WebElement with the
-
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 .
- "xpath" to find element by
- 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();
}
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
Syntax of
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.
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
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
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
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.
-
Select by index
selectIndex(String, String, int) public class classNameimplements 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 classNameimplements 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();
} -
Select by text
selectText(String, String, String) public class classNameimplements 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 classNameimplements 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();
} -
Select by value
selectValue(String, String, String) public class classNameimplements 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 classNameimplements 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
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
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
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
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
- Locator Type: This can be any of "xpath" "css" "id" "tag" "class" "link" "partialLink" "name"
- The value of the Locator. For example: value of an XPATH woulc be "//a[@class='link']"
- Name of the arrtibute whose value you want to fetch.
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
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
The
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
- Message that will display in the confirmation box. This will be a String input.
- A Boolean value.
- true to accept or click "Ok / Yes".
- falue to dismiss or click "Cancel / No".
The
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
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).
-
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.
-
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.
-
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.
-
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
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() 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.
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
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
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
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
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.
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
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,
- By ndex of the frame:
iframe(int) - By name of the frame:
iframe(String) - By locator:
iframe(String, String) - By WebElement:
iframe(WebElement)
Switch to the default / original frame
With the help of
Back
Just call / invoke the
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
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
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
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
Minimize browser window
Simply call
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.
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
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
Get Height of abrowser window
With the help of
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
If you have already found the WebElement with
Set position of element
Get position of element
Print current web-page
Resize and element
Take a screenshot
Add a wait | Pause in code execution
Cookie
Add Cookie
Delete Cookie
Get Cookie from the web-page
Write data in a file