Selenium Testing web applications Michal Ho ej ek, - - PowerPoint PPT Presentation

selenium
SMART_READER_LITE
LIVE PREVIEW

Selenium Testing web applications Michal Ho ej ek, - - PowerPoint PPT Presentation

Selenium Testing web applications Michal Ho ej ek, horejsekmichal@gmail.com def test_list_clients_contracts(api, client, contract): res = api.client.list_contracts(client.id) assert len(res) == 1 assert res[0] == contract www.seznam.cz


slide-1
SLIDE 1

Michal Hořejšek, horejsekmichal@gmail.com

Testing web applications

Selenium

slide-2
SLIDE 2

www.seznam.cz

def test_list_clients_contracts(api, client, contract): res = api.client.list_contracts(client.id) assert len(res) == 1 assert res[0] == contract

@horejsek

slide-3
SLIDE 3

www.seznam.cz

def test_client_persons_suggest(test_app, client): data = { 'query': client.surname, } response = test_app.json('/client/search', data) assert response.status_code == 200 assert response[‘clients'][0] == client

@horejsek

slide-4
SLIDE 4

www.seznam.cz

describe 'Restaurant', () -> restaurant = new models.Restaurant name: 'Delicious dream' lastUpdate: '2013-01-01 18:30' describe '#getPrintalbeLastUpdate()', () -> it 'should return date as pretty string', () -> res = restaurant.getPrintalbeLastUpdate() assert.equal res, '1. ledna v 18:30'

@horejsek

* Actually this is test of our web app with restaurant‘s lunch menus at Prague Anděl, lunchtimeandel.cz. Don‘t forget to use it when you will be around. :-)

slide-5
SLIDE 5

www.seznam.cz

Selenium

„Selenium automates browsers. That's it!“

@horejsek

slide-6
SLIDE 6

www.seznam.cz

from selenium import webdriver driver = webdriver.Chrome() driver.get('http://www.seznam.cz‘) print driver.title driver.quit()

@horejsek

slide-7
SLIDE 7

www.seznam.cz

from selenium.webdriver.common.keys import Keys input = driver.find_element_by_name('q') input.send_keys('selenium' + Keys.TAB) submit = driver.find_element_by_id(‘submit') submit.click()

@horejsek

slide-8
SLIDE 8

www.seznam.cz

driver.get_elm(‘f').fill_out_and_submit({ 'q': 'selenium', })

@horejsek

slide-9
SLIDE 9

www.seznam.cz

>>> driver.go_to('http://www.seznam.cz‘) >>> driver.get_elm('resultCount‘) NoSuchElementException: Message: u'No element <* id=resultCount> found at http://www.seznam.cz/’

@horejsek

slide-10
SLIDE 10

www.seznam.cz

f = driver.get_elm('link').download_file() f.method f.status_code f.headers f.data

@horejsek

slide-11
SLIDE 11

www.seznam.cz

elm = driver.get_elm('q') elm.send_keys('aa')

# JavaScript changes DOM

time.sleep(1)

# Raises StaleElementException

elm.send_keys('bb‘)

@horejsek

slide-12
SLIDE 12

www.seznam.cz

driver.get_elm('q').send_keys('aa')

# JavaScript changes DOM

time.sleep(1)

# OK now

driver.get_elm('q').send_keys('bb‘)

@horejsek

slide-13
SLIDE 13

www.seznam.cz

from selenium.webdriver.support.wait import WebDriverWait

def callback(driver): return driver.find_element_by_id('id') wait = WebDriverWait(driver, timeout=10) wait.until(callback)

@horejsek

slide-14
SLIDE 14

www.seznam.cz

driver.wait_for_element(id_='id')

@horejsek

slide-15
SLIDE 15

www.seznam.cz

driver.contains_text('sherlock') driver.find_element_by_text('sherlock')

@horejsek

slide-16
SLIDE 16

www.seznam.cz

//div[contains(text(), "xxx")] //select[@name="country“]/option[@value="cr"]

http://www.w3schools.com/xpath/xpath_intro.asp

@horejsek

slide-17
SLIDE 17

www.seznam.cz

class TestCase(WebdriverTestCase): domain = 'https://www.google.com' instances_of_driver = ONE_INSTANCE_PER_TESTCASE def _get_driver(self): return Chrome() def test_doodle(self): self.click('gbqfsb') res = self.contains_text('Doodles‘) self.assertTrue(res)

@horejsek

slide-18
SLIDE 18

www.seznam.cz

@expected_error_page(403) def test_no_permission_for_admin(self): self.go_to(‘/admin‘) @expected_error_messages('exists') def test_already_exists(self): self.get_elm('form').fill_out_and_submit({ 'item.username': 'admin', })

@horejsek

slide-19
SLIDE 19

www.seznam.cz

<script type="text/javascript"> window.jsErrors = []; window.onerror = function(errorMessage) { window.jsErrors.push(errorMessage); } </script>

@horejsek

slide-20
SLIDE 20

www.seznam.cz

def test_doodle(driver): driver.click('gbqfsb') assert driver.contains_text('Doodles‘) def test_search(driver): driver.get_elm('gbqf').fill_out_and_submit({ 'q': 'hello', }) driver.wait_for_element(id_='resultStats')

@horejsek

slide-21
SLIDE 21

www.seznam.cz

Choices: Xvfb, Xephyr, Xvnc # apt-get install xvfb # pip install pyvirtualdisplay

Virtual X server

@horejsek

slide-22
SLIDE 22

www.seznam.cz

from pyvirtualdisplay import Display display = Display(size=(800, 400)) display.start() # ... display.stop()

@horejsek

slide-23
SLIDE 23

www.seznam.cz

  • More browsers
  • More operating systems
  • Faster tests

Selenium grid

@horejsek

slide-24
SLIDE 24

www.seznam.cz

url = 'http://127.0.0.1:4444/wd/hub‘

desire = webdriver.DesiredCapabilities.FIREFOX.copy()

driver = webdriver.Remote( command_executor=url, desired_capabilities=desire, )

@horejsek

slide-25
SLIDE 25

www.seznam.cz

Check documentation for more info: http://horejsek.github.io/python-webdriverwrapper

@horejsek

slide-26
SLIDE 26

www.seznam.cz

Michal Hořejšek, horejsekmichal@gmail.com

Thank you!

@horejsek