Dropdown Menu and its action - ruby

I have a dropdown menu with around 5-6 items in it.
require 'Qt'
class Auth < Qt::Widget
entryIndex = $entryIndex
slots 'slotFunctionChanged(int)'
def initialize(parent=nil)
super(parent)
setWindowTitle("Action");
setFixedSize 750,530
entry_ui
show
end
def entry_ui
#entryLabel = Qt::Label.new "Entry: ", self
#entryLabel.setFont Qt::Font.new("Times New Roman", 14)
combo = Qt::ComboBox.new self
combo.setFont Qt::Font.new("Times New Roman", 12 )
combo.addItem "1- Standard"
combo.addItem "2- Custom"
combo.addItem "3- Non-custom"
combo.addItem "4- Non-Standard"
connect combo, SIGNAL('activated(int)'), self, SLOT('slotEntryChanged(int)')
combo.resize 170,20
combo.move 170,100
#funLabel.move 95,100
end
def slotEntryChanged(entryIndex)
case entryIndex
when 0
#acc.show
when 1
#acc.hide
when 2
#acc.show
end
end
Now there is a button called Submit , which is connected to slot 'on_clicked_submit()'.
I want a dialog box to pop up only when entryIndex item to be either custom or non- custom. I tried the following code:
def on_clicked_submit
if $entryIndex == 1 || $entryIndex == 8
text = Qt::InputDialog.getText self, "Swipe", "Thank you"
end
end
But the if statement to check entryIndex was not working fine. Suggestions are welcome

Related

Music and image failed to initialize

