Ruby Selenium - How to check if text is present only once - ruby

I am working on a application that reports status updates on certain services. I am using Ruby Selenium for testing the application.
For the same purpose I wana test some updates that are just plain text - these updates should appear exactly once in the page. Thus, how can I test if a web page has some text only once ?
I am looking for something like
assertTextPresentOnlyOnce ??

I believe you are looking something like this..:
!20.times { break if (selenium.is_visible(driver.find_element(:id, 'loginid'))
rescue false ); sleep 1}
.
.
[rest of the code you want to execute]
.
.
This will cover you and for page time-out as well if the element you want is not present yet. As soon as selenium finds the element you want the loop will break and the code below will be executed.

Related

Proper way to assert text in Selenium Webdriver/RSpec

So im writing a framework in pure selenium-webdriver and am curious what the proper way to assert text exists (Such as in an alert message for instance on an invalid login for example). Specifically with RSpec.
I can think of two ways that comes to mind. Doing something like so:
text_to_check = driver.find_element(locator).text and then doing something like expect(text_to_check).to be("Bad Login text") The locator in this case would probably be xpath or css locator I guess? Although I feel like xpath would probably make more sense (Im not super familiar with xpath tbh though)
Use the driver.page_source() and then check against that....but that seems brittle if that text exists somewhere else on the page. Also it seems unnecessary to do that and pull in the whole page source to check what is essentially one element.
String expected = “abc.com;
String actualURL= “abc.com”;
Assert.assertEquals(expected, actualURL);
message – Message to be displayed in case of an Assertion Error.
condition – Condition against which the assertion needs to be applied.
Assert.assertTrue(“Assert True test message”, true);

Simplify Move and Maximise screen using Selenium (Ruby)

I am running my selenium tests using a ruby Framework having recently joined a new company. As I am working on 3 screens I have added in a browser step to move the browser window to my left most screen before maximising using
#driver.manage.window.move_to(-1800, 1500)
#driver.manage.window.maximize
My question very simply is, having never used Ruby before, is there a way to refactor these 2 lines into 1 I have tried various ways but always get an error.
It's not possible in this place because both are returning string object so you can't cascade the calling like
#driver.manage.window.move_to(-1800, 1500).maximize
But If it's very important for you, then I will tell you a way to do this, open the window.rb file where you can return the self from move_to function as shown below
def move_to(x, y)
#bridge.reposition_window Integer(x), Integer(y)
self
end
And then you can make a call like this
#driver.manage.window.move_to(-1800, 1500).maximize

How can I get Watir to make a fresh reference on a non-stale element?

A portion of some tests I am writing calls for checking if an option gets removed from a select list once that option has been used. I am inconsistently getting this error: timed out after 60 seconds, waiting for {:xpath=>"//select[#id = 'newIdentifierType']//option", :index=>31} to be located (Watir::Exception::UnknownObjectException)
It causes my test to fail maybe 2-3 times out of 10 runs and seems kind of random. I think Watir is looking for the "old" select list with this ID since it caches the element and may also include that it had 32 items, but it times out since a select list with this ID and 32 items no longer exists. The new select list has the same ID but only 31 items.
Is there a way to always get a new reference on this element even though it's not technically going stale? Am I experiencing this problem due to a different issue?
My current code for getting the options in the select list:
#browser.elements(:xpath => "//select[#id = 'newIdentifierType']//option")
I am using Ruby/Cucumber with Selenium and Watir Webdriver level. I first tried defining the element as a select_list in a page-object but moved it to the step definitions using #browser.element to see if that would stop the timeout. I thought it may ignore Watir's cached elements and get the most current one with the ID, but that does not appear to be the case.
Please avoid using XPath with Watir. Everything you can do with XPath, Watir has a much more readable API to handle.
To check for a specific option not being there, you should avoid collections and locate directly:
el = browser.select_list(id: "newIdentifierType").option(value: "31"))
# or
el = browser.select_list(id: "newIdentifierType").option(text: "This one"))
Then to see if it has gone away:
el.stale?
# or
el.wait_until(:stale?)
That won't test the right thing if the entire DOM has changed, though, so you might need to just relocate:
browser.select_list(id: "newIdentifierType").option(text: "This one")).present?
If you are intent on using a collection, the correct way to get the list of options is:
options = #browser.select(id: 'newIdentifierType').options
el = options.find { |o| o.text == 'This one' }
# Do things
el.stale?

