SlideShare a Scribd company logo
www.companyname.com
© 2016 Startup theme. All Rights Reserved.
Intro to Selenium UI Tests with pytest
With some useful pytest plugins
Asif Mohaimen
QA Specialist
August 27, 2020
www.companyname.com
© 2016 Startup theme. All Rights Reserved.
About Me !
Asif Mohaimen
QA Specialist
Eskimi DSP
https://asifmohai.men
asif@eskimi.com
www.companyname.com
© 2016 Startup theme. All Rights Reserved.
Summary "
• What’s pytest? #
• What’s Selenium? $
• How to make them work together? %
• Some demonstration! < >
• Some useful pytest plugins! ⚒
www.companyname.com
© 2016 Startup theme. All Rights Reserved.
“pytest”? # '
• Python framework for tests.
def inc(x):
return x + 1
def test_true():
assert inc(3) == 4
def test_false():
assert not (not inc(3) == 4)
Result? (
$pytest blah.py
============================= test session starts ==============================
platform darwin -- Python 3.7.6, pytest-5.3.5, py-1.8.1, pluggy-0.13.1
rootdir: /location, inifile: pytest.ini
collected 2 items
test.py .. [100%]
============================== 2 passed in 0.32s ===============================
def inc(x):
return x + 1
def test_true():
assert inc(3) == 4
def test_false():
assert not (not inc(3) == 4)
blah.py
www.companyname.com
© 2016 Startup theme. All Rights Reserved.
“Selenium”? $ '
Selenium automates browsers.
That's it!
Communication with browser %
www.companyname.com
© 2016 Startup theme. All Rights Reserved.
Code Demo )
Web elements? *
<div class="login-form">
<form action=“#" method="post">
<h2 class="text-center">Log in</h2>
<div class="form-group">
<input type="text" id="username" class="form-control" placeholder="Username" required="required">
</div>
<div class="form-group">
<input type="password" id="password" class="form-control" placeholder="Password" required="required">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary btn-block">Log in</button>
</div>
</form>
</div>
How to locate? *
How to locate? *
How to locate? *
How to locate? *
Accessing web elements +
• find_element_by_id
• find_element_by_name
• find_element_by_xpath
• find_element_by_link_text
• find_element_by_partial_link_text
• find_element_by_tag_name
• find_element_by_class_name
• find_element_by_css_selector
• find_element
• find_elements
from selenium.webdriver.common.by import By
driver.find_element(By.XPATH, '//button[text()="Some text"]') # Private
driver.find_elements(By.XPATH, '//button') # Private
driver.find_element_by_xpath('//button[text()="Some text"]') # Public
driver.find_element_by_xpath('//button') # Public
Performing actions ☝
•click(on_element=None)
•click_and_hold(on_element=None)
•context_click(on_element=None)
•double_click(on_element=None)
•drag_and_drop(source, target)
•drag_and_drop_by_offset(source, xoffset, yoffset)
•key_down(value, element=None)
•key_up(value, element=None)
•move_by_offset(xoffset, yoffset)
•move_to_element(to_element)
•move_to_element_with_offset(to_element, xoffset, yoffset)
•pause(seconds)
•perform()
•release(on_element=None)
•reset_actions()
•send_keys(*keys_to_send)
•send_keys_to_element(element, *keys_to_send)
Demo Login Task -
Open Source Demo App
https://opensource-demo.orangehrmlive.com/index.php/auth/login
Demo Login Task -
Open Source Demo App
https://opensource-demo.orangehrmlive.com/index.php/auth/login
Demo Login Task -
Open Source Demo App
https://opensource-demo.orangehrmlive.com/index.php/auth/login
Demo Login Task -
Open Source Demo App
https://opensource-demo.orangehrmlive.com/index.php/auth/login
Demo Login Task -
Open Source Demo App
https://opensource-demo.orangehrmlive.com/index.php/auth/login
Demo Login Task -
Open Source Demo App
https://opensource-demo.orangehrmlive.com/index.php/auth/login
Simple Script "
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://opensource-demo.orangehrmlive.com/index.php/auth/login')
driver.find_element_by_id('txtUsername').send_keys('Admin')
driver.find_element_by_id('txtPassword').send_keys('admin123')
driver.find_element_by_id('btnLogin').click()
if driver.current_url.endswith('dashboard'):
print('PASS')
else:
print('FAIL')
driver.quit()
Structure the project? .
Structure the project? .
.
├── pages
│   ├── __init__.py
│   └── login.py
├── requirements.txt
└── tests
├── conftest.py
└── test_login.py
2 directories, 5 files
& use it with pytest!
requirements.txt /
pytest==x.x.x
selenium==x.x.x
.
├── pages
│   ├── __init__.py
│   └── login.py
├── requirements.txt
└── tests
├── conftest.py
└── test_login.py
$pip install -r requirements.txt
⚙ conftest.py #
import pytest
from selenium import webdriver
@pytest.fixture
def driver():
driver = webdriver.Chrome()
driver.implicitly_wait(10)
yield driver
driver.quit()
.
├── pages
│   ├── __init__.py
│   └── login.py
├── requirements.txt
└── tests
├── conftest.py
└── test_login.py
+ login.py #
from selenium.webdriver.common.by import By
class Login:
LOGIN_PAGE = 'https://opensource-demo.orangehrmlive.com/index.php/auth/login'
USERNAME_INPUT = (By.ID, 'txtUsername')
PASSWORD_INPUT = (By.ID, 'txtPassword')
LOGIN_BUTTON = (By.ID, 'btnLogin')
username = 'Admin'
password = 'admin123'
def __init__(self, driver):
self.driver = driver
def login(self):
self.driver.get(self.LOGIN_PAGE)
username_input = self.driver.find_element(*self.USERNAME_INPUT)
password_input = self.driver.find_element(*self.PASSWORD_INPUT)
login_button = self.driver.find_element(*self.LOGIN_BUTTON)
username_input.send_keys(self.username) # Give username input
password_input.send_keys(self.password) # Give password input
login_button.click() # Click the login button
return self.driver.current_url
.
├── pages
│   ├── __init__.py
│   └── login.py
├── requirements.txt
└── tests
├── conftest.py
└── test_login.py
1 test_login.py #
from pages.login import Login
def test_login(driver):
login_page = Login(driver)
assert login_page.login().endswith('dashboard')
.
├── pages
│   ├── __init__.py
│   └── login.py
├── requirements.txt
└── tests
├── conftest.py
└── test_login.py
⚒ Run the test with pytest #
$ python -m pytest
============================= test session starts ==============================
platform darwin -- Python x.x.x, pytest-x.x.x, py-x.x.x, pluggy-x.x.x
rootdir: project_location
collected 1 item
tests/test_login.py . [100%]
============================== 1 passed in 16.60s ==============================
www.companyname.com
© 2016 Startup theme. All Rights Reserved.
Why use pytest? #
• Flexibility
• Fixtures
• Test discovery
• Logging
• Better reporting
www.companyname.com
© 2016 Startup theme. All Rights Reserved.
• pytest-blockage
• pytest-benchmark
• pytest-browsermob-proxy
• pytest-capturelog
• pytest-cloud
• pytest-config
• pytest-rerunfailures
• pytest-xdist
Plugins? 2
pytest-xdist the cool one!
$pip install pytest-xdist
$python -m pytest -n 100 3
www.companyname.com
© 2016 Startup theme. All Rights Reserved.
Agreed?
www.companyname.com
© 2016 Startup theme. All Rights Reserved.
Thanks!
www.companyname.com
© 2016 Startup theme. All Rights Reserved.
C O N T A C T
Floor 5th, Flat-E2
House 12/A, Road-8
Gulshan-1, Dhaka-1212
business.eskimi.comasif@eskimi.com
Ad

