Selenium IDE - local (non-webhosted) file access for testing? - file

My Problem
When Selenium IDE is used to run a test, the "Base URL" is always accessed via http on some web server.
I need to know if (and how) its possible to get Selenium IDE to run a test on a locally stored file. When opening a file in Firefox, the path looks something like this in the Address Bar: file:///C:/Documents and Settings/username/My Documents/somehtml.html
However, Selenium gives a nasty error when I put this path in the Selenium IDE's Base URL field for my test (See bottom of my post).
What I tried
I run Windows XP and have tried switching the file path slashes to backslashes while keeping the protocol (file:///) slashes as is. I tried replacing spaces with %20 too.
I also considered whether my Firefox proxy settings might be causing an issue, but it isn't because if i open the file in Firefox it does so without issue.
The solution I'm working on is to avoid Selenium WebDriver at this phase. I know it is better on a technical but there are practical reasons why I can't use that at this point. Selenium IDE must be used for now.
Error
Error thrown when attempting to open local file:
[error] Unexpected Exception: name -> NS_ERROR_FAILURE, QueryInterface
-> function QueryInterface() { [native code] }, message -> Component returned failure code: 0x80004005 (NS_ERROR_FAILURE)
[nsIDOMLocation.href], result -> 2147500037, filename ->
chrome://selenium-ide/content/selenium-core/scripts/selenium-browserbot.js,
lineNumber -> 686, columnNumber -> 0, location -> JS frame ::
chrome://selenium-ide/content/selenium-core/scripts/selenium-browserbot.js
:: BrowserBot.prototype.setOpenLocation :: line 686, inner -> null,
data -> null, initialize -> function initialize() { [native code] }

open about:config then change security.fileuri.strict_origin_policy to false. enjoy

One solution is to use the open command as the first line in your test, and set the local path as the target. This is fine for a handful of tests, but may not be sufficient for larger test suites.
command: open
target: file///c:/path/to/your/file.html

I had the same issue. Just solved it. Here is how:
Remove the base url.
Enter open or openWindow in command
Enter the file url in target e.g. file:///C:/temp/junk/SomeSiteAutomation/ABC.htm
Worked for me on FF 26 and Selenium 2.5

Open Selenium IDE - chrome extension
As soon as we open it will ask for project name and project url - just cancel it
Go to selenium IDE
In selenium IDE - in "command" text box select "open"
in "target" text box enter the path to sample html file as below
"file:///D:/index2.html"
Then this will show up in the command, target and value columns of the selenium ide.
Now just run the test. This will open up the local html file

I faced the same problem. I'm using Ubuntu and apache, but on Windows you can do the same.
move your index.html to /var/www/html (localhost points per default to this folder)
use http://127.0.0.1 as base_url in selenium ide

Right click on the Selenium IDE icon in the upper right of the browser window. Select "Manage Extensions" from the context menu. Toggle on "Allow access to file URLs" in the settings dialog.
This works in Chrome/Windows 10 and Version 3.17.0 of Selenium IDE.

Related

Selenium4 and Snap install of Firefox with Geckodriver_v31 times out

I recently upgraded lubuntu 22.04 and it wanted a few things to be installed from the snap repository. Firefox was one of them. Currently I'm using Selenium 4.1.3, Python 3.10 and Firefox 99.0.1 with latest geckodriver V31.0
I've been using this python3 code for my testing for some time but now it completely fails to start.
First of it failed to find a profile, so I forced something in there:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
options = Options()
options.add_argument("-profile /path2temp/") # create profile
options.set_preference("browser.download.folderList", 2)
options.set_preference("browser.download.manager.showWhenStarting",
False)
options.set_preference("browser.download.dir", "./data_export")
options.set_preference(
"browser.helperApps.neverAsk.saveToDisk",
"application/vnd.google-earth.kml+xml,application/x-zip-compressed,application/gpx+xml,text/csv"
)
options.set_preference("devtools.debugger.remote-enabled", True)
options.set_preference("devtools.debugger.prompt-connection", False)
browser = webdriver.Firefox(options=options, executable_path=r"/usr/bin/geckodriver")
url = 'https://cnn.com'
browser.get(url)
If firefox is already open, it fails to communicate with it. Normally in the past it would just open a new tab and start working. But now I get this error:
Firefox is already running, but is not responding. To use Firefox, you
must first close the existing Firefox process, restart your device, or
use a different profile.
If I let it initiate the application, it then times out after a lot of time with the following error (note, the /path2temp/ is a real path to a directory where it has permissions).
1651528082918 geckodriver
INFO Listening on 127.0.0.1:54985 1651528083062 mozrunner::runner
INFO Running command: "/snap/bin/firefox" "--marionette" "-profile /path2temp/" "--remote-debugging-port" "47927" "-- remote-allow-hosts" "localhost" "-no-remote"
ATTENTION: default value of option mesa_glthread overridden by environment.
ATTENTION: default value of option mesa_glthread overridden by environment.
ATTENTION: default value of option mesa_glthread overridden by environment.
ATTENTION: default value of option mesa_glthread overridden by environment.
DevTools listening on ws://localhost:47927/devtools/browser/19a59834-6a4b-4d75-902c-06c36704d50e
Exiting due to channel error.
Exiting due to channel error.
Exiting due to channel error.
Exiting due to channel error.
Exiting due to channel error.
Any ideas of what I could do to fix this problem?
Edit: I was able to at least get it to work when it initiates firefox by passing it to the current users profile located in the snap file structure /home/username/snap/firefox/common/.mozilla/firefox/wnrrbapq.default-release
But it's not an ideal behavior as I have to close the browser every time for testing.
Snap version of Firefox cannot write to /tmp location.
Recommended workaround is to set TMPDIR environment variable to location that both geckodriver and firefox can write to e.g. $HOME/tmp.
More info in geckdriver 0.31.0 relaese notes and related github issue.
One solution is to not set up a profile at all and instead define the file paths for both firefox and geckodriver.
Since Snap likes to do consistent updates, I found it helpful to make my file paths dynamic. Here is some sample code:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.service import Service
from subprocess import getoutput
options = Options()
options.binary_location = getoutput("find /snap/firefox -name firefox").split("\n")[-1]
driver = webdriver.Firefox(service =
Service(executable_path = getoutput("find /snap/firefox -name geckodriver").split("\n")[-1]),
options = options)
url = 'https://cnn.com'
driver.get(url)
You can use getoutput to get a list of files that within /snap/firefox in 1 long string. Splitting them based on \n and getting the [-1] (last) record gives you the latest version of that file.
Its possible that you don't want to always latest version so you could always just pick the first record on the list if you needed consistency. Though it is nice that Snap will update both geckodriver and Firefox together so they should always work together.

Running AngularJS Protractor with IE11

I am having trouble running AngularJS Protractor through Internet Explorer 11 on Windows 7. It works fine if I use Google Chrome, but with IE11, I get the following error message:
WebDriverError: The path to the driver executable must be set by the webdriver.ie.driver system property: for more information, see https://github.com/SeleniumHQ/selenium/wiki/InternetExplorerDriver.
The latest version can be downloaded from http://selenium-release.storage.googleapis.com/index.html
Within the protractor directory is a sub-directory called selenium, and there is an IEDriverServer file. Furthermore, I have two environment variables set. In my user variables, the variable PATH has a value of C:\Users\user\AppData\Roaming\npm\node_modules\protractor\selenium\
Additionally, in my system variables, the variable "Path" is also set to C:\Users\user\AppData\Roaming\npm\node_modules\protractor\selenium\, but it will still not recognize the IE driver server, even though it DOES recognize the Chrome driver server.
Does anyone have any additional information on what I might be missing or doing wrong? Thank you very much.

Launch chrome browser

I am trying to launch chrome browser (version 26.0) using webdriver.
I am getting the following error message.
Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see http://code.google.com/p/selenium/wiki/ChromeDriver.
at com.google.common.base.Preconditions.checkState(Preconditions.java:176)
at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:105)
at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:69)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:107)
at googleSearch.main(googleSearch.java:13)
Code I used:
driver = new ChromeDriver();
driver.navigate().to("http://www.google.com/");
I use mac 10.8.2.
For this to work, you need to:
Install Chrome
Install Chrome Web Driver
Make sure you have the chrome web driver in you path, for example on Windows something pointing to chromedriver2_win32_0.8. You can put that in your path by either: (a) Modifying your windows path environment variable, or; (b) adding the following to your java command line options:
-Dwebdriver.chrome.driver='/path/to/driver'
In case of using selenium grid
-Dwebdriver.chrome.driver='/path/to/driver'
has to be added while creating a node from command line.
1) In case of using selenium without GRID:
System.setProperty("webdriver.chrome.driver","/absolute/path/to/chromedriver");
driver = new ChromeDriver();
does the job.
2) In case of using selenium with GRID:
System.setProperty("webdriver.chrome.driver","/absolute/path/to/chromedriver");
driver = new ChromeDriver();
And from command line, while creating a node for chrome browser one needs to pass
-Dwebdriver.chrome.driver='/absolute/path/to/chromedriver'
The above two changes did the job for me, apart from this I was getting this libnss3.so not found error which I solved by creating a symlink of libnss3.so present in /usr/lib/x86_64-linux-gnu/ folder to /usr/lib/
ln -s /usr/lib/x86_64-linux-gnu/libnss3.so /usr/lib/libnss3.so
PS: Also make sure that you are using 64bit OR 32bit version of chrome driver as per your system.
For chrome to work with selenium-webdriver you need to have not only a working chrome browser installed, but also the chromedriver executable. Note that these are TWO different executable files that both need to be specified.
change the permission of file and then run your code again.
Open command prompt and navigate to directory where your chrome exe exists and write
chmod 777 filename
Hope it will solve your problem.

