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

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

Related

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.

Overriding PromptService in newer XULRunner

Our application uses embedded xulrunner. We override the default PromptService to provide custom dialogs for alert, prompt, etc by
componentRegistrar.RegisterFactory (PROMPTSERVICE_CID, aClassName, aContractID, MyPromptServiceFactory);
where,
PROMPTSERVICE_CID is "a2112d6a-0e28-421f-b46a-25c0b308cbd0"
CONTRACT_ID is "#mozilla.org/embedcomp/prompt-service;1"
When using XULRunner 1.9.* versions, this works perfectly and the call comes to MyPromptSerivceFactory. But, this doesn't work on newer XULRunner versions (>= 4.0)
I have modified the PROMPTSERVICE_CID to "7ad1b327-6dfa-46ec-9234-f2a620ea7e00" (copied from nsPrompter.manifest). While registering the factory I get the error NS_ERROR_FACTORY_EXISTS.
If I continue to use the old PROMPTSERVICE_CID, then nsIPromptService2 is not used instead nsIWindowCreator2.CreateChromeWindow2 is used to display alerts and prompts.
I have googled on this, but I couldn't find a solution to either fix the NS_ERROR_FACTORY_EXISTS error or for MyPromptServiceFactory to be used.
Any help/suggestions?
It would probably be better to use the existing prompt service the way Firefox does it rather than replace it. If you look at nsPrompter.openPrompt(), before opening a modal dialog it will try to locate and call a JavaScript function getTabModalPrompt() in the window containing the browser. It expects to get a promptBox element back and will call methods appendPrompt() and removePrompt() on it. Obviously, you don't have to give it a promptBox element, just something that behaves similarly - and displays a message any way you like.
But if you really want to replace system components, you shouldn't duplicate prompter's CID - use your own one but #mozilla.org/prompter;1 as contract ID (the old contract ID is for backwards compatibility only).

Single-file app with xulrunner - possible?

