Watir PageObject Indexed_Property not using string substitution - ruby

In a couple places, I'm creating an indexed_property, but when I call it, the index is always 1.
indexed_property(:team, [
[:span, :team, {css: 'article#AssignTeams-Teams table tr:nth-child(%s) td:nth-child(1) span'}]
])
Implemented:
assign_teams_page.team[5].team_element
When I put a break point on it and make the call, the element's selector is:
article#AssignTeams-Teams table tr:nth-child(1) td:nth-child(1) span
it should be tr:nth-child(5) instead of tr:nth-child(1). Why am I seeing this, how do I fix it?

There's a current bug in page-object 1.0.2 that causes indexed properties to retain the definition of the first time you call them. So if you called:
assign_teams_page.team[1].team_element
assign_teams_page.team[5].team_element
It would be using the index 1 every time. There is a fix in place but not released yet. You can fix the issue by downgrading to page-object 1.0.0 or upgrading to 1.0.3 when it comes out.

Related

how to call clear method on the element object

In my project I have located the text_Field via some other element(via label), something like
element.following_sibling(tag_name: 'input').send_keys 'something'
But now it's typing into the text_field without any problem but I am missing clear method of text_field as it combines both clear and type together. Now I have to call clear method on element.following_sibling(tag_name: 'input') but since it's returning element object, I couldn't do that. Is there any way I can call clear method here? Or can I pass this object by some way to the text_field() method? Any help appreciated.
I know it can be called by converting the watir element into selenium element as given below
element.following_sibling(tag_name: 'input').wd.clear
But here I am missing WATIR waiting time for an element.
This appears to be a limitation in Watir's adjacent methods:
klass = if !plural && opt[:tag_name]
Watir.element_class_for(opt[:tag_name])
elsif !plural
HTMLElement
elsif opt[:tag_name]
Object.const_get("#{Watir.element_class_for(opt[:tag_name])}Collection")
else
HTMLElementCollection
end
Notice that only the :tag_name is used for determining the element's class. For input elements, we need to also consider the type attribute so that we can the right sub-class.
We should fix this in Watir (logged as Issue 878), but in the mean time, you can manually correct the class (ie Watir::TextField) using #to_subtype:
element.following_sibling(tag_name: 'input').to_subtype.clear
I got an answer to my question now.
Here is the answer.
b.text_field(element: element.following_sibling(tag_name: 'input').wd).set 'something'
If anybody has any better idea, please write your answer.Thanks.

How to Fix Document Not Found errors with find

