Ok trying to do a rspec capybara test for a table resultset.
So I have this table
**Username Fullname Address Location**
simon Simon Lau NY US
foo.bar Foo Bar AU AU
Now i want to get the first row and check if the username (simon) contains the string(mon)
How can i do it dynamically using rspec capybara if we take into consideration that the values might change but I will always want to check the first row and the username column.
Related
I am trying to scrape this site using import.io: ScoreCard
I am able to get the batting scores successfully but I want to insert additional column in the end which can tell me about the innings. So it should be relative to the name of batsman.
I tried to use XPATH: //*[#id="innings_1"]/div[1]/div/h4/b
but that will always return First Inning as ID is "innings_1".
Other IDs are innings_2/3/4 etc. Is there any way in XPATH where I can get this element relative to Batsman column?
Here is what I did in order to get the desired result:
I used following XPATH value.
.//a/ancestor::div/div[1]/div/h4/b
.//a was providing me name of Batsmen. I searched for its ancestors and the path div[1]/div/h4/b was being used by only Innings section.. So it did the trick :)
Try using starts-with():
//*[starts-with(#id,'innings_')/div/div/h4/b
I want to display the table in the below format. How can I achieve this?
---A----+-----B-----+----C-----
12335 | abcd | qwerty
45335 | efgh | poiuy
78956 | hjukukuk | mkloijhkll
12346 | sfsfsf | vbhkhadad
EDIT 1:
The contents of the table can be of any length. The width of the particular cell has to be decided by the content itself.
I got the column width from
col_width = a.transpose.map{|col| col.map{|cell| cell.to_s.length}.max}
and displayed the table contents with:
a.each{|row| puts '['+
row.zip(col_width).map{|cell, w| cell.to_s.ljust(w)}.join(' | ')+']'}
where 'a' contains the data from the database.
I only cannot get to print the column headers.
How i can achieve those so that it can align with the table cell contents.
I need to display the output in the console. I am using OCI for accessing the database.
The Sequel ORM can do this using the pretty_table extension. Otherwise HIRB is capable of doing it, either with ActiveRecord, or from arrays/hashes.
I use Sequel often, and have occasional need to display a table summary on the console, so pretty_table works nicely for me.
HIRB is used in the irbtools plugin for IRB, and provides table output for all sorts of things.
In either case, the displayed table width is dynamically determined using the lengths of the strings being displayed to find the widths of the columns. I've never tried pushing really long strings through either that would require wrapping inside a column, but they should automatically handle that since it's a common requirement.
I wrote a gem that will help you with this: http://github.com/arches/table_print
I'm processing a ton of book records (12.5 million) with Ruby and Datamapper. On rare occasion I need to grab associated identifiers for a particular book record, but Datamapper is creating a select statement grabbing all the associated identifiers for all the book records. The query take more than 2 minutes.
http://datamapper.org/why.html
The help document says this is "Strategic Eager Loading" and...
"The idea is that you aren't going to load a set of objects and use only an association in just one of them. This should hold up pretty well against a 99% rule.
When you don't want it to work like this, just load the item you want in it's own set. So DataMapper thinks ahead. We like to call it "performant by default". This feature single-handedly wipes out the "N+1 Query Problem"."
However, how do you load an item in it's own set? I can't seem to find a way to specify that I really only want to query the identifiers for one of the book records.
If you are experiencing this issue, it might be because you are using Model.first() rather than Model.get(). See my comments under the question too.
As of DM 1.1.0...
Example using Model.first:
# this will create a select statement for one book record
book = Books.first(:author => 'Jane Austen')
# this will create select statement for all isbns associated with all books
# if there are a lot of books and identifiers, it will take forever
book.isbns.each do |isbn|
# however, as expected it only iterates through related isbns
puts isbn
end
This is the same behavior as using Book.all, and then selecting the associations on one
Example using Model.get:
# this will create a select statement for one book record
book = Books.get(2345)
# this will create select statement for book with a primary key of 2345
book.isbns.each do |isbn|
puts isbn
end
Assume that, a page contains a table of records (created dynamically) with Delete, Edit and View options (for each row/record). Now i want to find a specific record with name/id and delete that record. Is this possible in Selenium?
Help me to resolve this! Thanks in Advance.
Sure, we do this all the time in our tests. You need to write an element locator that finds the row you want and identifies the appropriate button, then just Selenium.Click(...) on it. The exact locator value will depend on your application, but it will probably be something like xpath=//path/to/table/tr[td[pos() = 1 AND text() = 'My Name']]/td/button[#value='Delete']. In other words, "the button with the value 'Delete' in the row that has 'My Name' in its first cell in the table".
So I set a variable in my main ruby file that's handling all my post and get requests and then use ERB templates to actually show the pages. I pass the database handler itself into the erb templates, and then run a query in the template to get all (for this example) grants.
In my main ruby file:
grants_main_order = "id_num"
get '/grants' do
erb :grants, :locals => {:db=>db, :order=>grants_main_order, :message=>params[:message]}
end
In the erb template:
db = locals[:db]
getGrants = db.exec("SELECT * FROM grants ORDER BY $1", [locals[:order]])
This produces some very random ordering, however if I replace the $1 with id_num, it works as it should.
Is this a typing issue? How can I fix this? Using string replacement with #{locals[:order]} also gives funky results.
Parameters are there to put in constant values into the query. It's possible and legal, but not meaningful to use them in an ORDER BY-clause.
Say you want to issue this query:
SELECT first_name, last_name
FROM people
ORDER BY first_name
If you put "first_name" in a string and pass it in as a parameter, you instead get:
SELECT first_name, last_name
FROM people
ORDER BY "first_name"
The difference is huge. That last ORDER BY-clause really tells te database not to care about the column values for each row, and just sort as if all rows were identical. Sorting order will be random.
I would recommend using datamapper (http://datamapper.org/) for sinatra. It's a very slick ORM and handles the paramaterized queries you are trying to build quite well.
have you inspected what locals[:order] is? Maybe something funky in there.
p locals[:order]