Recommended

Real World Selenium Testing
Real World Selenium Testing
Mary Jo Sminkey
 
Oscon 20080724
Oscon 20080724
linkedin_resptest2
 
Djangoアプリのデプロイに関するプラクティス / Deploy django application
Djangoアプリのデプロイに関するプラクティス / Deploy django application
Masashi Shibata
 
Webdriver cheatsheets summary
Webdriver cheatsheets summary
Alan Richardson
 
Api
Api
randyhoyt
 
Integrating WordPress With Web APIs
Integrating WordPress With Web APIs
randyhoyt
 
Working with Images in WordPress
Working with Images in WordPress
randyhoyt
 
Selenium webdriver
Selenium webdriver
sean_todd
 
Web driver training
Web driver training
Dipesh Bhatewara
 
Selenium Automation Using Ruby
Selenium Automation Using Ruby
Kumari Warsha Goel
 
ADF 2.4.0 And Beyond
ADF 2.4.0 And Beyond
Eugenio Romano
 
클린코드를 위한 테스트 주도 개발 1장
클린코드를 위한 테스트 주도 개발 1장
Pilhwan Kim
 
Effective C++/WinRT for UWP and Win32
Effective C++/WinRT for UWP and Win32
Windows Developer
 
Automated Testing With Watir
Automated Testing With Watir
Timothy Fisher
 