I have tried to mess with xulrunner before, and now I'm trying once again :)
The "real" tutorial (Getting started with XULRunner - MDN) does, in fact, show that one is supposed to have application.ini and other files (possibly zipped as .xpi, which then requires --install-app ...), and then the call should be like:
xulrunner `pwd`/application.ini
... however, I'd like an easier way to start up - and hence, my hope for single-file XUL application approach :) (A good note here is that one also cannot use the zipped .xpi as an argument to xulrunner, see XULRunner question - DonationCoder.com)
The thing is, I am almost 100% certain that at some point in the past, I have used a simple single-file XUL application, as in (pseudocode):
xulrunner my-xul-app.extension
... but I cannot remember how it went :) So, was that possible with xulrunner, or only with firefox?
As far as I can remember, I used something like a 'my-xul-app.xul' file (as the single-file application), which would specify only, say, a window with a single button (that couldn't really do anything due to lack of javascript) - and I'd like to repeat the same thing now, to refresh my memory (unless I confused something from back then :))
First of all, I found HOWTO: Getting Started with Linux Standalone Apps using XUL - Ubuntu Forums (2007), and I modified the example.xul file used there as:
<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>
<window
id = "myapp"
title = "SQLite Example"
height = "420"
minHeight = "420"
width = "640"
minWidth = "640"
screenX = "10"
screenY = "10"
sizemode = "normal"
xmlns = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" >
<spacer style = "height: 4px; cursor: default;" />
</window>
and I'm trying to "run" this as recommended in link, with:
firefox -no-remote -chrome file:$PWD/example.xul
... and what happens is that Firefox opens, with window size being like 1x1 pixels; if you can find the handle, you can stretch the window, and read:
Remote XUL
This page uses an unsupported technology that is no longer available by default in Firefox.
Ouch :( Answers to this (like How do I fix the Remote XUL error I get when using Firefox 4.x and the Webmail Advanced Interface?) seem to be related to actual remote xul (and recommend a plugin to handle that); but what I want is simply to run a file locally?! Where did the "remote" part come from?
Also, seeing the firefox switch '-app' (Using Firefox 3 as a XUL runtime environment); although it refers to an application.ini, I tried this:
firefox -no-remote -app $PWD/example.xul
... and Firefox just started as usual.
Btw, I cannot see neither -app nor -chrome command line options in firefox --help ;)
But actually, I do not really want to use firefox as an engine - just the xulrunner; and I tried the Firefox approach because I thought it is more-less the same as xulrunner; turns out it isn't (even if you use application.ini: Why does 'firefox -App application.ini' and 'xulrunner application.ini' behave differentely? | Firefox Support Forum):
In any case, if I run just xulrunner (as I wanted to), I get:
$ xulrunner example.xul
Error: App:Name not specified in application.ini
So, I can see everything points to "single source file" app not being possible with xulrunner - but I just wanted to make sure (in case I missed some obscure tutorial :) ). And if it isn't - does anyone remember if it was possible at a previous point in time?
PS:
$ firefox --version
Mozilla Firefox 7.0.1
$ xulrunner --version
Mozilla XULRunner 2.0 - 20110402003021
$ uname -r
2.6.38-11-generic
$ cat /etc/issue
Ubuntu 11.04 \n \l
I don't believe you could ever create single-file XULRunner applications. The -chrome <...> parameter probably used to work, I guess the "Remote XUL" error comes from the fact that the URL is file://, not chrome://.
You could use something like the Live XUL Editor in the Developer Assistant (formerly Extension Developer's extension) to test XUL quickly.
The general idea I hear these days is that you should write HTML5 instead, whenever you can, since it is more actively developed, more well-known technology with less incompatible changes and better tooling...
Here is the answer I wrote for another related question, reproduced here because it may help someone who want to know how to package their XUL application.
It is too bad that xulrunner can not run a zipped .xpi or .xulapp directly, but it is possible to package most of your .js, .xul, .css and .png files into a jar and wrap everything up with a private copy of xulrunner, without having to run --install-app
These are the steps I went through to package our XUL application.
The first step is to put all your files (except application.ini, chrome.manifest, and prefs.js) into a .jar file like this (all of this was carried out under Windows, make appropriate adjustments for Linux and OSX)
zip -r d:\download\space\akenispace.jar * -i *.js *.css *.png *.xul *.dtd
Then in d:\download\space, layout your files as follows:
D:\download\space\akenispace.jar
D:\download\space\application.ini
D:\download\space\chrome.manifest
D:\download\space\defaults
D:\download\space\defaults\preferences
D:\download\space\defaults\preferences\prefs.js
The content of the files are as follows
application.ini
[App]
Vendor=Akeni.Technologies
Name=Akeni.Space
Version=1.2.3
BuildID=20150125
Copyright=Copyright (c) 2015
ID=space#akeni.com
[Gecko]
MinVersion=1.8
MaxVersion=35
chrome.manifest
content akenispace jar:akenispace.jar!/chrome/content/
skin akenispace default jar:akenispace.jar!/chrome/skin/
locale akenispace en-US jar:akenispace.jar!/chrome/locale/en-US/
resource akenispace jar:akenispace.jar!/chrome/resource/
prefs.js
pref("toolkit.defaultChromeURI", "chrome://akenispace/content/space.xul");
Now you can put these files into your .wxs for WiX and produce an MSI file for Wndows.
Of course you need to include all the files for XULRunner as well.

Watir-webdriver issue with options.yml file

I have been using firewatir for quite some time but thinking of switching to watir-webdriver. I was playing with my existing script and getting an error in the IRB when i use watir-webdriver
Here is my code from existing script
require 'rubygems'
require 'watir-webdriver'
Watir.options_file = 'classes/options.yml'
I am getting a following error
"undefined method `options_file=' for Watir:Module (NoMethodError)"
Can someone point me to the right direction since I am lost on this for couple of days.
Thanks
Watir != Watir-Webdriver. Watir-Webdriver does not support options.yml using options_file, or most likely the options you're setting in it (browser.speed, etc).
You can find a list of available methods, as well as a comparison list of the two here: http://jarib.github.com/watir-webdriver/doc/ .
If you post the options you are setting in that file we can help you determine if they're available in Watir-Webdriver, or how else you use them.
Thanks
You can read more about watir-webdriver at http://watirwebdriver.com/

Resources