verify text in selenium ruby - ruby

I have an issue with verifying text in selenium webdriver and Ruby.
Aim: Verify that the sentence 'Registration Form' is valid on the website.
I have the below code:
if /registration Form/.match(driver.page_source)
puts "Test Passed".green
else
puts "Test Failed".red
end
This code will output "Test Failed" even though the text is present. I have noticed if I only search for one word E.G. 'Registration' the test will pass, but my code needs to check for more than one word.
Additional question:
In addition, this may be a rather simple question to answer, but even when my test fails with "Test Failed" in Cucumber, my result is actually passed. Does anyone know how I could get the actual Cucumber test to fail via an extra line of code?
Thank you for the help.

Related

Selenium with Ruby - cannot access HTML elements that I want

I am following SauceLab's Ruby and Selenium guidebook - it's a free book to help teach Selenium in combination with Rspec. In the first real example of the book, the author leads us to write a test script that goes to this link:
http://the-internet.herokuapp.com/login
Our script then will insert the appropriate uname and pwd (listed in text on the page... it's tomsmith and SuperSecretPassword!), and then submit the form.
All of the above works - then it goes a little further and looks to introduce an assert to ensure that we got to the correct page (the page that results from successful login). This assert, even when I do as per the example in the book, does not work. It can't find the element, and says so as here:
Failure/Error: expect(#driver.find_element(css: '.flash.success').displayed?)
Selenium::WebDriver::Error::NoSuchElementError:
Unable to locate element: .flash.success
# ./ruby_spec.rb:20:in `block (2 levels) in <top (required)>'
I was hoping I could get some help from SO in helping me formulate a working assert to ensure I'm on the login page after successful login via test script. Here is my code so far
require 'selenium-webdriver'
describe 'Login' do
before(:each) do
#driver = Selenium::WebDriver.for :firefox
end
after(:each) do
#driver.quit
end
it 'test_to_succeed' do
#driver.get 'http://the-internet.herokuapp.com/login'
#driver.find_element(id: 'username').send_keys('tomsmith')
#driver.find_element(id: 'password').send_keys('SuperSecretPassword!')
#driver.find_element(id: 'login').submit
expect(#driver.find_element(css: '.flash.success').displayed?) #line that fails every run
end
end
Minus a little revised rspec syntax, this is exactly as what is listed in the book. The book has for the failing line as below, with some deprecated syntax:
#driver.find_element(css: '.flash.success').displayed?.should be_true
But this also fails with the exact same error above. To further suss out it being my expect(), I have tested the expect() syntax for other elements to test that it is working/not the source of the problem. The source of the problem, instead, is that my script is not resolving the css element as the book indicates it would be able to. It can't find '.flash.success', in loose terms.
So I went to page source of the successful login page and found some other things I could assert against. This came as attractive since it is unique to the success page:
<div data-alert id='flash' class='flash success'>
You logged into a secure area!
×
</div>
But alas, I found the exact same failures when trying this in my code:
expect(#driver.find_element(class: 'flash success').displayed?) #line that fails every run
or even
expect(#driver.find_element(id: 'flash').displayed?) #line that fails every run
Essentially, I'm a little mystified how I am supposed to get a unique element off of the successful login page to assert correctly. All the efforts above (including the one in the sample text that I'm learning from) would seem reasonable to work, to me... but it is not so. Rather than simply switching texts to one with working examples, I thought it would be more instructive for me to work out and try to discover why the examples above do not work.

My Cucumber script doesn't seem to be picking up my Ruby script

I am very new to Cucumber and Ruby, so please forgive me if what I'm missing seems simple. I'm able to run my Cucumber script, and it gives me the following result:
Feature: guru99 Demopage Login
In order to Login in Demopage we have to enter login details
Scenario: Register On Guru99 Demopage without email # test.feature:5
Given I am on the Guru homepage # test.feature:7
When enter blank details for Register # test.feature:9
Then error email shown # test.feature:11
1 scenario (1 undefined)
3 steps (3 undefined)
0m0.040s
You can implement step definitions for undefined steps with these snippets:
Given(/^I am on the Guru homepage$/) do
pending # Write code here that turns the phrase above into concrete actions
end
When(/^enter blank details for Register$/) do
pending # Write code here that turns the phrase above into concrete actions
end
Then(/^error email shown$/) do
pending # Write code here that turns the phrase above into concrete actions
end
This is expected. However, when I add my .rb file which contains:
require 'watir-webdriver'
require 'colorize'
browser = Watir::Browser.new
Given(/^I am on the Guru homepage$/) do
pending # Write code here that turns the phrase above into concrete actions
end
When(/^enter blank details for Register$/) do
pending # Write code here that turns the phrase above into concrete actions
end
Then(/^error email shown$/) do
pending # Write code here that turns the phrase above into concrete actions
end
Running the Cucumber script returns the exact same result as it did before. I have everything in a folder called "features" and the .rb file is in a subfolder of that called step_definitions. What am I doing wrong? I can't seem to find an answer online, so any help at all will be greatly appreciated!
Since your feature files and step definitions are in features folder, try to run cucumber with --require features option.

Having trouble telling ruby/watir webdriver to check for two fail conditions

I am trying to write a simple cucumber step where it checks to see if one of two phrases are on the page, but my attempts at if statements have failed. Here is the step i currently have
Then(/^I should see a pdf preview of the magazine$/) do
#browser.windows.last.use
#browser.text.should_not include "Page not found"
But, ideally, I want the step to check and see if the text "Page not found" is present, if so fail the test. If "page not found" is NOT present, then check to see if the text "Forbidden" is present, if so, fail the test. If neither phrase is present, then pass the test.
Where i think I am stumbling, is I dont know how to tell it, to stop and fail the test.
Ideally, I would think it would look like this...
if text "Page not found" visible?
fail test
elseif text "Forbidden" visible?
fail test
else
pass test
But I dont know how to put that into ruby/watir-webdriver code
Do I just have to make it throw exceptions if the test fails? If its that easy, will that fail the test, or pass it but give me a message and how do I make it pass if neither phrase is visible?
A Cucumber step will stop and fail the test as soon as any of the assertions fail (or an exception is raised). Conversely, a Cucumber step will pass if all assertions pass (or there are no exceptions raised).
As a result, you can simply do:
Then(/^I should see a pdf preview of the magazine$/) do
#browser.windows.last.use
#browser.text.should_not include "Page not found" # first assertion
#browser.text.should_not include "Forbidden" # second assertion
end
With this step:
In the situation where "Page not found" is displayed, the first assertion will fail. As a result the step will be marked as Fail (with the message that the text includes "Page not found"). The rest of the Cucumber step will not be executed - ie the second assertion will not be tested.
In the situation where "Forbidden" is displayed, the first assertion will pass so Cucumber will continue to execute the line. When the second assertion is run, it will fail and therefore cause the step to fail (with the message that the text includes "Forbidden").
In the situation where neither text is present, both assertions will pass. Since Cucumber was able to complete the step without any failed assertions (or exceptions), the step will be marked as Pass.
In the situation where both texts are displayed, the step will Fail, but the error message will only indicate the first failure - ie "Page not found" was displayed.

How to test that a substring, but not a superstring, is present using Cucumber web steps?

I understand that in Cucumber we can use regular expressions in the step definitions, but is there a way for me to use them within Gherkin?
The reason for this is so that I can use the cucumber websteps and keep my code as concise as possible.
Let's say I want to test whether a string is in one of these web pages:
Page 1
hey there
Page 2
hey there!
And I have this step:
Then I should see "hey there"
it passes for both pages. If I have this step:
Then I should see "hey there!"
then it only passes for the second page. But I want a step that passes only on the first page. Is there a way in Gherkin to tell it to not go to check if it goes to the end of the line, or do I have to suck it up and make my own step?
Answer for my own question:
You can't add reg ex's within Gherkin.
It sucks, but its the truth. BUT you can add a "not" :D
Here's the solution to searching for hey there in two instances when you'll have hey there! and hey there in two different files.
Here's one file:
hey there
Here's the second file:
hey there!
Here's one web step:
Scenario: See "hey there" in file 1
Given that I am on "file 1"
Then I should see "hey there"
When testing when you want to see "hey there" in file one, it returns true. Here's where it got complicated:
Scenario: Should not see "hey there" in file 2
Given that I am on "file 2"
Then I should not see "hey there"
Scenario for file 2 fails because it does see "hey there" in file two. To get around this:
Scenario: See not see "hey there!" in file 1
Given that I am on "file 1"
then I should not see "hey there!"
Scenario: Should see "hey there!" in file 2
Given that I am on "file 2"
Then I should see "hey there!"
Solution in Short: Just search for the longer form or, if your file permits it, search for other unique text. Small refactor saving ten seconds of work: fixed.

#driver.find_element(:id=>"body").text.include?(textcheck) not verifying the text only the id

I am using Selenium-WebDriver for Ruby and I am trying to verify that text is present on a page. I have done many searches and tried many things and the best answer I have found is to use something like
def check_page(textcheck)
if verify {#driver.find_element(:id=>"body").text.include?(textcheck)}
yield it_to "fail"
else
yield it_to "pass"
end
end
The expected outcome if the value of textcheck is present in the body would be pass and if the value of textcheck is not present in the body it would be fail. What is actually happening is if :id=>"body" is present then it is pass and if it is not present then it is fail regardless of .text.include?(textcheck)
If anyone could point me in the right direction for how to verify text is present on a page using Selenium-WebDriver in Ruby it would be greatly appreciated. I have found workarounds for certain cases where I can do
verify {#driver.find_element(:tag_name, 'h1').text!=(textcheck)}
but the element I am trying to verify I can't get to so easily. I looked into css locators and was very confused on how to simplify the tag so I could use it. Any help would be greatly appreciated. Thank you very much. If you require any more information from me please let me know and I will provide it as soon as possible.
I am using Ruby 1.93 with Selenium-WebDriver 2.25 testing in Firefox 14.0.1
I do it this way
#wait = Selenium::WebDriver::Wait.new(:timeout => 30)
begin
#wait.until { #driver.find_element(:tag_name => "body").text.include?("your text")}
rescue
puts "Failure! text is not present on the page"
#Or do one of the options below
#raise
#assert_match "true","false", "The text is not present"
end
UPDATE
Answer to your question in the comments section.
There are two kind of "waits", implicit wait and explicit wait. You can read more about it here. The reason your code failed was because you were searching by "id"=>"body" and not by "tag_name"=>"body". Usually all text is encompassed within the "body" HTML tags in your DOM.

Resources