Widget Summit 2008
Widget Summit 2008
Volkan Unsal
 
Clearance: Simple, complete Ruby web app authentication.
Clearance: Simple, complete Ruby web app authentication.
Jason Morrison
 
SPugPt Meeting 35: Manage govern and drive adoption of share point online wit...
SPugPt Meeting 35: Manage govern and drive adoption of share point online wit...
Comunidade Portuguesa de SharePoiint
 
Building Progressive Web Apps for Windows devices
Building Progressive Web Apps for Windows devices
Windows Developer
 
AEM hacker - approaching Adobe Experience Manager webapps in bug bounty programs
AEM hacker - approaching Adobe Experience Manager webapps in bug bounty programs
Mikhail Egorov
 
Search and play more than 50 clips
Search and play more than 50 clips
phanhung20
 
Selenium testing IDE 101
Selenium testing IDE 101
Adam Culp
 
Jasmine with JS-Test-Driver
Jasmine with JS-Test-Driver
Devesh Chanchlani
 
Web-Performance
Web-Performance
Walter Ebert
 
Fast by Default
Fast by Default
Abhay Kumar
 
Banquet 51
Banquet 51
Koubei UED
 
20150319 testotipsio
20150319 testotipsio
Kazuaki Matsuo
 
2013-06-25 - HTML5 & JavaScript Security
2013-06-25 - HTML5 & JavaScript Security
Johannes Hoppe
 
Selenium Tips & Tricks - StarWest 2015
Selenium Tips & Tricks - StarWest 2015
Andrew Krug
 
Automating Django Functional Tests Using Selenium on Cloud
Automating Django Functional Tests Using Selenium on Cloud
Jonghyun Park
 
Let's talk testing with Selenium
Let's talk testing with Selenium
anishanarang
 

More Related Content

What's hot (20)

Web driver training
Web driver training
Dipesh Bhatewara
 
Selenium Automation Using Ruby
Selenium Automation Using Ruby
Kumari Warsha Goel
 
ADF 2.4.0 And Beyond
ADF 2.4.0 And Beyond
Eugenio Romano
 
클린코드를 위한 테스트 주도 개발 1장
클린코드를 위한 테스트 주도 개발 1장
Pilhwan Kim
 
Effective C++/WinRT for UWP and Win32
Effective C++/WinRT for UWP and Win32
Windows Developer
 
Automated Testing With Watir
Automated Testing With Watir
Timothy Fisher
 
Widget Summit 2008
Widget Summit 2008
Volkan Unsal
 
Clearance: Simple, complete Ruby web app authentication.
Clearance: Simple, complete Ruby web app authentication.
Jason Morrison
 
SPugPt Meeting 35: Manage govern and drive adoption of share point online wit...
SPugPt Meeting 35: Manage govern and drive adoption of share point online wit...
Comunidade Portuguesa de SharePoiint
 
Building Progressive Web Apps for Windows devices
Building Progressive Web Apps for Windows devices
Windows Developer
 
AEM hacker - approaching Adobe Experience Manager webapps in bug bounty programs
AEM hacker - approaching Adobe Experience Manager webapps in bug bounty programs
Mikhail Egorov
 
Search and play more than 50 clips
Search and play more than 50 clips
phanhung20
 
