SlideShare una empresa de Scribd logo
1 de 30
Alexander Vasilyev
Selenium+py.test
• Selenium overwiew
• Some Samples
• Actions
• Fixtures
• Travis CI
• Jenkins CI
Agenda
Parts of Selenium:
•Selenium Webdriver (Selenium 2)
•Selenium IDE
•Selenium Server
•Selenium-Grid
Selenium Overview
• Google Chrome
• Internet Explorer 6, 7, 8, 9, 10 - 32 and 64-bit where applicable
• Firefox: latest ESR, previous ESR, current release, one previous
release
• Safari
• Opera
• HtmlUnit
• phantomjs
• Android (with Selendroid or appium)
• iOS (with ios-driver or appium)
Selenium Overview
Selenium Overview
• 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 & Interact
def login_field(self):
return self.wait.until(EC.element_to_be_clickable((By.NAME, 'Login')))
def password_field(self):
return self.wait.until(EC.element_to_be_clickable((By.NAME, 'Password')))
def login_button(self):
return self.wait.until(EC.element_to_be_clickable((By.XPATH, "//button[@type='submit']")))
def error_message(self):
return self.wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@class='error-text']")))
def login_username(self):
return self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "username")))
def logout_button(self):
return self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "logout")))
Find
Interact
def login(self, name, password):
self.login_field().send_keys(name)
self.password_field().send_keys(password)
self.login_button().click()
Actions
actions = ActionChains(driver)
test_plan = wait.until(EC.presence_of_element_located((By.XPATH, ".//ul/li[1]")))
actions.move_to_element(test_plan)
actions.click(test_plan)
actions.perform()
Asserts
Asserts
•assertEqual(a, b)
•assertNotEqual(a, b)
•assertTrue(x)
•assertFalse(x)
error_message = self.wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@class='error-
text']")))
unittest.TestCase.assertEqual(error_message.is_displayed(), True, "Should return error-
message")
All together
import pageObjects.login_page
import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
class TestClass(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.wait = WebDriverWait(self.driver, 10)
def tearDown(self):
self.driver.quit()
pass
def test_input_field(self):
self.driver.get("http://www.ukr.net")
login_page.login("name", "pasword")
unittest.TestCase.assertEqual(login_page.error_message().is_displayed(), True, "Should return error-
message")
if __name__ == '__main__':
unittest.main()
setUp&tearDown
def setUp(self):
self.driver = webdriver.Firefox()
self.wait = WebDriverWait(self.driver, 10)
def tearDown(self):
self.driver.quit()
pass
py.test
http://pytest.org/
The pytest testing tool makes it easy to write small tests, yet scales to support complex functional
testing. It provides
•auto-discovery of test modules and functions,
•detailed info on failing assert statements (no need to remember self.assert* names)
•fixtures for managing small or parametrized long-lived test resources.
•you can use pytest to run test suites based on unittest (or trial), nose
•single-source compatibility from Python2.6 all the way up to Python3.4, PyPy-2.3
•many external plugins.
py.test test discovery
pytest implements the following standard test discovery:
•collection starts from the initial command line arguments which may be directories, filenames or
test ids.
•recurse into directories, unless they match norecursedirs
•test_*.py or *_test.py files, imported by their test package name.
•Test prefixed test classes (without an __init__ method)
•test_ prefixed test functions or methods are test items
py.test asserts
def test_set_comparison():
set1 = set("1308")
set2 = set("8035")
assert set1 == set2
py.test asserts
================================= FAILURES =================================
___________________________ test_set_comparison ____________________________
def test_set_comparison():
set1 = set("1308")
set2 = set("8035")
> assert set1 == set2
E assert set(['0', '1', '3', '8']) == set(['0', '3', '5', '8'])
E Extra items in the left set:
E '1'
E Extra items in the right set:
E '5'
E Use -v to get the full diff
test_assert2.py:5: AssertionError
========================= 1 failed in 0.01 seconds =========================
py.test asserts
Special comparisons are done for a number of cases:
•comparing long strings: a context diff is shown
•comparing long sequences: first failing indices
•comparing dicts: different entries
py.test fixtures
The purpose of test fixtures is to provide a fixed baseline upon which tests
can reliably and repeatedly execute. pytest fixtures offer advantage over
the classic xUnit style of setup/teardown functions:
•fixtures have explicit names and are activated by declaring their use from
test functions, modules, classes or whole projects.
•fixtures are implemented in a modular manner, as each fixture name
triggers a fixture function which can itself use other fixtures.
•fixture management scales from simple unit to complex functional testing,
allowing to parametrize fixtures and tests according to configuration and
component options, or to re-use fixtures across class, module or whole test
session scopes.
py.test fixtures discovery
The discovery of fixtures functions starts at test classes, then test modules,
then conftest.py files and finally built-in and third party plugins.
py.test fixtures
@pytest.fixture
def driver():
_driver = webdriver.Firefox()
def driver_teardown():
_driver.quit()
request.addfinalizer(driver_teardown)
return _driver
def test_server_connect(driver):
driver.get("ukr.net")
assert "UKR" in driver.title
py.test fixtures
@pytest.yield_fixture
def driver():
_driver = webdriver.Firefox()
yield _driver
_driver.quit()
def test_server_connect(driver):
driver.get("ukr.net")
assert "UKR" in driver.title
py.test parameterize
chrome_driver = webdriver.Remote(selenium_grid_url, desired_capabilities={'platform': 'ANY',
'browserName': 'chrome', 'version': '', 'javascriptEnabled': True})
firefox_driver = webdriver.Remote(selenium_grid_url, desired_capabilities={'platform': 'ANY',
'browserName': 'firefox', 'version': '', 'javascriptEnabled': True})
@pytest.mark.parametrize('driver', [chrome_driver, firefox_driver])
def test_login(driver):
login_page = LoginPage(driver)
driver.get("http://www.ukr.net")
login_page.login(login, password)
assert login_page.login_username().is_displayed() is True
py.test pytest-xdist
The pytest-xdist plugin extends py.test with some unique test execution modes:
•test run parallelization: if you have multiple CPUs or hosts you can use those for a
combined test run. This allows to speed up development or to use special resources of
remote machines.
•--boxed: (not available on Windows) run each test in a boxed subprocess to survive
SEGFAULTS or otherwise dying processes
•--looponfail: run your tests repeatedly in a subprocess. After each run py.test waits until a
file in your project changes and then re-runs the previously failing tests. This is repeated
until all tests pass after which again a full run is performed.
•Multi-Platform coverage: you can specify different Python interpreters or different
platforms and run tests in parallel on all of them.
py.test pytest-xdist
Localy : py.test -n5 test_ukrnet.py
Distribute: py.test -d --tx socket=192.168.1.102:8888 --rsyncdir mypkg mypkg
py.test CI-Travis
Travis CI is a hosted continuous integration service. It is integrated with GitHub.
Travis CI's build environment provides different runtimes for different languages, for
instance multiple versions of Python, Ruby, PHP, Node.js. It also comes preinstalled
with a variety of data stores and common tools like message brokers.
py.test CI-Travis
language: python
python:
- "2.7"
before_install:
- "sh -e /etc/init.d/xvfb start"
- "export DISPLAY=:99.0"
- "wget http://selenium-release.storage.googleapis.com/2.44/selenium-server-standalone-2.44.0.ja
- "java -jar selenium-server-standalone-2.44.0.jar > /dev/null &"
- "sleep 10"
# command to install dependencies
install:
- "pip install -r requirements.txt"
# command to run tests
script: py.test
py.test SauceLabs
https://docs.saucelabs.com/tutorials/python/
from selenium import webdriver
sauce_url = "http://YOUR_USERNAME:YOUR_ACCESS_KEY@ondemand.saucelabs.com:80/wd/hub"
desired_capabilities = {
'platform': "Mac OS X 10.9",
'browserName': "chrome",
'version': "31",
}
driver = webdriver.Remote(desired_capabilities=desired_capabilities,
command_executor=sauce_url)
driver.implicitly_wait(10)
py.test CI-Jenkins
py.test --junitxml=path
py.test CI-Jenkins
Thank You!

Más contenido relacionado

La actualidad más candente

Python Unit Test
Python Unit TestPython Unit Test
Python Unit Test
David Xie
 
Python-nose: A unittest-based testing framework for Python that makes writing...
Python-nose: A unittest-based testing framework for Python that makes writing...Python-nose: A unittest-based testing framework for Python that makes writing...
Python-nose: A unittest-based testing framework for Python that makes writing...
Timo Stollenwerk
 
DjangoCon US 2011 - Monkeying around at New Relic
DjangoCon US 2011 - Monkeying around at New RelicDjangoCon US 2011 - Monkeying around at New Relic
DjangoCon US 2011 - Monkeying around at New Relic
Graham Dumpleton
 

La actualidad más candente (20)

Test Driven Development With Python
Test Driven Development With PythonTest Driven Development With Python
Test Driven Development With Python
 
Testing in-python-and-pytest-framework
Testing in-python-and-pytest-frameworkTesting in-python-and-pytest-framework
Testing in-python-and-pytest-framework
 
Token Testing Slides
Token  Testing SlidesToken  Testing Slides
Token Testing Slides
 
Python Unit Test
Python Unit TestPython Unit Test
Python Unit Test
 
JavaScript Test-Driven Development with Jasmine 2.0 and Karma
JavaScript Test-Driven Development with Jasmine 2.0 and Karma JavaScript Test-Driven Development with Jasmine 2.0 and Karma
JavaScript Test-Driven Development with Jasmine 2.0 and Karma
 
Keep your repo clean
Keep your repo cleanKeep your repo clean
Keep your repo clean
 
Unit testing in JavaScript with Jasmine and Karma
Unit testing in JavaScript with Jasmine and KarmaUnit testing in JavaScript with Jasmine and Karma
Unit testing in JavaScript with Jasmine and Karma
 
Modern Python Testing
Modern Python TestingModern Python Testing
Modern Python Testing
 
Testing My Patience
Testing My PatienceTesting My Patience
Testing My Patience
 
Python unittest
Python unittestPython unittest
Python unittest
 
Unit Testing JavaScript Applications
Unit Testing JavaScript ApplicationsUnit Testing JavaScript Applications
Unit Testing JavaScript Applications
 
Testing in Django
Testing in DjangoTesting in Django
Testing in Django
 
Unit testing PHP apps with PHPUnit
Unit testing PHP apps with PHPUnitUnit testing PHP apps with PHPUnit
Unit testing PHP apps with PHPUnit
 
Python-nose: A unittest-based testing framework for Python that makes writing...
Python-nose: A unittest-based testing framework for Python that makes writing...Python-nose: A unittest-based testing framework for Python that makes writing...
Python-nose: A unittest-based testing framework for Python that makes writing...
 
Automation patterns on practice
Automation patterns on practiceAutomation patterns on practice
Automation patterns on practice
 
Painless JavaScript Testing with Jest
Painless JavaScript Testing with JestPainless JavaScript Testing with Jest
Painless JavaScript Testing with Jest
 
DjangoCon US 2011 - Monkeying around at New Relic
DjangoCon US 2011 - Monkeying around at New RelicDjangoCon US 2011 - Monkeying around at New Relic
DjangoCon US 2011 - Monkeying around at New Relic
 
AngularJS Unit Testing w/Karma and Jasmine
AngularJS Unit Testing w/Karma and JasmineAngularJS Unit Testing w/Karma and Jasmine
AngularJS Unit Testing w/Karma and Jasmine
 
Angular testing
Angular testingAngular testing
Angular testing
 
Intro to testing Javascript with jasmine
Intro to testing Javascript with jasmineIntro to testing Javascript with jasmine
Intro to testing Javascript with jasmine
 

Similar a Selenium with py test by Alexandr Vasyliev for Lohika Odessa Python TechTalks

Unit testing in iOS featuring OCUnit, GHUnit & OCMock
Unit testing in iOS featuring OCUnit, GHUnit & OCMockUnit testing in iOS featuring OCUnit, GHUnit & OCMock
Unit testing in iOS featuring OCUnit, GHUnit & OCMock
Robot Media
 
Django’s nasal passage
Django’s nasal passageDjango’s nasal passage
Django’s nasal passage
Erik Rose
 

Similar a Selenium with py test by Alexandr Vasyliev for Lohika Odessa Python TechTalks (20)

Тестирование и Django
Тестирование и DjangoТестирование и Django
Тестирование и Django
 
Unit testing in iOS featuring OCUnit, GHUnit & OCMock
Unit testing in iOS featuring OCUnit, GHUnit & OCMockUnit testing in iOS featuring OCUnit, GHUnit & OCMock
Unit testing in iOS featuring OCUnit, GHUnit & OCMock
 
Browser testing with nightwatch.js
Browser testing with nightwatch.jsBrowser testing with nightwatch.js
Browser testing with nightwatch.js
 
JUnit5 and TestContainers
JUnit5 and TestContainersJUnit5 and TestContainers
JUnit5 and TestContainers
 
How to fake_properly
How to fake_properlyHow to fake_properly
How to fake_properly
 
Good Practices On Test Automation
Good Practices On Test AutomationGood Practices On Test Automation
Good Practices On Test Automation
 
Junit_.pptx
Junit_.pptxJunit_.pptx
Junit_.pptx
 
Grails unit testing
Grails unit testingGrails unit testing
Grails unit testing
 
Data driven testing using Integrant & Spec
Data driven testing using Integrant & SpecData driven testing using Integrant & Spec
Data driven testing using Integrant & Spec
 
Building frameworks over Selenium
Building frameworks over SeleniumBuilding frameworks over Selenium
Building frameworks over Selenium
 
Selenium Webdriver with data driven framework
Selenium Webdriver with data driven frameworkSelenium Webdriver with data driven framework
Selenium Webdriver with data driven framework
 
Django’s nasal passage
Django’s nasal passageDjango’s nasal passage
Django’s nasal passage
 
Testing And Drupal
Testing And DrupalTesting And Drupal
Testing And Drupal
 
Unit testing
Unit testingUnit testing
Unit testing
 
Testing ASP.NET - Progressive.NET
Testing ASP.NET - Progressive.NETTesting ASP.NET - Progressive.NET
Testing ASP.NET - Progressive.NET
 
Web UI test automation instruments
Web UI test automation instrumentsWeb UI test automation instruments
Web UI test automation instruments
 
Testing basics for developers
Testing basics for developersTesting basics for developers
Testing basics for developers
 
Django Pro ORM
Django Pro ORMDjango Pro ORM
Django Pro ORM
 
More on Fitnesse and Continuous Integration (Silicon Valley code camp 2012)
More on Fitnesse and Continuous Integration (Silicon Valley code camp 2012)More on Fitnesse and Continuous Integration (Silicon Valley code camp 2012)
More on Fitnesse and Continuous Integration (Silicon Valley code camp 2012)
 
Unit testing with Spock Framework
Unit testing with Spock FrameworkUnit testing with Spock Framework
Unit testing with Spock Framework
 

Más de Lohika_Odessa_TechTalks

Design and Evolution of APIs in Microservice Architecture
Design and Evolution of APIs in Microservice ArchitectureDesign and Evolution of APIs in Microservice Architecture
Design and Evolution of APIs in Microservice Architecture
Lohika_Odessa_TechTalks
 
Micro-frontends – is it a new normal?
Micro-frontends – is it a new normal?Micro-frontends – is it a new normal?
Micro-frontends – is it a new normal?
Lohika_Odessa_TechTalks
 

Más de Lohika_Odessa_TechTalks (20)

OAuth2 Authorization Server Under the Hood
OAuth2 Authorization Server Under the HoodOAuth2 Authorization Server Under the Hood
OAuth2 Authorization Server Under the Hood
 
Debugging Microservices - key challenges and techniques - Microservices Odesa...
Debugging Microservices - key challenges and techniques - Microservices Odesa...Debugging Microservices - key challenges and techniques - Microservices Odesa...
Debugging Microservices - key challenges and techniques - Microservices Odesa...
 
Design and Evolution of APIs in Microservice Architecture
Design and Evolution of APIs in Microservice ArchitectureDesign and Evolution of APIs in Microservice Architecture
Design and Evolution of APIs in Microservice Architecture
 
Micro-frontends – is it a new normal?
Micro-frontends – is it a new normal?Micro-frontends – is it a new normal?
Micro-frontends – is it a new normal?
 
Multithreading in go
Multithreading in goMultithreading in go
Multithreading in go
 
Druid - Interactive Analytics At Scale
Druid - Interactive Analytics At ScaleDruid - Interactive Analytics At Scale
Druid - Interactive Analytics At Scale
 
DevOps Odessa #TechTalks 21.01.2020
DevOps Odessa #TechTalks 21.01.2020DevOps Odessa #TechTalks 21.01.2020
DevOps Odessa #TechTalks 21.01.2020
 
Jenkins' shared libraries in action
Jenkins' shared libraries in actionJenkins' shared libraries in action
Jenkins' shared libraries in action
 
Prometheus: infrastructure and application monitoring in kubernetes cluster
Prometheus: infrastructure and application monitoring in kubernetes clusterPrometheus: infrastructure and application monitoring in kubernetes cluster
Prometheus: infrastructure and application monitoring in kubernetes cluster
 
Architectural peripherals of react by Vadym Zhiltsov
Architectural peripherals of react by Vadym ZhiltsovArchitectural peripherals of react by Vadym Zhiltsov
Architectural peripherals of react by Vadym Zhiltsov
 
React native by example by Vadim Ruban
React native by example by Vadim RubanReact native by example by Vadim Ruban
React native by example by Vadim Ruban
 
Aws lambda by Leonid Amigud
Aws lambda by Leonid AmigudAws lambda by Leonid Amigud
Aws lambda by Leonid Amigud
 
Congratulations, you have been promoted to a manager role. You`ve got new pro...
Congratulations, you have been promoted to a manager role. You`ve got new pro...Congratulations, you have been promoted to a manager role. You`ve got new pro...
Congratulations, you have been promoted to a manager role. You`ve got new pro...
 
"Don't touch me and give me my money" or how motivate people who can but don...
"Don't touch me and give me my money" or  how motivate people who can but don..."Don't touch me and give me my money" or  how motivate people who can but don...
"Don't touch me and give me my money" or how motivate people who can but don...
 
Docker based Architecture by Denys Serdiuk
Docker based Architecture by Denys SerdiukDocker based Architecture by Denys Serdiuk
Docker based Architecture by Denys Serdiuk
 
SparkSpark in the Big Data dark by Sergey Levandovskiy
SparkSpark in the Big Data dark by Sergey Levandovskiy  SparkSpark in the Big Data dark by Sergey Levandovskiy
SparkSpark in the Big Data dark by Sergey Levandovskiy
 
Burnout and how to avoid it in your team. Responsible person's issue by Andre...
Burnout and how to avoid it in your team. Responsible person's issue by Andre...Burnout and how to avoid it in your team. Responsible person's issue by Andre...
Burnout and how to avoid it in your team. Responsible person's issue by Andre...
 
Performance evaluation process as a way to empower your employees and help th...
Performance evaluation process as a way to empower your employees and help th...Performance evaluation process as a way to empower your employees and help th...
Performance evaluation process as a way to empower your employees and help th...
 
" Performance testing for Automation QA - why and how " by Andrey Kovalenko f...
" Performance testing for Automation QA - why and how " by Andrey Kovalenko f..." Performance testing for Automation QA - why and how " by Andrey Kovalenko f...
" Performance testing for Automation QA - why and how " by Andrey Kovalenko f...
 
"WEB applications security testing" by Kirill Semenov for Lohika Odessa QA Te...
"WEB applications security testing" by Kirill Semenov for Lohika Odessa QA Te..."WEB applications security testing" by Kirill Semenov for Lohika Odessa QA Te...
"WEB applications security testing" by Kirill Semenov for Lohika Odessa QA Te...
 

Último

result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
Tonystark477637
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Christo Ananth
 

Último (20)

Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGMANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 

Selenium with py test by Alexandr Vasyliev for Lohika Odessa Python TechTalks

  • 2. • Selenium overwiew • Some Samples • Actions • Fixtures • Travis CI • Jenkins CI Agenda
  • 3. Parts of Selenium: •Selenium Webdriver (Selenium 2) •Selenium IDE •Selenium Server •Selenium-Grid Selenium Overview
  • 4. • Google Chrome • Internet Explorer 6, 7, 8, 9, 10 - 32 and 64-bit where applicable • Firefox: latest ESR, previous ESR, current release, one previous release • Safari • Opera • HtmlUnit • phantomjs • Android (with Selendroid or appium) • iOS (with ios-driver or appium) Selenium Overview
  • 6. • 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 & Interact
  • 7. def login_field(self): return self.wait.until(EC.element_to_be_clickable((By.NAME, 'Login'))) def password_field(self): return self.wait.until(EC.element_to_be_clickable((By.NAME, 'Password'))) def login_button(self): return self.wait.until(EC.element_to_be_clickable((By.XPATH, "//button[@type='submit']"))) def error_message(self): return self.wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@class='error-text']"))) def login_username(self): return self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "username"))) def logout_button(self): return self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "logout"))) Find
  • 8. Interact def login(self, name, password): self.login_field().send_keys(name) self.password_field().send_keys(password) self.login_button().click()
  • 9. Actions actions = ActionChains(driver) test_plan = wait.until(EC.presence_of_element_located((By.XPATH, ".//ul/li[1]"))) actions.move_to_element(test_plan) actions.click(test_plan) actions.perform()
  • 10. Asserts Asserts •assertEqual(a, b) •assertNotEqual(a, b) •assertTrue(x) •assertFalse(x) error_message = self.wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@class='error- text']"))) unittest.TestCase.assertEqual(error_message.is_displayed(), True, "Should return error- message")
  • 11. All together import pageObjects.login_page import unittest from selenium import webdriver from selenium.webdriver.common.keys import Keys class TestClass(unittest.TestCase): def setUp(self): self.driver = webdriver.Firefox() self.wait = WebDriverWait(self.driver, 10) def tearDown(self): self.driver.quit() pass def test_input_field(self): self.driver.get("http://www.ukr.net") login_page.login("name", "pasword") unittest.TestCase.assertEqual(login_page.error_message().is_displayed(), True, "Should return error- message") if __name__ == '__main__': unittest.main()
  • 12. setUp&tearDown def setUp(self): self.driver = webdriver.Firefox() self.wait = WebDriverWait(self.driver, 10) def tearDown(self): self.driver.quit() pass
  • 13. py.test http://pytest.org/ The pytest testing tool makes it easy to write small tests, yet scales to support complex functional testing. It provides •auto-discovery of test modules and functions, •detailed info on failing assert statements (no need to remember self.assert* names) •fixtures for managing small or parametrized long-lived test resources. •you can use pytest to run test suites based on unittest (or trial), nose •single-source compatibility from Python2.6 all the way up to Python3.4, PyPy-2.3 •many external plugins.
  • 14. py.test test discovery pytest implements the following standard test discovery: •collection starts from the initial command line arguments which may be directories, filenames or test ids. •recurse into directories, unless they match norecursedirs •test_*.py or *_test.py files, imported by their test package name. •Test prefixed test classes (without an __init__ method) •test_ prefixed test functions or methods are test items
  • 15. py.test asserts def test_set_comparison(): set1 = set("1308") set2 = set("8035") assert set1 == set2
  • 16. py.test asserts ================================= FAILURES ================================= ___________________________ test_set_comparison ____________________________ def test_set_comparison(): set1 = set("1308") set2 = set("8035") > assert set1 == set2 E assert set(['0', '1', '3', '8']) == set(['0', '3', '5', '8']) E Extra items in the left set: E '1' E Extra items in the right set: E '5' E Use -v to get the full diff test_assert2.py:5: AssertionError ========================= 1 failed in 0.01 seconds =========================
  • 17. py.test asserts Special comparisons are done for a number of cases: •comparing long strings: a context diff is shown •comparing long sequences: first failing indices •comparing dicts: different entries
  • 18. py.test fixtures The purpose of test fixtures is to provide a fixed baseline upon which tests can reliably and repeatedly execute. pytest fixtures offer advantage over the classic xUnit style of setup/teardown functions: •fixtures have explicit names and are activated by declaring their use from test functions, modules, classes or whole projects. •fixtures are implemented in a modular manner, as each fixture name triggers a fixture function which can itself use other fixtures. •fixture management scales from simple unit to complex functional testing, allowing to parametrize fixtures and tests according to configuration and component options, or to re-use fixtures across class, module or whole test session scopes.
  • 19. py.test fixtures discovery The discovery of fixtures functions starts at test classes, then test modules, then conftest.py files and finally built-in and third party plugins.
  • 20. py.test fixtures @pytest.fixture def driver(): _driver = webdriver.Firefox() def driver_teardown(): _driver.quit() request.addfinalizer(driver_teardown) return _driver def test_server_connect(driver): driver.get("ukr.net") assert "UKR" in driver.title
  • 21. py.test fixtures @pytest.yield_fixture def driver(): _driver = webdriver.Firefox() yield _driver _driver.quit() def test_server_connect(driver): driver.get("ukr.net") assert "UKR" in driver.title
  • 22. py.test parameterize chrome_driver = webdriver.Remote(selenium_grid_url, desired_capabilities={'platform': 'ANY', 'browserName': 'chrome', 'version': '', 'javascriptEnabled': True}) firefox_driver = webdriver.Remote(selenium_grid_url, desired_capabilities={'platform': 'ANY', 'browserName': 'firefox', 'version': '', 'javascriptEnabled': True}) @pytest.mark.parametrize('driver', [chrome_driver, firefox_driver]) def test_login(driver): login_page = LoginPage(driver) driver.get("http://www.ukr.net") login_page.login(login, password) assert login_page.login_username().is_displayed() is True
  • 23. py.test pytest-xdist The pytest-xdist plugin extends py.test with some unique test execution modes: •test run parallelization: if you have multiple CPUs or hosts you can use those for a combined test run. This allows to speed up development or to use special resources of remote machines. •--boxed: (not available on Windows) run each test in a boxed subprocess to survive SEGFAULTS or otherwise dying processes •--looponfail: run your tests repeatedly in a subprocess. After each run py.test waits until a file in your project changes and then re-runs the previously failing tests. This is repeated until all tests pass after which again a full run is performed. •Multi-Platform coverage: you can specify different Python interpreters or different platforms and run tests in parallel on all of them.
  • 24. py.test pytest-xdist Localy : py.test -n5 test_ukrnet.py Distribute: py.test -d --tx socket=192.168.1.102:8888 --rsyncdir mypkg mypkg
  • 25. py.test CI-Travis Travis CI is a hosted continuous integration service. It is integrated with GitHub. Travis CI's build environment provides different runtimes for different languages, for instance multiple versions of Python, Ruby, PHP, Node.js. It also comes preinstalled with a variety of data stores and common tools like message brokers.
  • 26. py.test CI-Travis language: python python: - "2.7" before_install: - "sh -e /etc/init.d/xvfb start" - "export DISPLAY=:99.0" - "wget http://selenium-release.storage.googleapis.com/2.44/selenium-server-standalone-2.44.0.ja - "java -jar selenium-server-standalone-2.44.0.jar > /dev/null &" - "sleep 10" # command to install dependencies install: - "pip install -r requirements.txt" # command to run tests script: py.test
  • 27. py.test SauceLabs https://docs.saucelabs.com/tutorials/python/ from selenium import webdriver sauce_url = "http://YOUR_USERNAME:YOUR_ACCESS_KEY@ondemand.saucelabs.com:80/wd/hub" desired_capabilities = { 'platform': "Mac OS X 10.9", 'browserName': "chrome", 'version': "31", } driver = webdriver.Remote(desired_capabilities=desired_capabilities, command_executor=sauce_url) driver.implicitly_wait(10)