Configure Ruby/Watir to ignore browser console errors - ruby

This question relates to automation testing in chrome using Rubymine, Watir and Selenium.
We are getting this console error sometimes which is affecting automation tests via ruby/watir. When the error is present the page does not render and we get a blank page and of course the scenario will time out and fail. When the error is not present the page renders correctly and the scenario will complete.
main.8a29c926346b293d1508.js:1 Uncaught ReferenceError: hj is not defined
at main.8a29c926346b293d1508.js:1
at Object.a2P5 (main.8a29c926346b293d1508.js:1)
at u (runtime.d3b5d5ccc892eb7d0f13.js:1)
at Object.zUnb (main.8a29c926346b293d1508.js:1)
at u (runtime.d3b5d5ccc892eb7d0f13.js:1)
at Object.2 (main.8a29c926346b293d1508.js:1)
at u (runtime.d3b5d5ccc892eb7d0f13.js:1)
at t (runtime.d3b5d5ccc892eb7d0f13.js:1)
at Array.r [as push] (runtime.d3b5d5ccc892eb7d0f13.js:1)
at main.8a29c926346b293d1508.js:1
Does anyone know if there is a switch we can use to ignore console errors and continue to load the page, these are my current options -
options = Selenium::WebDriver::Chrome::Options.new(
options: {"excludeSwitches" => ["enable-automation", "load-extension"]},
args: ["ignore-certificate-errors", "disable-infobars"]
)
options.add_preference(:credentials_enable_service, false)
options.add_preference(:password_manager_enabled, false)
driver = Selenium::WebDriver.for :chrome, options: options

Related

Appium test not going any further after Splash Screen