Selenium testing IDE 101
Selenium testing IDE 101
Adam Culp
 
Jasmine with JS-Test-Driver
Jasmine with JS-Test-Driver
Devesh Chanchlani
 
Web-Performance
Web-Performance
Walter Ebert
 
Fast by Default
Fast by Default
Abhay Kumar
 
Banquet 51
Banquet 51
Koubei UED
 
20150319 testotipsio
20150319 testotipsio
Kazuaki Matsuo
 
2013-06-25 - HTML5 & JavaScript Security
2013-06-25 - HTML5 & JavaScript Security
Johannes Hoppe
 
Selenium Tips & Tricks - StarWest 2015
Selenium Tips & Tricks - StarWest 2015
Andrew Krug
 
Selenium Automation Using Ruby
Selenium Automation Using Ruby
Kumari Warsha Goel
 
클린코드를 위한 테스트 주도 개발 1장
클린코드를 위한 테스트 주도 개발 1장
Pilhwan Kim
 
Effective C++/WinRT for UWP and Win32
Effective C++/WinRT for UWP and Win32
Windows Developer
 
Automated Testing With Watir
Automated Testing With Watir
Timothy Fisher
 
Widget Summit 2008
Widget Summit 2008
Volkan Unsal
 
Clearance: Simple, complete Ruby web app authentication.
Clearance: Simple, complete Ruby web app authentication.
Jason Morrison
 
SPugPt Meeting 35: Manage govern and drive adoption of share point online wit...
SPugPt Meeting 35: Manage govern and drive adoption of share point online wit...
Comunidade Portuguesa de SharePoiint
 
Building Progressive Web Apps for Windows devices
Building Progressive Web Apps for Windows devices
Windows Developer
 
AEM hacker - approaching Adobe Experience Manager webapps in bug bounty programs
AEM hacker - approaching Adobe Experience Manager webapps in bug bounty programs
Mikhail Egorov
 
Search and play more than 50 clips
Search and play more than 50 clips
phanhung20
 
Selenium testing IDE 101
Selenium testing IDE 101
Adam Culp
 
2013-06-25 - HTML5 & JavaScript Security
2013-06-25 - HTML5 & JavaScript Security
Johannes Hoppe
 
Selenium Tips & Tricks - StarWest 2015
Selenium Tips & Tricks - StarWest 2015
Andrew Krug
 

Similar to Intro to Selenium UI Tests with pytest & some useful pytest plugins (20)

Automating Django Functional Tests Using Selenium on Cloud
Automating Django Functional Tests Using Selenium on Cloud
Jonghyun Park
 
Let's talk testing with Selenium
Let's talk testing with Selenium
anishanarang
 
Selenium with py test by Alexandr Vasyliev for Lohika Odessa Python TechTalks
Selenium with py test by Alexandr Vasyliev for Lohika Odessa Python TechTalks
Lohika_Odessa_TechTalks
 
Controlling the browser through python and selenium
Controlling the browser through python and selenium
Patrick Viafore
 
Selenium- A Software Testing Tool
Selenium- A Software Testing Tool
Zeba Tahseen
 
تست وب اپ ها با سلنیوم - علیرضا عظیم زاده میلانی
تست وب اپ ها با سلنیوم - علیرضا عظیم زاده میلانی
irpycon
 
Lesson2-Selenium installation 2-6-25.pptx
Lesson2-Selenium installation 2-6-25.pptx
131881omarfernandez1
 
automation with python and selenium
automation with python and selenium
Manish Kumar
 
Selenium.pptx
Selenium.pptx
Pandiya Rajan
 
Selenium Testing
Selenium Testing
Shreshtt Bhatt
 
One to rule them all
One to rule them all
Antonio Robres Turon
 
Selenium training
Selenium training
Suresh Arora
 
Intro Of Selenium
Intro Of Selenium
Kai Feng Zhang
 
Mastering Selenium WebDriver: A Comprehensive Tutorial with Real-World Examples
Mastering Selenium WebDriver: A Comprehensive Tutorial with Real-World Examples
jamescantor38
 
The Testing Planet Issue 2
The Testing Planet Issue 2
Rosie Sherry
 
