Given the simple GUI posted down below, how can I create and save a screenshot of what the results of the code is? I don't care if I need to print it, export it to pdf, or save as a picture but I don't want to ask the user to screenshot or snip the page.
Shoes.app do
# Post and location of logo
#moveme = image 'C:\Users\warnerl\Documents\RubyFiles\TK_Dashboard_Logo.jpg', :width => 200
#moveme.move(400,0)
# Gets users first and last name and saves them as variables
#UserFirstName = ask("Please input your first name.")
#UserLastName = ask("Please input your last name.")
# Create Certificate
stack{
para 'You got 100%'
para "#{#UserFirstName} #{#UserLastName}"
para 'Congratulations you passed!'
}
end
Related
I am dealing with Capybara + Ruby script and have an issue with it. When I run the script, it clicks buttons, fulfills the fields, and saves results. After execution, it starts to run again.
The script starts by reaching the specific page. On this page, there is the Start new questionnaire button which has to be clicked, and then all the main processes happen. After clicking the Apply rating button, the browser automatically redirects to the page where Start new questionnaire appears again (that's OK). But I don't need it to be clicked the second time. I need my script to continue the execution.
What is the reason my script is trying to run 2 times one part of the code?
content_rating = browser.find_link(title: 'Content rating')
content_rating.click
puts 'Content Rating'
begin
start_new_questionary_btn = browser.all(:button, text: 'Start new questionnaire')
start_new_questionary_btn[0].click
sleep 2
rescue
continue_btn = browser.all(:button, text: 'Continue')
continue_btn[0].click
end
email_address = browser.all(:label, "Email address")
email_address[0].fill_in(type: 'email', with: 'e-mail', wait: 100)
email_address[1].fill_in(type: 'email', with: 'e-mail', wait: 100)
content_questions = browser.all(:xpath, './/div[#class="iarcPageOne"]')
content_questions[2].click
##All the buttons have costant names
btn_names = ['775', '789', '817', '805', '1036', '1017', '1018', '1019']
for btn_name in btn_names
current_btn = browser.all(:radio_button, name: btn_name, visible: :all)
if btn_name == '1036'
current_btn[0].click
else
current_btn[1].click
end
end
browser.click_on('Save questionnaire', wait: 100)
browser.click_on('Calculate rating', wait: 100)
browser.click_on('Apply rating', wait: 100)
sleep 3
### Now the code should continue to execute and click ```App content``` (below) but it clicks ```Start new questionnaire``` again ###
### App content
app_content = browser.find_link(title: 'App content')
app_content.click
# It skips next two lines and continues with Privacy policy URL
# If I run the code below separately - everything works perfect
start_buttons = browser.all(:button, text: 'Start')
start_buttons[0].click
privacy_button = browser.find_field('Privacy policy URL', type: 'text')
privacy_button.fill_in(with: 'privacy')
sleep 1
browser.click_on('Save')
####### Continuation of the script #######
It isn't clicking it again - my guess is there a validation error on the page somewhere and it's opening the same questionnaire again for you to fix the error. Capybara only does what you tell it to, it's not going to start randomly clicking links.
Also when writing scripts like this there is no need to separate the finds and clicks, prefer something like below
browser.click_link(title: 'Content rating')
puts 'Content Rating'
begin
start_new_questionary_btn = browser.first(:button, text: 'Start new questionnaire').click
sleep 2
rescue
browser.first(:button, text: 'Continue').click
end
email_address = browser.all(:label, "Email address")
email_address[0].fill_in(type: 'email', with: 'e-mail', wait: 100)
email_address[1].fill_in(type: 'email', with: 'e-mail', wait: 100)
browser.all(:xpath, 'div.iarcPageOne', minimum: 3]')[2].click
##All the buttons have costant names
['775', '789', '817', '805', '1036', '1017', '1018', '1019'].each do |btn_name|
current_btn = browser.all(:radio_button, name: btn_name, visible: :all)
current_btn(btn_name == '1036' ? 0 : 1).click
end
browser.using_wait_time(100) do
browser.click_on('Save questionnaire')
browser.click_on('Calculate rating')
browser.click_on('Apply rating')
end
sleep 3
### Now the code should continue to execute and click ```App content``` (below) but it clicks ```Start new questionnaire``` again ###
### App content
browser.click_link(title: 'App content')
####### Continuation of the script #######
I have a code which asks user to upload a file. The file may be audio or image or anything. I asks user to enter file name. If he Enter file name my code adds extension to it. It is working fine. But if user enters extension say audio.mp3 then it saves as audio.mp3.mp3. So I have to check if user entered name contains dot then it should not take extension.
I used pregmatch but it is not working.
My code
$splitOptions = explode(',',$request->input('mediaName'));
$fileExtension = pathinfo($file[$i]->getClientOriginalName(),PATHINFO_EXTENSION);
$checkExtension = explode('.',$request->input('mediaName'));
if(preg_match("/[.]/", $checkExtension)){
$mediaName = $splitOptions[$i];
}
else
{
$mediaName = $splitOptions[$i]."_$fileExtension";
}
Please use laravel helper
$value = str_contains('This is my name', 'my');
// true
this is my first time trying to do some tasks in ruby. my cats name is ruby so i decided to use ruby language on my linux box to accomplish my needs.
the first thing i want to do is sending emails with ruby.
It's not so complicated and until now i have this code
require 'net/smtp'
message = <<MESSAGE_END
From: wakeupruby <wakeupruby#fromdomain.com>
To: Wakeup-Email<wakeuprasperry#todomain.com>
Subject: Wakeup-Email
It's 8 in the morning, get up, please.
MESSAGE_END
Net::SMTP.start('mail.mydomain.com', 25) do |smtp|
smtp.send_message message,
'test#mydomain.com',
'test#mydomain.com'
end
It works like expected.
After that, i created a textfile which looks like this:
emaildata.txt
MAILSERVER,DOMAIN,SENDER,RECIPIENT,SUBJECT,BODY
mailserver1.lab,lab,user1.lab,user2.lab,testemail,dont read this!.
so, here is whats failing
i would like to read the textfile named emaildata.txt and use this data (except the first line which describes the values) row by row and uses this entrys to fill up the variables. one row=one email.
i'm completly stucked how to do that.
any help getting it running would be fine.
the first thing should be naming all variables like the naming in the csv-file.
MAILSERVER,DOMAIN,SENDER,RECIPIENT,SUBJECT,BODY
to read from the csv file i used this:
require 'csv'
CSV.foreach('emaildata.txt',col_sep:',', row_sep: :auto, headers: true) do |row|
puts row.inspect
end
this gives me this list from my emaildata.txt file
<CSV::Row "MAILSERVER":"mailserver1.lab" "DOMAIN":"lab" "SENDER":"user1.lab" "RECIPIENT":"user2.lab" "SUBJECT":"testemail" "BODY":"dont read this!. ">
<CSV::Row>
there can be more columns, so doing it static would not help when we have more columns later.
how can i use the Head colmn values as variables? For example MAILSERVER and print them to the screen?
i tried
put $MAILSERVER
put #MAILSERVER
no success at all.
then, filling the variables for the first time with the values from the second row
sending the email
clearing the variables
checking, if this is the last entry , if no
filling the variables from the third row
sending the email
clearing the variables
checking if this is the last entry, if no, again, if no quit.
i did that manually with telnet like this:
telnet $mailserver 25
helo $domain
mail from:<$SENDER>
rcpt to:<$RECIPIENT>
data
Subject: $SUBJECT
<emptyline>
$BODY
.
quit
which works. but instead of doing it manually i would like to get it running with... ruby.
thank you for reading and helping
Joe
If I understand the question correctly you are trying to use a CSV file to form an email? I would avoid using the csv module for this. You can do every thing with basic ruby functionality.
first you open the file and store each line into an array
csv_array = []
csv_file = File.open("/path/to/file.csv", "r")
csv_file.each_line {|row| csv_array.push(row)}
Each row will contain a newline caracter so we will strip that.
csv_array.each {|row| row.strip!}
One final step to format everything correctly. We need to remove the first row that contains our column headers.
csv_array.shift
Now each row of our csv file is an element in our array. You can loop through the elements splitting each line at the comma and use the new array's elements as our variables.
csv_array.each do |line|
row = line.split(",")
telnet row[0] 25
helo row[2]
rcpt to::row[3]
data
Subject: row[4]
<emptyline>
row[5]
end
Telnet is not the best way to send an email though. I would use mailfactory. much easier and flexible than net/smtp.
thank you all for your contributions to solve this. It finally worked with the great help of #letitworknow.
Here is the complete code, done with mailfactory:
require 'rubygems'
require 'net/smtp'
require 'mailfactory'
#read csv file
csv_array = []
csv_file = File.open("/root/mailtest/emaildata.txt", "r")
csv_file.each_line {|row| csv_array.push(row)}
csv_array.each {|row| row.strip!}
csv_array.shift
csv_array.each do |line|
row = line.split(";")
#Connect to SMTP Server
smtp = Net::SMTP.new(row[0], 25)
smtp.start(row[1])
#Construct Mail Message
mail = MailFactory.new()
mail.from = row[2]
mail.to = row[3]
mail.subject = row[4]
mail.txt = row[5]
#Construct SMTP Message
smtp.send_message mail.construct, row[2], row[3]
#Send this (and all other) message
smtp.finish()
#close the each.loop
end
the first version fails with the issue described before, so its not completly finished at this point. until now i did not manage to get it running.
Best
John
I've made a small CLI script in ruby to manage a small shop for a friend, but then he wanted me to make a GUI for him, so I looked around and found shoes4.
So, I went and download it, created a small test, and run:
./bin/shoes -p swt:jar ./path/to/app.rb
and left it to create the package, then I got a warning from system that I'm running low on disc space, so I went to check the jar file, and it was over 1.5GB and still not done packaging... and the code is very small and basic:
require 'yaml'
Shoes.app do
button "Add client" do
filename = ask_open_file
para File.read(filename)
clients = YAML.load_file(filename)
id = clients[clients.length - 1][0].to_i + 1
name = ask("Enter the client's full name: ")
items = ask("Enter list of items.")
patients[id] = ["ID = "+ id.to_s,"Name = "+ pname,"list of items:\n"+ items]
File.open(filename, 'w') { |f| YAML.dump(clients, f) }
alert ("Added new patient.")
end
button "Exit" do
exit()
end
end
any idea why this small app is more than 1.5GB?? or did I try to package it wrong way??
The packager will include everything in the directory of your shoes script and below.
Hey all,
I am building a gui in wich there is an edit box, waiting for the user to write a name.
Currently I force the user to give a legitimate name with this code :
NewPNUName = get(handles.nameOfNewPNU, 'String');
if ( isempty(NewPNUName) ||...
strcmp(NewPNUName,'Enter the name for the new PNU') )
errordlg('Please enter a name for the new PNU.');
elseif (~ischar(NewPNUName(1)))
errordlg('The PNU name should start with a letter.');
else
handles.NewPNUName = NewPNUName;
end
if (~isempty(handles.NewPNUName))
% Do all the things needed if there is a legit name
end
What it does is nothing if the user didn't write a legit name.
What I want it to do is to make a popup with an edit box, asking the user to input the wanted name again, until it is a legitimate name.
Thanks for the help!
EDIT:
following #woodchips advice I corrected my code to the foloowing:
NewPNUName = get(handles.nameOfNewPNU, 'String');
ValidName = ~isempty(NewPNUName) && isletter(NewPNUName(1)) &&...
~strcmp(NewPNUName,'Enter the name for the new PNU');
while (~ValidName)
if ( isempty(NewPNUName) ||...
strcmp(NewPNUName,'Enter the name for the new PNU') )
NewPNUName = char(inputdlg('Please enter a name for the new PNU.','No name entered'));
elseif (~isletter(NewPNUName(1)))
NewPNUName = char(inputdlg('The name of the new PNU should start with a letter. Please enter a new name',...
'Invalid name entered'));
else
allConds = 'are met'
end
ValidName = ~isempty(NewPNUName) && isletter(NewPNUName(1)) &&...
~strcmp(NewPNUName,'Enter the name for the new PNU');
end
So, put a while loop around a block of code, that generates an inputdlg box. Set the condition on the while loop to be that the result is a valid one.