How to enable adblock with seleniumlibrary in robotframework - selenium

I would like to enable uBlock Origin plugin as ads on speedtest.net during robot framework test execution as ads completly jeopardize test result.
So far I have created a Python script to create a profile with extension
from selenium import webdriver
class WebDriverProfile:
def create_profile_with_adblock(self, path):
fp =webdriver.FirefoxProfile()
fp.add_extension(extension='d:/pathtoextension/ublock_origin-1.24.0-an+fx.xpi')
fp.set_preference("browser.download.folderList",2)
fp.set_preference("browser.download.manager.showWhenStarting",False)
fp.set_preference("browser.download.dir",path)
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", 'application/csv')
fp.update_preferences()
return fp.path
Also the following robot script to call the function:
*** settings ***
Library Selenium2Library
library Process
library Dialogs
Library WebDriverProfile.py
*** Variables ***
${URL} https://speedtest.net
*** Test Cases ***
Test with speedtest.net
${FF_PROFILE}= Create Profile With Adblock ${TEMPDIR}\\testdirff
Open Browser ${URL} browser=ff ff_profile_dir=${FF_PROFILE}
Execute Manual Step Is adblocker enabled?
The browser will open, but adblocker won't be there.
Anyone could help on this please?

Create webdriver instead of firefox profile and install add-on.
Python extension:
from selenium import webdriver
class WebDriverProfile:
def create_web_driver_with_addons(self):
extension_location='d:\\path_to_adblock\\ublock_origin-1.24.0-an+fx.xpi'
browser = webdriver.Firefox()
browser.install_addon(extension_location, temporary=True)
browser.get('http://speedtest.net')
Robot script:
*** settings ***
Library Selenium2Library
library Process
library Dialogs
Library WebDriverProfile.py
*** Variables ***
${URL} https://speedtest.net
*** Test Cases ***
Test with speedtest.net
create web driver with addons
Execute Manual Step Is adblocker enabled?

You need to create a new browser profile with adblock manually installed firest.
Now, Call the above respective profile the by anyone of the below commands.
Open Browser http://example.com Firefox ff_profile_dir=/path/to/profile # Using profile from disk
Open Browser http://example.com Firefox ff_profile_dir=${FirefoxProfile_instance} # Using instance of FirefoxProfile
Open Browser http://example.com Firefox ff_profile_dir=set_preference("key", "value");set_preference("other", "setting") # Defining profile using FirefoxProfile mehtods~~~

Related

TimeoutException: Message: Timed out waiting for page to load