Your Last manual Assessment
Your Last manual Assessment
devgrega
 
Selenium for-ops
Selenium for-ops
Łukasz Proszek
 
Selenium
Selenium
kalyan234
 
Selenium Ide Tutorial
Selenium Ide Tutorial
metapix
 
Scraping recalcitrant web sites with Python & Selenium
Scraping recalcitrant web sites with Python & Selenium
Roger Barnes
 
Automating Django Functional Tests Using Selenium on Cloud
Automating Django Functional Tests Using Selenium on Cloud
Jonghyun Park
 
Let's talk testing with Selenium
Let's talk testing with Selenium
anishanarang
 
Selenium with py test by Alexandr Vasyliev for Lohika Odessa Python TechTalks
Selenium with py test by Alexandr Vasyliev for Lohika Odessa Python TechTalks
Lohika_Odessa_TechTalks
 
Controlling the browser through python and selenium
Controlling the browser through python and selenium
Patrick Viafore
 
Selenium- A Software Testing Tool
Selenium- A Software Testing Tool
Zeba Tahseen
 
تست وب اپ ها با سلنیوم - علیرضا عظیم زاده میلانی
تست وب اپ ها با سلنیوم - علیرضا عظیم زاده میلانی
irpycon
 
Lesson2-Selenium installation 2-6-25.pptx
Lesson2-Selenium installation 2-6-25.pptx
131881omarfernandez1
 
automation with python and selenium
automation with python and selenium
Manish Kumar
 
Mastering Selenium WebDriver: A Comprehensive Tutorial with Real-World Examples
Mastering Selenium WebDriver: A Comprehensive Tutorial with Real-World Examples
jamescantor38
 
The Testing Planet Issue 2
The Testing Planet Issue 2
Rosie Sherry
 
Your Last manual Assessment
Your Last manual Assessment
devgrega
 
Selenium Ide Tutorial
Selenium Ide Tutorial
metapix
 
Scraping recalcitrant web sites with Python & Selenium
Scraping recalcitrant web sites with Python & Selenium
Roger Barnes
 
Ad

Recently uploaded (20)

Introduction to Python Programming Language
Introduction to Python Programming Language
merlinjohnsy
 
AI_Presentation (1). Artificial intelligence
AI_Presentation (1). Artificial intelligence
RoselynKaur8thD34
 
Rapid Prototyping for XR: Lecture 3 - Video and Paper Prototyping
Rapid Prototyping for XR: Lecture 3 - Video and Paper Prototyping
Mark Billinghurst
 
Deep Learning for Image Processing on 16 June 2025 MITS.pptx
Deep Learning for Image Processing on 16 June 2025 MITS.pptx
resming1
 
May 2025: Top 10 Read Articles in Data Mining & Knowledge Management Process
May 2025: Top 10 Read Articles in Data Mining & Knowledge Management Process
IJDKP
 
Abraham Silberschatz-Operating System Concepts (9th,2012.12).pdf
Abraham Silberschatz-Operating System Concepts (9th,2012.12).pdf
Shabista Imam
 
International Journal of Advanced Information Technology (IJAIT)
International Journal of Advanced Information Technology (IJAIT)
ijait
 
Rapid Prototyping for XR: Lecture 1 Introduction to Prototyping
Rapid Prototyping for XR: Lecture 1 Introduction to Prototyping
Mark Billinghurst
 
special_edition_using_visual_foxpro_6.pdf
special_edition_using_visual_foxpro_6.pdf
Shabista Imam
 
DESIGN OF REINFORCED CONCRETE ELEMENTS S
DESIGN OF REINFORCED CONCRETE ELEMENTS S
prabhusp8
 
Rapid Prototyping for XR: Lecture 2 - Low Fidelity Prototyping.
Rapid Prototyping for XR: Lecture 2 - Low Fidelity Prototyping.
Mark Billinghurst
 
Comparison of Flexible and Rigid Pavements in Bangladesh
Comparison of Flexible and Rigid Pavements in Bangladesh
Arifur Rahman
 
Bitumen Emulsion by Dr Sangita Ex CRRI Delhi
Bitumen Emulsion by Dr Sangita Ex CRRI Delhi
grilcodes
 
