3921 cards

Selenium: Network throttling via Chromedriver

You can throttle the network in your headless chrome via Selenium. This might be useful for debugging issues with flaky integration tests or slow page simulations.

page.driver.browser.network_conditions = {offline: false, latency: 5, download_throughput: 2 * 1024, upload_throughput: 2 * 1024}

The settings will match to the following UI component in Chrome:

Image

Were the values for the default profiles might match the values from this post:

**S...

Linked content

List of Chromium Command Line Switches « Peter Beverloo

An extensive list of command line options when booting Chrome.

This is useful for building a Capybara driver with custom Chrome options.

Repeats

Use the Git stash without shooting yourself in the foot

The Git stash does not work like a one-slot clipboard and you might shoot yourself in the foot if you pretend otherwise.

In particular git stash apply does not remove the stashed changes from the stash. That means you will probably apply the wrong stash when you do git stash apply after a future stashing.

To keep your stash clean, you can use

git stash pop

instead.

Another way to look at it:

git stash pop 

is the same as

git stash apply && git stash drop

Notice: In case of a conflict git will not pop th...

Repeats

Merging two arbitrary ActiveRecord scopes

Rails 3+ allows you to join two scopes from arbitrary sources by calling the merge method:

scope1 = User.where(:email => 'foo@bar.com')
scope2 = User.where(:first_name => 'hans')
merged_scope = scope1.merge(scope2)

merged_scope.to_a will now trigger a query for the combined scope chain:

SELECT `users`.* FROM `users` WHERE `users`.`email` = 'foo@bar.com' AND `users`.`first_name` = 'hans'

