change Watir browser headers - watir

How is it possible to change browser header with Watir?
I'd like to change browser headers (in Firefox or Chrome) when using Watir.
I know about watir-user-agent gem, but I'm interested in changing browser version.
Is that possible?
Thanks

Yes this can be done.
Unfortunately Watir does not seem to provide any very easy way to do this
However, here are 2 simple options which work:
A. Use a proxy server.
This is a well understood way to modify headers generally. However I have not personally used it during automation.
Steps :
1. Setup proxy server before your test code is executed
2. Ensure the proxy server will add the required headers to every request
3. Then when your test browser requests any page ----> the proxy server will automatically add the required headers.
B. Use browser extensions
Since Watir cannot seem to to modify headers by itself ... then we just ask Watir to use a normal browser extension which can!
I have done this successfully using Chrome and firefox
Note: These steps work with ONLY the indicated extensions - but a similar approach should also work fine for many other extensions.
Firefox Steps:
1. Start firefox
2. Search for 'Modify Headers Firefox' using a very popular search engine .... the top result is https://addons.mozilla.org/En-us/firefox/addon/modify-headers/
3. Download the .xpi file for this extension ... currently you can do this by right clicking on the button and clicking "save link as"
4. Install the extension as normal, change the headers as you wish, close firefox, then locate and save the "modifyheaders.conf" file ... this file should be somewhere in your user folder
5. Make the following class (which extends Profile)
class FirefoxProfileWithAddedFiles < Selenium::WebDriver::Firefox::Profile
# This method OVERRIDES the one in Profile
# This method creates the firefox profile folder
def layout_on_disk
#Call the superclass layout method
profile_directory = super
#Add custom file
if(!#file_to_add_to_profile.nil?)
FileUtils.cp(#file_to_add_to_profile, profile_directory)
end
profile_directory
end
def add_file_to_profile(filepath)
#file_to_add_to_profile = filepath
end
6. Set your test script up as follows
...
#Setup Firefox Profile
profile = Selenium::WebDriver::Firefox::Profile::FirefoxProfileWithAddedFiles.new
profile.add_extension("SOMEPATH/modifyheaders.xpi")
profile.add_file_to_profile("SOMEPATH/modifyheaders.conf")
profile["modifyheaders.config.active"] = true
#Start up Firefox
#browser = Watir::Browser.new :firefox, :profile => profile
...
Chrome Steps
1. Start chrome
2. Search for 'Modify Headers Firefox' using a very popular search engine .... the top result is https://chrome.google.com/webstore/detail/modify-headers-for-google/innpjfdalfhpcoinfnehdnbkglpmogdi
3. Install the extension as normal, change the headers as you wish, then close chrome
4. Locate the unpacked extension folder and copy it. On windows the folder will be something like...
C:\Users\MYNAME\AppData\Local\Google\Chrome\User Data\Default\Extensions\innpjfdalfhpcoinfnehdnbkglpmogdi\2.0.3_0
5. Locate the extension configuration file and copy it. On windows, the file will be something like...
C:\Users\MYNAME\AppData\Local\Google\Chrome\User Data\Default\Local Storage\chrome-extension_innpjfdalfhpcoinfnehdnbkglpmogdi_0.localstorage
6. Set your test script up as follows:
...
#Setup Chrome Profile Folder
profile_directory = Dir.mktmpdir("webdriver-chrome-profile")
extension_configuration_folder = FileUtils.mkdir_p "#{profile_directory}/Default/Local Storage"
FileUtils.cp("PATH_TO_MY_EXTENSION_CONFIGURATION_FILE", extension_configuration_folder[0])
#Start Webdriver
#browser = Watir::Browser.new :chrome, :switches => ["--user-data-dir=#{profile_directory}", "--load-extension=#{PATH_TO_MY_UNPACKED_EXTENSION_FOLDER"]
...

Watir automates the browser INSIDE the browser window, with very limited interaction up at the OS level (such as responding to alerts, etc), you would need to pre-configure the browser (presuming that was possible) to what you wanted, or use a tool such as Autoit to interact with the browser's OS level controls to do that.. (presuming the browser even has the feature to allow you to alter what it is reporting in terms of browser and version when it makes a request to a website)
If you are using Watir-Webdriver along with Firefox then you may be able to do this via a profile that sets those parameters. In that case you create the profile, then the browser object with that profile specified. it's pretty much a webdriver function, but easy enough to access when creating the browser object.
See this webdriver bug for the parameters to use (down in the comments) when creating the profile. Refer to webdriver docs for more info on how to setup and use profiles for firefox.
Another option that might be useful would be to fork your own version of the code for the user-agent gem and add browser_version as one of the things to be set. It's using profiles for FF, so doing that should be possible, at least for FF. for Chrome it is using the user-agent switch to override the useragent string, so it should be possible there also, although you would have to do a little work to modified the fixed strings the gem uses to replace the portion that has the version with the one you want.
Then if you get it working issue a pull request to add that enhancement to the gem..
or if you are not up to that sort of thing yourself, then beg, plead and offer to bribe the gem author with something appropriate if they would extend the gem for you to make version one of the things that could be set.

Related

Can you make a node server code communicate with HTML of a website open in browser?

For example I've written a code to access a global variable of a web page and access values from it and then I want to put that value in some HTML div of that page. Now I can copy and paste this code into browser console and it will work.
But instead of pasting it in console, is there any way I can run my own server (localhost) using NodeJS and do the same from there? I mean communicate to the browser and that page which is open?
If yes, what things will I need?
I would use selenium-webdriver
It takes a bit of setup, but afterwards it's pretty easy
go here and click on the folder just above the one that says 'icons' (this should be the latest version of chrome webdriver)
Then, once you download the latest version, drag the exe C:\WINDOWS (if you're not on windows just move it to any folder on the PATH environment variable
now that you've done that, set up a simple script like this:
const webdriver = require('selenium-webdriver')
let driver = new webdriver.Builder().forBrowser('chrome').build();
(async function example() {
await driver.get('http://example.com/')
await driver.executeScript('/* your code, for example: */ document.getElementsByTagName(\'h1\')[0].innerHTML = \'test\'; console.log(\'test\')')
}())
make sure to escape (but a backslash before) any single quotes in your driver.executeScript
I would recommend learning a bit about selenium-webdrivers api so that you can to more complicated things (such as running this without actually opening a new window). I would also that you use selenium-webdriver's api (which allows you to edit webpages, click things, input keys, etc.) instead of just putting everything in an executeScript as much as you can.

Access web page body text using VBA & Selenium

I am trying to convert an Excel macro that currently uses Internet Explorer and use the following line of code to extract the web page’s <body> text
x = .Document.DocumentElement.InnerText
Using the Selenium demo, I am able to produce a jpg of the page with Chrome & IE, but Firefox just loads a blank page and IE64 & Edge don’t work on Windows 10.
I have been unable to find the proper VBA command with Selenium to copy the body text to variable ”x”. I only want to read it.
I am trying to do this to make my macro browser independent.
The macro is for my use only.
Jim
You are not making it browser agnostic. You are simply widening the choice of browser to those supported via selenium basic. This brings some problems of its own which you are noticing.
Folders containing the drivers must be on the environmental path or the path passed to selenium webdriver as an argument.
You should use the latest Chrome browser and Chrome driver
You cannot use the latest FireFox browser and driver. It is not supported. I think you need FF v.46.0.1.
If using IE then zoom must be to 100%.
I suggest browsing the issues pages of Github for further known issues
Heuristically, I have heard some banter about problems with Windows 10 and Selenium Basic - would be interested to know if anyone has got this working as I am not on that version.
Review the examples.xlsm provided by selenium basic GitHub site to see which other browsers are supported (e.g. Opera, PhantomJS, FirefoxLight,CEF).
With Chrome you can get the body text with this:
Option Explicit
Public Sub GetInfo()
Dim d As WebDriver, s As String
Set d = New ChromeDriver
Const URL = "https://www.neutrinoapi.com/api/api-examples/python/"
With d
.Start "Chrome"
.get URL
s = .FindElementByTag("body").Text
Debug.Print s
.Quit
End With
End Sub
Other info: https://stackoverflow.com/a/52294259/6241235

I'm trying to fetch a value produced by JS using Watir, but failing

(Warning: this link is SFW, but the site hosts NSFW text/audio content on other pages, so maybe don't click through on a work computer.)
I am trying to access a series of pages like this. Each page streams an M4A audio file. Once the page loads, the jQuery document-ready event goes and fetches the duration of the audio and puts it on the page.
That's the value I want, the contents of div.jp-duration after the JavaScript has done its insertion.
To do this, I am writing
headless = Headless.new
headless.start
browser = Watir::Browser.start "https://soundgasm.net/u/tarkustrooper/F-Wasted-Time-The-Eagles"
duration = browser.execute_script %q{return $("div.jp-duration").innerHTML}
but I'm not returned anything.
If I try $("#jquery_jplayer_1").data('jPlayer').status.duration, expecting the number of seconds, I get back 0, even though this works if I try in a Chrome console.
I'm using Watir wrong somehow, but I don't knowit well enough to know how. I'd love some help if possible.
I'm using Ruby 2.1.5, Watir 5.0.0, Watir-Webdriver 0.8.0 and Headless 2.2.0 on CentOS 6.
This should work without the need for execute_script:
#browser.div(class: 'jp-duration').text

How can one debug the Chrome extension "options" page using the new OptionsV2 method?

https://developer.chrome.com/extensions/optionsV2 tells me that I should be using options_ui in my manifest, rather than options_page, and recommends I start upgrading immediately.
However, I can't find any way to actually debug the script run by my options page when I use options_ui—the Options popup is in an tag, and the developer tools don't show me the source, or even the HTML content.
For now, I just comment out options_ui and let options_page take effect when I need to debug. I'm guessing that setting "options_ui": {"open_in_tab": true,...} would have the same effect, but it would be really nice to figure out how to actually debug the script when it's running the new way.
Auspex,
Teepeemm's comment is correct.
Other way, you can launch your options page from other tab using its full URL
like,
chrome-extension://{your extension id here}/{your options page path here, from the extension root}
e.g. say my extension id aaabbbcccdddeeefffggg, and say, my options page is located (from extension root) at app/html/options.html; then i can load up below URL in a new tab ---
chrome-extension://aaabbbcccdddeeefffggg/app/html/options.html
Now here, in this tab; you can do your regular debugging around HTML and javascript.
I hope this suffices your debugging requirement for 'new options UI' for chrome.
Teepeemm's comment is correct.
It's as simple as right-clicking inside the options page modal and selecting "Inspect element" - it will open the correct Dev Tools.

Cucumber Profiles and Global Hooks to select browser

I'm new to Cucumber and Ruby. I'm a Business Analyst used to writing requirements and test cases, and I've read up on Cucumber and Watir, but I'm having trouble getting what I need to happen to actually work.
I'm going to be testing a set of web pages in IE, Firefox, and Chrome, and as different types of users. I'm writing and tagging features, and making profiles in cucumber.yml so I can run cucumber with -p to run just the appropriate tagged tests for different user types.
I am creating different page objects so that I can easily log in as Basic user, Reports user, Editor user, etc. The feature files will have slightly different scenarios and pass/fail conditions for each type of user, and the step defs will call the appropriate page objects for logging in as those user types.
Rather than duplicate the tests three times each to open the right browser with watir-webdriver, and rather than having the browser open and close with each scenario, I wanted to make a global hook in env.rb or hooks.rb to open the browser once and only once before any features are run, and then close it with and after statement from the global hooks when the features have all run. Again, I am using profiles calling tagged scenarios to pick which tests to run for each user type.
The problem I am having is a step before that, just opening the browser. I know how to user watir-webdriver to open each browser.
What I wanted to was put global hooks in to open a browser before any testing.
So in the env.rb I was going to put the browser = Watir::Browser.new call.
What I want to do is have three separate global hooks, one each for IE, Chrome, and Firefox, and call them based on a condition passed from a profile in the cucumber.yml, but I can't get it to work. I tried tagging them, and that didn't work. Can you not tag global hooks? I tried it with an if/then/else setup but that didn't work either.
I would like to be able to pass a parameter or tag from a profile in cucumber.yml to call only one global hook, such as:
basicuser_overviewtests_ie: --tags #basic #overview #ie
basicuser_overviewtests_ff: --tags #basic #overview #firefox
Basicuser_newpagetests_ie: --tags #basic #newpage #ie
Etc... the first two tags would refer to scenarios, the third to the correct env.rb hook.
Or, if I could pass a variable from the profile, that'd work too. I couldn't figure out how I could use an environment variable there though.
Am I missing something? Is this enough information to explain the issue?
Thank you!
UPDATE: Part of the problem appears to be that cucumber didn't recognize the [support] folder when it was on the same level as the [features] folder, it needed to be under [features]. The cuke info I had said it would be Ok at the same level rather than inside features. Not the case.
So now I can get my three global hooks for the browsers to run, but tagging doesn't help, they all run at once even if tagged to only run before features tagged #ie or with profiles tagged only to run #ie.
EDIT: pp. 147-148 from The Cucumber Book leads me to believe that this should work:
Before ('#ie') do
#browser = Watir::Browser.new :ie
end
I haven't tested it, but that would probably get you what you wanted if you wanted to pass the browser as a tag.
I was not able to figure out how to read tags either, though I think there's probably a way to do it. I'll update you if I come across it in The Cucumber Book. I ended up using environment variables. Here's how I did it:
case ENV['BROWSER']
when 'ie', 'Internet Explorer'
browser = Watir::Browser.new :ie
when 'ff', 'Firefox'
browser = Watir::Browser.new :ff
when 'chrome'
browser = Watir::Browser.new :chrome
when 'opera'
browser = Watir::Browser.new :opera
when 'debug'
debug_profile = Selenium::WebDriver::Firefox::Profile.new
debug_profile.add_extension 'features/support/firebug.xpi'
browser = Watir::Browser.new :firefox, :profile => debug_profile
else
browser = Watir::Browser.new :ie
end
Before do
#browser = browser
end
at_exit() do
browser.close
end
Then I just pass in BROWSER=<browser> on the command line. For a more in-depth discussion of what I did to use ENV variables, you can read the blog post I wrote here.

Resources