Use Redis with Ruby (learn fast, forget faster)

First thing first: Install redis gem and then require it from you ruby code:

 require 'redis'

Example 1: Create a new Redis client instance

 r = Redis.new

Example 2: List all keys

 r.keys

Example 3: Append a value to a list, show ALL the values of the list, pop one value from the left of the list

 r.RPUSH("mylist", "myvalue")
 r.LRANGE("mylist", 0, -1)
 r.LPOP("mylist")

Example 4: Insert a value from the left of the list, read the value without removing it from the list

 r.LPUSH("mylist", "myvalue")
 r.LINDEX("mylist", 0)

Use ruby gem “mechanize” as httpclient

Need to require ‘mechanize’

Simple login example:
method 1:


    login_form = @agent.get("#{DD_DEV_ADMIN_LOGIN_URL}").forms[0]
    user_email_input = login_form.field_with(:name => 'admin_user[email]')
    user_password_input = login_form.field_with(:name => 'admin_user[password]')
    user_email_input.value = USERNAME
    user_password_input.value = PASSWORD
    @agent.submit(login_form, login_form.buttons.first)

method 2:

Ruby String.scan and sprintf example

scan(/\w+/): covert ruby string to string array.
sprintf ‘%02d’: in this example it converts 1 to 01

 chapter_node_index = "Chaper 1 Node 1".scan(/\w+/)
 chapter_index = sprintf '%02d', chapter_node_index[1]
 node_index = sprintf '%02d', Integer(chapter_node_index[3])-1
 p ("Chapter_#{chapter_index}_Buildings_#{node_index}")

A piece of workable ruby code to update jira tickets – with ruby 1.9.3-p448 and jiralicious(0.3.0)

require 'rubygems'
require 'jiralicious'

USERNAME = "***"
PASSWORD = "***"
JIRA_URL = "https://example.com"

Jiralicious.configure do |config|
# Leave out username and password
config.username = USERNAME
config.password = PASSWORD
config.uri = JIRA_URL
config.api_version = "latest"
config.auth_type = :basic
end

r = Jiralicious.search("project = PHX AND issuetype = Bug AND status = Resolved")
r.issues.each do |issue|

#print the key with issue.jira_key
p "+++++++++++++ resolved bug found: #{issue.jira_key} +++++++++++++"

#Trigger a transition on this issue: the id of this transition may different on another project.
Jiralicious::Issue::Transitions.go(issue.jira_key, 351)

#Add a comment on the issue.
Jiralicious::Issue::Comment.add("fix included in build: you build No.", issue.jira_key)
end

What is under Calabash-ios

Assume you already worked with calabash-ios for some time, but now you wonder how calabash do the job and what is underlying the cucumber code?

First layer: UIA functions:
Go to open “calabash-cucumber” gem and you will find this file:  uia.rb,  in this file there are uia_* methods. You can use those methods to simulate touch swipe and keyboard type on connected physical devices.

what are uia_functions?  UIA mean UIAutomation which is a xcode instrument. You can think them as “official api for interacting with IOS”, here is the link, and also another link for more detailed reference.

Lets use this method as the example: uia_tap_mark()

def uia_tap_mark(mark)
  uia_handle_command(:tapMark, mark)
end

Now the question is how calabash-ios send the uia command to ios and run it?

Second layer: uia-scripts
There comes the calabash-script, you can download it from calabash github repo. I am still tying to figure out how calabash-cucumber interact with calabash-script, but before that, we still can take a look at what is inside calabash-script.

The definition for tabMark is as below:

(defn tap-mark
  [mark]
  (tap [:view {:marked mark}]))

Basically, calabash-script translate the calabash uia commands to UI Automation commands. Read this:

CalabashScript is a ClojureScript library for writing automated 
functional tests for native iOS Apps.
CalabashScript provides friendly ClojureScript APIs based on 
the (perhaps not so friendly) UIAutomation APIs.

Third layer:
UI Automation (native ios api)

use ipfw on Mac to simulate slow network traffic (verified on 02/14/2014)

(1) create a pipe which limits the speed to 56kbit/s

  sudo ipfw pipe 1 config bw 56Kbit/s  

(2) add the pipe to ipfw list with simple option:

  sudo ipfw add 1 pipe 1 ip from any to any 

(3) delete the pipe:

 udo ipfw delete 1 

Reference links: (note some of them already been expired, but worth a reading)

http://cs.baylor.edu/~donahoo/tools/dummy/tutorial.htm
http://titaniumninja.com/simulating-slow-network-links-on-os-x/
http://backup.noiseandheat.com/blog/2012/02/throttling-bandwidth-on-os-x/
http://benlakey.com/2012/10/14/throttle-bandwidth-on-mac-os-x/

Caputre an IOS screenshot with UIAtuomation Instruments

This piece of script can do the job (inside UIAutomation Instruments):


var target = UIATarget.localTarget();
target.delay(5)
target.captureScreenWithName("1234.png")
target.delay(2)
target.tap({x:478.00, y:571.00})
target.tap({x:326.00, y:443.00})
target.delay(40)
target.captureScreenWithName("1234.png")
target.delay(2)

On tricky thing to notice is:

 you MUST add target.delay after the screenshot function, 
otherwise the screenshot will be empty. 

Also reference this link: http://www.smallte.ch/blog-read_en_29001.html

RSpec notes (copied from other websites)

Rails and RSpec equality tests have a variety of choices.

Rails 3.2 ActiveRecord::Base uses the == equality matcher.

It returns true two different ways:

If self is the same exact object as the comparison object
If self is the same type as the comparison object and has the same ID
Note that ActiveRecord::Base has the == method which is aliased as eql?. This is different than typical Ruby objects, which define == and eql? differently.

RSpec 2.0 has these equality matchers in rspec-expectations:

a.should equal(b) # passes if a.equal?(b)
a.should eql(b) # passes if a.eql?(b)
a.should == b # passes if a == b
RSpec also has two equality matchers intended to have more of a DSL feel to them:

a.should be(b) # passes if a.equal?(b)
a.should eq(b) # passes if a == b
In your example you’re creating a record then finding it.

So you have two choices for testing #find_by_name:

To test if it retrieves the exact same object OR an equivalent Person record with the same ID, then use should == or its equivalent a.should eql or its DSL version should eq

To test if it uses the exact same object NOT an equivalent Person record with the same ID, then use should equal or its DSL version should be

And difference between == eql equal

http://stackoverflow.com/questions/7156955/whats-the-difference-between-equal-eql-and