Clear Firefox cache in Selenium IDE - firefox

I'm using Selenium IDE to test a web application. Sometimes my tests succeed even though they should have failed. The reason is that the browser happens to load a previous version of a page from the cache instead of loading the newer version of that page. In other words, I might introduce a bug to my app without being aware of it because the tests may pass after loading a previous working version instead of loading the new buggy version.
The best solution I could have thought of is to delete the browser cache before running the tests. I have a Selenium script in which I run set-up selenium commands before running the tests. Is there a selenium command to clear Firefox cache? Alternatively, is there another way to prevent loading pages from the cache during the tests?

In python this should disable firefox cache:
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.cache.disk.enable", False)
profile.set_preference("browser.cache.memory.enable", False)
profile.set_preference("browser.cache.offline.enable", False)
profile.set_preference("network.http.use-cache", False)
driver = webdriver.Firefox(profile)
hope this helps someone

You can disable the cache in firefox profile.
See this link for more details.

For those programming in Java, here is how I solve the issue:
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.cache.disk.enable", false);
profile.setPreference("browser.cache.memory.enable", false);
profile.setPreference("browser.cache.offline.enable", false);
profile.setPreference("network.http.use-cache", false);
FirefoxOptions options = new FirefoxOptions().setProfile(profile);
driver = new FirefoxDriver(options);

Disclaimer: I've never had to do this before (clearing the cookies has always been sufficient for me), but from what I can see, this is functionality that is lacking in the current builds of Selenium, although from recent changelogs, it looks like the developers are making a push to make a standard way of doing this. In 2.33 of iedriverserver, They have the following changenote:
Introduced ability to clean browser cache before launching IE. This version
introduces the ie.ensureCleanSession capability, which will clear the
browser cache, history, and cookies before launching IE. When using this
capability, be aware that this clears the cache for all running instances of
Internet Explorer. Using this capability while attempting to run multiple
instances of the IE driver may cause unexpected behavior. Note this also
will cause a performance drop when launching the browser, as the driver will
wait for the cache clearing process to complete before actually launching
IE
http://selenium.googlecode.com/git/cpp/iedriverserver/CHANGELOG
To do this, you would specify this at driver creation time in the DesiredCapabilities Map using ensureCleanSession.
http://code.google.com/p/selenium/wiki/DesiredCapabilities
Since you're using firefox, it looks like you're out of luck in using a native way to do this. If you haven't tried driver.manage().deleteAllCookies();, I'd try that to see if it gets you where you need to be.

For C# and Geckodriver v0.31.0
public Task<WebDriver> newInstance()
{
return Task.Run(() =>
{
foreach (var process in Process.GetProcessesByName("geckodriver"))
{
process.Kill();
}
FirefoxProfileManager profilemanager = new FirefoxProfileManager();
System.Collections.ObjectModel.ReadOnlyCollection<String> profilesList = profilemanager.ExistingProfiles;
foreach (String profileFound in profilesList)
{
Console.WriteLine(profileFound);
}
FirefoxOptions options = new FirefoxOptions();
FirefoxProfile profile = profilemanager.GetProfile("default");
//profile = webdriver.FirefoxProfile()
profile.SetPreference("browser.cache.disk.enable", false);
profile.SetPreference("browser.cache.memory.enable", false);
profile.SetPreference("browser.cache.offline.enable", false);
profile.SetPreference("network.http.use-cache", false);
WebDriver driver = new FirefoxDriver(options);
return driver;
});
}

Related

Firefox pop up obscures the screen paritally in selenium test

I ran my test in BrowserStack and here is an example outcome
How can I turn off this behaviour of Firefox in a selenium test via DesiredCapabilities?
I want to be able to drive this behaviour with settings instead of adding code for a specific browser.
This prompt is related to firefox health report, you can disable it in as: (in java)
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("datareporting.healthreport.uploadEnabled", false);
profile.setPreference("datareporting.healthreport.service.enabled",
false);
profile.setPreference("datareporting.healthreport.service.firstRun",
false);
WebDriver driver = new FirefoxDriver(profile);

How to relaunch firefox browser using WebDriver while retaining the previous session id