I want to Open browser in Edge with IE mode.
My environment: IE7, windows 11, Python 3.10.4, Edge version 108.0.1462.46
And I follow the required configuration from below:
https://www.selenium.dev/documentation/ie_driver_server/
I made the same value for Enhanced Protected Mode by setting the REG_DWORD 2500 value to 0 in Zones 0,1,2,3,4:
Registry Editer path: Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Zones
I add the IEDriverServer to my PATH
Is there any wrong steps about my configuration?
Below is my code:
*** Settings ***
Library SeleniumLibrary
*** Variables ***
${IEDriver} D:\\IEDriver\\64bits\\IEDriverServer.exe
*** Test Cases ***
Example Test
Open Browser https://www.google.com.tw/ ie executable_path=${IEDriver} options=ignore_zoom_level=True; attach_to_edge_chrome=True; edge_executable_path="C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe"
Maximize Browser Window
After I executing my code, I can open google in Edge with IE mode, but after that web page stuck and always get thie error message:
TimeoutException: Message: Timed out waiting for page to load.
And I found something interesting: I use python and selenium and below is my code:
ieOptions = webdriver.IeOptions()
ieOptions.add_additional_option("ie.edgechromium", True)
ieOptions.add_additional_option("ie.edgepath",'C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe')
driver = webdriver.Ie(options=ieOptions)
driver.maximize_window()
driver.get('https://google.com')
driver.quit()
If I remove this line "driver.get('https://google.com')" and my code runs perfectly.
But If I add it back, the page will go to goole and stuck there (It means that this code will not do driver.quit()
This option may work for you in Windows 11 IE mode automation. Check it out:
ignore_protected_mode_settings = True
UPDATE
Or you can simply try setting the page load timeout according to your requirement:
*** Variables ***
${orig timeout} 15 seconds
*** Test Cases ***
Example Test
Set Selenium Timeout ${orig timeout}
If the element you'd like to interact with has been fully loaded, you can skip the page loading and continue. A native Python example:
driver.set_page_load_timeout(10)
try:
driver.get('https://google.com')
except TimeoutException:
print
'!!!!!!time out after 10 seconds!!!!!!'
driver.execute_script("window.stop()")
In Robot Framework:
*** Test Cases ***
Example Test
Set Selenium Timeout ${orig timeout}
TRY
Open Browser https://www.google.com ie options=ignore_zoom_level=True; attach_to_edge_chrome=True; edge_executable_path="C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe"
EXCEPT '!!!!!!time out after 10 seconds!!!!!!'
Execute Javascript window.stop()
END
Maximize Browser Window

Is there a way to add -Dchrome.switches chrome properties to serenity.properties or serenity.conf files?

I tried to add the below in the serenity.conf file to always load the chrome browser with these options but it fails to load the browser. When I pass in the below options via command line like so "gradle test -Dchrome.switches="--no-sandbox,--ignore-certificate-errors,--homepage=about:blank,--no-first-run" the browser starts successfully.
"-Dchrome.switches="--no-sandbox,--ignore-certificate-errors,--homepage=about:blank,--no-first-run"
Is there a way to always open chrome browser without having to pass this via command line or have the chrome driver as part of the framework?
serenity.conf
#
# WebDriver configuration
#
webdriver {
driver = chrome
autodownload = true
}
#headless.mode = true
serenity.test.root = java
#
# Chrome options can be defined using the chrome.switches property
#
chrome.switches = """--start-maximized;--test-type;--no-sandbox;--ignore-certificate-errors;
--disable-popup-blocking;--disable-default-apps;--disable-extensions-file-access-check;
--disable-web-security;--incognito;--disable-infobars,--disable-gpu,--homepage=about:blank,--no-first-run"""
Thanks!
Try
chrome {
switches = "--start-maximized;--enable-automation;--no-sandbox;--disable-popup-blocking;--disable-default-apps;--disable-infobars;--disable-gpu;--disable-extensions;"
preferences {
download: "{prompt_for_download: false,directory_upgrade: true,default_directory:'${user.dir}/downloaded-files'}"
}
}
Thank you!
I have switched to
#Managed
WebDriver driver;
https://serenity-bdd.github.io/theserenitybook/latest/web-testing-in-serenity.html#_a_simple_selenium_web_test
Below is the information on manned drivers in serenity.
Serenity reduces the amount of code you need to write and maintain when you write web tests. For example, it takes care of creating WebDriver instances, and of opening and closing the browser for you. The following is a very simple Selenium web test using Serenity:

I'm getting Non-existing setting 'Library Selenium Library'

In my settings
*** Settings ***
Library SeleniumLibrary
is written this way, but somehow the robotframework adds a space in between when I try to run the test and I get this ERROR "Non-existing setting 'Library Selenium Library'". Any ideas why? everything should be the latest version. I have tried also with Selenium2Library.
pip install --upgrade robotframework-selenium2library
make sure you have installed it as above and use it as :
*** Settings ***
Library SeleniumLibrary
*** Test Cases ***
Input Text
Open Browser https://www.google.com chrome

Roboframework - SeleniumLibrary - set specific user profile path for Chrome

my goal is to create a Roboframework script using SeleniumLibrary (or Selenium2Library), selecting a specific Profile Path for Chrome (I need to reuse profile).
This is the code I'm using:
*** Settings ***
Library Selenium2Library
Library OperatingSystem
*** Test Cases ***
Test Profile
${options}= Evaluate sys.modules['selenium.webdriver'].ChromeOptions() sys, selenium.webdriver
${options.add_argument}= Set Variable --user-data-dir=/tmp/.org.chromium.Chromium.Test
Create WebDriver Chrome chrome_options=${options}
Go To chrome://version
Wait Until Page Contains JavaScript
Capture Page Screenshot
If I run the script I don't receive any error, but unfortunately the chrome path is not effected by the desired setting (all the time is creating root+random path):
In the past I've successfully achieve this using Python code and Selenium Grid:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--disable-notifications')
chrome_options.add_argument('--user-data-dir=/tmp/.com.google.Chrome.Test')
browser = webdriver.Remote(command_executor='http://192.168.99.100:4444/wd/hub',desired_capabilities = chrome_options.to_capabilities())
Any help/comment is appreciated.
Many Thanks
Try something like below
*** Settings ***
Library Selenium2Library
Library OperatingSystem
*** Test Cases ***
Test Profile
${options}= Evaluate sys.modules['selenium.webdriver'].ChromeOptions() sys, selenium.webdriver
Call Method ${options} add_argument --user-data-dir=/tmp/.org.chromium.Chromium.Test
Create WebDriver Chrome chrome_options=${options}
Go To chrome://version
Wait Until Page Contains JavaScript
Capture Page Screenshot
If that doesn't work try without the --, i.e.
Try something like below
*** Settings ***
Library Selenium2Library
Library OperatingSystem
*** Test Cases ***
Test Profile
${options}= Evaluate sys.modules['selenium.webdriver'].ChromeOptions() sys, selenium.webdriver
Call Method ${options} add_argument user-data-dir=/tmp/.org.chromium.Chromium.Test
Create WebDriver Chrome chrome_options=${options}
Go To chrome://version
Wait Until Page Contains JavaScript
Capture Page Screenshot
I made it work with this syntax:
${chrome_options}= Evaluate sys.modules['selenium.webdriver'].ChromeOptions() sys
Call Method ${chrome options} add_argument user-data-dir\=/tmp/.org.chromium.Chromium.Test
${dc} Evaluate sys.modules['selenium.webdriver'].DesiredCapabilities.CHROME sys, selenium.webdriver
${Options}= Call Method ${ChromeOptions} to_capabilities
Create WebDriver Chrome chrome_options=${chrome_options} desired_capabilities=${dc}
Go To chrome://version
Wait Until Page Contains JavaScript
Capture Page Screenshot