I'm hoping someone can help.
I am testing the Three App where correct behavior should be once the app launches, a splash screen is shown for a few seconds, then a login page is presented.
I'm trying to automate this using appium but once the automated test opens the splash screen, the test ends.
I am shown this error message:
NoSuchElementError: An element could not be located on the page using the given search parameters. at AndroidUiautomator2Driver.findElOrEls (C:\Users\zminhas\AppData\Local\Programs\Appium\resources\app\node_modules\appium\node_modules\appium-android-driver\lib\commands\find.js:75:11)
Here is my env.rb code:
require "appium_lib"
require "pry"
opts = {
caps: {
deviceName: :Anyname,
platformName: :Android,
app: 'C:\Users\zminhas\Desktop\three_app_automation\features\support\biometrics.apk',
appPackage: "com.hutchison3g.planet3",
appActivity: ".SplashScreenActivity",
noReset: true
},
appium_lib: {
#wait_timeout: 30
}
}
Appium::Driver.new(opts, true).start_driver
Appium.promote_appium_methods Object
And my hooks.rb code:
Before do
$driver.start_driver
end
After do
$driver.driver_quit
end
Here is the full results screen:
$ cucumber
*** WARNING: You must use ANSICON 1.31 or higher (https://github.com/adoxa/ansicon/) to get coloured output on Windows
Feature: All external URLs work as expected within the More Tab
2019-11-11 12:08:13 WARN Selenium [DEPRECATION] Selenium::WebDriver::Error::TimeOutError is deprecated. Use Selenium::WebDriver::Error::TimeoutError (ensure the driver supports W3C WebDriver specification) instead.
Scenario: test # features/more_tab.feature:3
Given I see the enter number screen # features/step_definitions/more_tab_steps.rb:1
An element could not be located on the page using the given search parameters. (Selenium::WebDriver::Error::NoSuchElementError)
NoSuchElementError: An element could not be located on the page using the given search parameters.
at AndroidUiautomator2Driver.findElOrEls (C:\Users\zminhas\AppData\Local\Programs\Appium\resources\app\node_modules\appium\node_modules\appium-android-driver\lib\commands\find.js:75:11)
./features/step_definitions/more_tab_steps.rb:2:in `"I see the enter number screen"'
features/more_tab.feature:4:in `Given I see the enter number screen'
Failing Scenarios:
cucumber features/more_tab.feature:3 # Scenario: test
1 scenario (1 failed)
1 step (1 failed)
0m14.888s
Thank you for your time
Try with following capabilities
deviceName: :Anyname,
platformName: :Android,
automation_name: :uiautomator2,
app: 'C:\Users\zminhas\Desktop\three_app_automation\features\support\biometrics.apk',
appPackage: "com.hutchison3g.planet3",
appActivity: ".SplashScreenActivity",
noReset: true
I was having the same issue when an app was given to me in an interview. but after 2 days I was able to find the solution to the issue related to Splash Screen being stuck and not going to the second screen using script/appium but manually it was working fine.
The issue is something related to INTENT Activity
Solution:
I added some extra line of code to script/capabilities to appium and it worked fine.
Try using these lines in capabilities for the apps that have splash screen issue.
capabilities.setCapability("androidCoverage","com.my.Pkg/com.my.Pkg.instrumentation.MyInstrumentation");
capabilities.setCapability("androidCoverageEndIntent","com.example.pkg.END_EMMA");
capabilities.setCapability("intentAction","android.intent.action.MAIN");
capabilities.setCapability("intentCategory","android.intent.category.LAUNCHER");
capabilities.setCapability("intentFlags","0x10200000");
Please Let me know if it worked for you also?

Browser.back is not working

Using watir, I've written scripts to check multiple links are being directed to the right page as below.
Links= ["Link", "Link1"]
Links.each do |LinkValue|
#browser.link(:text => LinkValue).wait_until_present.click
fail unless #browser.text.include?(LinkValue)
#browser.back
end
What I am trying is:
maintaining Linktext in an array
iterating with each linktext
verify
navigate to the previous page to start verifying with next linktext.
But the script is not working. It is not executing after first value and also not navigating back.
The following scrip working for me
require 'watir'
browser = Watir::Browser.new(:firefox) # :chrome also work
browser.goto 'https://www.google.com/'
browser.link(text: 'Gmail').wait_until_present.click
sleep(10)
browser.back
sleep(10)
You are calling Kernel::Fail, which will raise an exception if the condition isn't satisfied.
In this case, it looks like you are expecting that the destination page will contain the same link text that was clicked on the originating page. If that's not true, then the script will raise an exception and terminate.
Here's a contrived "working" example (which only "works" because the link text exists on both originating and destination pages):
require 'watir'
b = Watir::Browser.new :chrome
b.goto "http://www.iana.org/domains/reserved"
links = ["Overview", "Root Zone Management"]
links.each do |link|
b.link(:text => link).click
fail unless b.text.include? link
b.back
end
b.close
Some observations:
I wouldn't use fail here. You should investigate a testing framework like Minitest or rspec, which have assertion methods for validating application behavior.
In ruby, variables (and methods and symbols) should be in snake_case.

Error while switching windows using Selenium Webdriver

I've written a Ruby script to automate some user operations using IE.
I'm using Selenium Web Driver for IE. Below is my code.
require 'selenium-webdriver'
browser = Selenium::WebDriver.for :ie
first_window = browser.window_handle
browser.switch_to.frame(browser.find_element(:id=> 'outerFrame'))
browser.switch_to.frame(browser.find_element(:id=> 'innerFrame'))
table_rows = browser.find_element(:id=> 'AllItems').find_element(:tag_name=> 'table').find_elements(:tag_name=> 'tr')
count_cell = table_rows.at(table_rows.length-1).find_elements(:tag_name=> 'td').at(1).find_element(:tag_name=> 'a')
count_cell.click
sleep(5)
all_windows = browser.window_handles
new_window = browser.window_handles.last
browser.switch_to.window(new_window)
btn = browser.find_element(:id=> 'btn_export')
btn.click
At one point, after clicking a button, a new page is opened. Now, when I try to switch to the new window, I get the following error.
C:/Ruby21/lib/ruby/2.1.0/net/http.rb:879:in 'initialize': No
connection could be made because the target machine actively refused
it. - connect(2) for "127.0.0.1" port 5555 (Errno::ECONNREFUSED)
from C:/Ruby21/lib/ruby/2.1.0/net/http.rb:879:in 'open'
from C:/Ruby21/lib/ruby/2.1.0/net/http.rb:879:in 'block in connect'
from C:/Ruby21/lib/ruby/2.1.0/timeout.rb:75:in 'timeout'
from C:/Ruby21/lib/ruby/2.1.0/net/http.rb:878:in 'connect'
from C:/Ruby21/lib/ruby/2.1.0/net/http.rb:863:in 'do_start'
from C:/Ruby21/lib/ruby/2.1.0/net/http.rb:852:in 'start'
from C:/Ruby21/lib/ruby/2.1.0/net/http.rb:1375:in 'request'
from C:/Ruby21/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.48.1/lib/selenium/webdriver/remote/http/default.rb:107:in 'response_for'
from C:/Ruby21/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.48.1/lib/selenium/webdriver/remote/http/default.rb:58:in
'request'
from C:/Ruby21/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.48.1/lib/selenium/webdriver/remote/http/common.rb:59:in
'call'
from C:/Ruby21/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.48.1/lib/selenium/webdriver/remote/bridge.rb:664:in
'raw_execute'
from C:/Ruby21/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.48.1/lib/selenium/webdriver/remote/bridge.rb:642:in
'execute'
from C:/Ruby21/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.48.1/lib/selenium/webdriver/remote/bridge.rb:216:in
'switchToWindow'
from C:/Ruby21/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.48.1/lib/selenium/webdriver/common/target_locator.rb:81:in
'window'
from script.rb:18:in ''
I tried my bit to resolve the error, by modifying firewall and added rules for port 5555. Even then issue persists. I found some already exsting questions on this, but most of them are related to Java or .Net. How can I resolve this error? Can anyone point me in the right direction?
I am not a ruby expert, but I have seen similar problems happening. I have a Python-ic solution. All you need to do is Ruby-fy the code. https://gist.github.com/ab9-er/08c3ce7d2d5cdfa94bc7
def change_window(browser):
"""
Simple window switcher without the need of playing with ids.
#param browser: Current browser instance
"""
curr = browser.current_window_handle
all_handles = browser.window_handles
for handle in list(set([curr]) - set(all_handles)):
return browser.switch_to_window(handle)
Try switching back to the top level browsing context before switching to a new window.
browser.switch_to.default_content
current_window = browser.window_handle
new_window = browser.window_handles.find { |win| win != current_window }
browser.switch_to.window(new_window)
It should do this implicitly, so if it isn't, it's likely a bug. If this works, please let me know so I can see if we need to file a bug report.

I'm using the Selenium Webdriver gem to try to click on the facebook chat bar, sometimes it work and sometimes it doesn't

I'm using the Selenium Webdriver gem to try to click on the facebook chat bar, sometimes it work and sometimes it doesn't. When it does not work it returns the Selenium Element not visible error, but it clearly is visible. I'm not sure what's wrong with my code.
require 'selenium-webdriver'
driver = Selenium::WebDriver.for :chrome # instantiates a google chrome session
driver.navigate.to 'https://www.facebook.com/' # takes you to facebook.com
emailBar = driver.find_element(:id,"email") #finds email input bar
passwordBar = driver.find_element(:id,"pass") #find password input bar
loginButton = driver.find_element(:id,"u_0_n") #finds login button
emailBar.send_keys "austinspreadsheet#gmail.com" # puts in the email
passwordBar.send_keys "YOURPASSWORD" # puts in the password
loginButton.click # clicks the login button
#THIS IS THE CODE BLOCK THAT DOES NOT WORK
links = driver.find_elements(:class,"fbNubButton") # finds the chat bar
#driver.manage.timeouts.page_load = 10
links[0].click # opens the chat bar
links[1].click # NOTE that sometime it clicks and sometimes it doesn't but if you click both chat box classes it usually works, so the error is ok
I have tried not clicking both chat links and it works less when I do that.
I am using Selenium with Python. In case like yours the issue is related to waiting until all the elements in the page are full loaded.
The basic behavior of Selenium offers you Explicit and Implicit Waits. So basicly you can force the system to wait a default number of second or wait until an element is loaded.
From Selenium documentation (http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp)
Explicit wait
An explicit waits is code you define to wait for a certain condition to occur before proceeding further in the code. The worst case of this is Thread.sleep(), which sets the condition to an exact time period to wait. There are some convenience methods provided that help you write code that will wait only as long as required. WebDriverWait in combination with ExpectedCondition is one way this can be accomplished.
require 'rubygems' # not required for ruby 1.9 or if you installed without gem
require 'selenium-webdriver'
driver = Selenium::WebDriver.for :firefox
driver.get "http://somedomain/url_that_delays_loading"
wait = Selenium::WebDriver::Wait.new(:timeout => 10) # seconds
begin
element = wait.until { driver.find_element(:id => "some-dynamic-element") }
ensure
driver.quit
end
Implicit wait
An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.
require 'rubygems' # not required for ruby 1.9 or if you installed without gem
require 'selenium-webdriver'
driver = Selenium::WebDriver.for :firefox
driver.manage.timeouts.implicit_wait = 10 # seconds
driver.get "http://somedomain/url_that_delays_loading"
element = driver.find_element(:id => "some-dynamic-element")
The answer that aberna gives you on this thread has a lot of great information but it isn't going to solve your issue. If you use the Explicit wait method that aberna suggests, you also probably need to make sure the element is visible. Using .findElements on its own doesn't guarantee clickability/visibility . You could try to use expectedconditions .visibilityOfElementLocated which will also check for visibility as well as presence.
Or, alternatively, you can check for presence of the element on the DOM using .findElement but then use the expectedconditions.visibilityOf to check for the visibility part of it.
I am using sleep(5) before run main logic
I was facing the same issue. Solution that worked for me was to maximise my browser window. This solved many of failing specs.
Capybara.current_session.driver.browser.manage.window.maximize

How to disable JavaScript in Capybara

I'm using Capybara to fill in a form and download the results.
It's a bit slow when filling in the form, and I want to check if JavaScript is the culprit.
How do I turn off JavaScript?
The Ruby code was something similar to, but not the same as, the following (the following won't reproduce the error message, but it is somewhat slow).
require "capybara"
url = "http://www.hiv.lanl.gov/content/sequence/HIGHLIGHT/highlighter.html"
fasta_text = [">seq1", "gattaca" * 1000, ">seq2", "aattaca" * 1000].join("\n")
session = Capybara::Session.new(:selenium)
# Code similar to this was run several times
session.visit(url)
session.fill_in('sample', :with => fasta_text)
session.click_on('Submit')
And the error I was getting (with my real code, but not the code I have above) was
Warning: Unresponsive script
A script on this page may be busy, or it may have stopped responding.
You can stop the script now, open the script in the debugger, or let
the script continue.
Script: chrome://browser/content/tabbrowser.xml:2884
I wasn't running Capybara as part of a test or as part of a spec.
To confirm that the code I wrote currently has JavaScript enabled (which is something I want to disable), doing
url = "http://www.isjavascriptenabled.com"
session = Capybara::Session.new(:selenium)
session.visit(url)
indicates that JavaScript is enabled.
Capybara only uses JavaScript if you've specified a javascript_browser:
Capybara.javascript_driver = :poltergeist
And if you've specified js: true as metadata in your spec:
context "this is a test", js: true do
Check for both of those things. If they're not there and the test is not running in a browser or using Poltergeist, then it's probably not using JavaScript.

Resources