I have a collection of Person, stored in a legacy mongodb server (2.4) and accessed with the mongoid gem via the ruby mongodb driver.
If I perform a
Person.where(email: 'some.existing.email#server.tld').first
I get a result (let's assume I store the id in a variable called "the_very_same_id_obtained_above")
If I perform a
Person.find(the_very_same_id_obtained_above)
I got a
Mongoid::Errors::DocumentNotFound
exception
If I use the javascript syntax to perform the query, the result is found
Person.where("this._id == #{the_very_same_id_obtained_above}").first # this works!
I'm currently trying to migrate the data to a newever version. Currently mongodbrestore-ing on amazon documentdb to make tests (mongodb 3.6 compatible) and the issue remains.
One thing I noticed is that those object ids are peculiar:
5ce24b1169902e72c9739ff6 this works anyway
59de48f53137ec054b000004 this requires the trick
The small number of zeroes toward the end of the id seems to be highly correlated with the problem (I have no idea of the reason).
That's the default:
# Raise an error when performing a #find and the document is not found.
# (default: true)
raise_not_found_error: true
Source: https://docs.mongodb.com/mongoid/current/tutorials/mongoid-configuration/#anatomy-of-a-mongoid-config
If this doesn't answer your question, it's very likely the find method is overridden somewhere in your code!

How to convert View<doc> or Async<doc> to Doc in webSharper?

Please, I am new to WebSharper does anyone know how I can convert View (doc) or Async (doc) to Doc in webSharper? I have tried to open "WebSharper.UI.Next.Client" but does not seem to be compatible with the Doc I am using. The code giving such error is the one I found on the WebSharper website https://try.websharper.com/snippet/adam.granicz/00003b
On the conversions: the functions are Doc.EmbedView and Doc.Async respectively.
The problem with the references was that WebSharper.UI has another separate version still called on its earlier name WebSharper.UI.Next for back-compatibility in older projects. It is recommended to use WebSharper.UI only.
The snippet you have linked was not updated accordingly, I have done it now, and will check others too. The main differences are that Next is no longer used in the namespace names and default HTML combinators take attribute and children lists both (An empty div is now div [] [] while previously there it was a div [] or divAttr [] []).

Watir can't find element but it's present

I'm transitioning from the following environment:
chromedriver 2.27
google-chrome 58
selenium-webdriver 3.0.5
watir 6.0.2
page-object 2.0.0
to the this environment:
chromedriver 2.36
google-chrome 63
selenium-webdriver 3.9.0
watir 6.7.3
page-object 2.2.2
I already have my cucumber tests ready and working on the old environment and some of them are working properly in the new enviorment, but for some reason there is one test that refuses to find a certain element and gives me the following exception (even though the element is clearly present in the page and in the DOM):
timed out after 30 seconds, Element not present in 30 seconds (Watir::Wait::TimeoutError)
the element is defined as such:
table(:some_table, :css => 'table.table.table-condensed.table-striped.sortable tbody')
Did anybody encounter the same issue, I've been looking a lot and couldn't find a solution to this problem.
EXAMPLE:
Feature file:
Feature: HTML Tables
Scenario: Demo of reproducing the problem
Given I am logged in to HTML Tables page
Then within 10 I expect to see Alfreds Futterkiste in the table
Steps:
Given(/I am logged in to HTML Tables page$/) do
visit HtmlTables
end
Then(/^within (.+) I expect to see (.+) in the table$/) do |time_limit, element|
on HtmlTables do |page|
actual_element = page.search_for_element(element, time_limit)
expect(actual_element).to match(element), "Expected element to match: #{element}; got: #{actual_element}"
end
end
Page:
class HtmlTables
include PageObject
page_url "https://www.w3schools.com/html/html_tables.asp"
table(:html_table, :css => 'tbody')
def search_for_element(element, time_limit)
Retriable.retriable on: [Watir::Exception::UnknownObjectException],
tries: time_limit.to_i/10,
base_interval: 10 do
html_table_element.each do |row|
return row[0].text if row[0].text == element
end
end
end
end
It gives out the same exact problem - I think it has a problem locating using css and/or xpath.
The problem is that Watir is being told to find a table element, however the :css locator is finding a tbody element. The current version of Watir validates that the element found has the matching tag name. While I thought Watir had always been doing this, it's possible that there were some edge cases addressed in the recent re-factoring of the Locator class.
Ideally you would be able to switch the table accessor to a tbody one. However, there isn't one implemented. Instead, you'll need to use the generic element one:
element(:html_table, :tbody, css: 'tbody')
Iterating the rows of the table will require explicitly calling #trs:
html_table_element.trs.each do |row|
return row[0].text if row[0].text == element
end

How to use Bigquery streaming insertall on Ruby Rails

EDIT: Fixed - for ruby use "insert_all" instead of "insertAll" like the api specifies. The api for ruby needs updating.
Im using v 0.6.4 of the google-api-client gem and trying to create a streaming insert, but keep getting the following error:
google_bigquery.rb:233:in undefined method `insertAll' for #<Google::APIClient::Resource:0xcbc974 NAME:tabledata> (NoMethodError)
My code is as follows:
def streaming_insert_data_in_table(table, dataset=DATASET)
body = {"rows"=>[
{"json"=> {"person_id"=>1, "name"=>"john"}},
{"json"=> {"person_id"=>2, "name"=>"doe"}},
]}
result = #client.execute(
:api_method=> #bigquery.tabledata.insert_all,
:parameters=> {
:projectId=> #project_id.to_s,
:datasetId=> dataset,
:tableId=>table},
:body_object=>body,
)
puts result.body
end
Could someone tell me if the insetAll has been created for the google-api-client gem? I have tried 'insert' as that is what table, dataset etc use and get the same error as well.. I can however run tabledata.list perfectly fine.. I've tried digging throught the gem source code and didn't get anywhere with that.
Is the body object that I created correct or do I need to alter it?
Any help is much appreciated.
Thanks in advance and have a great day.
Ok. So fixed it and updated the code in the question. For ruby: the method is called "insert_all". Also note that the table & schema must be created BEFORE the insert_all. This id different when compared to the the "jobs.insert" method which will create the table if it doesn't exist

Resources