There is a bug in my app wherein Logout doesn't work. I need to workaround this issue in my automation that is in Java using WebDriver. The workaround is to close the browser and reopen it and open the Login page.
To automate this workaround, here is what I have tried:
browserDriver.quit();
browserDriver = new FirefoxDriver(capabilities);
browserDriver.get(loginPageURL);
This returns a new session id. Is there a way to retain the previous session id and set it back. I can get the previous session id using
((RemoteWebDriver)browserDriver).getSessionId();
I also tried deleting all the cookies for the current domain using the following code, but the user was still logged in.
browserDriver.manage().deleteAllCookies();
browserDriver.navigate().refresh();
browserDriver.get(loginPageURL);
Appreciate any help on this.
Up to my knowledge after calling the quit() method on driver it will not retain the previous session id.
Anyway try to launch the browser using specific firefox profile by disabling cache in that.
FirefoxProfile profile = new ProfilesIni().getProfile(profilePath);
profile.setPreference("browser.cache.disk.enable", false);
profile.setPreference("browser.cache.memory.enable", false);
profile.setPreference("browser.cache.offline.enable", false);
profile.setPreference("network.http.use-cache", false);
DesiredCapabilities dc = DesiredCapabilities.firefox();
dc.setCapability(FirefoxDriver.PROFILE, profile);
driver = new FirefoxDriver(dc);
driver.get(url);
Firefox Profile creation ==>
https://support.mozilla.org/en-US/kb/profile-manager-create-and-remove-firefox-profiles
Edit-I
Change below setting in that profile
In "about:config" you can set "Browser.sessionstore.enabled" to false, in which case firefox will not restore your browsing session after it closed.
When you either use
driver.quit();
or
driver.close();
Selenium always starts a new session. This means it's always directs you to the 'Login' page.
Just use below piece of code, no need to set capabilities.
browserDriver.quit();
browserDriver.get(loginPageURL);
You will see the Login page.

How can I delete all cookies from all domains?

Webdriver Wire Protocol doesn't contain a method for deleting all cookies from all domains. It can only delete cookies from current domain.
I'd want to delete all cookies from all domains as AUT has integration with 3rd party sites that set cookies and I'd want to ensure clean state in the beginning of each test to improve ease of maintainability.
So I started to think about driver-specific ways to delete all cookies. I'm interested particularly in Firefox.
In Firefox it can be done by either:
pressing Ctrl+Shift+Delete and then Enter
writing Firefox extension that will allow to do it in one step
Do I miss something? Is there a cross-driver option to delete all cookies (from all domains)?
There are several ways to accomplish this. This is how I normally implement the work in my frameworks.
When creating a new driver object (in this case ChromerDriver) set the ENSURING_CLEAN_SESSION capability:
public WebDriver driver() {
File driverServer = new File(WebDriverConfig.class.getClassLoader().getResource("webDrivers/chromedriver.exe").getFile());
System.setProperty("webdriver.chrome.driver", driverServer.getAbsolutePath());
DesiredCapabilities caps = DesiredCapabilities.chrome();
caps.setCapability(CapabilityType.ForSeleniumServer.ENSURING_CLEAN_SESSION, true);
return new ChromeDriver();
}
Then at the beginning of each of my tests using TestNG's framework:
#BeforeMethod(alwaysRun = true)
public void setup() {
driver.manage().deleteAllCookies();
// Do other stuff before each test executes
}
You can also delete only specific cookies by getting the cookies and finding the one you want and then removing that one.
driver.manage().getCookies();
I hope this helps you in finding a resolution to your issue.

How to Stop the page loading in firefox programmatically?

