Skip to content

Commit 064dc7e

Browse files
Add selenium-webdriver-python project
1 parent a9b7754 commit 064dc7e

13 files changed

+183
-3
lines changed

.vscode/settings.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"python.testing.pytestArgs": [
3+
"tests"
4+
],
5+
"python.testing.unittestEnabled": false,
6+
"python.testing.pytestEnabled": true
7+
}

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2024 Tech With Alex
3+
Copyright (c) 2023-2024 Tech With Alex / TechWithAlexDuta / Alexandru Duta
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

+34-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,34 @@
1-
# selenium-webdriver-python
2-
Selenium WebDriver tutorials with python and pytest
1+
# python
2+
Tutorials and sample web automation tests project using Selenium and Python
3+
4+
## Installation
5+
1. Make sure you have [.Python 3](https://www.python.org/downloads/) or newer installed on your machine (project was developed using python 3.12). Check installation
6+
```PS
7+
python -V
8+
```
9+
2. Clone this repository to your local machine.
10+
3. Open folder (`python`) in VS Code.
11+
4. Using terminal navigate to a project and run `pip install`
12+
```PS
13+
pip install -r requirements.txt
14+
```
15+
16+
## Usage
17+
1. Make sure you have the appropriate browser installed (`https://www.selenium.dev/documentation/webdriver/browsers/`)
18+
2. Open the project directory in VS Code or your preferred IDE.
19+
4. Run the tests using your preferred test runner or IDE or from terminal, e.g. use any of the below:
20+
```PS
21+
pytest .\tests\test_selenium_web_form.py
22+
pytest -m selenium
23+
```
24+
25+
## Tech
26+
- python 3.12
27+
- selenium 4
28+
- pytest 8
29+
30+
## YT channel
31+
Please check my YouTube channel for step by step implementation or detailed tutorials on automation and more: https://www.youtube.com/@TechWithAlexDuta
32+
33+
## License
34+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

pom/__init__.py

Whitespace-only changes.

pom/web_form_page.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""Web form page"""
2+
3+
from selenium import webdriver
4+
from selenium.webdriver.common.by import By
5+
6+
from pom.web_form_page_target import WebFormTargetPage
7+
8+
9+
class WebFormPage:
10+
def __init__(self, driver: webdriver) -> None:
11+
self.driver = driver
12+
self.title = "Web form"
13+
14+
_my_textarea = By.NAME, "my-textarea"
15+
_submit_form = By.CSS_SELECTOR, ".btn"
16+
17+
def set_textarea(self, text: str):
18+
self.driver.find_element(*self._my_textarea).send_keys(text)
19+
return self
20+
21+
def submit_form(self):
22+
self.driver.find_element(*self._submit_form).click()
23+
return WebFormTargetPage(self.driver)

pom/web_form_page_target.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""Web form - parget page"""
2+
3+
from selenium import webdriver
4+
from selenium.webdriver.common.by import By
5+
6+
7+
class WebFormTargetPage:
8+
def __init__(self, driver: webdriver):
9+
self.driver = driver
10+
self.title = "Web form - target page"
11+
12+
_message = By.ID, "message"
13+
14+
def get_message(self):
15+
return self.driver.find_element(*self._message).text

pyproject.toml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[project]
2+
name = "python"
3+
version = "1.0.0"
4+
dependencies = [
5+
"selenium",
6+
"pytest"
7+
]
8+
requires-python = ">=3.12"
9+
authors = [
10+
{name = "Alex"}
11+
]
12+
maintainers = [
13+
{name = "Alex"}
14+
]
15+
description = "Learn Selenium with Python"
16+
readme = "README.md"
17+
18+
[project.urls]
19+
Repository = "https://github.com/TechWithAlexDuta/vs_code_selenium.git"
20+
21+
[tool.pytest.ini_options]
22+
markers = [
23+
"selenium",
24+
"selenium_pom"
25+
]

requirements.txt

134 Bytes
Binary file not shown.

tests/__init__.py

Whitespace-only changes.

tests/conftest.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""Test fixture"""
2+
3+
import pytest
4+
from selenium import webdriver
5+
6+
7+
@pytest.fixture
8+
def browser():
9+
# Setup: Create a browser instance
10+
driver = webdriver.Chrome()
11+
driver.get("https://www.selenium.dev/selenium/web/web-form.html")
12+
driver.maximize_window()
13+
14+
# Provide the fixture value
15+
yield driver
16+
17+
# Teardown: Quit the browser instance
18+
driver.quit()

tests/test_selenium_web_form.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""Basic sample test """
2+
3+
import uuid
4+
import pytest
5+
from selenium import webdriver
6+
from selenium.webdriver.common.by import By
7+
8+
9+
@pytest.mark.selenium
10+
def test_write_to_text_area_and_submit():
11+
expected_message = "Received!"
12+
text = str(uuid.uuid4())
13+
14+
driver = webdriver.Chrome()
15+
driver.get("https://www.selenium.dev/selenium/web/web-form.html")
16+
driver.maximize_window()
17+
18+
driver.find_element(By.NAME, "my-textarea").send_keys(text)
19+
driver.find_element(By.CSS_SELECTOR, ".btn").click()
20+
message = driver.find_element(By.ID, "message").text
21+
22+
assert message == expected_message
23+
24+
driver.quit()
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""Sample test with conftest and pytest fixture"""
2+
3+
import uuid
4+
import pytest
5+
from selenium.webdriver.common.by import By
6+
7+
8+
@pytest.mark.selenium
9+
def test_write_to_text_area_and_submit_fixture(browser):
10+
expected_message = "Received!"
11+
text = str(uuid.uuid4())
12+
13+
browser.find_element(By.NAME, "my-textarea").send_keys(text)
14+
browser.find_element(By.CSS_SELECTOR, ".btn").click()
15+
message = browser.find_element(By.ID, "message").text
16+
17+
assert message == expected_message

tests/test_selenium_web_form_pom.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""Sample test with Page Object Model pattern"""
2+
3+
import uuid
4+
import pytest
5+
6+
from pom.web_form_page import WebFormPage
7+
8+
@pytest.mark.selenium_pom
9+
def test_write_to_text_area_and_submit_fixture(browser):
10+
expected_message = "Received!"
11+
text = str(uuid.uuid4())
12+
13+
web_form = WebFormPage(browser)
14+
message = web_form\
15+
.set_textarea(text)\
16+
.submit_form()\
17+
.get_message()
18+
19+
assert message == expected_message

0 commit comments

Comments
 (0)