How to run Chrome/Firefox with disabled flash plugin from watir script? - watir

How to run Chrome/Firefox with disabled flash plugin from watir script?
If I disable flash in Chrome, next time when I execute watir-webdriver script Flash is enabled again.

For Firefox it is easy. Disable flash in your default profile (the one you normally use) and then start the browser like this:
browser = Watir::Browser.new :firefox, :profile => "default"
More information: http://watirwebdriver.com/firefox/
For Chrome I thought this would do it, but looks like I am doing something wrong, since flash is enabled:
browser = Watir::Browser.new :chrome, :switches => ["--disable-internal-flash"]
More information: http://watirwebdriver.com/chrome/
Workaround is to run this every time you want Flash disabled in Chrome:
browser.goto "about:plugins"
browser.span(:text => "Flash").parent.parent.parent.a(:text => "Disable", :class => "disable-group-link").click

Use the --disable-bundled-ppapi-flash switch. The one above did not work for me.

Related

Selenium with Python hangs on the next line of code after logging into a website

I have been experimenting with automating some tasks with Selenium, and was able to get it to work successfully once, however, when I try and recreate what I did to write it into a class, with both headless and the browser open, I run into an issue where it either hangs after logging in (with the browser open) or the login will fail due to incorrect password when headless (same info that is used when the browser is open and logs in successfully).
When using default settings (Chromedriver), I can navigate to the login page, populate my info, hit the submit button, the webpage acts as it should, but the next line of code I run, no matter what (get_cookies, any kind of find call, get_screenshot_as_file), it will hang up, and timeout if I have the timeout setting applied, if not applied, I have let it run for 5 minutes with nothing happening.
When applying the headless option to the driver (desired functionality), through screenshots of the webpage, I am able to see that it is navigating to the page and populating all of the info properly, however it will always say login failed due to incorrect user/password when submitting, but it is the exact info used in the above when it logged in just fine (could this be cookie related? Not sure what all changes when headless is applied).
I have restarted my computer, updated all packages, restarted the text editor (Atom running Hydrogen), updated Chrome, uninstalled Chrome, did every combination of option I could think of, and I am still unable to recreate my first run where it worked just fine.
Versions:
* Mac OSX Catalina 10.15
* Python: 3.6.8
* Selenium: 3.141.0
* Chrome: 80.0.3987.163
-Current Code EDIT (My text editor is setup to run like a Jupyter notebook for reference, and I listed out all of the options I have attempted, not all are included in each run I have tried)-
LOGIN = os.environ.get['BANKUSER']
PASSWORD = os.environ.get['BANKPASS']
chrme_drvr = '/usr/local/bin/chromedriver'
options = webdriver.ChromeOptions()
options.binary_location = "/Applications/Google
Chrome.app/Contents/MacOS/Google Chrome"
options.add_argument("--window-size=300,150")
options.add_argument("--disable-gpu")
options.add_argument("--disable-extensions")
options.add_argument("--proxy-server='direct://'")
options.add_argument("--proxy-bypass-list=*")
options.add_argument("--start-maximized")
options.add_argument("--headless")
options.add_argument("--webdriver-logfile=webdrive.log")
options.add_argument("--DBUS_SESSION_BUS_ADDRESS=/dev/null ")
service_args = []
service_log_path = './chromedriver.log'
driver = webdriver.Chrome(chrme_drvr,
options=options,
service_args=service_args,
service_log_path=service_log_path)
login_url = 'https://www.schwab.com/public/schwab/nn/login/login.html&lang=en'
driver.get(login_url)
driver.switch_to.frame('loginIframe')
username = driver.find_element_by_id('LoginId')
password = driver.find_element_by_id('Password')
username.send_keys(LOGIN)
password.send_keys(PASSWORD)
driver.find_element_by_id('LoginSubmitBtn').click() #Code works through this line when not in headless mode, will be an invalid login when in headless mode
driver.get_screenshot_as_file("test.png") # This hangs
driver.get_cookies() # This hangs
element = browser.find_element_by_id("accounts_summary") # This hangs
``

Poltergeist JS/Headless Chrome - Switch to offline mode

I am looking to write a test where I can switch between Offline mode and back to Online mode mid way through a cucumber test. I can manually achieve this via Dev Tools in Chrome but is there a way to automate this using Poltergeist JS or Headless Chrome.
I know that page.driver is accessible, infact I use this for setting cookie values in another test
Given(/^I set the "([^"]*)" cookie value to "([^"]*)" for the domain "([^"]*)"$/) do |cookieName,cookieValue,cookieDomain|
if "#{DRIVER}" == "headless_chrome"
page.driver.browser.manage.add_cookie name: cookieName, value: cookieValue, domain: cookieDomain
else
page.driver.set_cookie(cookieName, cookieValue, {:domain => cookieDomain})
end
sleep 1
end
Unless I'm missing something I can't see how to switch between Offline and Online modes. Anyone done or do this in their test setup?
When using Selenium with Chrome as the driver you can use network_conditions=
page.driver.browser.network_conditions = { offline: true }
I don't believe Poltergeist had similar functionality.