FUNDAMENTALS OF COMPUTER ORGANIZATION AND ARCHITECTURE
FUNDAMENTALS OF COMPUTER ORGANIZATION AND ARCHITECTURE
Shabista Imam
 
Stability of IBR Dominated Grids - IEEE PEDG 2025 - short.pptx
Stability of IBR Dominated Grids - IEEE PEDG 2025 - short.pptx
ssuser307730
 
Rapid Prototyping for XR: Lecture 5 - Cross Platform Development
Rapid Prototyping for XR: Lecture 5 - Cross Platform Development
Mark Billinghurst
 
Generative AI & Scientific Research : Catalyst for Innovation, Ethics & Impact
Generative AI & Scientific Research : Catalyst for Innovation, Ethics & Impact
AlqualsaDIResearchGr
 
Modern multi-proposer consensus implementations
Modern multi-proposer consensus implementations
François Garillot
 
دراسة حاله لقرية تقع في جنوب غرب السودان
دراسة حاله لقرية تقع في جنوب غرب السودان
محمد قصص فتوتة
 
LECTURE 7 COMPUTATIONS OF LEVELING DATA APRIL 2025.pptx
LECTURE 7 COMPUTATIONS OF LEVELING DATA APRIL 2025.pptx
rr22001247
 
Introduction to Python Programming Language
Introduction to Python Programming Language
merlinjohnsy
 
AI_Presentation (1). Artificial intelligence
AI_Presentation (1). Artificial intelligence
RoselynKaur8thD34
 
Rapid Prototyping for XR: Lecture 3 - Video and Paper Prototyping
Rapid Prototyping for XR: Lecture 3 - Video and Paper Prototyping
Mark Billinghurst
 
Deep Learning for Image Processing on 16 June 2025 MITS.pptx
Deep Learning for Image Processing on 16 June 2025 MITS.pptx
resming1
 
May 2025: Top 10 Read Articles in Data Mining & Knowledge Management Process
May 2025: Top 10 Read Articles in Data Mining & Knowledge Management Process
IJDKP
 
Abraham Silberschatz-Operating System Concepts (9th,2012.12).pdf
Abraham Silberschatz-Operating System Concepts (9th,2012.12).pdf
Shabista Imam
 
International Journal of Advanced Information Technology (IJAIT)
International Journal of Advanced Information Technology (IJAIT)
ijait
 
Rapid Prototyping for XR: Lecture 1 Introduction to Prototyping
Rapid Prototyping for XR: Lecture 1 Introduction to Prototyping
Mark Billinghurst
 
special_edition_using_visual_foxpro_6.pdf
special_edition_using_visual_foxpro_6.pdf
Shabista Imam
 
DESIGN OF REINFORCED CONCRETE ELEMENTS S
DESIGN OF REINFORCED CONCRETE ELEMENTS S
prabhusp8
 
Rapid Prototyping for XR: Lecture 2 - Low Fidelity Prototyping.
Rapid Prototyping for XR: Lecture 2 - Low Fidelity Prototyping.
Mark Billinghurst
 
Comparison of Flexible and Rigid Pavements in Bangladesh
Comparison of Flexible and Rigid Pavements in Bangladesh
Arifur Rahman
 
Bitumen Emulsion by Dr Sangita Ex CRRI Delhi
Bitumen Emulsion by Dr Sangita Ex CRRI Delhi
grilcodes
 
FUNDAMENTALS OF COMPUTER ORGANIZATION AND ARCHITECTURE
FUNDAMENTALS OF COMPUTER ORGANIZATION AND ARCHITECTURE
Shabista Imam
 
Stability of IBR Dominated Grids - IEEE PEDG 2025 - short.pptx
Stability of IBR Dominated Grids - IEEE PEDG 2025 - short.pptx
ssuser307730
 
Rapid Prototyping for XR: Lecture 5 - Cross Platform Development
Rapid Prototyping for XR: Lecture 5 - Cross Platform Development
Mark Billinghurst
 
Generative AI & Scientific Research : Catalyst for Innovation, Ethics & Impact
Generative AI & Scientific Research : Catalyst for Innovation, Ethics & Impact
AlqualsaDIResearchGr
 