If you are joining two models, you can also merge scopes for different models ([old blog post](https://web.archive.org/web/2018040106...

Repeats

Gatekeeping: Guide for developer

Note: This process is tailored to our specific needs and tools at makandra. While it will certainly not apply to all (especially larger teams), we think it is a helpful starting point. Compare also the Gatekeeping: Guide for gatekeeper card.


In order to reduce the number of rejects we get from clients, we want to review all code written before it goes to the staging server.

If your project manager wants to do gatekeeping o...

Unpoly 2.1.0 released

Built-in loading indicator

Unpoly now shows an animated progress bar that appears for late requests.

Video of progress bar animation

The progress bar is enabled by default. If you're using unpoly-migrate.js, the progress bar is disabled if you have an up:request:late listener, assuming that you have built a custom loading indicator.

Simplified history opt...

Linked contentAuto-destruct in 38 days

Updated: Don't use migrations to seed default data

It's OK to add data in a migration if your test suite cleans the database before it starts.

For instance, in RSpec:

config.before(:suite) do
  DatabaseCleaner.clean_with(:deletion)
end

Yarn: if integrity check won't let you start rails console

I ran into a situation in which I received the yarn integrity check warning when starting the rails console even though everything was up to date and correct versions in use.

TLDR: run spring stop

I tried starting the rails console without switching to the correct node version first and received the yarn integrity warning.

warning Integrity check: System parameters don't match                                                                                     
error Integrity check failed                                         ...
Linked contentAuto-destruct in 37 days

capybara-lockstep 1.1.0 released

capybara-lockstep synchronizes Capybara commands with client-side JavaScript and AJAX requests. This greatly improves the stability of an end-to-end ("E2E") test suite, even if that suite has timing issues.

1.1.0

  • The [data-initializing] attribute is no longer handled or required.
    Apps that have late initialization after the load event can just use CapybaraLockstep.startWork().
  • Remove useless tracking of interaction events like "click" or "focus".
    If such an event handler...
Linked contentRepeats

HTTP Status codes for redirecting

Formerly 301 (Moved Permanently) and 302 (Found) were used for redirecting. Browsers did implement them in different ways, so since HTTP 1.1 there are some new status codes which allow for finer distinctions.

The interesting part is how non-GET requests are handled by the redirect. It is preferrable to use the newer status code to avoid unexpected behavior.

303 See Other

The response to the request can be found under another URI using the GET method. When received in response to a POST (or PUT/DELETE), the client should presume that ...

Repeats

Summarizing heredoc in Ruby and Rails

This card tries to summarize by example the different uses of heredoc.

  • In Ruby << vs. <<- vs. <<~
  • In Rails strip_heredoc vs. squish

strip_heredoc should be used for a text, where you want to preserve newlines. (multi-line -> multi-line)

squish should be used for a text, where you want to squish newlines. (multi-line -> one-line)

Ruby 2.3+

def foo
  bar = <<~TEXT
    line1
    line2
    line3
  TEXT
  puts bar.inspect
end
foo => "line1\nline2\nline3\n"

Read more: [Unindent HEREDOCs in Ruby 2.3](/makan...

Linked contentRepeats

CSS: Matching against attributes and their values (or parts of them)

You probably know that you can use CSS selectors to match against elements and their attributes, such as:

a[title] { /* any <a> that has a "title" */ }
a[data-fancy="true"] { /* any <a> that has their "data-fancy" attribute set to "true" */ }

But there is more: You do not need to match against "full" attribute values but can match against parts of them.

They work in all somewhat modern browsers, and IE9 or later.

Exact match (CSS2)

[foo="bar"] (matches <div foo="bar" />, but not `<...

Ruby: required keyword arguments in the pre-2.1 era

Starting with Ruby 2.0 you can define methods with keyword arguments.

In 2.1+ required keyword arguments can be defined by using a colon without default value:

def match(value, ignore:)
  ...
end

To accomplish something similar in ruby 1.8, use:

def match(value, options = {})
  ignore = options.fetch(:ignore)
  ...
end
Linked contentAuto-destruct in 30 days

Updated: How to doubleclick in Selenium

When removing the w3c option from Selenium, the attached workaround was deprecated. The working Monkeypacth would be session.driver.browser.action.double_click(self.native).perform, but Capybara Elements nowadays support the double_click method natively, so the Monkeypatch can be removed entirely!

How to avoid raising RestClient exceptions for 4xx or 5xx results

When using RestClient to make an HTTP request, it will raise an exception when receiving a non-successful response.
HTTP status codes like 422 or 403 might be totally expected when talking to APIs, so plastering your code with rescue RestClient::Exception or similar can feel annoying.

It may not be intuitive, but the readme says you can also pass a block to methods like RestClient.get or RestClient::Request.execute. In that case, RestClient will not raise ...

Linked content

Die Grenzen von SEO: was Suchmaschinenoptimierung nicht ist

Der verlinkte Artikel grenzt präzise ab, welche Aufgaben zur Suchmaschinenoptimierung (SEO) gehören und welche nicht.

Suchmaschinenoptimierung ist eine Querschnittsfunktion: die Arbeit vieler unterschiedlicher Abteilungen hat Einfluss auf den SEO-Erfolg der Firma. In diesem Beitrag versuchen wir deswegen, den Kern von SEO zu definieren.

Repeats

Capybara: Easiest way to get the parent of an element

If you already selected an element and want to get its parent, you can call find(:xpath, '..') on it.
To get the parents parent, call find(:xpath, '../..').

Example

Find a link which contains a twitter icon and check that it links to the correct page:

<a href="http://twitter.com/">
  <i class="icon is-twitter"></i>
</a>
link = page.find("a .icon.is-twitter").find(:xpath, '..')
link[:href].should == "http://twitter.com/"

There is a good overview on xpath syntax on [w3schools](https://www.w3schools.com...

Linked contentRepeats

Rails SQL Injection Examples

This page lists many query methods and options in ActiveRecord which do not sanitize raw SQL arguments and are not intended to be called with unsafe user input. Careless use of these methods can open up code to SQL Injection exploits. The examples here do not include SQL injection from known CVEs and are not vulnerabilites themselves, only potential misuses of the methods.

Please use this list as a guide of what not to do.

This website uses short-lived cookies to improve usability.
Accept or learn more