I want to open IE browser and then Chrome Browser from a same program - watir

require 'watir'
b1=Watir::Browser.new
b=Watir::Browser.new :chrome
b1.goto "www.google.com"
b.goto 'www.yahoo.com'
I am writing the above code to open google.com in IE and yahoo.com in Chrome but both are opening in IE, May I know how to accomplish this task?

Firstly, use require 'watir-webdriver' because the default behavior when using require 'watir' right now is using a deprecated approach with IE. (We'll be fixing this soon).
Also, please specify each instead of relying on the default.
require 'watir-webdriver'
b1 = Watir::Browser.new :ie
b = Watir::Browser.new :chrome
b1.goto 'www.google.com'
b.goto 'www.yahoo.com'

Related

How to close facebook's chatbox in Watir

I'm currently struggling atm on closing facebook's chatbox whenever my program goes to a page's page (intended kek). It pops up after a few secs upon page load completion. I cant seem to access elements inside it even if its already loaded specifically the "x button" at the top right as long as the chatbox has focus (header is blue).
#b.a(href: "https://www.facebook.com/"+handle+"/?ref=br_rs").click()
#b.a(aria_label: "Close tab").wait_until_present.click()
Running the 2nd line returns a timeout error even though Im staring straight at it for 30secs. Magically (at least for me) the 2nd line runs if the chatbox loses focus (turns gray, mostly when i tab out). I've tried setting focus on some of the main page's element but to no avail. I can also close it if i do a send_keys but It requires me to add a sleep for like 3 seconds or so till the chatbox automatically opens upon page load but I heard adding hard coded sleep is not recommended. Any help? Thanks in advance.
Tried this and it worked on Windows 10, watir 6.12, chromedriver 2.41, Chrome 68.0.3
b.a(:'aria-label' => 'Close tab').click
For some reason it did not on my other machine, Ubuntu 16.04, Watir 6.11, chromedriver 2.40, Chrome 67.0.3
It magically worked after this
require 'watir'
p = Selenium::WebDriver::Chrome::Profile.new
p['enable_aria_label'] = true
b = Watir::Browser.new :chrome, :profile => p
sleep 40 #logged in manually and opened chat box
b.a(:'aria-label' => 'Close tab').click
sleep 20
It closes first chat box it finds. You can close specific one via
b.as(:'aria-label' => 'Close tab')[0].click
Maybe it will work for you too.

watir - is it able to use watir-webdriver and selenium-webdriver in the same time?

I'm new to automation, sorry if the title is not appropiate.
I have Ruby, Devkit, gem json, cucumber, capybara, selenium-webdriver, rspec installed following the guide http://www.swtestacademy.com/ruby-cucumber-and-capybara-on-windows/
However I cannot locate an element using a full xpath (checked and verified with xpath plugin and developer tool), my action is:
page.find(:xpath, "my_xpath").send_keys(yyy)
and I got:
Unable to find visible xpath
I also tried:
page.findElement(By.xpath("my_xpath")).send_keys("ori_pw")
and I got:
uninitialized constant By (NameError)
And I would like to try using watir, I have installed gem watir, watir-webdriver.
and added require 'watir' into my env.rb
then I try:
page.input(:name => "xxx").set(yyy)
But I get: undefined method `input' for # (NoMethodError)
May I have some suggestions please?
Thanks.
=============================================Edit#1
My env.rb now looks like this:
require 'rubygems'
require 'capybara'
require 'capybara/dsl'
require 'rspec'
require "selenium-webdriver"
require 'watir'
require 'cucumber'
Selenium::WebDriver::Firefox::Binary.path='C:\Program Files\Mozilla Firefox\firefox.exe'
Capybara.run_server = false
#Set default driver as Selenium
Capybara.default_driver = :selenium
#Set default selector as css
Capybara.default_selector = :cs
#Syncronization related settings
module Helpers
def without_resynchronize
    page.driver.options[:resynchronize] = false
    yield
    page.driver.options[:resynchronize] = true
end
end
World(Capybara::DSL, Helpers)
How can I disable capybara and set watir properly?
Sorry that I have no technical background...
Thomas, Yes the element is visible,
HTML screenshot
Actually I will be setting value to these 3 password fields.
The xpath I'm trying is:
/html/body[#class='modal-open']/app-root/div[#id='wrapper']/app-navigation/user-change-password/div[#id='myModal']/div[#class='modal-dialog modal-lg']/div[#class='modal-content']/div[#class='modal-body']/form[#class='ng-pristine ng-invalid ng-touched']/fieldset[#class='form-horizontal']/div[#class='form-group'][1]/div[#class='col-sm-7']/input[1]
(at the end I use avoid using class as I see the class name different sometimes)
Thanks.
=============================================Edit#2
The problem now shift to chromedriver.
Double clicking it able to show
Starting ChromeDriver 2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f) on port 9515
Only local connections are allowed.
But enter chromedriver on cmd will show Chromedriver.exe has stopped working.
The reason it's not working for you is due to an unfortunate bug in geckodriver/firefox - which is probably also going to affect watir - whereby it assumes any element with a hidden attribute is actually non-visible (if the display style is set to anything other than default it actually overrides the hidden attribute but not as far as selenium is concerned). This is affecting you because of the hidden attribute on the element div#myModal which makes selenium think the entire modal is non-visible - https://github.com/mozilla/geckodriver/issues/864. If you swap to testing with Chrome instead the issue will go away.
Additionally using XPaths as specific as you show is a terrible idea and will lead to highly fragile tests. If you swap to Chrome (Capybara.default_driver = :selenium_chrome) you'd be much better off just doing something like
page.fill_in('Original Password', with: 'blah')
or
page.find('input[name="originalPassword"]`).set('blah')
One final point, the :resynchronize option went away a long time ago, you might want to find a more up to date guide to follow

