Watir open multiple browser's or tab's - ruby

How can I open more than one browser using my code-watir, for example via a while loop from 0 to 10?
Here is my code:
require 'watir-webdriver'
require 'headless'
class Page
#headless = Headless.new
#headless.start
#browser = Watir::Browser.start 'bit.ly/***'
def self.get_connection
puts "Browser started"
puts #browser.title
#browser.driver.manage.timeouts.implicit_wait = 3 #3 seconds
#browser.select_list(:name => 'ctl00$tresc$111').select_value('6')
puts "Selected country"
#browser.select_list(:name => 'ctl00$tresc$222').wait_until_present
#browser.select_list(:name => 'ctl00$tresc$333').select_value('95')
puts "Selected city"
end
def self.close_connection
#browser.close
#headless.destroy
end
end
Page.get_connection
Page.close_connection
But how to do something like this?
while i < 10
Page.get_connection
end

This should open ten browsers:
10.times {Watir::Browser.new}
If you want to use the browsers later, you can put them in a hash:
browsers = {}
(0..9).each {|i| browsers[i] = Watir::Browser.new}
browsers[0].goto "google.com"
browsers[1].goto "yahoo.com"

Related

How can I select multiple options from a select list in ruby using selenium webdriver

I want to select multiple options.
Then I want to verify it whether it is selected or not?
Here is my code:
require 'selenium-webdriver'
class Ques7
def test_multiple_select
driver = Selenium::WebDriver.for :firefox
driver.get(url)
driver.find_element(:xpath, './/*[#id="select_1"]').click
puts driver.find_element(:xpath, './/*[#id="select_1"]').selected?
driver.find_element(:xpath, './/*[#id="select_2"]').click
driver.find_element(:xpath, './/*[#id="select_4"]').click
puts driver.find_element(:xpath =>'.//*[#id="select_1"]',
:xpath => './/*[#id="select_2"]',:xpath => './/*[#id="select_4"]').selected?
end
end
obj = Ques7.new
obj.test_multiple_select

Selenium Does Click on Firefox but PhantomJS doesn't (Ruby)

