How to write a simple Cucumber script - ruby

I'm following the tutorial to run my first Cucumber script:
Feature: guru99 Demopage Login
In order to Login in Demopage we have to enter login details
Scenario:
Register On Guru99 Demopage without email
Given I am on the Guru99 homepage
When enter blank details for register
Then error email shown
I have the project in Idea but when I run it I get errors.
When using chrome:
Failed to open TCP connection to 127.0.0.1:9515 (No connection could be made because the target machine actively refused it.
I have no idea how to resolve it.
When using Firefox, the script successfully opens the browser but fails after that:
require 'watir'
require 'colorize'
Selenium::WebDriver::Firefox::Binary.path='C:\soft\Mozilla Firefox\firefox.exe'
case ENV['BROWSER']
when 'chrome'
browser = Watir::Browser.new :chrome
when 'firefox'
browser = Watir::Browser.new :firefox
end
Given(/^I am on the Guru99 homepage$/)do
#browser = Watir::Browser.new :firefox
#browser.goto "http://demo.guru99.com"
end
When(/^enter blank details for register$/) do
browser.text_filed(:name,"emaiid").set("")
browser.button(:name,"btnLogin").click
end
Then(/^error email shown$/) do
puts "Email is Required!".red
browser.close
end
And returns:
NoMethodError: undefined method `text_filed' for nil:NilClass
on this line:
browser.text_filed(:name,"emaiid").set("")
I found some references that I need to write a class to call a method. I tried it but didn't succeed.

Connection refused, I'm unsure but "Watir+Cucumber Connection refused" looks a fix.
Copy pasta:
AfterConfiguration do |config|
yourCodeStartUp() # Put your SETUP code here including the launch of webdriver
at_exit
yourCodeTearDown() # Put your CLOSING routine here
puts 'stopped'
end
end
The code error is a typo, it should be browser.text_field(...

Regarding the issue you are observing on chrome, it sounds like you need to update chromedriver (and make sure the exe is in PATH). If you are running chrome v56-58, you need ChromeDriver 2.29.
Regarding the NoMethodError: undefined method error, you have a typo when you call the text_field method (i.e. browser.text_filed).

Nope, I was mistaken, I had an older version of chromedriver. Now it's running in chrome as well. Thank you very much. Appreciate your time!
So, the answers are:
1. Update chromedriver.
2. Check your code for typos one more time.
Was really easy but took me a lot of time%

Related

Unable to upload a file with Selenium - Ruby

I am trying to upload a file using a bot written in Ruby. It failed but I think my synthax to be good. I suppose that the recent upgrade of Firefox unabled me to do the job. Have you got an idea please ?
I used these resources to do my code :
https://saucelabs.com/resources/articles/best-practices-tips-selenium-file-upload
http://elementalselenium.com/tips/1-upload-a-file
I got the last version of selenium here :
https://rubygems.org/gems/selenium-webdriver/versions/2.53.0?locale=fr
I got the last version of geckodriver here :
https://rubygems.org/gems/geckodriver-helper/versions/0.0.3
But it didn't work : WebDriverError#chrome://marionnette/content/error.js:235:5 File not found
I am using wordpress in local mode with local by flywheel.
Firefox Quantum 57.0 64bits
Here is my little code :
require 'selenium-webdriver'
require 'rspec/expectations'
include RSpec::Matchers
def setup
#driver = Selenium::WebDriver.for :firefox
end
def teardown
#driver.quit
end
def run
setup
yield
teardown
end
run do
#driver.get "http://mywebsite.dev/wp-admin/profile.php"
inputlogin = #driver.find_element :id => "user_login"
inputpwd = #driver.find_element :id => "user_pass"
inputlogin.send_keys "mylogin"
inputpwd.send_keys "mypwd"
#driver.find_element(:id, "wp-submit").click
#driver.get "http://mywebsite.dev"
element = #driver.find_element(:id, 'uploadInput1')
element.send_keys "C:\\path\\image.jpg"
# #driver.find_element(id: 'btn-submit').click
puts "Successful on #{#driver.title}"
end
As it didn't work, I was looking for an other way to upload my file so I tried using AutoIT but I failed... Here is an example.
http://www.maisasolutions.com/blog/How-to-upload-images-using-selenium-webdriver-with-ruby
Moreover, this solution is very heavy (downloads, installations, non reproductability etc.) and don't seems to be proper code.
Thank you for your help.
Find a file field on the page and attach a file given its path.
The file field can be found via its name, id or label text.
page.attach_file(locator, '/path/to/file.png')
Here, locator will beCSS id or class of file upload path.

PhantomJS - Watir Ruby - Watir::Exception::ObjectDisabledException: element present and enabled, but timed out after 30 seconds

I have downloaded PhantomJS and added the code below to my script:
Selenium::WebDriver::PhantomJS.path = 'C:\phantomjs-2.1.1-windows\bin\phantomjs.exe'
#browser = Watir::Browser.new :phantomjs
#browser.goto "www.samplewebsite.com"
Then I getting below error message:
Watir::Exception::ObjectDisabledException: element present and
enabled, but timed out after 30 seconds, waiting for
"ctl00$ContentPlaceHolder1$Login1$UserName",
:tag_name=>"input"}> to not be disabled
Here is the code in the login.rb file:
def browser()
Watir::Wait.until{#browser.text.include? 'Login'}
end
def credentials()
#browser.wait
username = #browser.text_field(name:"ctl00$ContentPlaceHolder1$Login1$UserName").send_keys 'abcd123' #Is this line of code showing the error
password = #browser.text_field(name:"ctl00$ContentPlaceHolder1$Login1$Password").send_keys'Password'
end
I'm pretty sure it is This Bug. It's been fixed in Watir 6.8.2, which was released yesterday.
Read some post from other sources and I tried maximize the window: #browser.window.maximize then it worked! And yes this issue is not related to Page Object. Regarding the error message I didn't write them, I copied and pasted it from my IDE log.

Why won't my Rails function abort?

I'm trying to debug an application that someone else wrote. In my production.log, I see:
Processing by Friendster::AppsController#home as HTML
Parameters: {SOMESTUFF}
Completed 500 Internal Server Error in 3ms
So I go to the app/controller/friendster/apps_controllers and look at the home function and it is:
def home
show_app_container
end
So I changed it to:
def home
puts "container"
abort "SHAMOON"
show_app_container
end
Just so I can see some sort of error or log. But nothing shows up anywhere. Nothing renders differently. I don't know if there's caching going on or if I'm in the right function. Any help debugging this would be greatly appreciated.
I also ran a bundle exec rake routes and got:
friendster_app_home POST /publishers/:publisher_id/apps/:app_id/home(.:format) {:action=>"home", :controller=>"friendster/apps"}
GET /publishers/:publisher_id/apps/:app_id/home(.:format) {:action=>"home", :controller=>"friendster/apps"}
Although there are quite a few routes with GET /publishers/:publisher_id/apps/:app_id/home(.:format), so I'm not sure what that means. This is the only friendster one.
EDIT: Adding Base controller parent
class Friendster::BaseController < AppsController
protected
end
Any help debugging this would be greatly appreciated.
Include the pry gem, call binding.pry inside of #home and Pry will spawn an interactive debugger in the console.
def home
binding.pry
show_app_container
end

Watir webdriver Errno::ECONNREFUSED: No connection could be made because the target machine actively refused it. - connect(2)

Updated:
I'm running the code below to execute my sample test cases: (Windows 7, Watir 3.0.0, Watir-webdriver-0.6.1)
require "watir-webdriver"
require 'test/unit'
class Teste1
$var = Watir::Browser.new :chrome
def met1
$var.goto 'google.com'
$var.text_field(:name, "q").set 'sample'
$var.button(:name =>'btnG').click
end
end
class Teste2 < Test::Unit::TestCase
$test = Teste1.new
def test_gomet1
$test.met1()
end
end
The browser opens but the script throws the following error:
test_gomet1(Teste2):
Errno::ECONNREFUSED: No connection could be made because the target machine actively refused it. - connect(2)
C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.25.0/lib/selenium/webdriver/remote/http/default.rb:82:in `response_for'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.25.0/lib/selenium/webdriver/remote/http/default.rb:38:in `request'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.25.0/lib/selenium/webdriver/remote/http/common.rb:40:in `call'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.25.0/lib/selenium/webdriver/remote/bridge.rb:598:in `raw_execute'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.25.0/lib/selenium/webdriver/remote/bridge.rb:576:in `execute'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.25.0/lib/selenium/webdriver/remote/bridge.rb:99:in `get'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.25.0/lib/selenium/webdriver/common/navigation.rb:14:in `to'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.6.1/lib/watir-webdriver/browser.rb:63:in `goto'
maisum.rb:11:in `met1'
maisum.rb:21:in `test_gomet1'
Can anyone help me on that?
I cannot get that exception for the test_closeVar test, however I can reproduce it for your other two tests. You get that exception when you try to interact with the browser after it has been closed.
When using Test::Unit, keep in mind that the tests run in alphabetically order. Therefore your test_closeVar test will close the browser before test_gomet1 and test_gomet2 even run. test_gomet1 and test_gomet2 will throw that exception because the browser is closed.
As a quick fix, you could add numbers to the test names to get them to run in a specific order.
The long term fix though is really to make your tests independent so that order does not matter.
Update
A couple of observations:
The problem occurs using Selenium-Webdriver, so not a Watir-Webdriver specific issue.
The script runs fine in Firefox, so it might be a chromedriver specific issue.
Workaround: It seems like the chrome browser does not like being declared outside the test case. I do not understand why, but the quick fix is to declare the browser in the setup of the test case. If you want to use the same browser for each test, you can just declare it if it does not already exist.
The following will run (though I would suggest cleaning it up to reduce the usage of global variables):
class Teste1
def met1
$var.goto 'google.com'
$var.text_field(:name, "q").set 'sample'
$var.button(:name =>'btnG').click
end
end
class Teste2 < Test::Unit::TestCase
def setup()
unless defined?($var)
$var = Watir::Browser.new :chrome
end
$test = Teste1.new()
end
def test_gomet1
$test.met1()
end
end

Automating authentication with Watir

I've searched and searched for this, but I can't seem to get this automation to work. Having used all of the basic authentication code on the OpenQA site, I still cannot get the authentication box to work.
I'm using IE8, with a website that has HTTPS enabled.
By using Watir I'm able to open IE to the correct page, but nothing I try allows me to enter any content into the login form.
Here is the code I've whittled it down to:
require 'watir'
url = 'https://thewebsite.com'
#username = 'myusername'
#password = 'mypassword'
browser = Watir::Browser.new
browser.goto url
sleep 5
Watir.autoit.WinWait('Blank Page')
Watir.autoit.Send(#username)
Watir.autoit.Send('{TAB}')
Watir.autoit.Send(#password)
Watir.autoit.Send('{ENTER}')
Does anyone have any suggestions, or links? A lot of the information I've found on the OpenQA site seems quite out of date.
Thanks
Did you try RAutomation instead of autoit?
Have you tried using the url with user and pass in it? like url = 'https://username:password#thewebsite.com', you can try visiting the url manually in a browser, if it works manually it should work in your script too.
I came here having the same problem, although it looks like the answer is different due to the latest version of Watir and watir-webdriver. I'll show what worked for me using:
watir (4.0.2 x86-mingw32)
watir-classic (3.6.0)
watir-webdriver (0.6.2)
Watir doesn't have autoit built in any more and it looks like the other suggestion I found (require 'watir/ie') doesn't work any more either. In the spirit of solving this with the original tech requested:
Make sure after installation AutoIT has been registered with windows.
Go to the AutotIT dll (installed with the rautomation gem mentioned above, think Watir installed this)
cd C:\Ruby193\lib\ruby\gems\1.9.1\gems\rautomation-0.8.0\ext\AutoItX
regsvr32 AutoItX3.dll
Then the code below should work
require 'watir'
require 'win32ole'
$b = Watir::Browser.new :ie
begin
$b.goto( 'http://10.254.157.34:8383/mywebsite/stuff.html');
rescue Exception => e
puts "Trapped Error, expecting modal dialog exception"
puts e.backtrace
puts "Continuing"
end
login_title = "Windows Security" #For Windows 7, dialog title for anything else
username = "myuser"
password = "mypassword"
sleep 1 #Just in case
au3 = WIN32OLE.new("AutoItX3.Control")
win_exists = au3.WinWait(login_title, "", 5)
if (win_exists > 0)
au3.WinActivate(login_title)
au3.Send('!u')
au3.Send(username)
au3.Send('{TAB}')
au3.Send(password)
au3.Send('{ENTER}')
end

Resources