I am running several tests with WebDriver and Firefox.
I'm running into a problem with the following command:
WebDriver.get(www.google.com);
With this command, WebDriver blocks till the onload event is fired. While this can normally takes seconds, it can take hours on websites which never finish loading.
What I'd like to do is stop loading the page after a certain timeout, somehow simulating Firefox's stop button.
I first tried execute the following JS code every time that I tried loading a page:
var loadTimeout=setTimeout(\"window.stop();\", 10000);
Unfortunately this doesn't work, probably because :
Because of the order in which scripts are loaded, the stop() method cannot stop the document in which it is contained from loading 1
UPDATE 1: I tried to use SquidProxy in order to add connect and request timeouts, but the problem persisted.
One weird thing that I found today is that one web site that never stopped loading on my machine (FF3.6 - 4.0 and Mac Os 10.6.7) loaded normally on other browsers and/or computers.
UPDATE 2: The problem apparently can be solved by telling Firefox not to load images. hopefully, everything will work after that...
I wish WebDriver had a better Chrome driver in order to use it. Firefox is disappointing me every day!
UPDATE 3: Selenium 2.9 added a new feature to handle cases where the driver appears to hang. This can be used with FirefoxProfile as follows:
FirefoxProfile firefoxProfile = new ProfilesIni().getProfile("web");
firefoxProfile.setPreference("webdriver.load.strategy", "fast");
I'll post whether this works after I try it.
UPDATE 4: at the end none of the above methods worked. I end up "killing" the threads that are taking to long to finish. I am planing to try Ghostdriver which is a Remote WebDriver that uses PhantomJS as back-end. PhantomJS is a headless WebKit scriptable, so i expect not to have the problems of a real browser such as firefox. For people that are not obligate to use firefox(crawling purposes) i will update with the results
UPDATE 5: Time for an update. Using for 5 months the ghostdriver 1.1 instead FirefoxDriver i can say that i am really happy with his performance and stability. I got some cases where we have not the appropriate behaviour but looks like in general ghostdriver is stable enough. So if you need, like me, a browser for crawling/web scraping purposes i recomend you use ghostdriver instead firefox and xvfb which will give you several headaches...
I was able to get around this doing a few things.
First, set a timeout for the webdriver. E.g.,
WebDriver wd;
... initialize wd ...
wd.manage().timeouts().pageLoadTimeout(5000, TimeUnit.MILLISECONDS);
Second, when doing your get, wrap it around a TimeoutException. (I added a UnhandledAlertException catch there just for good measure.) E.g.,
for (int i = 0; i < 10; i++) {
try {
wd.get(url);
break;
} catch (org.openqa.selenium.TimeoutException te) {
((JavascriptExecutor)wd).executeScript("window.stop();");
} catch (UnhandledAlertException uae) {
Alert alert = wd.switchTo().alert();
alert.accept();
}
}
This basically tries to load the page, but if it times out, it forces the page to stop loading via javascript, then tries to get the page again. It might not help in your case, but it definitely helped in mine, particularly when doing a webdriver's getCurrentUrl() command, which can also take too long, have an alert, and require the page to stop loading before you get the url.
I've run into the same problem, and there's no general solution it seems. There is, however, a bug about it in their bug tracking system which you could 'star' to vote for it.
http://code.google.com/p/selenium/issues/detail?id=687
One of the comments on that bug has a workaround which may work for you - Basically, it creates a separate thread which waits for the required time, and then tries to simulate pressing escape in the browser, but that requires the browser window to be frontmost, which may be a problem.
http://code.google.com/p/selenium/issues/detail?id=687#c4
My solution is to use this class:
WebDriverBackedSelenium;
//When creating a new browser:
WebDriver driver = _initBrowser(); //Just returns firefox WebDriver
WebDriverBackedSelenium backedSelenuium =
new WebDriverBackedSelenium(driver,"about:blank");
//This code has to be put where a TimeOut is detected
//I use ExecutorService and Future<?> Object
void onTimeOut()
{
backedSelenuium.runScript("window.stop();");
}
It was a really tedious issue to solve. However, I am wondering why people are complicating it. I just did the following and the problem got resolved (perhaps got supported recently):
driver= webdriver.Firefox()
driver.set_page_load_timeout(5)
driver.get('somewebpage')
It worked for me using Firefox driver (and Chrome driver as well).
One weird thing that i found today is that one web site that never stop loading on my machine (FF3.6 - 4.0 and Mac Os 10.6.7), is stop loading NORMALy in Chrome in my machine and also in another Mac Os and Windows machines of some colleague of mine!
I think the problem is closely related to Firefox bugs. See this blog post for details. Maybe upgrade of FireFox to the latest version will solve your problem. Anyway I wish to see Selenium update that simulates the "stop" button...
Basically I set the browser timeout lower than my selenium hub, and then catch the error. And then stop the browser from loading, then continue the test.
webdriver.manage().timeouts().pageLoadTimeout(55000);
function handleError(err){
console.log(err.stack);
};
return webdriver.get(url).then(null,handleError).then(function () {
return webdriver.executeScript("return window.stop()");
});
Well , the following concept worked with me on Chrome , try the same:
1) Navigate to "about:blank"
2) get element "body"
3) on the elemënt , just Send Keys Ësc
Just in case someone else might be stuck with the same forever loading annoyance, you can use simple add-ons such as Killspinners for Firefox to do the job effortlessly.
Edit : This solution doesn't work if javascript is the problem. Then you could go for a Greasemonkey script such as :
// ==UserScript==
// #name auto kill
// #namespace default
// #description auto kill
// #include *
// #version 1
// #grant none
// ==/UserScript==
function sleep1() {
window.stop();
setTimeout(sleep1, 1500);
}
setTimeout(sleep1, 5000);

