Skip to content

Commit b750f39

Browse files
committed
docs: test automation
1 parent 30355cb commit b750f39

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed
+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
## 8.5. Test Automation with Selenium
2+
3+
Selenium is a powerful open-source framework for automating web browsers. It provides a way to script interactions with web browsers, making it invaluable for automating repetitive tasks, such as testing web applications or web scraping. In this section, we'll dive into the world of Selenium test automation with examples.
4+
5+
### Why Selenium?
6+
7+
Selenium is widely used for web test automation for the following reasons:
8+
9+
1. **Cross-browser Testing:** Selenium supports various web browsers, including Chrome, Firefox, Safari, and Edge, allowing you to test your web applications across different browsers.
10+
11+
2. **Cross-Platform:** Selenium is platform-independent, making it suitable for Windows, macOS, and Linux environments.
12+
13+
3. **Programming Language Support:** Selenium supports multiple programming languages, including Java, Python, C#, and more. You can choose the language you're most comfortable with.
14+
15+
4. **Ecosystem:** Selenium has a vast ecosystem of tools and libraries for test automation, reporting, and parallel testing.
16+
17+
### Getting Started with Selenium
18+
19+
Before diving into examples, you need to set up a Selenium environment. Here, we'll use the Selenium WebDriver for Python as an example.
20+
21+
#### Step 1: Install Selenium
22+
You can install Selenium for Python using pip:
23+
24+
```bash
25+
pip install selenium
26+
```
27+
28+
#### Step 2: Download Web Drivers
29+
Selenium requires browser-specific drivers to control web browsers. For example, you'll need the ChromeDriver for Google Chrome or the GeckoDriver for Mozilla Firefox. Download the appropriate driver for your browser and add its location to your system's PATH.
30+
31+
#### Step 3: Write Your First Selenium Script
32+
33+
Let's write a simple Selenium script in Python that opens a web page and interacts with it.
34+
35+
```python
36+
from selenium import webdriver
37+
38+
# Set the path to your web driver (e.g., chromedriver.exe)
39+
driver = webdriver.Chrome(executable_path='path/to/chromedriver.exe')
40+
41+
# Open a webpage
42+
driver.get('https://example.com')
43+
44+
# Find an element by its CSS selector and interact with it
45+
element = driver.find_element_by_css_selector('input[name="q"]')
46+
element.send_keys('Selenium automation')
47+
element.submit()
48+
49+
# Close the browser
50+
driver.quit()
51+
```
52+
53+
In this script:
54+
55+
- We import the `webdriver` module from Selenium.
56+
- We create a driver instance for the Chrome browser (you should use the appropriate driver for your browser).
57+
- We open a webpage, which, in this case, is 'https://example.com'.
58+
- We find an HTML input element using its CSS selector, send text to it, and submit a form.
59+
- Finally, we close the browser.
60+
61+
### Selenium Test Automation Example
62+
63+
Let's look at an example of a Selenium test script for a simple web application. We'll automate a login process on a demo website.
64+
65+
```python
66+
from selenium import webdriver
67+
68+
# Set the path to your web driver
69+
driver = webdriver.Chrome(executable_path='path/to/chromedriver.exe')
70+
71+
# Open the login page
72+
driver.get('https://example.com/login')
73+
74+
# Find username and password fields and submit button
75+
username = driver.find_element_by_id('username')
76+
password = driver.find_element_by_id('password')
77+
login_button = driver.find_element_by_id('login-button')
78+
79+
# Enter credentials and submit
80+
username.send_keys('your_username')
81+
password.send_keys('your_password')
82+
login_button.click()
83+
84+
# Verify the login was successful
85+
welcome_message = driver.find_element_by_css_selector('.welcome-message')
86+
assert welcome_message.text == 'Welcome, Your Name!'
87+
88+
# Close the browser
89+
driver.quit()
90+
```
91+
92+
In this script, we:
93+
94+
- Open a login page.
95+
- Locate the username and password input fields and the login button.
96+
- Enter credentials.
97+
- Click the login button.
98+
- Verify that the login was successful by checking for a welcome message.
99+
100+
This is a simple example, but Selenium can be used for complex web application testing, including form submissions, navigation, and data extraction.
101+
102+
Selenium is a powerful tool for web automation, including testing web applications. By combining it with your preferred programming language, you can create robust, automated test suites to ensure the quality of your web applications.

0 commit comments

Comments
 (0)