Watir-webdriver keeps opening multiple browsers

Hello StackOverflow members,
I've been searching the site (and the rest of the web) for an answer to this question, but all my search queries returned the awesome features of Watir... I seem to be one of the few people running into this particular problem. I hope someone out there has an easy answer for me :)
I'm working on website test automation. The current test set is written with Cucumber/Ruby/Selenium-Webdriver/Capybara. I'm personally interested in switching to Watir-Webdriver in combination with Cucumber and Ruby, but I'm struggling with the following:
Every time I run my cucumber test, Watir opens not one, but TWO browser screens. It seems to want to initiate a blank screen (which just goes to the site I configurated as default), plus another browser screen in which the actual test steps are executed.
Keep in mind, I'm rather new to this and I'm having this trouble when simply following a beginners tutorial. Nothing fancy as of yet.
In my 'Support/env.rb' file, I have the following:
require "cucumber"
require 'watir-webdriver'
app_host = ENV['apphost']
Before do
#browser = Watir::Browser.start app_host, :firefox
end
Before do |scenario|
#scenario_tag = scenario.source_tag_names
#browser.cookies.clear
end
at_exit do
#browser.close
end
The first bit in my steps file (GoogleSearch.rb -- yes it's that basic):
require_relative "../support/env"
Given(/^that I have gone to the Google page$/) do
#browser.goto 'http://www.google.com'
end
Now, when I run this test, I expect just ONE browser to be initiated. But instead, the automation initiates TWO browser screens. One just stays in the background doing nothing, the other contains the test steps.
Again, I've searched for a while now (which I'm usually pretty good at), but I haven't found the answer to my problem anywhere. The only way I got it to work, is to start with a step in my steps file, initiating a browser (instead of doing this in the env.rb file). But I don't want to start each test with opening a browser..
Any help would be very much appreciated. If any more information from me is required, I'll update as soon as I can.
Thanks in advance!
The problem is that env.rb is being loaded twice:
It is automatically included when running the cucumber command
It is being included a second time in GoogleSearch.rb when calling the line require_relative "../support/env".
As a result, each of the hooks is registered twice. In other words, Cucumber is seeing the hooks to run before each scenario as:
Before do
#browser = Watir::Browser.start app_host, :firefox
end
Before do |scenario|
#scenario_tag = scenario.source_tag_names
#browser.cookies.clear
end
Before do
#browser = Watir::Browser.start app_host, :firefox
end
Before do |scenario|
#scenario_tag = scenario.source_tag_names
#browser.cookies.clear
end
As you can see, Watir::Browser.start is called twice resulting in the two browsers. The first one is not used since the second call uses the same variable.
To solve the problem, simply remove the require_relative "../support/env" line.
Note that this will only address the issue with opening two browsers for each scenario. You will notice that you will still get a new browser for each scenario and that only the last browser gets closed. If you only want one browser for all scenarios, you should look at the global hooks.

Using watir-webdriver, can I change the driver temporarily?

Can I change the driver temporarily. Basically I have Phantomjs setup as my default driver but need to use a different driver for 1 feature. The issue is, Phantomjs cannot find a certain element on a page as it is hidden, but on a normal browser it shows perfectly fine and feature passes with no issues.
If anyone has come across the need to temporarily change driver and has a solution, please let me know.
You can use a tags to specify scenarios that should use a specific browser/driver.
For example, you could have the following in your env.rb:
require 'watir'
Before('~#firefox') do
#browser = Watir::Browser.new :phantomjs
end
# Use the firefox browser
Before('#firefox') do
#browser = Watir::Browser.new :firefox
end
After do
#browser.close
end
During your scenarios/features that are tagged with #firefox, they will use the firefox browser. Otherwise, they will use your default phantomjs driver.

selenium-webdriver error for just opening IE