Access to file download dialog in Firefox

Is there any kind of API that can allow me to manipulate a file download dialog in Firefox?
(I want to access the one that appears when user does something, not initiate one myself).
What I want to do is to access this dialog from Selenium (and whether Selenium "privileged mode" is enough to access chrome interface is something I am not sure about as well).
I have a solution for this issue, check the code:
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.setPreference("browser.download.folderList",2);
firefoxProfile.setPreference("browser.download.manager.showWhenStarting",false);
firefoxProfile.setPreference("browser.download.dir","c:\\downloads");
firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk","text/csv");
WebDriver driver = new FirefoxDriver(firefoxProfile);//new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
driver.navigate().to("http://www.myfile.com/hey.csv");
I was stuck with the same problem, but I found a solution. I did it the same way as this blog did.
Of course this was Java, I've translated it to Python:
fp = webdriver.FirefoxProfile()
fp.set_preference("browser.download.folderList",2)
fp.set_preference("browser.download.manager.showWhenStarting",False)
fp.set_preference("browser.download.dir",getcwd())
fp.set_preference("browser.helperApps.neverAsk.saveToDisk","text/csv")
browser = webdriver.Firefox(firefox_profile=fp)
In my example it was a CSV file. But when you need more, there are stored in the ~/.mozilla/$USER_PROFILE/mimeTypes.rdf
Not that I know of. But you can configure Firefox to automatically start the download and save the file in a specific place. Your test could then check that the file actually arrived.
Web Applications generate 3 different types of pop-ups; namely,
1| JavaScript PopUps
2| Browser PopUps
3| Native OS PopUps [e.g., Windows Popup like Upload/Download]
In General, the JavaScript pop-ups are generated by the web application code. Selenium provides an API to handle these JavaScript pop-ups, such as Alert.
Eventually, the simplest way to ignore Browser pop-up and download files is done by making use of Browser profiles; There are couple of ways to do this:
Manually involve changes on browser properties (or)
Customize browser properties using profile setPreference
Method1
Before you start working with pop-ups on Browser profiles, make sure that the Download options are set default to Save File.
(Open Firefox) Tools > Options > Applications
Method2
Make use of the below snippet and do edits whenever necessary.
FirefoxProfile profile = new FirefoxProfile();
String path = "C:\\Test\\";
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.dir", path);
profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/msword, application/csv, application/ris, text/csv, image/png, application/pdf, text/html, text/plain, application/zip, application/x-zip, application/x-zip-compressed, application/download, application/octet-stream");
profile.setPreference("browser.download.manager.showWhenStarting", false);
profile.setPreference("browser.download.manager.focusWhenStarting", false);
profile.setPreference("browser.download.useDownloadDir", true);
profile.setPreference("browser.helperApps.alwaysAsk.force", false);
profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
profile.setPreference("browser.download.manager.closeWhenDone", true);
profile.setPreference("browser.download.manager.showAlertOnComplete", false);
profile.setPreference("browser.download.manager.useWindow", false);
profile.setPreference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", false);
profile.setPreference("pdfjs.disabled", true);
driver = new FirefoxDriver(profile);
Most browsers (in mine case Firefox) select the OK button by default. So I managed to solve this by using the following code. It basically presses enter for you and the file is downloaded.
Robot robot = new Robot();
// A short pause, just to be sure that OK is selected
Thread.sleep(3000);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
I was facing the same issue.
In our application the instance of FireFox was created by passing the DesiredCapabilities as follows
driver = new FirefoxDriver(capabilities);
Based on the suggestions by others, I did my changes as
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk",
"application/octet-stream");
driver = new FirefoxDrvier(firefoxProfile);
This served the purpose but unfortunately my other automation tests started failing. And the reason was, I have removed the capabilities which were being passed earlier.
Some more browsing on net and found an alternate way. We can set the profile itself in the desired Capabilities.
So the new working code looks like
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
// add more capabilities as per your need.
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk",
"application/octet-stream");
// set the firefoxprofile as a capability
capabilities.setCapability(FirefoxDriver.PROFILE, firefoxProfile);
driver = new FirefoxDriver(capabilities);
Dont know, but you could perhaps check the source of one of the Firefox download addons.
Here is the source for one that I use Download Statusbar.
I had the same problem, I wanted no access of Save Dialogue.
Below code can help:
FirefoxProfile fp = new FirefoxProfile();
fp.setPreference("browser.download.folderList",2);
fp.setPreference("browser.download.manager.showWhenStarting",false);
fp.setPreference("browser.helperApps.alwaysAsk.force", false);
// Below you have to set the content-type of downloading file(I have set simple CSV file)
fp.setPreference("browser.helperApps.neverAsk.saveToDisk","text/csv");
According to the file type which is being downloaded, You need to specify content types.
You can specify multiple content-types separated with ' ; '
e.g:
fp.setPreference("browser.helperApps.neverAsk.saveToDisk","text/csv;application/vnd.ms-excel;application/msword");
Instead of triggering the native file-download dialog like so:
By DOWNLOAD_ANCHOR = By.partialLinkText("download");
driver.findElement(DOWNLOAD_ANCHOR).click();
I usually do this instead, to bypass the native File Download dialog. This way it works on ALL browsers:
String downloadURL = driver.findElement(DOWNLOAD_ANCHOR).getAttribute("href");
File downloadedFile = getFileFromURL(downloadURL);
This just requires that you implement method getFileFromURL that uses Apache HttpClient to download a file and return a File reference to you.
Similarly, if you happen to be using Selenide, it works the same way using the built-in download() function for handling file downloads.
I didnt unserstood your objective,
Do you wanted your test to automatically download file when test is getting executed, if yes, then You need to use custom Firefox profile in your test execution.
In the custom profile, for first time execute test manually and if download dialog comes, the set it Save it to Disk, also check Always perform this action checkbox which will ensure that file automatically get downloaded next time you run your test.
In addition you can add
profile.setPreference("browser.download.panel.shown",false);
To remove the downloaded file list that gets shown by default and covers up part of the web page.
My total settings are:
DesiredCapabilities dc = DesiredCapabilities.firefox();
dc.merge(capabillities);
FirefoxProfile profile = new FirefoxProfile();
profile.setAcceptUntrustedCertificates(true);
profile.setPreference("browser.download.folderList", 4);
profile.setPreference("browser.download.dir", TestConstants.downloadDir.getAbsolutePath());
profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/msword, application/csv, application/ris, text/csv, data:image/png, image/png, application/pdf, text/html, text/plain, application/zip, application/x-zip, application/x-zip-compressed, application/download, application/octet-stream");
profile.setPreference("browser.download.manager.showWhenStarting", false);
profile.setPreference("browser.download.manager.focusWhenStarting", false);
profile.setPreference("browser.download.useDownloadDir", true);
profile.setPreference("browser.helperApps.alwaysAsk.force", false);
profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
profile.setPreference("browser.download.manager.closeWhenDone", true);
profile.setPreference("browser.download.manager.showAlertOnComplete", false);
profile.setPreference("browser.download.manager.useWindow", false);
profile.setPreference("browser.download.panel.shown",false);
dc.setCapability(FirefoxDriver.PROFILE, profile);
this.driver = new FirefoxDriver(dc);

Resources