i'm really new to Selenium WebDriver. I write a simple code for out of curiousity. I made an spam bot to open a page, shuffle in online users list and click everyone of them in order. Send some message, close the new window and repeat.
I made this work on Selenium Firefox driver. It seems work good. But i want to do it silent, not opening firefox everytime. So i found out i can do that by PhantomJS.
Here is my working code for firefox:
require 'selenium-webdriver'
def setup
#driver = Selenium::WebDriver.for :firefox
#reklam = 'Some testing message.'
end
def run
setup
#driver.get 'http://c2.me/okanb3'
first_window = #driver.window_handle
begin
#driver.switch_to.window(first_window)
#driver.find_element(link_text: "Shuffle").click
sleep 20
elements = #driver.find_elements(:class, 'shufflelink')
elements.each do |x|
x.click
all_windows = #driver.window_handles
new_window = all_windows.select { |this_window| this_window != first_window }
#driver.switch_to.window(new_window)
if #driver.page_source.include? 'The user is not available right now.' or #driver.page_source.include? 'User account is disabled.'
#driver.close
#driver.switch_to.window(first_window)
else
input = #driver.find_element(:id, 'inputbox')
input.send_keys(#reklam)
input.send_keys:return
#driver.close
popup = #driver.switch_to.alert
popup.accept
#driver.switch_to.window(first_window)
end
end
end while TRUE
end
run
But when i switch Webdriver from Firefox to PhantomJS, x.click method doesn't work. I made some tests and program doesnt go further from click method. After a while program ends with (Net::ReadTimeout) error.
Here is my last try to work it proper;
require 'selenium-webdriver'
def setup
#driver = Selenium::WebDriver.for :phantomjs
#reklam = 'http://peyloride.com siteme beklerim.'
end
def teardown
#driver.quit
end
def run
setup
#driver.manage.window.resize_to(1920, 1080)
#driver.get 'http://c2.me/okanb3'
first_window = #driver.window_handle
begin
#driver.switch_to.window(first_window)
#driver.find_element(link_text: "Shuffle").click
puts "Shuffleandı"
sleep 20
elements = #driver.find_elements(:class, 'shufflelink')
elements.each do |x|
puts "click'e geldik"
#driver.save_screenshot "phantomjs.png"
x.click
#driver.save_screenshot "phantomjs2.png"
puts "click yaptı sonunda aq"
all_windows = #driver.window_handles
new_window = all_windows.select { |this_window| this_window != first_window }
#driver.switch_to.window(new_window)
if #driver.page_source.include? 'The user is not available right now.' or #driver.page_source.include? 'User account is disabled.'
#driver.close
#driver.switch_to.window(first_window)
else
input = #driver.find_element(:id, 'inputbox')
input.send_keys(#reklam)
input.send_keys:return
#driver.close
popup = #driver.switch_to.alert
popup.accept
#driver.switch_to.window(first_window)
end
end
end while TRUE
end
run
As you can see i tried to take a screenshot to understand the problem but screenshot seems really fine. Here it is:
EDIT: Versions:
ruby -v
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-linux]
gem list
...
selenium-webdriver (2.48.1)
...
phantomjs -v
1.9.0

Fail to use statement IF in Selenium

Currently I'm trying use a statement IF (If a button appears in the page, then run the IF), see the method Login in the system:
If the button doesn't appear in the page, I would like to run the next method Remove and add new expense
require "selenium-webdriver"
require "rspec"
require "rspec/expectations"
describe "#Add simple expense and after add a receipt", :suitee => true do
before(:all) do
#driver = Selenium::WebDriver.for :chrome
#base_url = "http://sitetest.com"
#driver.manage.window.maximize
end
it "Login in the system" do
#driver.get(#base_url)
#driver.find_element(:id, "user_email").send_keys "rafael#gmail.com"
#driver.find_element(:id, "user_password").send_keys "123456"
#driver.find_element(:name, "commit").click
if(#driver.find_element(:css, ".btn.btn-lg.btn-success.btn-block").displayed?)
#driver.find_element(:css, ".btn.btn-lg.btn-success.btn-block").click
#driver.find_element(:css, ".introjs-button.introjs-skipbutton").click
#driver.find_element(:css, ".i.i-pencil").click
end
end
it "Remove and add new expense" do
begin
while(#driver.find_element(:css, ".i.i-pencil.icon").displayed?)
button = #driver.find_element(:id, "expense-bulk-select")
#driver.action.click(button).perform
delete = #driver.find_element(:id, "delete-multi-btn")
#driver.action.click(delete).perform
waitDisplayModal = Selenium::WebDriver::Wait.new(:timeout => 10)
waitDisplayModal.until {#driver.find_element(:class => "bootstrap-dialog-footer-buttons")}
#driver.find_element(:xpath, "//div[3]/div/div/button[2]").click
sleep 3
end
rescue Selenium::WebDriver::Error::NoSuchElementError
#driver.find_element(:id, "current_expense_merchant").send_keys "Taxi to work"
#driver.find_element(:id, "current_expense_amount").send_keys "50"
#driver.find_element(:id, "button-add-expense").click
waitDisplayIconTrash = Selenium::WebDriver::Wait.new(:timeout => 20)
waitDisplayIconTrash.until {#driver.find_element(:css => ".i.i-pencil.icon")}
end
end
after(:all) do
#driver.quit
end
end
My problem: When I run this script, appears this in my console:
Failure/Error: if(#driver.find_element(:css, ".btn.btn-lg.btn-success.btn-block").displayed?)
Selenium::WebDriver::Error::NoSuchElementError:
no such element
(Session info: chrome=42.0.2311.135)
(Driver info: chromedriver=2.9.248304,platform=Linux 3.13.0-24-generic x86_64)
That is, the IF is not working as I would like. How can I fix it?
CHeers
https://code.google.com/p/selenium/wiki/RubyBindings
Use the Wait class to explicitly wait for some condition:
wait = Selenium::WebDriver::Wait.new(:timeout => 3)
wait.until { driver.find_element(:css, ".btn.btn-lg.btn-success.btn-block").displayed? }
....

anemone print links on first page

wanted to see what i was doing wrong. here.
I need to print the links on the parent page, even they are for another domain. And get out.
require 'anemone'
url = ARGV[0]
Anemone.crawl(url, :depth_limit => 1) do |anemone|
anemone.on_every_page do |page|
page.links.each do |link|
puts link
end
end
end
what am i not doing right?
Edit: Outputs nothing.
This worked for me
require 'anemone'
require 'optparse'
file = ARGV[0]
File.open(file).each do |url|
url = URI.parse(URI.encode(url.strip))
Anemone.crawl(url, :discard_page_bodies => true) do |anemone|
anemone.on_every_page do |page|
links = page.doc.xpath("//a/#href")
if (links != nil)
links.each do |link|
puts link.to_s
end
end
end
end
end

Undefined method `heade' for DealerDetail:Class (NoMethodError)

I am having trouble when running this, I keep getting error
.rb:45:in <main>': undefined method heade' for DealerDetail:Class (NoMethodError)
What am I doing wrong I have looked around but cant find any solutions help will be greatly appreciated
require 'watir-webdriver'
#browser = Watir::Browser.new
#page = [:dealerdetail => "www.google.com",
:findadealer => "www.google.com",
:findadealer_results => "www.google.com"
]
class DealerDetail
attr_accessor :browser
def heade
#browser.goto #page [:dealerdetail]
#browser.element(:id, 'mainNav').exists?
puts 'main navigation bar appears'
puts 'navigation bar does not appear'
end
def footer
if #browser.element(:id, 'pageFooter').exists?
puts 'footer appears'
elsif
puts 'Footer does not appear'
end
end
end
#deal = DealerDetail .new
#deal.heade
heade method trying to access #page, which in not available inside instance cause you defined it outside the scope.
I suppose you want to define #page instance variable inside class:
require 'watir-webdriver'
class DealerDetail
attr_accessor :browser
def initialize
#browser = Watir::Browser.new
#page = [:dealerdetail => "www.google.com",
:findadealer => "www.google.com",
:findadealer_results => "www.google.com"
]
end
def heade
#browser.goto #page[:dealerdetail]
#browser.element(:id, 'mainNav').exists?
puts 'main navigation bar appears'
puts 'navigation bar does not appear'
end
end
#deal = DealerDetail.new
#deal.heade
Also make sure you have no spaces between class/object and called method.
DealerDetail .new should be DealerDetail.new
#page [:dealerdetail] should be #page[:dealerdetail]

Resources