I was trying to make a multiple-choice music player using Gosu but the picture and music Iwanted would not initialize despite the program running, it showed a black screen. The single block of codes works:
require 'gosu'
require './input_functions'
class MW < Gosu::Window
def initialize
super 200, 135
#beth = Gosu::Image.new("media/beth.jpg")
#song = Gosu::Song.new("media/3rdmovement.mp3")
#song.play
end
def draw
#beth.draw(0, 0)
end
end
window = MW.new
window.show
But adding the multiple choice elements would not work(note: read_integer_in_range is defined in input function, the name itself is self explanatory). Full code:
require 'gosu'
require './input_functions'
class MW < Gosu::Window
def initialize
super 200, 135
#beth = Gosu::Image.new("media/beth.jpg")
#dimitri = Gosu::Image.new("media/dimitri.png")
#vil = Gosu::Image.new("media/vilva.png")
#song = Gosu::Song.new("media/3rdmovement.mp3")
#song2=Gosu::Song.new("media/2ndwaltz.mp3")
#song3=Gosu::Song.new("media/1stseason.mp3")
read_integer_in_range( "What song you want play
1st Spring
2nd Waltz
3rd movement", 1, 3)
choice = gets.chomp.to_i()
case choice
when 1
#song3.play
#vil.draw(0, 0)
when 2
#song2.play
#dimitri.draw(0, 0)
when 3
#song.play
draw_beth()
end
end
end
def draw_beth
#beth.draw(0, 0)
end
window = MW.new
window.show
All of the Png/Jpg and mp3 file works just fine..
I tried separating the draw_beth to call it in case but it did not work. I hope some passing by could help me with this one
As I can see, you are creating a music player with GUI, and if you are doing so, you shouldn't use gets function, instead you should track for the cursor's position and return a test value; for example:
def update
#locs = [mouse_x, mouse_y]
#cursor_choice_album = mouse_over_button(mouse_x, mouse_y)
end
def needs_cursor?; true; end
def mouse_over_button(mouse_x, mouse_y)
if ((mouse_x > 100 and mouse_x < 500) and (mouse_y < 500 and mouse_y > 100))
return 1
end
then you can use the case condition in the "button down ID" function

Is there any validation check for already registered email id in ruby programming

# load in the webdriver gem to interect with selenium
require 'selenium-webdriver'
#setup chrome plugin
driver = Selenium::WebDriver::Chrome.driver_path='C:\Users\vidhi\Desktop\Ruby Folder\chromedriver.exe'
#this line will start the browser
driver = Selenium::WebDriver.for :chrome
wait = Selenium::WebDriver::Wait.new(:timeout => 20)
def document_initialised(driver)
driver.execute_script('return initialised')
end
#Navigate to URl
driver.get "http://automationpractice.com/index.php?controller=authentication&back=my-account#account-creation"
#Maximize the window
driver.manage.window.maximize
sleep 6
driver.action.key_down(:enter).perform
sleep 5
driver.find_element(:id,"email_create").send_keys "demouser099#gmail.com"
sleep 5
driver.action.key_down(:enter).perform
driver.find_element(:id,"SubmitCreate").click
sleep 2
driver.action.key_down(:enter).perform
#### Check that the radio button exists
puts "Test Passed: Radio button found" if wait.until {
driver.find_element(:id,"uniform-id_gender2").displayed?
}
#Change the state of the Radio Buttons
cb1 = wait.until {
element1 = driver.find_element(:id,"uniform-id_gender1")
element1 if element1.displayed?
}
cb1.click if cb1.selected? == false
cb3 = wait.until {
element2 = driver.find_element(:id,"uniform-id_gender2")
element2 if element2.displayed?
}
cb3.click if cb3.selected? == false
sleep 4
driver.find_element(:id,"customer_firstname").send_keys "demo"
sleep 3
driver.find_element(:id,"customer_lastname").send_keys "user"
sleep 5
password=driver.find_element(:id,"passwd").send_keys "demo#123"
sleep 4
if driver.find_element(:id,"uniform-days").displayed?
puts "Days dropdown is displayed.."
select = driver.find_element(:id,"uniform-days")
alloptions = select.find_elements(:tag_name,"option")
puts alloptions.size
alloptions.each do |option|
puts "Value is.."+option.attribute("value")
if option.attribute("value")=="20"
option.click
puts "Value has been selected.."
sleep 5
break
end
end
end
month_dropdown = driver.find_element(:id,"months")
months=Selenium::WebDriver::Support::Select.new(month_dropdown)
months.select_by(:text,"April ")
puts driver.find_element(:id,"months").text.include?("April")
years_dropdown = driver.find_element(:id,"years")
years=Selenium::WebDriver::Support::Select.new(years_dropdown)
years.select_by(:index,28)
sleep 3
driver.find_element(:id,"address1").send_keys "45 calony A"
sleep 4
if driver.find_element(:id,"uniform-id_state").displayed?
puts "State dropdown is displayed.."
select = driver.find_element(:id,"uniform-id_state")
alloptions = select.find_elements(:tag_name,"option")
puts alloptions.size
alloptions.each do |option|
puts "Text is.."+option.attribute("text")
if option.attribute("text")=="California"
option.click
puts "text has been selected.."
sleep 5
break
end
end
end
#Enter city
driver.find_element(:id,"city").send_keys "Los Angeles"
sleep 4
driver.find_element(:id,"postcode").send_keys "23654"
Country_dropdown =driver.find_element(:id,"id_country")
country=Selenium::WebDriver::Support::Select.new(Country_dropdown)
country.select_by(:index,1)
sleep 5
#Input Mobile Number
driver.find_element(:id,"phone_mobile").send_keys "985256238"
sleep 5
#Click on Submit button
driver.find_element(:id,"submitAccount").click
sleep 5
I have write script for registration form I have run my automation script then on first time it is successfully pass but when run twice with same email id then it is showing validation message as email already exists try with new one..so for this validation what is the assertion in ruby?
Here is the link for registration form-http://automationpractice.com/index.php?controller=authentication&back=my-account#account-creation
General side notes:
I see that you have a method defined that you don't use.
def document_initialised(driver)
driver.execute_script('return initialised')
end
Also there seems to be some un-nneeded variable assignment and not-needed driver.action.key_down(:enter).perform
It is possible to create a class to keep your code more structured ;)
To your question:
I would create an if to check if the error is displayed on the page or not after you did you submit with driver.find_element(:id,"SubmitCreate").click. You can see an example in about it in #register_email in my example.
If I create some class from it and do some other removal of code I think is obsolete, it looks like this (but it has not been tested by me).
# load in the webdriver gem to interect with selenium
require 'selenium-webdriver'
class RegisterUser
def run
return unless register_email
do_the_rest
end
private
def driver
return #driver if #driver.present?
Selenium::WebDriver::Chrome.driver_path='C:\Users\vidhi\Desktop\Ruby Folder\chromedriver.exe'
#driver = Selenium::WebDriver.for(:chrome)
#driver.get "http://automationpractice.com/index.php?controller=authentication&back=my-account#account-creation"
sleep 6
#driver.manage.window.maximize
#driver
end
def wait
Selenium::WebDriver::Wait.new(:timeout => 20)
end
def register_email
driver.find_element(:id,"email_create").send_keys "demouser099#gmail.com"
driver.find_element(:id,"SubmitCreate").click
sleep 5
return true unless driver.find_element(:id,"create_account_error").displayed?
# An account using this email address has already been registered.
# Please enter a valid password or request a new one.
puts driver.find_element(:id,"create_account_error").text
false
end
def do_the_rest
# Here you can put the rest of the code, or clean up even more and split into multiple methods
end
end
RegisterUser.new.run

QtRuby: Check Box not toggling

