Automation Testing

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Contents

Automation Testing: ............................................................................................................... 1


Automation Testing: ........................................................................................................... 1
Why we need automation testing?....................................................................................... 1
Steps to test a scenario using automation approach: ........................................................... 2
Different types of locators:.................................................................................................. 2
Absolute vs Relative Xpath: ................................................................................................. 3
Automation Frameworks: ................................................................................................... 4
Write a script to click the login button: ................................................................................ 4
Database Testing:............................................................................................................... 5
Selenium............................................................................................................................ 6

Automation Testing:

Automation Testing:
Automation testing is when software tools and scripts are used to control
the execution of tests and compare the actual outcome with the expected
outcome. It's like having a robot that can test software for you
automatically, saving time and effort compared to manual testing.

Why we need automation testing?


Automation testing is needed for several reasons:
- It saves time and effort by automating repetitive tasks.
- It improves test coverage by running a large number of tests quickly.
- It enhances accuracy by reducing human error.
- It enables regression testing, ensuring that new changes don't break
existing functionality.
Steps to test a scenario using automation approach:
The steps generally involve:
- Identifying the scenario to be tested.
- Creating test scripts or test cases.
- Setting up test data if necessary.
- Running the test scripts automatically.
- Analyzing the test results.

Different types of locators:


Locators are used to identify web elements on a webpage. Common types
include:
- ID
- Class Name
- Name
- Tag Name
- Link Text
- Partial Link Text
- CSS Selector

- XPath:
XPath is a query language used for selecting nodes from an XML or HTML
document. It allows you to navigate through the elements and attributes of
an XML/HTML structure to locate specific elements.

For example, if you have an HTML document with the following structure:
```html
<html>
<body>
<div id="content">
<h1>Welcome</h1>
<p>This is a paragraph.</p>
</div>
</body>
</html>
```
You can use XPath to identify elements based on their attributes or
position in the document. For instance:
- `/html/body/div` would select the `<div>` element.
- `/html/body/div/h1` would select the `<h1>` element.
- `/html/body/div/p` would select the `<p>` element.

XPath expressions can also use various functions, axes, and operators to
refine the selection criteria.

In automation testing, XPath is commonly used with tools like Selenium


WebDriver to locate elements on a webpage, enabling actions such as
clicking buttons, filling forms, or extracting data. For instance, you might
use XPath to locate the "Login" button on a login page and then interact
with it programmatically.

Absolute vs Relative Xpath:


- Absolute Xpath: Specifies the exact location of an element in the HTML
structure from the root node. It starts with a single forward slash (/).
- Relative Xpath: Specifies the location of an element relative to another
element in the HTML structure. It starts with a double forward slash (//).
Automation Frameworks:
- Data-driven: Tests are driven by external data sources.
- Behavior-driven: Tests are written in a human-readable format focusing
on behaviors.
- Modular: Tests are divided into smaller, reusable modules.
- Hybrid: Combines two or more frameworks, such as data-driven and
modular.
- Linear: Simplest framework where tests are written sequentially.

Write a script to click the login button:

```python
from selenium import webdriver

# Start a new instance of Chrome WebDriver


driver = webdriver.Chrome()

# Open the webpage containing the login button


driver.get("https://example.com/login")

# Find the login button by its XPath


login_button = driver.find_element_by_xpath("//button[contains(text(),
'Login')]")

# Click the login button


login_button.click()

# Close the WebDriver session


driver.quit()
```

In this script:
- We import the necessary module `webdriver` from `selenium`.
- We initiate a new instance of Chrome WebDriver.
- We navigate to the login page (replace `"https://example.com/login"`
with the actual URL of the login page).
- We locate the login button using XPath. Here, we're using a partial text
match with `contains(text(), 'Login')` assuming the button text contains
"Login". Adjust the XPath expression according to the actual HTML
structure.
- We perform a click action on the login button.
- Finally, we close the WebDriver session to release the resources.

Make sure you have the Selenium WebDriver for Python installed (`pip
install selenium`) and the appropriate WebDriver (e.g., ChromeDriver)
downloaded and configured.

Database Testing:
Database testing involves verifying that data is being stored, retrieved, and
manipulated correctly in a database. This includes checking data integrity,
validation rules, and database schema. It ensures that the data stored in
the database is accurate and consistent.
Selenium is a popular open-source automation testing framework used
for automating web browsers. It allows you to control web browsers
programmatically, mimicking user interactions such as clicking buttons,
filling forms, navigating pages, and verifying content. Essentially, Selenium
acts as a bridge between your code and the web browser, enabling you to
write scripts that interact with web applications just like a real user would.
Selenium is widely used in software testing for:
- Functional testing: Verifying that the application behaves as expected
from a user's perspective.
- Regression testing: Ensuring that new changes do not break existing
functionality.
- Cross-browser testing: Checking compatibility across different web
browsers.
- Performance testing: Automating repetitive tasks to evaluate system
performance.

Overall, Selenium simplifies the process of testing web applications by


providing a flexible and powerful framework for automating browser
interactions, thereby improving testing efficiency and reliability.

You might also like