apache2 .htm extension

Since I've installed LAMP stack on ubuntu server with apache2, I've been having a weird issue. Whenever I create a file called filename.html, it renders and displays on browser fine. But whenever I create file with extension .htm, it will prompt the browser (firefox) to save the file.
I've already looked into /etc/mime.types and /etc/apache2/mod-enabled/mime.load
And what's in these files for .htm extension?
Can you check what mime-type returned by the Apache? (via browser plugins/sniffer)
After that you will see the issue...

Selenium: IE7 Not Launching

I am gettnig the exception:
"Failed to start new browser session: error while launching browser"
when trying to run a test suite with IE7.
Info:
python binding
using cygwin
I tried doing "*custom path_to_browser" and even printed out the path to browser directory in selenium module's do_command() function using os.listdir, and can see the iexplore.exe file. So I know the path is right.
I also tried setting localhost:4444 in IE7 and that didn't work either. Any ideas?
Use the new IWebDriver from Selenium v2 instead of Selenium RC. It will find your browser executables automatically (and now there's no need to start the Java Selenium RC instance anymore!)
This'll get you off the ground...
I'm using MINGW BASH on Windows XP SP3
(sorry, I can't get Markdown to render this as code, I've indented 4, then 8, phfhttt...)
#!/c/Python27/python
from selenium import webdriver
driver = webdriver.Ie()
driver.get("http://seleniumhq.org")
all_elements = driver.find_elements_by_xpath("//*") # pretty slow in IE
for index, element in enumerate(all_elements):
print "%03d element tag_name == %s" % (index, element.tag_name)

Resources