I have created a Checkbox with label AVS . When I click the checkbox, I need the below block of code to be executed. I am neither getting any error, nor is the toggle working.
require 'Qt'
class Auth < Qt::Widget
slots 'on_toggled(bool)'
def initialize(parent=nil)
super(parent)
setWindowTitle("Org");
setFixedSize 750,530
avs_ui
show
end
def avs_ui
#label = Qt::Label.new "AVS?: ", self
#label.setFont Qt::Font.new("Times New Roman", 14)
cb = Qt::CheckBox.new " ",self
cb.setChecked false
connect cb, SIGNAL("toggled(bool)"), self, SLOT("on_toggled(bool)")
#label.move 65,130
cb.move 170,130
end
def on_toggled state
if state
#lineedit = Qt::LineEdit.new(self)
#lineedit.setFont Qt::Font.new("Times New Roman", 12)
#label = Qt::Label.new "Address: ", self
#label.setFont Qt::Font.new("Times New Roman", 14)
#label.move 380,130
#label.resize 170,20
#lineedit.move 500,130
#lineedit1 = Qt::LineEdit.new(self)
#lineedit1.setFont Qt::Font.new("Times New Roman", 12)
#label1 = Qt::Label.new "Zip Code: ", self
#label.setFont Qt::Font.new("Times New Roman", 14)
#label.move 420,160
#label.resize 70,20
#lineedit.move 500,160
end
end
end
app = Qt::Application.new(ARGV)
widget = Auth.new
widget.show
app.exec
Can someone let me know what changes I should make so that, when I click the checkbox, I get the other required widgets?

Keyboard Shortcut doesn't work in ruby-gtk2 TextView

I'm practicing on a GUI based on Gtk and Ruby. I planned to make a little window that has a TextView and a button. When you input text in the TextView and press the button, it calls the Twitter gem's method and tweets to Twitter.
After that succeeded, I wanted to make a keyboard short-cut to do this: like, when you hit Ctrl+Enter it Tweets.
Here's part of the source code I have written. (Only around the GUI)
# require "gtk2"
# require "twitter"
## error classes and methods first
class Error_pop
def initialize(text)
#pop = Gtk::Dialog.new(
"Error",
nil,
Gtk::Dialog::MODAL,
[Gtk::Stock::OK, Gtk::Dialog::RESPONSE_OK]
)
#pop.default_response = Gtk::Dialog::RESPONSE_OK
#pop.set_size_request(400, 100)
#label = Gtk::Label.new("#{ text }")
#table = Gtk::Table.new(3, 1, false)
#table.attach_defaults(#label, 0, 1, 0, 2)
#pop.vbox.add(#table)
end
attr_accessor :pop, :table, :label
end
module Tueet
def tueet(text, account)
begin
account.rest_api.update(text) # calls gem: twitter
rescue => reason
error_pop = Error_pop.new(reason)
error_pop.pop.show_all
error_pop.pop.run do |response|
if response == Gtk::Dialog::RESPONSE_OK
end
end
error_pop.pop.destroy
return text
else
return nil
end
end
module_function :tueet
end
## making gui
window = Gtk::Window.new(Gtk::Window::TOPLEVEL)
window.title = "Tweet Box"
window.set_size_request(300, 150)
window.resizable = false
window.signal_connect('delete_event') do
Gtk.main_quit
false
end
inputter = Gtk::TextView.new
inputter.editable = true
inputter.wrap_mode = Gtk::TextTag::WRAP_WORD_CHAR
inputter_event = Gtk::EventBox.new # for key binding
inputter_event.events = Gdk::Event::KEY_PRESS_MASK
tweetbutton = Gtk::Button.new("tweet")
table = Gtk::Table.new(4, 9, true)
table.attach(inputter, 0, 9, 0, 3, Gtk::FILL||Gtk::EXPAND, Gtk::FILL||Gtk::EXPAND, 5, 5)
table.attach(tweetbutton, 3, 6, 3, 4)
inputter_event.add(table)
window.add(inputter_event)
inputter_event.realize
## signals and stuff
text = nil
tweetbutton.signal_connect('clicked') do
text = inputter.buffer.text
returned_text = Tueet.tueet(text, client[0]) # client[n] holds the authorized account information
inputter.buffer.text = String(returned_text)
end
inputter_event.signal_connect('key-press-event') do |wdt, evt|
key = Gdk::Keyval.to_name(evt.keyval)
p ("KeyPress: #{ key }was pressed") # these lines
p evt.state # are for
p evt.keyval # debugging
if key == 'Return' && # when "Ctrl + Return", do Tueet.tueet
( evt.state & Gdk::Window::CONTROL_MASK ==
Gdk::Window::ModifierType::CONTROL_MASK )
Tueet.tueet(text, client[0])
end
end
window.show_all
Gtk.main
And, this doesn't work. When I press Ctrl + Enter, it simply linebreaks inside the TextView.
Only, when doing this:
inputter = Gtk::TextView.new
inputter.editable = false # <- making this false
inputter.wrap_mode = Gtk::TextTag::WRAP_WORD_CHAR
makes the keybinding work, but disables me to input what I want to tweet to the TextView.
How can I work this out? I wonder how many GUI applications do this.

Storing Information in Stack/Flow Objects

Say I have the following sort of app:
Shoes.app do
#i = 0
def add_button
button ("#{#i += 1}")
end
button("0") {add_button}
end
So that each time you click the button it adds a new button with a higher number. Is there any way to code it so that clicking one of the new buttons displays its number? Since self always points to the app, the obvious approach
button ("#{#i += 1}") {alert #i}
Doesn't work, since then clicking any button just displays the current value of #i.
Shoes.app do
#i = 0
def add_button
n = #i+1
button ("#{#i += 1}") {alert n}
end
button("0") {add_button}
end

Resources