How to handle security alert in Firefox with Selenium

I'm using selenium-webdriver with ruby to write automated tests.
Chrome and the chromedriver binary work really well, but I have an issue with Firefox that is related to the configuration of the browser and that's making my tests fail, whereas they pass with Chrome.
When executing the tests in Firefox, sometimes I get an alert with this message:
Although this page is encrypted, the information you have entered is
to be sent over an unencrypted connection and could easily be read by
a third party
And it breaks the execution. Is there a way of disabling this warning in recent Firefox versions (10+) or handling this behavior with Selenium?
In the process of finding a solution for this, I think I might have found a bug in Capybara (v1.1.2).
I managed to get around this problem using the following approach, instead of using the click from capybara (which would not allow me to capture an exception), I started using the click method from selenium-webdriver.
It seems that when Firefox triggers this alertbox, a string with the message
Although this page is encrypted, the information you have entered is
to be sent over an unencrypted connection and could easily be read by
a third party
is returned as a result of object.native.click, otherwise the string
ok
is returned.
# Define the click method in ruby and call it when you're executing a 'click'
# Public: Click the object.
#
# object - Capybara::Node::Element object to click.
def click object
return_string = object.native.click
check_firefox_alertbox if return_string == "ok"
end
def check_firefox_alertbox
if #current_browser == :firefox
#session.driver.browser.switch_to.alert.accept
end
rescue Exception => e
puts e
end
Here is what you can do. Type about:config in the firefox. You would be presented a number of options (once you pass through a warning message).
You have to look for security.warn_leaving_secure; and security.warn_leaving_insecure. Make both of them false. And you would be good to go.
Please note: This would work only on the FF instance you have made modification to, so you will need to use firefox profile to launch this instance.
Hope this helps.
Actually this meant to be a comment but I need to go above 50 in order to be able to comment..I suppose by 'breaking' the execution you mean that of the Ruby Script right? What happens to Firefox? Needs a click to proceed? If that is the case you can improvise by capturing the Ruby error after inserting the sensitive code (where it breaks) between a BEGIN and a RESCUE clause..Something like this..
BEGIN
.
.
Code that is giving you a headache
.
.
RESCUE
.
Capture the exception and give Ruby a chance to continue the script normally.
.
.
END
Alternatively if you don't fancy the above solution you can go to Firefox and then type in the address box about:config. Filter by 'security.warn' and set to false all the boolean variables you see there. Good riddance, fingers crossed ;)

Issues with Sinatra and Heroku

So I've created and published a Sinatra app to Heroku without any issues. I've even tested it locally with rackup to make sure it functions fine. There are a series of API calls to various places after a zip code is consumed from the URL, but Heroku just wants to tell me there is an server error.
I've added an error page that tries to give me more description, however, it tells me it can't perform a `count' for #, which I assume means hash. Here's the code that I think it's trying to execute...
if weather_doc.root.elements["weather"].children.count > 1
curr_temp = weather_doc.root.elements["weather/current_conditions/temp_f"].attributes["data"]
else
raise error(404, "Not A Valid Zip Code!")
end
If anyone wants to bang on it, it can be reached at, http://quiet-journey-14.heroku.com/ , but there's not much to be had.
Hash doesn't have a count method. It has a length method. If # really does refer to a hash object, then the problem is that you're calling a method that doesn't exist.
That # doesn't refer to Hash, it's the first character of #<Array:0x2b2080a3e028>. The part between the < and > is not shown in browsers (hiding the tags themselves), but visible with View Source.
Your real problem is not related to Ruby though, but to your navigation in the HTML or XML document (via DOM). Your statement
weather_doc.root.elements["weather"].children.count > 1
navigates the HTML/XML document, selecting the 'weather' elements, and (tries to) count the children. The result of the children call does not have a method count. Use length instead.
BTW, are you sure that the document contains a tag <weather>? Because that's what your're trying to select.
If you want to see what's behind #, try
raise probably_hash.class.to_s

Resources