Chrome.windows.onCreated in background script wont recognize browsers first opening

I need to call some method on opening browser from background page in my chrome extension. It works for me on my pc and chrome, but on some other pc's and same version chrome not working. Working only if second browser is opened. Somehow that chrome's api dont see first browser is opened.
Chrome version latest (75.0.3770.90)
chrome.windows.onCreated.addListener((window) => {
this.doSomething(window);
});
function doSomething(window) {
console.log('Window with id: ', window.id, 'opened');
}
I expect to console log every windowId which is opened, but i get only from second opened window.

Browser detection problems

I have a website that will be displayed in all browser but I need it to display an alert box on load only in Firefox.
How can I detect the browser and specify that if it is mozilla the alert should pop up?
You could include jQuery in your website and try the following code:
if ($.browser.mozilla) {
alert( "this is mozilla!" ); // popup if the browser is mozilla
}
PS: Bear in mind that all Mozilla derivates are recognised here.

Tests using Webdriver with remote HTMLUnit having trouble logging into gmail

For some reason, after logging into a site like gmail, htmlunit is not working. It is not able to find html elements.
The following is a very simple ruby script that shows the problem, note it assumes that webdriver server is running on the same machine running it:
require 'rubygems'
require 'watir-webdriver'
require 'rspec/expectations'
##
## THE FOLLOWING TWO WAYS WORK
#
#browser = Watir::Browser.new(:remote, :url => "http://127.0.0.1:4444/wd/hub", :desired_capabilities => :firefox)
#browser = Watir::Browser.new(:remote, :url => "http://127.0.0.1:4444/wd/hub", :desired_capabilities => :internet_explorer)
##
## THIS WAY FAILS
##
capabilities = Selenium::WebDriver::Remote::Capabilities.htmlunit(:javascript_enabled => true)
browser = Watir::Browser.new(:remote, :url => "http://127.0.0.1:4444/wd/hub", :desired_capabilities => capabilities)
#Login to gmail
browser.goto "http://gmail.com"
browser.text_field(:id,'Email').set 'roberttestingstuff041'
browser.text_field(:id,'Passwd').set 'k4238chsj55983w'
browser.button(:id,'signIn').click
sleep 5.0 #sleep shouldnt be needed, but just to be sure we are waiting long enough for log in to complete
frame = browser.frame(:id,'canvas_frame')
#It fails on the next line when using htmlunit
frame.link(:text, 'Sign out').exist?.should == true
frame.link(:text, 'Sign out').visible?.should == true
frame.div(:id, 'guser').exist?.should == true
frame.div(:text,'Compose mail').exist?.should == true
Note that if I create the browser object using firefox or IE, this simple test works.
It seems to get hung up on the redirects that happen during the login process. The site I am really trying to test follows a very similar pattern, so I set up this simplified example with gmail which seems to show the same problem.
Can anyone help turn this into a passing test? Note that I can get a similar test to work using Celerity, which is also based on HTMLUnit, so I believe there should be some way to make this work?
This is the error that shows in the webdriver server, clearly showing it failing to find the attribute:
12:31:16.321 INFO - WebDriver remote server: INFO: Executing: [find element: By.xpath: .//a[normalize-space()='Sign out'
] at URL: /session/1297704604365/element)
12:31:17.996 WARN - WebDriver remote server: WARN:
org.openqa.selenium.NoSuchElementException: Unable to locate a node using .//a[normalize-space()='Sign out']
System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.6.0_21'
Driver info: driver.version: EventFiringWebDriver
at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementByXPath(HtmlUnitDriver.java:699)
at org.openqa.selenium.By$6.findElement(By.java:205)
at org.openqa.selenium.htmlunit.HtmlUnitDriver$4.call(HtmlUnitDriver.java:1133)
I'm thinking that Gmail is detecting our headless browser (in this case HtmlUnit with Rhino) does not support JavaScript.
If you look at the return from Gmail after
browser.button(:id,'signIn').click
You will see that we are on a "JavaScript must be enabled" page
p browser.text
"<style> #loading {display:none} </style> <font face=arial>JavaScript must be enabled in order for you to use Gmail in standard view. However, it seems JavaScript is either disabled or not supported by your browser. To use standard view, enable JavaScript by changing your browser options, then try again. <p>To use Gmail's basic HTML view, which does not require JavaScript, click here.</p></font><p><font face=arial>If you want to view Gmail on a mobile phone or similar device click here.</font></p> \n Loading tim.koops#gmail.com\342\200\246 \n\n\n\n Loading standard view | Load basic HTML (for slow connections)"
In this case we could go to the HTML only version of Gmail to get you through, but unfortunately I think we're stuck for now. I will pass on this failing test case to the webdriver developers for review.
Also, hope those aren't your real Gmail credentials!
you need to enable javascript for the HtmlUnit driver.
It's disabled by default.
Use the Capability HTMLUNITWITHJS as opposed to the default HTMLUNIT.
I'm using the names in the Python bindings, but I'm sure Ruby is using something similar.

Resources