STA - UNIT V march 13
STA - UNIT V march 13
UNIT V
TEST AUTOMATION AND TOOLS
Automated Software Testing, Automate Testing of Web Applications, Selenium:
Introducing Web Driver and Web Elements, Locating Web Elements, Actions on Web
Elements, Different Web Drivers, Understanding Web Driver Events, TestNG:
Understanding TestNG.xml, Adding Classes, Packages, Methods to Test, Test Reports.
In order to determine whether a test is suitable for automation, testers can check if it fits the
following criteria:
• The tests should be highly repetitive and take a long period of time to perform if it is
done manually
• The testing path must be predictable, as it has been verified earlier through manual testing
• The tests that involve the testing of frequently used features that introduce high-risk
conditions
• The tests that require multiple datasets and run on several different hardware or
software platforms and configurations
• Tests that are not possible for human manual testing, e.g., thousands of concurrent users
trying to log in at the same time
If a test meets all these criteria mentioned above, you can consider test automation platforms
By following the pyramid approach, test automation becomes a structured and balanced part of
your software development lifecycle, helping you catch issues early without slowing down the
release process.
CCS366- SOFTWARE TESTING AND
✔ Functional Testing
A single end-user can make the whole system crash in minutes, even after unit, integration,
and performance tests have passed. This usually happens because the user does something
the developers did not expect. The purpose of functional testing is therefore to ensure that the
functionality of the software works as intended for an end-user. It tests this through the UI of
the application. Examples of functional tests in a web application UI include testing:
● The login to your web application is successful across browsers and devices
● The web application is interacting as intended with external databases and syncing
successfully
● Invoices are being sent and received with the correct information and securely
● Buttons, text fields, menus, etc., are working as per the requirements
✔ Regression Testing
Regression testing is critical whenever updates or changes are made to the application. It
ensures that new code doesn't negatively impact existing functionality. This type of testing:
● Verifies Existing Functionality: Ensures that previous functions still operate as intended
after modifications.
● Identifies Unintended Consequences: Catches any new bugs introduced by recent changes.
✔ End-to-End Testing
End-to-end testing examines the complete functionality of the web application from start to
finish, emulating real user scenarios. It aims to ensure that all components of the application
work together seamlessly. This involves:
● Workflow Testing: Ensuring all the integrated parts of the application interact
correctly.
● Data Integrity Testing: Confirming that data maintains its integrity throughout all
CCS366- SOFTWARE TESTING AND
transactions.
✔ Cross-Browser Testing
With the variety of browsers available, browser-based testing ensures that the web application
performs consistently across different browsers and their versions. This testing type:
● Ensures Compatibility: Verifies that the application functions correctly on various
browsers.
● Identifies Browser-Specific Issues: Highlights any layout or functional issues
unique to certain browsers.
● Related reading: How to automate web testing across browsers and devices
✔ Performance Testing
Performance testing evaluates the web application’s stability and responsiveness under various
conditions. This includes:
● Load Testing: Assessing the application's ability to handle high volumes of users.
● Stress Testing: Determining the application's breaking point and how it recovers
from failure.
● Speed Testing: Measuring response times and the speed of page loading under
normal conditions.
5.2.4 Why to automate Web application testing / Benefits of Web Application testing
There are so many reasons to automate web application testing including
✔ Increased Speed
✔ Reduced Cost
✔ Improved Quality
✔ Increased Confidence
Cucumber: Cucumber is a free, open-source testing framework that helps users write
automated tests in plain English. It's used for behavior-driven development (BDD).
Advantages:
● It is an open-source automated software testing tool.
● It helps in writing acceptance tests for our web applications.
Disadvantages:
● Integration and its dependency on generating reports through plugins can be
challenging enough.
● Every time a new attribute or feature undergoes it, we have to ensure all current steps
and validate them to see if they can be used.
Selenium : Selenium is an open-source testing tool that automates web application testing
across browsers and operating systems. It can be used for a variety of test types, including
system testing, regression testing, and performance testing.
Advantages:
● This tool is open-source and widely supports all languages and frameworks.
● It comes with heavy library packages.
● It supports cross-browser automation, API automation, and database automation.
CCS366- SOFTWARE TESTING AND
● Testers can use it for regression, exploratory testing, and quick reproduction of bugs.
● It is highly known for its flexibility with ease of implementation.
Disadvantages:
● Test Maintenance in selenium can become cumbersome and even expensive
sometimes
● Selenium requires coding skills, if not exceptional but above average
● It is only supported for web applications
● Technical support and its reliability can cause problems
5.3 SELENIUM: INTRODUCING WEB DRIVER AND WEB ELEMENTS
Selenium is a powerful tool for controlling web browsers through programs. It is functional
for all browsers, works on all major OS, and its scripts are written in various languages i.e.,
Python, Java, C#, etc., we will be working with Python. Selenium has four major
components : Selenium IDE, Selenium RC, Selenium Web driver, and Selenium GRID.
1. Selenium IDE
Selenium IDE is a Firefox add-on that allows users to record, edit, debug, and play back tests
captured in the Selenese format, which was introduced in the Selenium Core version. It also
provides us with the ability to convert these tests into the Selenium RC or Selenium
WebDriver format. We can use Selenium IDE to do the following:
● Create quick and simple scripts using record and replay, or use them in exploratory testing
● Create scripts to aid in automation-aided exploratory testing
● Create macros to perform repetitive tasks on Web pages
2. Selenium RC (Remote control)
Selenium Remote Control (RC) was one of the earliest Selenium tools,
preceding WebDriver. It allowed testers to write automated web application tests in various
programming languages like Java, C#, Python, etc. The key feature of Selenium RC was its
ability to interact with web browsers using a server, which acted as an intermediary between
the testing code and the browser. Its architecture is complex and has limitations. One must
have good programming language while working with Selenium RC.
3. Selenium WebDriver
Selenium WebDriver is the successor of Selenium RC (Remote Control), which has been
officially deprecated. Selenium WebDriver accepts commands using the JSON-Wire protocol
(also called Client API) and sends them to a browser launched by the specific driver class
(such as ChromeDriver, FirefoxDriver, or IEDriver). This is implemented through a browser-
specific browser driver. It works with the following sequence:
1. The driver listens to the commands from Selenium
2. It converts these commands into the browser's native API
CCS366- SOFTWARE TESTING AND
3. The driver takes the result of native commands and sends the result back to Selenium.
5.3.1 WebElements
A web page is composed of many different types of HTML elements, such as links,
textboxes, dropdown buttons, a body, labels, and forms. These are called WebElements in the
context of WebDriver. Together, these elements on a web page will achieve the user
functionality.
For example, let's look at the HTML code of the login page of a website:
<html>
<body>
<form id="loginForm">
<label>Enter Username: </label>
<input type="text" name="Username"/>
<label>Enter Password: </label>
<input type="password" name="Password"/>
<input type="submit"/>
</form>
<a href="forgotPassword.html">Forgot Password ?</a>
</body>
</html>
In the preceding HTML code, there are different types of WebElements, such as <html>,
<body>, <form>, <label>, <input>, and <a>, which together make a web page provide the
Login feature for the user. Let's analyze the following WebElement:
CCS366- SOFTWARE TESTING AND
<label>Enter Username: </label>
Here, <label> is the start tag of the WebElement label. Enter Username: is the text present
on the label element. Finally, </label> is the end tag, which indicates the end of a
WebElement. Similarly, take another WebElement:
<input type="text" name="Username"/>
In the preceding code, type and name are the attributes of the WebElement input with the
text and Username values, respectively.
UI-automation using Selenium is mostly about locating these WebElements on a web
page and executing user actions on them.
Eg : 1
<html>
<body>
<form id="loginForm">
<input name="username" type="text" />
<input name="password" type="password" />
<input name="continue" type="submit" value="Login" />
</form>
</body>
<html>
We can locate elements using the following commands
login_form = driver.find_element(By.ID, 'loginForm')
● The first element with the id attribute value matching the location will be returned. If
no element has a matching id attribute, a NoSuchElementException will be raised.
Eg : 2
<html>
<body>
CCS366- SOFTWARE TESTING AND
<h1>Welcome</h1>
<p>Are you sure you want to do this?</p>
<a href="continue.html">Continue</a>
<a href="cancel.html">Cancel</a>
</body>
<html>
Now after you have created a driver, you can locate an element using
login_form = driver.find_element(By.LINK_TEXT, 'Continue')
login_form = driver.find_element(By.PARTIAL_LINK_TEXT, 'Conti')
login_form = driver.find_element(By.TAG_NAME, 'h1')
Eg :
WebElement element=driver.findElement(By.Id("inputId"));
element.sendKeys(“Hello”);
iii. Clear
The element clear command resets the content of an element. This requires an element to
be editable, and resettable. Typically, this means an element is an input element of a form with
a text type or an element with a content-editable attribute. If these conditions are not met, an
invalid element state error is returned.
Eg : WebElement element=driver.findElement(By.Id("inputId"));
element.clear();
iv. submit() method
The submit() action can be taken on a Form or on an element, which is inside a Form element.
This is used to submit a form of a web page to the server hosting the web application.
Eg :
WebElement form=driver.findElement(By.Id("formId"));
Form.submit();
v. select
Selenium provide Select class to interact with dropdowns and select options. It can
create an instance of the Select class, locate the drop down element and then use methods
like selectByValue( ), selectByIndex( ), etc to choose the desired option
Eg :
WebElement element=driver.findElement(By.Id("dropdownId"));
Select sel = new Select(dropdown);
Sel.selectByIndex(1)
Methods of Action Class
Action class is useful mainly for mouse and keyboard actions. In order to perform such actions,
Selenium provides various methods.
Mouse Actions in Selenium:
1. doubleClick(): Performs double click on the element
2. clickAndHold(): Performs long click on the mouse without releasing it
3. dragAndDrop(): Drags the element from one point and drops to another
4. moveToElement(): Shifts the mouse pointer to the center of the element
5. contextClick(): Performs right-click on the mouse
Keyboard Actions in Selenium:
1. sendKeys(): Sends a series of keys to the element
2. keyUp(): Performs key release
3. keyDown(): Performs keypress without release
Different WebElements will have different actions that can be taken on them. For
example, in a textbox element, we can type in some text or clear the text that is already typed
in it. Similarly, for a button, we can click on it, get the dimensions of it, and so on, but we
CCS366- SOFTWARE TESTING AND
cannot type into a button, and for a link, we cannot type into it.
So, though all the actions are listed in one WebElement interface, it is the test script
developer's responsibility to use the actions that are supported by the target element. In case
we try to execute the wrong action on a WebElement, we don't see any exception or error
thrown and we don't see any action get executed; WebDriver ignores such actions silently.
Each Web driver has its own specific configuration requirements such as downloading and
setting up the correct driver ensuring the compatibility with the browser version used. The
web driver implementation act as intermediate between Selenium Web driver API and the
respective Web browsers. This enables integration and automation with the browser actions
for testing purposes.
Comparison of different WebDrivers in Selenium
WebDriver events provide hooks or listeners that allow testers to observe and interact with
different stages of the automation process. Here are the key aspects to understand about
WebDriver events in Selenium:
Event listeners:
● Selenium WebDriver provides an event-driven architecture that allows the registration of
event listeners. Event listeners are objects that implement specific interfaces, such as
WebDriverEventListener or EventListener, to handle different types of events during test
execution.
● In the example above, the EventFiringWebDriver class is used to wrap the original
WebDriver instance. This allows the event listener to intercept WebDriver events.
Example (Java):
public class MyEventListener implements WebDriverEventListener {
// Implement methods for desired events
@Override
public void beforeClickOn(WebElement element, WebDriver driver) {
// Perform custom actions before clicking on an element System.out.println("About to click
on element: + element);
}
@Override
public void afterClickOn(WebElement element, WebDriver driver) {
// Perform custom actions after clicking on an element System.out.println("Clicked on
element: " + element);
}
// Implement other event methods as needed
}
● The example above demonstrates a custom event listener that logs messages before and
after clicking on web elements.
By utilizing WebDriver events and implementing custom event listeners, testers can gain
more control and insight into the test execution process. This allows for custom actions,
logging, error handling, and validation based on specific events during test automation,
leading to enhanced reporting and improved debugging capabilities
5.8 TestNG
TestNG is an open-source automated testing framework; where NG means Next Generation.
CCS366- SOFTWARE TESTING AND
The design goal of TestNG is to cover a wider range of test categories: unit, functional, end-to-
end, integration, etc., with more powerful and easy-to-use functionalities.
Advantages Of TestNG.xml
● It provides parallel execution of test methods.
● It allows the dependency of one test method on another test method.
● It helps in prioritizing our test methods.
● It allows grouping of test methods into test groups.
● It supports the parameterization of test cases using @Parameters annotation.
● It helps in Data-driven testing using @DataProvider annotation.
● It has different types of assertions that help in validating the expected results with the
actual results.
● It has different types of HTML reports, Extent reports, etc. for a better and clear
understanding of our test summary.
● It has listeners who help in creating logs.
#1) A Suite is represented by one XML file. It can contain one or more tests and is defined
by the <suite> tag.
Example: <suite name=”Search Suite”>
#2) A Test is represented by <test> and can contain one or more TestNG classes.
Example: <test name=”Search Test”>
#3) A Class is a Java class that contains TestNG annotations. Here it is represented by the
<class> tag and can contain one or more test methods.
Example:
<classes>
<class name="com.example.SearchTest"/>
</classes>
CCS366- SOFTWARE TESTING AND
#4) Method : This defines a test method. A test method is a Java method that is annotated with
@Test annotation
#5) Listeners : Listeners are used to listen to events during test execution
TestNG.xml Example:
2. Define XML structure: Define the basic structure of the XML file by adding the root
element. The root element in TestNG XML is typically <suite.
xml
<suite name="Test Suite">
<- Add test configurations and test tags here ->
</suite>
3. Add test configurations: Within the <suite> element, it can add various test
configurations. These configurations may include details such as test parameters, test
environment setup or other global settings.
xml
<suite name="Test Suite">
<parameter name="browser" value="chrome' />
<!-- Add more configurations as needed -->
</suite>
4. Define test tags: Within the <suite> element, it can define one or more <test> tags to
represent individual tests or test groups. Each <test> tag can have a unique name and
may contain one or more <classes> or <packages> tags to specify the test classes or
packages to be executed.
xml
<suite name="Test Suite">
<test name "Test 1">
<classes>
<class name="com.example.tests.TestCase1"/>
<class name="com.example.tests TestCase2"/>
</classes>
</test>
<!-- Add more tests as needed -->
</suite>
5. Save the XML file: Save the testng.xml file with the defined structure and
configurations.
CCS366- SOFTWARE TESTING AND
6. Execute TestNG XML file: To execute the TestNG XML file, it can use various
methods depending on its environment and tools.
shell
$ testng testng.xml
7. Build automation tools: If it is using build automation tools like Maven or Gradle, it
can configure the build script to execute the TestNG XML file as part of the build
process.
These steps provide a basic overview of creating and executing a TestNG XML file. The
specific details may vary based on itr project setup, environment and testing requirements. It's
recommended to refer to the TestNG documentation or consult the documentation of its specific
IDE or build automation tool for more detailed instructions on creating and executing TestNG
XML files.
XML
<class name="com.example.MyTestClass3"/>
● Once it has added the classes to the XML file, it can run the tests by running the
"testing.xml" file from the command line or from an IDE.
● Here are some additional tips for adding classes to testing.xml:
● The name of the class element must match the fully qualified name of the Java class.
● The order in which the classes are defined in the XML file determines the order in which
the tests will be executed.
● It can use the "groups" element to group the classes together. This can be helpful for
running the tests in parallel or filtering them out.
XML
<packages>
<package name="com.example.mypackage1"/>
<package name="com.example.mypackage2"/>
</packages>
CCS366- SOFTWARE TESTING AND
● Once it has added the packages to the XML file, it can run the tests by running the XML
file from the command line or from an IDE.
● To define packages in a testing.xml file, it can follow these steps:
1. Determine the organization strategy: Decide on the organization strategy for its test cases.
Identify how it wants to group and categorize them using packages. Consider factors such as
functionality, modules, features or any other meaningful criteria.
2. Define XML structure: Determine the XML structure or schema for representing packages
in the testing.xml file. Decide on the appropriate XML elements, attributes and hierarchy that
will be used to define packages
3. Open the XML file: Open the testing.xml file using a text editor or an XML editor. Ensure
that it has the necessary permissions to modify the file.
4. Identify the appropriate section: Identify the section in the XML file where it wants to add
the packages. This section could be specifically designated for packages or any other section
suitable for organizing test cases.
5. Add package elements: Within the appropriate section, add XML elements to represent the
packages. Use the defined XML structure or schema to ensure consistency.
6. Set package attributes: For each package element, set the necessary attributes to define the
package. This could include attributes like package names, identifiers, descriptions or any other
relevant metadata.
7. Nest packages if required: If its organization strategy involves nested or hierarchical
packages, create the necessary nested package elements within the appropriate parent packages.
8. Associate test cases: Associate the relevant test cases with their respective packages. It can
use XML elements or attributes to reference or include test cases within the package elements.
9. Save the XML file: Once it has defined the packages and associated test cases, save the
testing.xml file.
● The above steps provide a general guideline, but the actual implementation may differ
based on the requirements of its specific testing environment.
● Here are some of the benefits of using packages in XML for software testing:
• Improved organization: Packages can help to improve the organization of the tests by
grouping together classes and methods that are related to a specific functionality or
feature.
• Parallel execution: Packages can be used to run tests in parallel, which can help to
improve the performance of the testing process.
• Filtering: Packages can be used to filter out tests that it do not want to run.
Overall, packages in XML can be a valuable tool for organizing and running tests in
software testing.
• By applying these testing methods to its XML file, it can ensure its correctness, validate
its adherence to defined rules, verify data extraction, test integration scenarios and assess
performance characteristics.
● The testing methods it choose depend on the specific requirements and context of its
XML usage. It's often beneficial to employ a combination of these techniques to
thoroughly test XML files and their related processes.
• Suppose it has an XML file that represents a collection of books. Each book has attributes
such as title, author, publication year, and price. Here's a sample XML file:
xml
<library>
<book>
<title>The Great Gatsby</title>
<author>F. Scott Fitzgerald</author>
<year>1925</year>
<price>10.99</price>
</book> <book>
<title>To Kill a Mockingbird</title>
<author>Harper Lee</author>
<year>1960</year>
<price>12.99</price>
</book> </library>
To view the reports, it can open them in a web browser. The HTML reports will provide a
graphical overview of the test execution, while the XML reports can be used for further
processing or analysis.
• By using this XML structure, it can generate comprehensive test reports that capture metadata,
summary information, individual test case details, logs and error information, providing valuable
CCS366- SOFTWARE TESTING AND
insights into the software testing process.