Modern multi-proposer consensus implementations
Modern multi-proposer consensus implementations
François Garillot
 
دراسة حاله لقرية تقع في جنوب غرب السودان
دراسة حاله لقرية تقع في جنوب غرب السودان
محمد قصص فتوتة
 
LECTURE 7 COMPUTATIONS OF LEVELING DATA APRIL 2025.pptx
LECTURE 7 COMPUTATIONS OF LEVELING DATA APRIL 2025.pptx
rr22001247
 
Ad

Intro to Selenium UI Tests with pytest & some useful pytest plugins

  • 1. www.companyname.com © 2016 Startup theme. All Rights Reserved. Intro to Selenium UI Tests with pytest With some useful pytest plugins Asif Mohaimen QA Specialist August 27, 2020
  • 2. www.companyname.com © 2016 Startup theme. All Rights Reserved. About Me ! Asif Mohaimen QA Specialist Eskimi DSP https://asifmohai.men [email protected]
  • 3. www.companyname.com © 2016 Startup theme. All Rights Reserved. Summary " • What’s pytest? # • What’s Selenium? $ • How to make them work together? % • Some demonstration! < > • Some useful pytest plugins! ⚒
  • 4. www.companyname.com © 2016 Startup theme. All Rights Reserved. “pytest”? # ' • Python framework for tests. def inc(x): return x + 1 def test_true(): assert inc(3) == 4 def test_false(): assert not (not inc(3) == 4)
  • 5. Result? ( $pytest blah.py ============================= test session starts ============================== platform darwin -- Python 3.7.6, pytest-5.3.5, py-1.8.1, pluggy-0.13.1 rootdir: /location, inifile: pytest.ini collected 2 items test.py .. [100%] ============================== 2 passed in 0.32s =============================== def inc(x): return x + 1 def test_true(): assert inc(3) == 4 def test_false(): assert not (not inc(3) == 4) blah.py
  • 6. www.companyname.com © 2016 Startup theme. All Rights Reserved. “Selenium”? $ ' Selenium automates browsers. That's it!
  • 8. www.companyname.com © 2016 Startup theme. All Rights Reserved. Code Demo )
  • 9. Web elements? * <div class="login-form"> <form action=“#" method="post"> <h2 class="text-center">Log in</h2> <div class="form-group"> <input type="text" id="username" class="form-control" placeholder="Username" required="required"> </div> <div class="form-group"> <input type="password" id="password" class="form-control" placeholder="Password" required="required"> </div> <div class="form-group"> <button type="submit" class="btn btn-primary btn-block">Log in</button> </div> </form> </div>
  • 14. Accessing web elements + • find_element_by_id • find_element_by_name • find_element_by_xpath • find_element_by_link_text • find_element_by_partial_link_text • find_element_by_tag_name • find_element_by_class_name • find_element_by_css_selector • find_element • find_elements from selenium.webdriver.common.by import By driver.find_element(By.XPATH, '//button[text()="Some text"]') # Private driver.find_elements(By.XPATH, '//button') # Private driver.find_element_by_xpath('//button[text()="Some text"]') # Public driver.find_element_by_xpath('//button') # Public
  • 15. Performing actions ☝ •click(on_element=None) •click_and_hold(on_element=None) •context_click(on_element=None) •double_click(on_element=None) •drag_and_drop(source, target) •drag_and_drop_by_offset(source, xoffset, yoffset) •key_down(value, element=None) •key_up(value, element=None) •move_by_offset(xoffset, yoffset) •move_to_element(to_element) •move_to_element_with_offset(to_element, xoffset, yoffset) •pause(seconds) •perform() •release(on_element=None) •reset_actions() •send_keys(*keys_to_send) •send_keys_to_element(element, *keys_to_send)
  • 16. Demo Login Task - Open Source Demo App https://opensource-demo.orangehrmlive.com/index.php/auth/login
  • 17. Demo Login Task - Open Source Demo App https://opensource-demo.orangehrmlive.com/index.php/auth/login
  • 18. Demo Login Task - Open Source Demo App https://opensource-demo.orangehrmlive.com/index.php/auth/login
  • 19. Demo Login Task - Open Source Demo App https://opensource-demo.orangehrmlive.com/index.php/auth/login
  • 20. Demo Login Task - Open Source Demo App https://opensource-demo.orangehrmlive.com/index.php/auth/login
  • 21. Demo Login Task - Open Source Demo App https://opensource-demo.orangehrmlive.com/index.php/auth/login
  • 22. Simple Script " from selenium import webdriver driver = webdriver.Chrome() driver.get('https://opensource-demo.orangehrmlive.com/index.php/auth/login') driver.find_element_by_id('txtUsername').send_keys('Admin') driver.find_element_by_id('txtPassword').send_keys('admin123') driver.find_element_by_id('btnLogin').click() if driver.current_url.endswith('dashboard'): print('PASS') else: print('FAIL') driver.quit()
  • 24. Structure the project? . . ├── pages │   ├── __init__.py │   └── login.py ├── requirements.txt └── tests ├── conftest.py └── test_login.py 2 directories, 5 files & use it with pytest!
  • 25. requirements.txt / pytest==x.x.x selenium==x.x.x . ├── pages │   ├── __init__.py │   └── login.py ├── requirements.txt └── tests ├── conftest.py └── test_login.py $pip install -r requirements.txt
  • 26. ⚙ conftest.py # import pytest from selenium import webdriver @pytest.fixture def driver(): driver = webdriver.Chrome() driver.implicitly_wait(10) yield driver driver.quit() . ├── pages │   ├── __init__.py │   └── login.py ├── requirements.txt └── tests ├── conftest.py └── test_login.py
  • 27. + login.py # from selenium.webdriver.common.by import By class Login: LOGIN_PAGE = 'https://opensource-demo.orangehrmlive.com/index.php/auth/login' USERNAME_INPUT = (By.ID, 'txtUsername') PASSWORD_INPUT = (By.ID, 'txtPassword') LOGIN_BUTTON = (By.ID, 'btnLogin') username = 'Admin' password = 'admin123' def __init__(self, driver): self.driver = driver def login(self): self.driver.get(self.LOGIN_PAGE) username_input = self.driver.find_element(*self.USERNAME_INPUT) password_input = self.driver.find_element(*self.PASSWORD_INPUT) login_button = self.driver.find_element(*self.LOGIN_BUTTON) username_input.send_keys(self.username) # Give username input password_input.send_keys(self.password) # Give password input login_button.click() # Click the login button return self.driver.current_url . ├── pages │   ├── __init__.py │   └── login.py ├── requirements.txt └── tests ├── conftest.py └── test_login.py
  • 28. 1 test_login.py # from pages.login import Login def test_login(driver): login_page = Login(driver) assert login_page.login().endswith('dashboard') . ├── pages │   ├── __init__.py │   └── login.py ├── requirements.txt └── tests ├── conftest.py └── test_login.py
  • 29. ⚒ Run the test with pytest # $ python -m pytest ============================= test session starts ============================== platform darwin -- Python x.x.x, pytest-x.x.x, py-x.x.x, pluggy-x.x.x rootdir: project_location collected 1 item tests/test_login.py . [100%] ============================== 1 passed in 16.60s ==============================
  • 30. www.companyname.com © 2016 Startup theme. All Rights Reserved. Why use pytest? # • Flexibility • Fixtures • Test discovery • Logging • Better reporting
  • 31. www.companyname.com © 2016 Startup theme. All Rights Reserved. • pytest-blockage • pytest-benchmark • pytest-browsermob-proxy • pytest-capturelog • pytest-cloud • pytest-config • pytest-rerunfailures • pytest-xdist Plugins? 2
  • 32. pytest-xdist the cool one! $pip install pytest-xdist $python -m pytest -n 100 3
  • 33. www.companyname.com © 2016 Startup theme. All Rights Reserved. Agreed?
  • 34. www.companyname.com © 2016 Startup theme. All Rights Reserved. Thanks!
  • 35. www.companyname.com © 2016 Startup theme. All Rights Reserved. C O N T A C T Floor 5th, Flat-E2 House 12/A, Road-8 Gulshan-1, Dhaka-1212 [email protected]