Selenium with py test by Alexandr Vasyliev for Lohika Odessa Python TechTalks
Python Testing 101 with Selenium
-
Upload
leonardo-jimenez -
Category
Software
-
view
254 -
download
7
Transcript of Python Testing 101 with Selenium
![Page 1: Python Testing 101 with Selenium](https://reader031.fdocuments.us/reader031/viewer/2022020110/55a4487d1a28ab48748b45d8/html5/thumbnails/1.jpg)
Testing WebApps 101For newbies by newbies
![Page 2: Python Testing 101 with Selenium](https://reader031.fdocuments.us/reader031/viewer/2022020110/55a4487d1a28ab48748b45d8/html5/thumbnails/2.jpg)
Requisites for the talk● Virtualenv (of course)● Django● Selenium
![Page 3: Python Testing 101 with Selenium](https://reader031.fdocuments.us/reader031/viewer/2022020110/55a4487d1a28ab48748b45d8/html5/thumbnails/3.jpg)
Different Types of tests● Unit Tests
A unit test establishes that the code does what you intended the code to do (e.g. you wanted to add parameter a and b, you in fact add them, and don't subtract them)
![Page 4: Python Testing 101 with Selenium](https://reader031.fdocuments.us/reader031/viewer/2022020110/55a4487d1a28ab48748b45d8/html5/thumbnails/4.jpg)
Different Types of tests● Functional Tests
Functional tests test that all of the code works together to get a correct result, so that what you intended the code to do in fact gets the right result in the system.
![Page 5: Python Testing 101 with Selenium](https://reader031.fdocuments.us/reader031/viewer/2022020110/55a4487d1a28ab48748b45d8/html5/thumbnails/5.jpg)
Different Types of tests
● Integration Tests
Is the phase in software testing in which individual software modules are combined and tested as a group
Integration tests tell what's not working. But they are of no use in guessing where the problem could be.
![Page 6: Python Testing 101 with Selenium](https://reader031.fdocuments.us/reader031/viewer/2022020110/55a4487d1a28ab48748b45d8/html5/thumbnails/6.jpg)
Some bugs are dependent of other modules
![Page 7: Python Testing 101 with Selenium](https://reader031.fdocuments.us/reader031/viewer/2022020110/55a4487d1a28ab48748b45d8/html5/thumbnails/7.jpg)
A single bug will break several features, and several integration tests will fail
![Page 8: Python Testing 101 with Selenium](https://reader031.fdocuments.us/reader031/viewer/2022020110/55a4487d1a28ab48748b45d8/html5/thumbnails/8.jpg)
On the other hand, the same bug will break just one unit test
![Page 9: Python Testing 101 with Selenium](https://reader031.fdocuments.us/reader031/viewer/2022020110/55a4487d1a28ab48748b45d8/html5/thumbnails/9.jpg)
virtualenv --python=/Library/Frameworks/Python.framework/Versions/3.3/bin/python3.3 testdir
Installing Virtualenv
![Page 10: Python Testing 101 with Selenium](https://reader031.fdocuments.us/reader031/viewer/2022020110/55a4487d1a28ab48748b45d8/html5/thumbnails/10.jpg)
Functional Testing
![Page 11: Python Testing 101 with Selenium](https://reader031.fdocuments.us/reader031/viewer/2022020110/55a4487d1a28ab48748b45d8/html5/thumbnails/11.jpg)
from selenium import webdriver
browser = webdriver.Firefox()browser.get('http://localhost:8000')
assert 'Django' in browser.title
funcional_test.py
![Page 12: Python Testing 101 with Selenium](https://reader031.fdocuments.us/reader031/viewer/2022020110/55a4487d1a28ab48748b45d8/html5/thumbnails/12.jpg)
from selenium import webdriver
browser = webdriver.Firefox()browser.get('http://localhost:8000')
assert 'Django' in browser.title
browser.quit()
funcional_test.py
![Page 13: Python Testing 101 with Selenium](https://reader031.fdocuments.us/reader031/viewer/2022020110/55a4487d1a28ab48748b45d8/html5/thumbnails/13.jpg)
def setUp(self): self.browser = webdriver.Firefox() self.browser.implicitly_wait(3)
Implicits wait
![Page 14: Python Testing 101 with Selenium](https://reader031.fdocuments.us/reader031/viewer/2022020110/55a4487d1a28ab48748b45d8/html5/thumbnails/14.jpg)
from selenium import webdriver
import unittest
class NewVisitorTest(unittest.TestCase): #
def setUp(self): #
self.browser = webdriver.Firefox()
def tearDown(self): #
self.browser.quit()
def test_can_start_a_list_and_retrieve_it_later(self):
self.browser.get('http://localhost:8000')
self.assertIn('To-Do', self.browser.title) #
self.fail('Finish the test!') #
if __name__ == '__main__': #
unittest.main(warnings='ignore') #
![Page 15: Python Testing 101 with Selenium](https://reader031.fdocuments.us/reader031/viewer/2022020110/55a4487d1a28ab48748b45d8/html5/thumbnails/15.jpg)
from selenium import webdriverfrom selenium.webdriver.common.keys import Keysimport unittest
class NewVisitorTest(unittest.TestCase):
def setUp(self): self.browser = webdriver.Firefox() self.browser.implicitly_wait(3)
def tearDown(self): self.browser.quit()
#It continues below…. next slide
![Page 16: Python Testing 101 with Selenium](https://reader031.fdocuments.us/reader031/viewer/2022020110/55a4487d1a28ab48748b45d8/html5/thumbnails/16.jpg)
# We continue here... def test_can_start_a_list_and_retrieve_it_later(self): # Edith has heard about a cool new online to-do app. She goes # to check out its homepage self.browser.get('http://localhost:8000')
# She notices the page title and header mention to-do lists self.assertIn('To-Do', self.browser.title) header_text = self.browser.find_element_by_tag_name('h1').text self.assertIn('To-Do', header_text)
# She is invited to enter a to-do item straight away inputbox = self.browser.find_element_by_id('id_new_item') self.assertEqual( inputbox.get_attribute('placeholder'), 'Enter a to-do item' )
![Page 17: Python Testing 101 with Selenium](https://reader031.fdocuments.us/reader031/viewer/2022020110/55a4487d1a28ab48748b45d8/html5/thumbnails/17.jpg)
Django UnitTest
![Page 18: Python Testing 101 with Selenium](https://reader031.fdocuments.us/reader031/viewer/2022020110/55a4487d1a28ab48748b45d8/html5/thumbnails/18.jpg)
from django.test import TestCase
Django Tests
![Page 19: Python Testing 101 with Selenium](https://reader031.fdocuments.us/reader031/viewer/2022020110/55a4487d1a28ab48748b45d8/html5/thumbnails/19.jpg)
Views
![Page 20: Python Testing 101 with Selenium](https://reader031.fdocuments.us/reader031/viewer/2022020110/55a4487d1a28ab48748b45d8/html5/thumbnails/20.jpg)
from django.core.urlresolvers import resolvefrom django.test import TestCasefrom django.http import HttpRequestfrom lists.views import home_page
class HomePageTest(TestCase):
def test_root_url_resolves_to_home_page_view(self): found = resolve('/') self.assertEqual(found.func, home_page)
def test_home_page_returns_correct_html(self): request = HttpRequest() # response = home_page(request) # self.assertTrue(response.content.startswith(b'<html>')) # self.assertIn(b'<title>To-Do lists</title>', response.content) # self.assertTrue(response.content.endswith(b'</html>')) #
![Page 21: Python Testing 101 with Selenium](https://reader031.fdocuments.us/reader031/viewer/2022020110/55a4487d1a28ab48748b45d8/html5/thumbnails/21.jpg)
from django.core.urlresolvers import resolvefrom django.test import TestCasefrom lists.views import home_page #
class HomePageTest(TestCase):
def test_root_url_resolves_to_home_page_view(self): found = resolve('/') # self.assertEqual(found.func, home_page) #
Views Tests
![Page 22: Python Testing 101 with Selenium](https://reader031.fdocuments.us/reader031/viewer/2022020110/55a4487d1a28ab48748b45d8/html5/thumbnails/22.jpg)
MODELS
![Page 23: Python Testing 101 with Selenium](https://reader031.fdocuments.us/reader031/viewer/2022020110/55a4487d1a28ab48748b45d8/html5/thumbnails/23.jpg)
from lists.models import Item[...]
class ItemModelTest(TestCase):
def test_saving_and_retrieving_items(self): first_item = Item() first_item.text = 'The first (ever) list item' first_item.save() second_item = Item() second_item.text = 'Item the second' second_item.save() saved_items = Item.objects.all() self.assertEqual(saved_items.count(), 2)
first_saved_item = saved_items[0] second_saved_item = saved_items[1] self.assertEqual(first_saved_item.text, 'The first (ever) list item') self.assertEqual(second_saved_item.text, 'Item the second')
![Page 24: Python Testing 101 with Selenium](https://reader031.fdocuments.us/reader031/viewer/2022020110/55a4487d1a28ab48748b45d8/html5/thumbnails/24.jpg)
POST Requests
![Page 25: Python Testing 101 with Selenium](https://reader031.fdocuments.us/reader031/viewer/2022020110/55a4487d1a28ab48748b45d8/html5/thumbnails/25.jpg)
def test_home_page_can_save_a_POST_request(self): request = HttpRequest() request.method = 'POST' request.POST['item_text'] = 'A new list item'
response = home_page(request)
self.assertEqual(Item.objects.count(), 1) # new_item = Item.objects.first() # self.assertEqual(new_item.text, 'A new list item') #
self.assertIn('A new list item', response.content.decode()) expected_html = render_to_string( 'home.html', {'new_item_text': 'A new list item'} ) self.assertEqual(response.content.decode(), expected_html)
![Page 26: Python Testing 101 with Selenium](https://reader031.fdocuments.us/reader031/viewer/2022020110/55a4487d1a28ab48748b45d8/html5/thumbnails/26.jpg)
def test_home_page_can_redirect_after_POST(self): request = HttpRequest() request.method = 'POST' request.POST['item_text'] = 'A new list item'
response = home_page(request)
self.assertEqual(Item.objects.count(), 1) new_item = Item.objects.first() self.assertEqual(new_item.text, 'A new list item')#Redirect instead of template self.assertEqual(response.status_code, 302) self.assertEqual(response['location'], '/') #W