by Gopi







Splinter is an open source tool for testing web applications using Python.
It lets you automate browser actions, such as visiting URLs and interacting with their items.
from splinter import Browser
with Browser() as browser:
# Visit URL
url = "http://www.google.com"
browser.visit(url)
browser.fill('q', 'splinter - python acceptance testing for web applications')
# Find and click the 'search' button
button = browser.find_by_name('btnG')
# Interact with elements
button.click()
if browser.is_text_present('splinter.cobrateam.info'):
print "Yes, the official website was found!"
else:
print "No, it wasn't found... We need to improve our SEO techniques"Splinter is an abstraction layer on top of existing browser automation tools such as Selenium, PhantomJS and zope.testbrowser.
Features
For example fill out a form in Selenium:
elem = browser.find_element.by_name('username')
elem.send_keys('janedoe')the equivalent code in Splinter:
browser.fill('username', 'janedoe')Basic browsing and interactions
from splinter import Browser
browser = Browser()browser = Browser('chrome')
browser = Browser('firefox')
browser = Browser('zope.testbrowser')b = Browser(user_agent="Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en)")
b = Browser("firefox", profile="C:\Users\IEUser\AppData\Roaming\Mozilla..")browser.visit('http://cobrateam.info')
browser.visit('http://username:password@cobrateam.info/protected')browser.back()
browser.forward()
browser.reload()browser.title
browser.html
browser.urlbrowser.find_by_css('h1')
browser.find_by_xpath('//h1')
browser.find_by_tag('h1')
browser.find_by_name('name')
browser.find_by_id('firstheader')
browser.find_by_value('query')first_found = browser.find_by_name('name').first
last_found = browser.find_by_name('name').last
second_found = browser.find_by_name('name')[1]Range and indexes
links_found = browser.find_link_by_text('Link for Example.com')
links_found = browser.find_link_by_partial_text('for Example')
links_found = browser.find_link_by_href('http://example.com')
links_found = browser.find_link_by_partial_href('example')Finding links
divs = browser.find_by_tag("div")
within_elements = divs.first.find_by_name("name")Chaining find of elements
browser.find_by_tag('h1').mouse_over()Mouse over
browser.find_by_tag('h1').mouse_out()Mouse out
browser.find_by_tag('h1').click()Click
browser.find_by_tag('h1').double_click()Double Click
browser.find_by_tag('h1').right_click()Right Click
draggable = browser.find_by_tag('h1')
target = browser.find_by_css('.container')
draggable.drag_and_drop(target)Drag and drop
browser.fill('query', 'my name')
browser.attach_file('file', '/path/to/file/somefile.jpg')
browser.choose('some-radio', 'radio-value')
browser.check('some-check')
browser.uncheck('some-check')
browser.select('uf', 'rj')Interacting with forms
browser.find_by_css('h1').first.valueGet Value of element
browser.click_link_by_href('http://www.the_site.com/my_link')
browser.click_link_by_partial_href('my_link')Clicking links
browser.find_by_css('h1').first.visible
browser.find_by_css('.content').first.has_class('content')Verifying elements
browser.is_text_present('splinter') # True
browser.is_text_present('splinter', wait_time=10) # True, using wait_time
browser.is_text_not_present('text not present') # True
browser.is_text_not_present('text not present', wait_time=10) # True, using wait_timeChecking the presence of text
browser.is_element_present_by_css('h1')
browser.is_element_present_by_xpath('//h1')
browser.is_element_present_by_tag('h1')
browser.is_element_present_by_name('name')
browser.is_element_present_by_id('firstheader')
browser.is_element_present_by_value('query')
browser.is_element_present_by_value('query', wait_time=10)
browser.is_element_not_present_by_css('h6')
browser.is_element_not_present_by_xpath('//h6')
browser.is_element_not_present_by_tag('h6')
browser.is_element_not_present_by_name('unexisting-name')
browser.is_element_not_present_by_id('unexisting-header')
browser.is_element_not_present_by_id('unexisting-header', wait_time=10)Checking the presence of elements
It’s possible manipulate cookies using cookies attribute from a Browser instance.
#create cookie
browser.cookies.add({'whatever': 'and ever'})
#Retrieve all cookies
browser.cookies.all()
#Delete a cookie
browser.cookies.delete('mwahahahaha') #deletes the cookie 'mwahahahaha'
browser.cookies.delete('whatever', 'wherever') #deletes two cookies
#Delete all cookies
browser.cookies.delete() #deletes all cookieswith browser.get_iframe('iframemodal') as iframe:
iframe.do_stuff()
#or
with browser.get_iframe(1) as iframe:
iframe.do_stuff()Using iframe
Handling alerts and prompts
alert = browser.get_alert()
alert.text
alert.accept()
alert.dismiss()
prompt = browser.get_alert()
prompt.text
prompt.fill_with('text')
prompt.accept()
prompt.dismiss()browser.windows # all open windows
browser.windows[0] # the first window
browser.windows[window_name] # the window_name window
browser.windows.current # the current window
browser.windows.current = browser.windows[3] # set current window to window 3
window = browser.windows[0]
window.is_current # boolean - whether window is current active window
window.is_current = True # set this window to be current window
window.next # the next window
window.prev # the previous window
window.close() # close this window
window.close_others() # close all windows except this oneYou can manage multiple windows (such as popups) through the windows object
The biggest problem was double authentication on emstools
Solution
b = Browser("firefox", profile="C:\Users\IEUser\AppData\Roaming\Mozilla..")
brow.find_by_id('main-menu-list-manager').mouse_over()
brow.find_by_id('main-menu-sub-item-data-import').click()
brow.find_by_id('newImport').click()
brow.attach_file('list',folder+"import_qrs.csv")
brow.find_by_css('.select2-choice').click()
brow.find_by_id('s2id_autogen14_search').fill('E-Mail')
#...
brow.find_by_id('submitButton').click()
1. Precondition: create HTML camp.
table = brow.find_by_css('.htmlarea') # Find the html part
table.find_by_css('.button')[18].click()
brow.fill('ta','<html><body>'+htmlcampname+'</body></html>')
brow.execute_script("javascript:if(document.getElementById('auto_autotl_y').checked) {document.workform.allow_auto_autotl.value='y'}; CheckChanges(); isLaunched();")2. Schedule a recurring program
brow.find_by_css('div.node').double_click()
fivemoremin = datetime.datetime.now() + datetime.timedelta(minutes=5)
brow.select('hourOfRecurrence',str(fivemoremin.hour).zfill(2))
brow.select('minuteOfRecurrence',str(fivemoremin.minute).zfill(2))3. Create blank program
- window handling
brow.windows.current = brow.windows.current.next
mainpage = brow.windows.current.prev- keep original filename
filename = glob.glob(folder+'rename*.png')[0]
renamed = 'rename_'+str(timename)+'.png'
os.rename(filename,folder+renamed)
brow.attach_file('fd-file',folder+renamed)- Multiple file upload
brow.attach_file('fd-file',folder+"Media_db_1.docx")
brow.attach_file('fd-file',folder+"Media_db_2.doc")
brow.attach_file('fd-file',folder+"Sad_face.gif")
brow.attach_file('fd-file',folder+"jusztin.jpg")
brow.attach_file('fd-file',folder+"Splinter_tf.png")
brow.find_by_css('.upload-button').click()sel = iframe.find_by_xpath("//select[@name='template']/option[text()='Test (mobile ready)']").value
iframe.select('template',sel)- Find template on every env
- select segment
iframe.find_by_id("s2id_autogen1_search")
.fill('segment for test last name')
iframe.find_by_css(".select2-result-label").click()- Click draft button for section
<div id="section_status" class="bootstrap-switch switch-small js-btn-stickit-switch pull-left has-switch" data-off-label="<i class='icon-remove'></i>" data-on-label="<i class='icon-ok icon-white'></i>" data-animated="false">
<div class="switch-off">
<input type="checkbox">
<span class="switch-left switch-small">
<label class="switch-small"> </label>
<span class="switch-right switch-small">
</div>
</div>div3 = brow.find_by_id('section_status')
div3.find_by_css('.switch-small')[1].click()Open a mail and click on GenReg, and NEWS registration forms link, and do registration
- find today's mail on gmail
YM = datetime.datetime.now().strftime('%Y. %B')
day = datetime.datetime.now().strftime('%d.')
today = str(YM)+' '+str(day).lstrip('0')
brow.find_by_css('.ajy').click()
if(brow.is_text_present(today.lower())):
Splinter:
http://splinter.cobrateam.info/en/latest/
https://github.com/cobrateam/splinter
QRS Test-O-Matic:
https://emarsys.jira.com/wiki/pages/viewpage.action?pageId=115999027

(except this :) )