Selenium: FirefoxProfile exception Can't load the profile, No Browser is open on OSX

I've installed my selenium and after run pybot myTest, there is error about the profile of firefox.
It's exactly the same issue as this topic. Unfortunately I did run pip install -U selenium but
it still return the same error, my firefox version is 48.0.1. Here is my errors
<msg timestamp="20160824 16:01:19.947" level="INFO">Opening browser 'firefox' to base url 'https://www.google.com'</msg>
<kw name="Capture Page Screenshot" library="Selenium2Library">
<doc>Takes a screenshot of the current page and embeds it into the log.</doc>
<msg timestamp="20160824 16:01:50.167" level="FAIL">No browser is open</msg>
<status status="FAIL" endtime="20160824 16:01:50.167" starttime="20160824 16:01:50.157"></status>
</kw>
<msg timestamp="20160824 16:01:50.167" level="WARN">Keyword 'Capture Page Screenshot' could not be run on failure: No browser is open</msg>
<msg timestamp="20160824 16:01:50.168" level="FAIL">WebDriverException: Message: Can't load the profile. Profile Dir: /var/folders/b9/th8_45d55_16rlk9wsdfp8300000gn/T/tmppUXVZI/webdriver-py-profilecopy If you specified a log_file in the FirefoxBinary constructor, check it for details.
</msg>
Any help or suggestion would be very helpful, thank you in advance.
What version of Selenium are you running? They have decided to discontinue supporting Firefox and introduced Marionette. You can either use Marionette or upgrade your Selenium to 2.53.1, i believe that this release was dedicated for the Firefox support. Goodluck!