0% found this document useful (0 votes)
2 views9 pages

Pytest Framework - Notes

The document provides a comprehensive guide on using the Pytest framework with Selenium for testing, covering installation, running tests, using markers, fixtures, and parallel execution. It includes examples of test cases, parameterization, and the Page Object Model structure. Additionally, it concludes with a personal pitch highlighting the author's strengths and desire for opportunities to learn and grow.

Uploaded by

himanshu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views9 pages

Pytest Framework - Notes

The document provides a comprehensive guide on using the Pytest framework with Selenium for testing, covering installation, running tests, using markers, fixtures, and parallel execution. It includes examples of test cases, parameterization, and the Page Object Model structure. Additionally, it concludes with a personal pitch highlighting the author's strengths and desire for opportunities to learn and grow.

Uploaded by

himanshu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Pytest Framework

repsitory: [Link]

[Link] - to run all test

[Link] -k login -v - to run test with login text

=================>Part 2: Run your test with Markers and In Parallel Mode

@[Link]

def test_m1():

assert "admin" == "admin"

@[Link]

[Link] -m login - to run test with marker

or [Link] /.../[Link] -m login

to run in parallel mode

test_webdriver_login.py

def login_google():

driver = [Link](ChromeDriverManager().install())

driver.implicitly_wait(10)

[Link]('[Link]

assert [Link] == "Google"

[Link]()

install pytest-xdist

pip install pytest-xdist

[Link] -n 5 - will run all test on 5 browser in parallel

[Link] /.../test_webdriver_login.py -n 5 -> will run all test under test_webdriver_login.py on 5


browser in parallel
==========Part 3: PyTest Fixtures with Selenium || PyTest Html Report

from selenium import webdriver

from webdriver_manager.chrome import ChromeDriverManger

driver = None

def setup_module(module):

global driver

driver = [Link](ChromeDriverManager().install())

driver.implicitly_wait(10)

driver.delete_all_cookies

[Link]('[Link]

def teardown_module(module):

[Link]()

def test_google_title():

assert [Link] == "Google"

def test_google_url():

assert driver.current_url == "[Link]"

to run

[Link] -v -s test_googletest.py (-v is verbose for more detailed and -s is for print statement)

=> to generate html report

pip install pytest-html

[Link] test_googletest.py -v -s --html=google_test_report.html


============Using Fixtures==============

@[Link](scope='module')

def init_driver():

global driver

print("----setup-----")

driver = [Link](ChromeDriverManager().install())

driver.implicitly_wait(10)

driver.delete_all_cookies

[Link]('[Link]

yield

print("----testdown----")

[Link]()

def test_google_title(init_driver):

assert [Link] == "Google"

def test_google_url(init_driver):

assert driver.current_url == "[Link]"

or by using marker and fixtures together

@[Link](init_driver)

def test_google_title():

assert [Link] == "Google"

@[Link](init_driver)

def test_google_url():

assert driver.current_url == "[Link]"


=>to run:

pytest test_google_fixture.py -v -s --html=test_google_fixtureReport.html

=============Part 4 - PyTest Fixtures with Class Scope || Fixture parameters || PyTest Selenium
Test

file- test_fixture_withclasses.py

import pytest

from selenium import webdriver

from webdriver_manager.chrome import ChromeDriverManger

@[Link](scope='class')

def init_chrome_driver(request):

ch_driver = [Link](ChromeDriver>manager().install())

[Link] = ch_driver

yield

ch_driver.close()

@[Link]("init_chrome_driver")

class Base_Chrome_Test:

pass

class Test_Google_Chrome(Base_Chrome_Test):

def test_google_title_chrome(self):

[Link]('[Link]

assert [Link] == "Google"


Now use Params to avoid typing similar code for other browser

import pytest

from selenium import webdriver

from webdriver_manager.chrome import ChromeDriverManger

@[Link](params=["chrome","firefox"],scope='class')

def init_driver(request):

if [Link] == "chrome":

web_driver = [Link](ChromeDriverManager().install())

if [Link] == "firefox":

web_driver =
[Link](executable_path=GeckoDriverManager().install())

[Link] = web_driver

yield

web_driver.close()

@[Link]("init_driver")

class Base_Test:

pass

class Test_Google(Base_Test):

def test_google_title(self):

[Link]('[Link]

assert [Link] == "Google"

to run [Link] [Link] -v -s --html=[Link]

to run in paralle mode => [Link] [Link] -v -s -n 2 --html=[Link]

this will execute on both chrome and firefox one by one


============Part 5 - Create Global Pytest Fixture using [Link] file

in [Link] - write below code... this fixture will be available globally across all module

We can add multiple fixture in this file. this will be like global precondition

use this fixture for your test case precondition and tear down

import pytest

from selenium import webdriver

from webdriver_manager.chrome import ChromeDriverManger

@[Link](params=["chrome","firefox"],scope='class') ---- > This is fixture


parametrization

def init_driver(request):

if [Link] == "chrome":

web_driver = [Link](ChromeDriverManager().install())

if [Link] == "firefox":

web_driver =
[Link](executable_path=GeckoDriverManager().install())

[Link] = web_driver

yield

web_driver.close()

to run [Link] [Link] -v -s -n 2 --html=[Link]

=================Part 6 - How to Parametrize test in PyTest (Parameterization)

import pytest

from selenium import webdriver

from webdriver_manager.chrome import ChromeDriverManger

@[Link]("init_driver")
class Base_Test():

pass

class TestHubSpot(Base_Test)

@[Link]("username,password", ---- > This is Test parametrization

("admin@[Link]","admin123")

("naveen@[Link]","naveen123")

])

def test_login(self,username,password):

[Link]("[Link]

[Link].find_element(([Link], 'username').send_keys(username)

[Link].find_element(([Link], 'password').send_keys(password)

for parallel execution

pytest test_params.py -v -s -n 4 --html==test_param_result.html

=====================Page Object Model - Python Selenium with PyTest - Part 1

pip install selenium

pip install pytest

Structure:

--Config

--[Link]

--Pages

--[Link]

--[Link]

--[Link]

--Test

--[Link]

test_HomePage.py

test_LoginPage.py
--[Link]:

class TestData:

CHROME_EXECUTABLE_PATH = '/Users/downloads/chromedriver'

FIREFOX_EXECUTABLE_PATH = '/Users/downloads/geckodriver'

BASE_URL = "[Link]

USERNAME = "naveenanimation30@[Link]"

PASSWORD = "Selenium@12345"

--[Link]

"""This class is the parent of all class

it contain all generic methods and utilities of all the pages"""

class BasePage:

def __init__(self,driver):

[Link] = driver

--[Link]

class LoginPage(BasePage):

from [Link] import By

EMAIL = ([Link], "username")

PASSWORD = ([Link], "password")

LOGIN_BUTTON = ([Link], "loginBtn")

SIGNUP_LINK = (By.LINK_TEXT, "Sign up")

def __init__(self, driver):


super().__init__(driver)

def get_login_page_title(self,title):

return self.get_title(title)

AT the end I would like to pitch for myself

I am a test match player....

usually I cannot impress in short time.

I just need one opoutunity to showcase my skill.

Its my weakness :

I have fear... fear of lossing... i will rty to overcome that.

I cannot impress anyone in a sort time..

Strength:

I never leave incomplete task...

will always try to achieve the target within timeline

Last 6 months has given me opportunity to do retrospective on myself

some changes which I want to bring within me:

I will not work for Money, I will work to gain knowledge. Money will always be the byproduct of my
work

i just want to learn and i will do anything for that

I Just need one opportunity to prove myself

I can share my previous company manager number if you really want to know about me

You might also like