I am trying to build a ruby CLI application and I want my user to select from a certain amount of options that I provide him with.
Remember: This is not a ruby on rails app and there is no views or anything. It is one file of ruby code that I intend to create which shall be responsive to whatever the user types.
When a user runs the script. He should be able to see 3 options and should be able to select them and each of these should have different implications.
A very simple approach without any additional gem might be:
puts "Choose an option"
puts "1 – Option 1"
puts "2 – Option 2"
# ...
case gets.chomp.to_i
when 1
puts "run code for option 1"
when 2
puts "run code for option 2"
# ...
else
puts "invalid option"
end
Related
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.
I'm currently getting an unusual error message in that during the run of my BDD scripts, I get the following response when running through the command line:
Feature: As a user I want to purchase a mobile on a monthly plan
#ACQ_Test_01
Scenario: Buy a pay monthly phone
Given I am on the Store Homepage
When I click on the Mobile Phones roundel link
And I select a "Apple" "Iphone 6s"
-->And I select the "1GB+AYCE min" price plan<--
Then I can complete my order
1 scenario (1 undefined)
5 steps (1 skipped, 1 undefined, 3 passed)
0m0.130s
(The one in arrows is the one highlighted in my command line as being undefined)
However, in my .rb script under step_definitions folder, I have the following:
Given (/^I am on the Store Homepage$/) do
**CONTENT-HERE**
end
When (/^I click on the Mobile Phones roundel link$/) do
**CONTENT-HERE**
end
When (/^I select a "Apple" "Iphone 6s"$/) do
**CONTENT-HERE**
end
When (/^I select the "1GB+AYCE min" price plan$/) do
**CONTENT-HERE**
end
Then (/^I can complete my order$/) do
**CONTENT-HERE**
end
I'm not sure why this cucumber script is missing out a step, but it's infuriating me to no end. Can anyone help?
EDIT: Off the back of that, if anyone can also answer why it's not showing me the snippets that it's expecting, that'd be great.
You should not be creating a step for each plan.
When (/^I select the "([^"]*)" price plan$/) do |plan|
case plan
when "1GB+AYCE min"
# do something
end
end
You're getting a missing step-def error because your regex isn't matching your text.
What you want is:
When (/^I select the "1GB\+AYCE min" price plan$/) do
Which will match the literal character + rather than B one or more times.
Remember, ruby-cucumber uses regular expression syntax rather than literal strings to match step-defs.
Ruby Newbie here. I am working on a landlord application for class.
The idea is to be able to do certain functions that a landlord would be able to do to manage their properties. To accomplish this, I have a main menu method, which functions like this:
def main_menu
puts "Please choose an option from the following menu:"
puts " 1. List All Apartments"
puts " 2. View Apartment Details"
puts " 3. Add an Apartment"
puts " 4. Add a Tenant"
puts " 5. Evict a Tenant"
puts " 6. Quit"
input = gets.chomp
case input
when "1"
list
when "2"
view(select_apt)
when "3"
add
when "4"
tenant(select_apt)
when "5"
evict(select_apt)
when "6"
File.open('listing.txt', 'w') {|file| file.truncate(0) }
File.open('listing.txt', 'w') {|file| file.write(Marshal.dump($array))}
else
puts "I'm sorry, that's not a valid option.\n\n"
main_menu
end
end
The purpose of the two lines in the quit option (6) is to be able to retrieve the array which contains each of the apartment objects. The global apartment array, $array, is created at the beginning of the program using:
$array = Marshal.load(File.binread('listing.txt'))
Originally, I was using a built in array to test, and everything was working the way I expected it to. Now, however, every function works with the exception of the List method:
def list
puts "You have the following apartments:\n"
$array.count.times do |i|
if $array[i].renters.empty?
print "#{$array[i].address}: is #{$array[i].sqft} square feet, has #{$array[i].num_beds} bedrooms and #{$array[i].num_baths} bathrooms. The monthly rent is $#{$array[i].monthly_rent}. \n\n"
else
print "#{($array[i].address)}: "
print "#{view_tenants(i)}"
end
end
main_menu
end
When I call the list method by selecting 1 at the main menu, it will only list the 1st apartment. Weirdly enough, when I attempt to exit, using "6", the program instead prints out info about the apartments, one by one. The output of the terminal is displayed here:
Please choose an option from the following menu:
1. List All Apartments
2. View Apartment Details
3. Add an Apartment
4. Add a Tenant
5. Evict a Tenant
6. Quit
6
500Jane Street: Nicole is the sole tenant at this apartment.
Please choose an option from the following menu:
1. List All Apartments
2. View Apartment Details
3. Add an Apartment
4. Add a Tenant
5. Evict a Tenant
6. Quit
6
5001776, Floor 8: The renters living at the apartment are Jessica, and Jamie.
Please choose an option from the following menu:
1. List All Apartments
2. View Apartment Details
3. Add an Apartment
4. Add a Tenant
5. Evict a Tenant
6. Quit
6
500Please choose an option from the following menu:
1. List All Apartments
2. View Apartment Details
3. Add an Apartment
4. Add a Tenant
5. Evict a Tenant
6. Quit
6
Brandons-MacBook: Brandon $
Everything worked how I expected it to when I manually set an array equal to what I wanted it to be. Also, if I select the "View Apartment Details" option, it correctly lists out all of my current apartments(so that I can select one). It is only the "List All Apartments" option that gives me trouble. It is my first time using Marshall, so please let me know if I am doing something incorrectly. If necessary, I can provide more code.
Thanks in advance!
I don't know exactly whats causing the problem.
However, don't use globals in ruby, unless the variable represents the state of the program itself.
Read more here http://ruby.about.com/od/variables/a/Global-Variables.htm.
Try changing your global variable to an instance variable #array.
This variable is now seen throughout your class. Or even better a Constant.
If you have more than one instance of your class you could use a class variable ##array.
Now this variable can be seen by many instances of this class.
Note, ruby programmers try to avoid using class variables.
Also don't do array.count.times. Do each or each_with_index.
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.
I typed up a simple Ruby code for a tutorial question, as shown below.
#Grandma is deaf!
puts "Hey Sonny! It's your lovely Grandmother! How are you?"
response = gets.chomp
while response != "BYE"
if response != response.upcase
puts "Huh?! I CAN'T HEAR YOU!"
else
puts "NO! NOT SINCE " + (1930 + rand(21)).to_s + "!"
end
response = gets.chomp
end
puts "GOOD BYE, SONNY!"
However, when I run this, the window displays:
Hey Sonny! It's your lovely Grandmother! How are you?
NoMethodError: private method ‘chomp’ called for nil:NilClass
at top level in deafGrandma.rb at line 3
I don't understand why chomp is not recognized. I'm using textMate on a Mac I have Ruby version 1.8.7, which should be fine. Any solutions?
Thank you so much :)
Adrian is right about interactive input being disabled in TextMate 1.5.9 (r1510). See this post from TextMate's developer.
However, you can upgrade to a "cutting-edge" TextMate release that restores interactive input, and will allow you to run the above code just fine. Go to TextMate's Preferences -> Software Updates and make sure Automatically check for updates is checked.
Select Cutting-Edge in the Watch For: dropdown menu. Finally, click Check Now. The latest release (r1589) should automatically download. Interactive input is re-enabled in this release.
If you are using the Cmd-R shortcut in TextMate to run your code, you will not be able to supply it input because textmate only supports output. You will have to run it in a terminal instead. The reason you are getting that error is because $stdin is closed, so gets returns nil.