Run script directly in 2 various browsers - ruby

I have created Ruby test script that use Selenium RC to test my web app directly in 2 browsers(IE, Firefox). My script runs - first on IE then continue on Firefox and then should be continued and finished in already opened IE browser. My problem is: I can't continue(reconnect) to run my script in already opened IE browser. I use:
#browser = RSpecSeleniumHelper.connect_browser("URL")
but it opens with new session (it needs to keep previous session).

Is there a particular reason you need to switch between browsers half way through?
I have no idea how you'd fix the problem, but it seems like it would be best solved by running the tests in one browser at a time.

I'm also unsure why you need to switch back and forth in your browsers.
Regardless, I'm doing something similar, but instead I use a different library. I'm using the "Selenium" gem. (gem install selenium) and here's what I would do in your situation.
#ie_driver = Selenium::SeleniumDriver.new(rc_host, port, "*iexplore", url, 1000)
#ie_driver.start
#ie_driver.whatever //Test code
#ff_driver = Selenium::SeleniumDriver.new(rc_host, port, "*firefox", url, 1000)
#ff_driver.start
#ff_driver.whatever //Test code
#ff_driver.stop
#ie_driver.whatever //Continue test code with IE
#ie_driver.stop
In summary, while I'm not really familiar with your selenium library, typically I would create 2 instances of the R/C driver, that way I won't have to interrupt the session.

Related

Scrape a website using Selenium and Tor with python 3 on Windows 10

