Clear browser cache and history with Selenium WebDriver - selenium

I use Selenium WebDriver 3.141.59 with ChromeDriver and try to remove browser cache and history during test execution.
I have found several solutions about using chrome://settings/clearBrowserData page in test scripts, but these seems flaky.
In v4.0, there will be a better solution to interact with DevTools, but it isn't yet released.
driver.getDevTools().createSessionIfThereIsNotOne();
driver.getDevTools().send(Network.clearBrowserCookies());

Whenever chrome opens it stores info in a profile folder. The best solution might be to create a custom profile folder every time you start the driver.
ChromeOptions chromeProfile = new ChromeOptions();
chromeProfile.addArguments("user-data-dir=" + chromeProfilePath);
WebDriver driver = new ChromeDriver(chromeProfile);

Related

Blank page on headless Selenium tests run with Jenkins

Headless Selenium tests can be well run on my machine (yup, I should definitely move in this ideal place where problems doesn't exist).
However, when I launch those tests via Jenkins, none of the page elements are found. I took a screenshot to figure out why, and it shows a blank page.
This is how I instanciate my headless Chrome browser.
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.NONE);
chromeOptions.addArguments("--headless");
chromeOptions.addArguments("--window-size=1920x1080");
chromeOptions.addArguments("start-maximised");
chromeOptions.addArguments("enable-automation");
chromeOptions.addArguments("--no-sandbox");
chromeOptions.addArguments("--disable-infobars");
chromeOptions.addArguments("--disable-dev-shm-usage");
chromeOptions.addArguments("--disable-browser-side-navigation");
chromeOptions.addArguments("--disable-gpu");
driver = new ChromeDriver(chromeOptions);
driver.manage().timeouts().pageLoadTimeout(30L, TimeUnit.SECONDS);
driver.manage().timeouts().setScriptTimeout(3L, TimeUnit.SECONDS);
driver.manage().window().maximize();
I have a guess but my coworker says it can't be this: should I install Xvfb on Jenkins server? I mean, is it mandatory? (I must be at least 51% sure before trying this approach ^^)
Thanks in advance.
I tried lots of solutions but it turned out that Jenkins wasn't allowed to access the resources I needed to test. So... I couldn't solve it myself anyway. Sysadmins did.

What is the best way to check whether a browser extension has finished installing in Selenium?

A browser extension can be installed for a ChromeDriver object by using the following code:
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File(pathToExtension));
ChromeDriver driver = new ChromeDriver(options);
This works perfectly, however I'd like to check whether installation of this extension is completed. Is there a way to do this in Selenium? I know how to let Selenium wait for a given amount of time, but I wondered if there is a cleaner solution.

Does browser support WebDriver or does WebDriver support browser

Is it the browser job to support WebDriver or is it WebDriver job to support each browser?
I mean if there is a new release of Chrome does this mean that WebDriver works right away because the support is included in the new Chrome release?
Or is WebDriver support added later by the developers of WebDriver?
To run selenium webdriver in Chrome browser, we need to take the help of ChromeDriver which is a separate executable that selenium webdriver uses to control chrome. ChromeDriver is supported by the Chromium team, ChromeDriver is a standalone server which implements WebDriver's wire protocol for Chromium.
http://www.seleniumeasy.com/selenium-tutorials/how-to-run-webdriver-in-chrome-browser
Yes, Webdriver made changes later that's the reason new jars of selenium launches so rapidly.
Hope it will help you :)

HtmlUnitDriver fails to load url.

I am currently using HtmlUnitDriver 2.45 version and when I run below code snippet
BrowserVersion version = BrowserVersion.CHROME;
WebDriver driver = new HtmlUnitDriver(version);
driver.get("http://www.google.com");
System.out.println(driver.getCurrentUrl());
my output is "about:blank".
I have noticed that driver instance created from default constructor
WebDriver driver = new HtmlUnitDriver(true);
creates driver object with deprecated default browser version "INTERNET_EXPLORER_8"
/** The default browser version. */
private static BrowserVersion DefaultBrowserVersion_ = INTERNET_EXPLORER_8;
Am I missing something while creating HtmlUnitDriver??
My experience with HTMLUnitDriver has been pretty terrible so far. It doesn't really serve as a viable testing driver due to the multiple compatibility issues it has with different applications (depends on the application).
If you are trying to do headless browser testing, I would suggest running PhantomJSDriver rather than HTMLUnitDriver. In your use-case this should be fine, since you are trying to run HTMLUnitDriver as the CHROME browser version, and PhantomJS is webkit-based.

How to disable Flash in selenium remote webdriver

How do I disable the loading of flash objects when using Selenium Remote WebDriver.
It will be helpful if I get a solution for the normal webdriver also.
Since in most cases the Flash object is loaded by a JavaScript
I have tried disabling the javascript on the webdriver and remote webdriver both, but it does not work.
I tried to to disable the JavaScript by:
WebDriver driver = new FirefoxDriver();
((DesiredCapabilities) driver.getCapabilities()).setJavascriptEnabled(false);
I also tried:
DesiredCapabilities caps = new DesiredCapabilities();
caps.setJavascriptEnabled(false);
WebDriver driver = new FireFoxDriver(caps);
For Remote WebDriver i tried:
final DesiredCapabilities firefoxCapability = DesiredCapabilities.firefox();
firefoxCapability.setJavascriptEnabled(false);
new RemoteWebDriver(new URL("http://" + windowsIP + ":4444/wd/hub"), firefoxCapability);
After execution of the above statement the remote server displays
Executing: [new session: <platform=ANY, javascriptEnabled=false, browserName=firefox, version=>] at URL:/session>
but still all the Javascript is executing on the pages the driver loads and the Flash is also loading.
Please help me :
1. how can stop the flash from loading.
2. need it on remote driver as I need to test the pages on IE, Firefox, Chrome. Hence loading the forefox profile will not work
Thank you for the help.
I used this code on Linux mint and it works:
FirefoxProfile profile= new FirefoxProfile();
profile.setPreference("plugin.state.flash", 0);
FirefoxDriver driver = new FirefoxDriver(profile);
though it's already answered question but on different forum... so i will consolidate for you...
I'm not sure if flash objects are loaded by javascript....but if disabling javascript is problem then...
Never disable Javascript for Firefox driver, in case if you want to use it being disabled try with HTMLUNITDRIVER which specially meant for non-javascript pages.
Reason being important parts of firefox driver is implemented in javascript and disabling would have serious concerns.
HtmlUnitDriver on the other hand is fastest and best way for automation tests (splly for pages with no JS)
please check this group discussion
https://groups.google.com/forum/?fromgroups=#!topic/webdriver/daLOzCiU_h4%5B1-25%5D
I had the same problem and needed it to be solved for Chrome. This is how I got it to work:
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-bundled-ppapi-flash");
WebDriver webDriver = new org.openqa.selenium.chrome.ChromeDriver(options);