Cucumber Profiles and Global Hooks to select browser - cucumber

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.

Related

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.

How to reset the state of the browser without closing it?

I am automating using Cucumber and Watir Webdriver and I want to know if there is a way to clear the state of the browser instead of closing it after every run so that I can use Scenario Outline and I open just one instance of the browser and clear state of the browser for other examples listed in the example table
Scenario Outline: This is an example of what I want to achieve.
Given I visit the <Website>
Then the current page must be <page_title>
Example:
|Website|page_title|
|google| Google|
|Facebook|Welcome to Facebook|
Assuming that, in terms of resetting the browser, you just need to clear the cookies, you can use the following hooks.
# Create a browser that will be used for all scenarios
browser = Watir::Browser.new
Before do
#browser = browser
end
# Clear the state (cookies) before each scenario
Before do |scenario|
#browser.cookies.clear
end
# Close the browser after all scenarios completed
at_exit do
browser.close
end
Are u talking about the tITEL becuse if you are there is any of tutorials out there and it is easy try typeing changeing thw state of the titel in google

Chrome Extension sandbox and message passing

I have an old Chrome extension that I want to update for manifest version 2. I noticed I have an eval function call that I cannot get rid of. I'm trying to understand the new Chrome sandbox feature that lets me do an eval call inside of it. The question is: by loading a sandbox page in the background, can I pass it a string containing something to execute, then have it executing the script, outputting the result on the popup page?
At first I thought this was possible but looking at the examples apparently the script to be evaluated has to be already in the sandbox page.
Thanks

chrome content script to access and modify window

I am writing a chrome extension that is a 'content script'
I want to inject a google map on to a webpage.
Problem:
It appears that i have no way to add functions on to the window object, thus i cannot define a callback function for googlemaps to call when it loads.
How do people usually go about mucking with the window?
--
someone on the interwebs suggested i do this:
You can do this easily with a JavaScript URL: window.location =
"javascript:obj.funcvar = function() {}; void(0);"
but when i did this i got an access denied error. it seems like a lot of search results about this problem are outdated.
Content scripts have a separate JavaScript execution ennvironment from the page they run on, so they cannot alter JS variables in the page itself. However, the content script shares the DOM with the page, so you can inject a <script> tag into the DOM which will be loaded and run in the actual page's execution environment.

change Watir browser headers

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.

Resources