Skip to content

Instantly share code, notes, and snippets.

View devhero's full-sized avatar
😁

Andrea Parisi devhero

😁
  • devhero
  • Milan
View GitHub Profile
from django.db import transaction
def before_all(context):
transaction.set_autocommit(False)
def before_scenario(context, scenario):
context._savepoint = transaction.savepoint()
# retrieve webelement
input = driver.find_element_by_xpath("//*[@name='fieldname']")
# set attribute from js script referencing webelement found in python code
driver.execute_script("arguments[0].setAttribute('value', 'new value!')", input);
# or
driver.execute_script("arguments[0].setAttribute('value', arguments[1])", input, 'new value!');
# Code for File Upload
file_input = driver.find_element_by_id("uploadBtn")
file_input.send_keys("/absolute/path/to/file")
# Use a custom compare function that converts the strings into sub-lists of integers. Those will sort correctly without problems.
ls = ['1.1', '1.10', '1.2', '1.2.3', '1.2.1', '1.9']
def section(s):
return [int(_) for _ in s.split(".")]
sorted(ls, key=section)
# ['1.1', '1.2', '1.2.1', '1.2.3', '1.9', '1.10']
@devhero
devhero / selenium_async_scripts.py
Created January 13, 2017 14:45
selenium async scripts
driver = context.browser
# first set a long timeout for async scripts
driver.set_script_timeout(10)
# then prepare an async function
# eg. retrieve status of a jquery ajax GET
status_code = driver.execute_async_script("""
var done = arguments[0];
$.ajax({
# You can pass additional context to the template using keyword arguments:
{% include "name_snippet.html" with person="Jane" greeting="Hello" %}
# If you want to render the context only with the variables provided (or even no variables at all), use the only option. No other variables are available to the included template:
{% include "name_snippet.html" with greeting="Hi" only %}
@devhero
devhero / spin_icon_fa.html
Created December 13, 2016 15:01
rotating spinning icon font awesome - http://fontawesome.io/examples/
Use the fa-spin class to get any icon to rotate, and use fa-pulse to have it rotate with 8 steps. Works well with fa-spinner, fa-refresh, and fa-cog.
<i class="fa fa-spinner fa-spin fa-3x fa-fw"></i>
<span class="sr-only">Loading...</span>
<i class="fa fa-circle-o-notch fa-spin fa-3x fa-fw"></i>
<span class="sr-only">Loading...</span>
<i class="fa fa-refresh fa-spin fa-3x fa-fw"></i>
<span class="sr-only">Loading...</span>
@receiver(post_save, sender=Article)
def generate_thumbnails(sender, instance=None, created=False, **kwargs):
if not instance:
return
if hasattr(instance, '_dirty'):
return
do_something()
@devhero
devhero / docker_virtualbox_default_machine
Created December 5, 2016 09:46
docker virtualbox default machine
// create
docker-machine create -d virtualbox --virtualbox-memory '2048' --virtualbox-cpu-count '2' default
// list and start
docker-machine ls
docker-machine start default
docker-machine env
eval $(docker-machine env)
// stop
@devhero
devhero / tuple_to_dict.py
Last active December 2, 2016 11:02
tuple to dict
dict(map(list, (("a",1), ("b",2))))
# {'a': 1, 'b': 2}
dict(map(reversed, (("a",1), ("b",2))))
# {1: 'a', 2: 'b'}
# from function list, usable for eval
dict(map(lambda x: (x.__name__, x), [round, abs]))
# {'abs': <function abs>, 'round': <function round>}
import os
import socket
os.environ['DJANGO_LIVE_TEST_SERVER_ADDRESS'] = '0.0.0.0:8000'
class IntegrationTests(StaticLiveServerTestCase):
live_server_url = 'http://{}:8000'.format(
socket.gethostbyname(socket.gethostname())
)