So, I am using -
gem 'watir-webdriver', '0.5.4'
gem 'selenium-webdriver', '2.21.1'
and JRuby - 1.6.5
All I am trying to do is open IE. I have IE version 8.
>> require "rubygems"
=> true
?> require "watir-webdriver"
=> true
?> #b = Watir::Browser.new :ie
Selenium::WebDriver::Error::UnhandledAlertError: Modal dialog present
from C:/jruby-1.6.5/lib/ruby/gems/1.8/gems/selenium-webdriver-2.21.2/lib/selenium/webdriver/remote/response.rb:52:in `assert_ok'
from C:/jruby-1.6.5/lib/ruby/gems/1.8/gems/selenium-webdriver-2.21.2/lib/selenium/webdriver/remote/response.rb:15:in `initialize'
from C:/jruby-1.6.5/lib/ruby/gems/1.8/gems/selenium-webdriver-2.21.2/lib/selenium/webdriver/remote/http/common.rb:59:in `create_response'
from C:/jruby-1.6.5/lib/ruby/gems/1.8/gems/selenium-webdriver-2.21.2/lib/selenium/webdriver/remote/http/default.rb:64:in `request'
from C:/jruby-1.6.5/lib/ruby/gems/1.8/gems/selenium-webdriver-2.21.2/lib/selenium/webdriver/remote/http/default.rb:62:in `request'
from C:/jruby-1.6.5/lib/ruby/gems/1.8/gems/selenium-webdriver-2.21.2/lib/selenium/webdriver/remote/http/common.rb:40:in `call'
from C:/jruby-1.6.5/lib/ruby/gems/1.8/gems/selenium-webdriver-2.21.2/lib/selenium/webdriver/remote/bridge.rb:598:in `raw_execute'
from C:/jruby-1.6.5/lib/ruby/gems/1.8/gems/selenium-webdriver-2.21.2/lib/selenium/webdriver/remote/bridge.rb:92:in `create_session'
from C:/jruby-1.6.5/lib/ruby/gems/1.8/gems/selenium-webdriver-2.21.2/lib/selenium/webdriver/remote/bridge.rb:68:in `initialize'
from C:/jruby-1.6.5/lib/ruby/gems/1.8/gems/selenium-webdriver-2.21.2/lib/selenium/webdriver/ie/bridge.rb:40:in `initialize'
from C:/jruby-1.6.5/lib/ruby/gems/1.8/gems/selenium-webdriver-2.21.2/lib/selenium/webdriver/common/driver.rb:35:in `for'
from C:/jruby-1.6.5/lib/ruby/gems/1.8/gems/selenium-webdriver-2.21.2/lib/selenium/webdriver.rb:63:in `for'
from C:/jruby-1.6.5/lib/ruby/gems/1.8/gems/watir-webdriver-0.5.8/lib/watir-webdriver/browser.rb:35:in `initialize'
from (irb):5:in `evaluate'
from C:/jruby-1.6.5/lib/ruby/1.8/irb.rb:158:in `eval_input'
from C:/jruby-1.6.5/lib/ruby/1.8/irb.rb:271:in `signal_status'
from C:/jruby-1.6.5/lib/ruby/1.8/irb.rb:155:in `eval_input'
from C:/jruby-1.6.5/lib/ruby/1.8/irb.rb:154:in `eval_input'
from C:/jruby-1.6.5/lib/ruby/1.8/irb.rb:71:in `start'
from C:/jruby-1.6.5/lib/ruby/1.8/irb.rb:70:in `catch'
from C:/jruby-1.6.5/lib/ruby/1.8/irb.rb:70:in `start'
from C:/jruby-1.6.5/bin/jirb:13:in `(root)'
from -e:1:in `load'
from -e:1:in `(root)'>>
IE webdriver gets opened, but above error comes.
When I use any other browser - firefox or chrome - they just open as they were supposed to open. It started happen just from this week. I mean I am using selenium-webdriver for years now and never had a problem opening IE or any browser. Error says there is a modal dialog box, but how can there be a modal dialog box when I just open IE, and I don't see any dialog when IE webdriver opens up. And if so, why other browsers don't complain?
I am assuming it might need to do with some windows or security updates that gets pushed on company laptop, but I just want to see if someone else have encountered this before and what was the solution?
There are two possibilities I can think of. One is that you have the Windows Task Manager open when you're trying to execute your tests. There is a bug in previous versions of the IE driver that could improperly throw the UnexpectedAlertException if the Task Manager were open while running code with the IE driver.
The second option is a browser plugin that internally creates a dialog window, but keeps it hidden. There are a number of antivirus and other anti-malware plugins for IE that may cause the issue.
In either case, update to a later version of IEDriverServer.exe to see if that solves the problem.
okay, found a solution. It was anti-virus thingy. The issue was - when you are running script with IE the McAfee site adviser modal window is sitting on top of IE browser so we can not get to IE browser to even open it. and it was specific to IE only because IE being 'default' and 'preferred' browser in my workplace.
McAfee supplied two dll files and copy-pasting them to some specific location solved the issue. Sorry, I used all of your minds in this question while it was not related to selenium but was mcafee issue. Closing the question.

Resources