How to use DevTools Network with Selenium 4 in Ruby - ruby

I am using Selenium 4 with Ruby and I'm trying to capture the Network activity, but I can't figure out how to use the libraries and haven't found anything related to this for Ruby.
So what I did is:
I added the DevTool gem in the Gemfile: gem 'selenium-devtools', '~> 0.91.1'
I the code I'm trying this:
devtools = browser.devtools
network = Selenium::DevTools::V91::Network.new(devtools)
network.enable
but it doesn't seem to work.
When debugging I see something strange: on creating the DevTools object there seems to be an exception at the socket method:
Unfortunately I can't figure out why this happens and didn't find anything about this. I tried to force into the .new method which fails somewhere but it doesn't take me anywhere.
Have you stumbled upon something similar or does anyone have some advice on how to properly use the DevTools in Ruby?

I haven't used Selenium 4 DevTools, but there is another way you can incorporate this.
To access DevTools, you need to first mention them in your YML file in Ruby. Something like this
chrome:
switches:
- auto-open-devtools-for-tabs
prefs:
download:
default_directory: downloads/
devtools:
preferences:
panel-selectedTab: '"network"'
Similarly, if you want to open Console tab you can change from network to console tab.
Also if you want to record the content you can try this particular gem in Ruby which helps to screen record test cases in Ruby Screenrecord

Related

Can anyone suggest how to take screenshot of full webpage using ruby selenium?

I want to capture full webpage screenshot in chrome browser using ruby selenium. I am using Rspec testing framework. save_screenshot method captures screenshot only for visible area.
I have gone through the following link,
How to take a screenshot of a full browser page and its elements using selenium-webdriver/capybara in Ruby?
But I don't want to use window resizing or watir gem. Is there any other way or gem to achieve same.
1) You can use https://github.com/samnissen/watir-screenshot-stitch where
Directly employing geckodriver's new full page screenshot functionality (only on Firefox).
Screenshot stitching, paging down a given URL by the size of the viewport, capturing screenshots and adjoining them.
Employing a bundled html2canvas script against the page to generate a png from a canvas element.
2) or use native instrument for your OS - https://paulhammond.org/webkit2png
In code will look like this
webkit2png https://stackoverflow.com/questions/60728482/can-anyone-suggest-how-to-take-screenshot-of-full-webpage-using-ruby-selenium
where:
- main command - webkit2png
- link page - all else
It's old question, but it doesn't hurt to post this. Use Scrot to take screenshot, and later make gif. Since OP only want screenshots, command would be:
require 'screenscrot'
#screen = ScreenScrot.new
#screen.capture(:all)
On linux:
sudo apt install scrot -y && gem install screenscrot

Firefox "Mouseover" issues in Selenium Ruby bindings on Windows

