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

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

Related

How to redirect to a website in Godot Web

I'm planning on trying to see if it's possible to make a website in Godot(Yes, I know I shouldn't I just want to try just to try). I thinking about and looking over the features I need and I have one problem.
I just need a way for a person to press a button and get redirected to my itch games. I don't care if it creates a new tab or changes the current tab. Thank you for any help.
If you dont export to web you can call
OS.shell_open("url")
Sadly this does not work in an html export. A solution I found for myself is the JavaScript Interface. As the name suggested it allows you to execute Javascript.
So to open a URL you could connect the pressed signal of a button to something like this:
if OS.has_feature('JavaScript'):
JavaScript.eval("""
window.open('https://google.com', '_blank').focus();
""")
This will open a new tab in the active browser.
I also found an article on the godot site, basically asking the same question (https://godotengine.org/qa/46978/how-do-i-open-richtextlabel-bbcode-links-in-an-html5-export). Here they tried to use an RichTextLabel with BBCode.
The solution did not work for me, when I tested it, though.
As pointed in the comments you can try OS.shell_open, for example:
OS.shell_open("https://example.com")
That only works if it is not an HTML export.
Your other alternative is to eval JavaScript, for example this navigates the current tab:
JavaScript.eval("window.location.href='https://example.com'")
Which only works if it is an HTML export.
Since that only works for an HTML export and the other does not work on an HTML export... If you need both you can do this:
if OS.get_name() == "HTML5":
JavaScript.eval("window.location.href='https://example.com'")
else:
OS.shell_open("https://example.com")
See also Close a game in Godot.

Selenium , Python and Chrome Webdriver problem

I am learning Python and attempting to build a program that will scrape specific data from a website, store it and then manipulate it.
Currently I run my application, it opens a new chrome browser window and loads the page correctly. The problem is it should begin to start scrolling down and loading the remaining elements on the page.
I know the code works because if I manually click somewhere on the page that doesn't normally illicit a response (white space/empty areas) the browser somehow comes into "focus" and begins to iterate through the loop that scrolls down the page (by sending keys) prints the data I am after. I also noticed if I click another similar "dead space" area that contains the header, it doesn't have the same effect. I am unsure if this is something specific to Chrome, iFrames or something of that nature but I am completely stumped and would greatly appreciate any help.
Any thoughts on why I need to manually click on the new chrome window for it to work would be great.
Update: Still having the same issue, even tried with Safari and the same problem seems to exist.
Fixed this with:
element = driver.find_element_by_css_selector("div[id^='app-container']")
action = ActionChains(driver)
action.click(on_element = element)
action.perform()

Selenium Python: delete_all_cookies() is not deleting all the cookies stored

There is this website : http://justnotepad.com/
Here we can write notes and it will store it in browser's storage so if we reload/reopen the page again it restores the previous notes.
So I want to open this website 3 times in same Browser Instance without closing it by opening and closing new tabs.
Between Closing and Opening of Tabs browser.delete_all_cookies() is executed.
But the website is still able to fetch previous notes.
The following code is working for http://temp-mail.org after each load the mail address changes. Which means the cookies are getting deleted. But can't understand why it isn't working for http://justnotepad.com/
My Full Code:
browser = webdriver.Chrome(chrome_options=chrome_options,executable_path=cpath)
for i in range(2):
browser.get('http://justnotepad.com')
t=browser.find_element_by_xpath('//div[#id="editable_text_box"]/textarea[#name="editable_text"][#id="editable_text"]')
t.send_keys(Keys.ENTER + "This is a new line")
'''
browser.get("https://temp-mail.org")
print(len(browser.window_handles))
#print(browser.get_cookie('mail'))
eid=browser.find_element_by_xpath('//input[#id="mail"]').get_attribute('value')
print(eid)
'''
print("current:",browser.current_window_handle)
browser.execute_script("window.open('', 'new_tab')")
print(len(browser.window_handles))
next_tab=browser.window_handles[len(browser.window_handles)-1]
print(next_tab)
print(browser.title)
browser.delete_all_cookies()
#browser.delete_cookie('mail')
browser.close()
print(len(browser.window_handles))
browser.switch_to_window(next_tab)
I know this is old, but like when you inject cookies and refresh the page for them to load, the same goes for deleting.
Try to refresh the page and see if that makes a difference.
driver.refresh()
time.sleep(seconds) # Wait for it to load
In this case, the information is not stored in the cookies. It is in the browser's Local Storage.
To show:
browse to http://justnotepad.com/
Open Dev Tools (F12)
Use the Application tab
Click on Local Storage
Click on justnotepad.com leaf
Write something in the notepad.
Watch the key and value twitch.
Delete the note. Watch the key and value twitch.

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.

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