This is a set of instructions to set Selenium with Python for the first time:
- Install Python
- Make sure pip was installed running the command
pip -V
- Install virtualenv
py -m pip install --user virtualenv
- Create a new virtual environment
python -m venv se-python-env
- Activate it
cd se-python-env/Scripts
activate
- Download PyCharm
- Click on "New Project"
- Change the project name
- Select "Previously configured interpreter"
- Click on "Add Interpreter"
- In the environment section select "Existing"
- Look for the folder where you created your virtual env and select \se-python-env\Scripts\python.exe
- Wait for a few seconds until project is configured.
- Create a requirements.txt file in the root.
- Add the pytest requirement: https://docs.pytest.org/en/7.4.x/getting-started.html
pytest==7.4.2
- Add the Selenium requirement: https://pypi.org/project/selenium/#history
selenium==4.13.0
- Add the webdriver manager requirement: https://pypi.org/project/webdriver-manager/#history
webdriver-manager==4.0.1
- Create a test folder
- Under the test folder create the file named "conftest.py"
- Create a way to receive browser parameter from console based on documentation: https://docs.pytest.org/en/7.1.x/example/simple.html
def pytest_addoption(parser):
parser.addoption(
"--browser", action="store", default="chrome", help="Send 'chrome' or 'firefox' as parameter for execution"
)
- Then create a general fixture for setup & tear download (notice the request parameter comes from the pytest_addoption method)
@pytest.fixture()
def driver(request):
browser = request.config.getoption("--browser")
# Default driver value
driver = "chrome"
# Option setup to run in headless mode (in order to run this in GH Actions)
options = Options()
options.add_argument('--headless')
# Setup
print(f"\nSetting up: {browser} driver")
if browser == "chrome":
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
elif browser == "firefox":
driver = webdriver.Firefox(service=FirefoxService(GeckoDriverManager().install()))
# Implicit wait setup for our framework
driver.implicitly_wait(10)
yield driver
# Tear down
print(f"\nTear down: {browser} driver")
driver.quit()
def test_valid_login(self, driver):
driver.get("https://www.saucedemo.com/")
time.sleep(2)
# Type username
username_input = driver.find_element(By.ID, "user-name")
username_input.send_keys("standard_user")
# Type password
password_input = driver.find_element(By.ID, "password")
password_input.send_keys("secret_sauce")
# Click on the login button
login_btn = driver.find_element(By.ID, "login-button")
login_btn.click()
# URL Validation
actual_url = driver.current_url
assert actual_url == "https://www.saucedemo.com/inventory.html"
We can send parameters to functions following the guide in the official doc. Reference: https://docs.pytest.org/en/7.3.x/how-to/parametrize.html Check this example where the same example is going to be executed twice with two set of data:
@pytest.mark.parametrize("username, password, error", [
("locked_out_user", "secret_sauce", "Epic sadface: Sorry, this user has been locked out."),
("invalidUser", "invalidPass", "Epic sadface: Username and password do not match any user in this service")])
def test_invalid_login(self, driver, username, password, error):
driver.get("https://www.saucedemo.com/")
# Type username
username_input = driver.find_element(By.ID, "user-name")
username_input.send_keys(username)
# Type password
password_input = driver.find_element(By.ID, "password")
password_input.send_keys(password)
# Click on the login button
login_btn = driver.find_element(By.ID, "login-button")
login_btn.click()
# Error message validation
error_msg_h3 = driver.find_element(By.TAG_NAME, "h3")
error_msg_text = error_msg_h3.text
assert error_msg_text == error
Library: https://pypi.org/project/pytest-html/
If you want to add the report just add to the "requirements.txt" the following line(check the version you want to implement):
pytest-html==4.0.2
- Create a folder named "reports"
- Add to the execution the following "Additional Arguments":
--html=reports/automation-report-(BROWSER)-.html
It is easy, use this command as example:
pytest test_login_page.py --browser=chrome --html=reports/automation-report-chrome.html
pytest test_login_page.py --browser=firefox --html=reports/automation-report-firefox.html
Use the following command:
pip install -r requirements.txt
Check the yml file I created to run our project in GH Actions. It is amazing, isn't it?
Contributions are always welcome!
See contributing.md
for ways to get started.
Please adhere to this project's code of conduct
.