I've been struggling with mouse hover for a couple of days and found a couple of threads here on the topic, but none have helped. I've tried dozens of different approaches and have also modified my code to be more in sync with the examples here, especially Dave Haeffner's suggestions. The current code looks like:
Selenium::WebDriver::Wait.new(timeout: 2).until do
#driver.find_element(link: "ADMIN").displayed?
end
#driver.action.move_to(#driver.find_element(link: "ADMIN")).perform
Selenium::WebDriver::Wait.new(timeout: 2).until do
#driver.find_element(link: "ORGANIZATION").displayed?
end
driver.action.move_to(#driver.find_element(link: "ORGANIZATION")).perform
Selenium::WebDriver::Wait.new(timeout: 2).until do
#driver.find_element(link: "TEAMS").displayed?
end
#driver.find_element(link: "TEAMS").click
end
On macs, this code works fine. On Windows however, it produces:
Failure/Error: #driver.action.move_to(#driver.find_element(link: "ADMIN")).perform
Selenium::WebDriver::Error::InvalidElementStateError:
Cannot perform native interaction: Could not load native events component.
I'm sure the element access is fine, because if I change the first mouse hover to a click action, it works great.
Any help would be appreciated.
You need to use Firefox version 31.0.6 . Versions of Firefox after that don't have the native events support. If you need to use a later version of Firefox, then just make sure your test actions are all non-native, such as using a JavascriptExecutor to create a hover (instead of relying on native events in the firefox driver).

API KIT Console in Mule not showing any Output

I tried to look through all the tutorials for RAML and I was pretty excited.
I found most of the online resources available but I could not understand why, when i set up everything and the flows are generated, then i run it locally as a mule application, when i point to localhost:8081/api/console/ i get a huuuuuuuge json response, but not the UI described for example here.
Yes i also faced the same issue with Any Point Studio. It is not at all displaying in the API KIT Console present in Any Point Studio. But to feel good and to see the output i have just tried it with Google Chrome Browser and i got the expected User Interface as i was expecting from API KIT Console. Hope this issue will be fixed from next release onwards.
Here is the URL i used to see it on Browser : http://localhost:8081/remote-vending/api/console/
Here is my output from Google Chrome browser for the API KIT Tutorial
My GUI didn't show in the Anytime studio tab: APIkit Consoles, one way to fix this:
make sure you have started your application
right click for the context menu
select encoding
click auto-select
I have had the same problem and resolved it by removing invalid whitespace. One of the example files I was including had an invalid trailing space.
The way I found out;
Open the Network panel in the Developer Toolbar in your browser
Go to http://localhost:8081/api/console/
Find the response for a request to '/api' with the request header 'Accept:application/raml+yaml'.
This response should contain the fully compiled RAML, where all include files have been included.
Copy this entire content into a new RAML-file in Anypoint Platform API Designer or some other YAML editor with error reporting.
It highlighted the invalid whitespace immediately for me.
It should work Out of the box.
It could be a bug of an earlier version.
Could you check the behaviour in more recent releases?
Changing my default browser from Firefox to Chrome resolved the issue for me.

Unable to trigger mouse event in Capybara test

I am using Capybara 1.0.0, and I have a link in my page which gets visible when mouse hover over that block. So I want to trigger mouse over in test so that I can click that hidden link.
I googled it, but couldn't find the solution that suits me. Can you guys help me with this?
I've chosen to use Capybara webkit, and sadly I had to resort to executing javascript using jQuery:
page.execute_script('$(".ClassSelector").trigger("hover")')
This blog has the answer:
http://aokolish.me/blog/2012/01/22/testing-hover-events-with-capybara
page.find('#element').trigger(:mouseover)
This doesn't work with the selenium driver though:
http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Element#trigger-instance_method
This commit added the hover method. It has the advantage of including css hover effects on elements.
Usage:
find('#posts .post .comments .comment').hover
I had a lot of trouble getting this to work as well. There's a lot of conflicting information on the web. Here's how I did it:
Setting: rails 3.2.1, capybara 1.1.2, rspec 2.8.0, selenium-webdriver 2.20.0
page.execute_script "$('tr.user-#{user.id}').trigger('mouseover')"
This will cause the previously hidden links to appear (by virtue of jQuery's hover function), which are then accessible for have_link and click_link.
Note: you do not want to write page.evaluate_script as this won't work.
Edit: Well I just realized that #kelly-sutton's solution is the same as mine. So I can confirm this.
For visibility problems sometimes it helps to change window size for poltergeist. I've done it
in spec_helper.rb
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, window_size: [1280, 600])
end
As well as using either find('#element').hover or page.execute_script "$('#element_2').trigger('mouseover');" one must also pass js: true to the describe block in order to turn on javascript (unless you've done that in your configuration). This tripped me up for a bit.

How do you log to Firebug from an extension?

I'm writing an extension for Firefox, and I need to log some data to Firebug's console. Within the scope of my addon, "console" is undefined, and "window.content.console" is also undefined. So how do I log to the console?
Since you're not writing Javascript that executes within a window, console is not defined.
So you need to reference the Firebug extension first:
Firebug.Console.log(str);
To log to the console from inside a firefox extension’s javascript:
Application.console.log("Hello from my Firefox Extension!");
As far as I know you can only do that if you are creating a JetPack Add-on. Normal debugging is done with Venkman from Mozilla at http://www.mozilla.org/projects/venkman/
Firebug console is associated with a particular page, so it wouldn't be very convenient even if you managed to log messages there. Did you try Chromebug? I didn't use it, but I would expect to find a similar console for extensions to use there.
You could also use the regular Error Console, although you won't get all the niceties Firebug's console provides. You could install Console^2 https://addons.mozilla.org/en-US/firefox/addon/1815 to make using the Error Console a little less painful.
If in your extension you have access to the content Window object, you can unwrap it, and call the console methods directly:
window.wrappedJSObject.console.log('something important');
There are contexts in which even the Firebug object is unknown, like if you're trying to call it from a sidebar... in which case you have to go all the way back to the original window to get the firebug object:
var Firebug = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
.getInterface(Components.interfaces.nsIWebNavigation)
.QueryInterface(Components.interfaces.nsIDocShellTreeItem)
.rootTreeItem
.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
.getInterface(Components.interfaces.nsIDOMWindow).Firebug;
You can then from within your sidebar call Firebug like so:
Firebug.Console.log("foo");
This is documented here: https://developer.mozilla.org/en/Code_snippets/Sidebar

Resources