I know that there are many threads talking about this but i've tried many of the solutions suggested but nothing seems to work. Im gonna be very specific so you guys could please help me!
Im trying to do web scraping to a website using Selenium in Python 3 on Windows 10. This website blocks me after a certain number of requests so what I've red is that if I use Tor as the Selenium web driver I can just ask Tor for a new identity (which means a different IP) every specific number of requests.
The following code lets me do the scraping I want using Tor firefox profile in the Tor Browser folder. The only thing missing with these code is that I've havent been able to request a new identity (new IP).
profiler = webdriver.FirefoxProfile(r"C:\Users\Samir\Desktop\Tor
Browser\Browser\TorBrowser\Data\Browser\profile.default")
profiler.set_preference("network.proxy.type", 1)
profiler.set_preference("network.proxy.socks",'127.0.0.1')
profiler.set_preference("network.proxy.socks_port",9050)
driver = webdriver.Firefox(firefox_profile=profiler)
driver.implicitly_wait(15)
driver.get("The URL I want to scrape")
#Extract whatever information i want from the URL
I tried to use the Stem library to get new identity but this does not seem to work with the Tor firefox profile of the previous code.However this works fine if I just open the browser double clicking on the Tor Browser shortcut icon that is created when I install Tor.
#This is the code in stem that gets new identity using Stem. As I said,
#this does not work with the selenium firefox profile for Tor.
from stem import Signal
from stem.control import Controller
with Controller.from_port(port = 9051) as controller:
controller.authenticate()
controller.signal(Signal.NEWNYM)
Okay so to wrap up, is there a way to get a new IP with the previous code I showed? Or what can I do to achieve what I want using python 3, selenium and tor on windows 10 plus anyother library or whatever thats necessary.
If you have questions or need more information to help me just let me know.
Thaks a lot!!

Selenium profile is getting detected by Google?

Basically I'm trying out Selenium webdriver (using FireFox) and right now I am trying to sign up to a Google account.
However, the strange thing is that whenever I run Selenium and let it use the (empty I assume?) Selenium FireFox profile Google seems to detect it and block me (asking for phone vertification).
This is even the case when I load up the selenium profile and manually sign up.
When I sign up manually (and don't use the selenium profile) I can sign up just fine.
Is the Selenium FireFox profile some how special which enables the servers to detect it?
EDIT: I'm trying to startup selenium with my default FF profile (however it keeps starting up in an empty profile) - here's the code:
OpenQA.Selenium.Proxy proxySetting = new OpenQA.Selenium.Proxy();
proxySetting.HttpProxy = proxy;
proxySetting.FtpProxy = proxy;
proxySetting.SslProxy = proxy;
FirefoxProfile profile = new FirefoxProfile("default");
profile.SetProxyPreferences(proxySetting);
profile.SetPreference("browser.privatebrowsing.autostart", true);
_driver = new FirefoxDriver(profile);
EDIT:
I managed to open the default firefox profile but now it doesn't use my proxy settings. How can I use the normal profile and still customize the profile proxies?
This post talks about an HtmlDriver tag being added to the HTML in the FirefoxDriver which would be a dead giveaway
Google is a strong supporter of Open Source, and even Selenium itself, however I don't think Google would particularly condone a Selenium script creating a bunch of spam accounts that probably would never be used, and just take space.
That being said, I believe that it would be possible potentially.
The only way that Google would be able to know you are using Selenium, is based on the Request Headers. It's possible either the User-Agent has something to do with Selenium, or one of the other Headers.
My solution would be to use something like Fiddler to listen to the requests that Firefox is sending, and then edit your Selenium scripts to account for, and change those requests so Google does not know that you are using Selenium.
This most likely goes against their terms of use, so exercise caution, and use this answer for educational purposes only.
Is there a chance, if you were using the complete path to your firefox profile directory? (e.g. C:\Users\???\AppData\Roaming\Mozilla\Firefox\Profiles\your_profile.default)

why send_keys can't work after right click in chrome and ie, but firefox is fine?

i want to copy a image in the page with right click and alt+y,and follow code is work well in the firefox,but chrome and ie. so i can't get the image from the clipboard.
require 'watir-webdriver'
b = Watir::Browser.new :chrome
b.goto('www.baidu.com')
b.img(:src=>"http://www.baidu.com/img/baidu_sylogo1.gif").right_click
b.send_keys ("{alt}y")
and follow code can work well
require 'watir-webdriver'
b = Watir::Browser.new :chrome
b.goto('www.baidu.com')
b.send_keys :tab
this can work well
When you do b.send_keys ("{alt}y"), you are just typing the letters {, a, l, etc.
Try this instead:
b.send_keys([:alt, 'y'])
Watir and Watir-Webdriver use different inputs for send_keys - see here.
Note: I did not test your full script with this. To be honest, I cannot figure out how to manually copy the image to clipboard using alt+y.
It would help to know why you want to save the image. Would a screen shot work? If it will, try this:
b.driver.save_screenshot "#{Time.now.to_i}.png"
Based on one of the previous comments, its sounds like you ultimately just need a way to save the image. I assume copying it to clipboard is not actually important.
So you do all your navigation in Watir-Webdriver and then switch to one of the other Ruby gems (ex Open-URI or Net-Http) to download the image.
The following is an example using Open-URI:
require 'watir-webdriver'
require "open-uri"
save_file = 'C:\Documents and Settings\Setup\Desktop\image.png'
b = Watir::Browser.new :chrome
b.goto('www.baidu.com')
image_location = b.img.src
File.open(save_file, 'wb') do |fo|
fo.write open(image_location).read
end
Do not waste any time trying to automate validation such as a Captcha (Completely Automated Public Turing Test To Tell Computers and Humans Apart) or other authentication system designed to thwart automation. Yes it can be done but it is effectively engaging in an arms race or tilting at windmills. More importantly it brings no value to your employer.
The right way to automate around a captcha or verification code is to configure the test environment such that the code is predictable. Many captcha tools, or verification tools, have the ability to operate in 'test' mode where they have a set response. You'd never configure a production environment that way, but you can easily (and ought to) configure a test environment that way.
Talk to the folks who setup and control the test environment. They just need some logic that knows it's a test env and then uses the proper api call for the captcha or authorization tool (like the google authorizer, or the widgets that spit out numbers every 5 minutes) to put it in test mode so it uses a predictable validation response.
Here is an example of what I am talking about: http://captcha.biz/doc/aspnet/api/captcha-configuration-reference.html#BotDetect.Configuration.ITestModeConfiguration
If you are using something that does not support this, then you can either configure the system to skip the captcha entirely, OR you can configure it to point some test stub that you create (instead of the real captcha system). This will be a small bit of code that you or your developers create that uses the same api as a real captcha, but always expects the same answer. In that case everything behaves exactly like it was talking to a real captcha service, but the little stub code it is talking to always sends the same image with the same 'answer' e.g. 'testing'
This sort of thing is normal for test environments, where all sorts of third party stuff is either disabled, or put into a test mode, or connected to a test sandbox (e.g. captcha's, advertising, website analytics, tracking pixels, credit card authorization services, etc)

Multiple concurrent browser tests with watir-webdriver through different browsers

so I'm working on a website here and I would like to run multiple browser tests at one time. What I mean by this is it should perform my smoke tests on ie, firefox and chrome at the same time and report back each browser results. I'm currently only testing with ie with rpsec and watir-webdriver but would like to automate for the other 2 browsers. Are there any existing gems out there (I haven't been able to find any), if not what would be the best way to go around solving this issue?
You should try WatirGrid.
It won't make all the work for you but it will give you a platform to launch multiple tests at once. The you can just launch the same test 3 times changing the target browser and the grid will handle where they will be executed.
Why don't need anything except watir-webdriver to run multiple browsers on the same machine.
ie = Watir::Browser.new :ie
firefox = Watir::Browser.new :firefox
chrome = Watir::Browser.new :chrome
opera = Watir::Browser.new :opera
If you have multiple machines or VMs to work with, Jenkins in the answer. My approach is similar to Chuck's, but instead of a flat configuration file I let Jenkins prompt for these values via drop-down menu, etc. Jenkins is easy to set up and can automatically distribute test jobs to any available machine for testing.
So, I click "Google Search Test" and select "Internet Explorer"... then I do the same thing and select a different browser. Concurrent tests in various browsers, with HTML/email output and a great log history.
I'll also be writing more about this, but I'm still on vacation!
Here is an example of configuration file (these assign default values if for example Jenkins is not used to launch them). NOTE: "||=" means "if nil, use this value. If not nil, use the current value". I'm only setting values if Jenkins has not already.
ENV['BROWSER'] ||= "firefox"
ENV['ENVIRONMENT'] ||= "qa"
ENV['LIMIT'] ||= "10"
ENV['DISTRICT'] ||= "any"
ENV['TYPE'] ||= "pkg-new"
# Not necessary, but added for sanity/cleanliness:
$type = ENV['TYPE'].downcase
$browser = ENV['BROWSER'].downcase
$env = ENV['ENVIRONMENT'].downcase
$district = ENV['DISTRICT'].downcase
puts "\t** Testing #{$env.upcase} using #{$browser.upcase}... **"
The Jenkins portion is surprisingly easy - so easy I didn't think it was set up. You create a Variable for your script, and whatever you name the variable becomes ENV["VariableName"] - and is immediately available to your script.
So I have a variable named "BROWSER" that is set by a drop down with Firefox, IE and Chrome options. The user has no room to confuse the script with free text, and they can run a custom test whenever they want. My Devs/PMs/Users love me :D.
If you want to run the exact same test code for the tests you will need to externalize the browser type, either as an environment variable, or a YAML file or somesuch.
Ruby has some stuff that makes dealing with yaml files super easy (I need to write a blog posting on this) so you can put something at the top of your script that calls a method to get the config info and then set the browser type accordingly.
In my testconfig.yml YAML file I have:
global:
browser: ie #possible values: ie, ff, chrome
note I don't currently test against opera (market segment too small) or it would be in the list of possible values. The comment is just there to make life easy on whoever might have to edit that file.
I have a read_config method defined in a readconfig.rb file which looks (in part) like this
require 'yaml'
def read_config
config = YAML.load_file('testconfig.yml')
$browser_type = config['global']['browser']
end
And at the top of my tests there is code like this
require 'rubygems'
require 'readconfig'
require 'watir-webdriver'
read_config
$browser = Watir::Browser.new $browser_type.to_sym
This way I can have a different config file on each system (which also sets up a lot of other things like the current passwords (changed on a regular basis), which test environment to use, and settings for each environment for stuff like the test server URL's, database server and name, etc. When developing tests a simple change to the config file lets me run all the tests facing a given browser. or if I want to run in parallel I can have the systems setup with their own customized config file, let them pull the current scripts from source control, and then run them against whatever browser, server etc is configured in the config file.
This is probably dirt simple stuff to any accomplished ruby dev, but it's like magic for any of us new to ruby, and especially for getting hard-coded values OUT of my scripts and into one single place where I can control and change them.

Can Watir interact with Firefox Extensions?

I know that with watir-WebDriver, I can make use of RubyBindings to have the browser load specific profiles or Firefox add-ons when I create a new browser instance. However, can I use Watir to actually use the add-on(s) I open?
The reason I ask is that I am trying to implement a web scraper to navigate to websites and record HTTP interactions. However, since Tamper Data already does the HTTP request/response logging I require, I'd rather use its functionality instead of having to redo it myself.
If this is not possible, I'm wondering if anyone knows a unit tester that will allow me to:
Open a Firefox browser & load Tamper Data
Navigate to specified pages
Click a button on Tamper Data's UI
You can't interact with extensions using bare watir/webdriver as far as i know, need to find a workaround ... Try something like rautomation - https://github.com/jarmo/RAutomation or autoit - http://www.autoitscript.com/site/
This works for me to launch firebug:
Win 7 & XP:
require 'watir-webdriver'
default = Watir::WebDriver::Firefox::Profile.new
If you are admin on your machine it will be the following... otherwise search and provide path:
default.add_extension("/Users/Administrator/AppData/Roaming/Mozilla/Firefox/Profiles/krqve9uc.firebug/extensions/firebug#software.joehewitt.com.xpi")
b = Watir::Browser.new(:firefox, :profile => default)

Resources