PDF is not being opened in Chrome started by Selenium - java

I am using Selenium and ChromeDriver 2.43.1 with the latest Chrome (Version 42.0.2311.135 at the time the quetion was asked). My web application generates a PDF. It is being sent with the correct MIME type and it also correctly opens in the Chrome PDF viewer. However when I try to open the PDF using Selenium in Chrome that is started by the WebDriver, it gets downloaded.
I believe it might be some settings that Selenium or WebDriver use to start Chrome.
I've tried settings a few switches, but nothing worked yet. My code:
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
// Add ChromeDriver-specific capabilities through ChromeOptions.
ChromeOptions options = new ChromeOptions();
options.addArguments("--please-make-it-work"); // not a real switch
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
webDriver = new ChromeDriver(capabilities);
webDriver.get(url);
What I really need is to start the browser in "normal" mode. It doesn't need any profile settings, just the defaults that will open the PDF.

The problem was caused by the recent change to the behaviour of the --test-type switch. It is described in the ChromeDriver issue tracker.
The workaround is to disable this switch. Here's my code changed:
// Add ChromeDriver-specific capabilities through ChromeOptions.
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("excludeSwitches", Arrays.asList("test-type"));
webDriver = new ChromeDriver(options);
webDriver.get(url);

Related

Creating Firefox profile and switching off the marionette

I am coming from Ruby background, I know how to do this in Ruby Selenium Binding, but I don't know how to do it Java Selenium Binding,
I have this code to create Firefox profile
FirefoxProfile firefoxProfile = new FirefoxProfile(pathToProfile);
WebDriver driver=new FirefoxDriver(firefoxProfile);
It works in selenium 2.53 but it's throws error in very recent selenium binding 3.11.0, Can anyone tell me what's the alternative?
And also I wanted to switch off the marionette to connect to Legacy Firefox driver, I can do this with the following code
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", false);
WebDriver driver=new FirefoxDriver(capabilities);
But if I use the above line, then it gives the FirefoxDriver is deprecated. Can anyone guide me how to create profile as well as how to switch off the marionette?
Yes FirefoxDriver(desiredCapabilities) is deprecated.
Alternate way would be to go with options:
FirefoxOptions foptions = new FirefoxOptions(capabilities);
WebDriver driver=new FirefoxDriver(foptions);
Update : [In order]
FirefoxOptions foptions = new FirefoxOptions();
FirefoxProfile firefoxProfile = new FirefoxProfile(pathToProfile);
foptions.setProfile(firefoxProfile);
foptions.setCapability("marionette", false);
foptions.setBinary("C:\\Program Files\\Mozilla Firefox 52\\firefox.exe");
WebDriver driver = new FirefoxDriver(foptions);
To use an existing Firefox Profile for your Test Execution first you have to create a Firefox Profile manually following the instructions at Creating a new Firefox profile on Windows. Now you have to pass the Firefox Profile to a FirefoxOptions class object. Additionally as you would be using the Legacy Firefox Browser
you have to set marionatte to false through a DesiredCapabilities class object which you need to merge() into the FirefoxOptions class object as follows :
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
ProfilesIni profile = new ProfilesIni();
FirefoxProfile testprofile = profile.getProfile("debanjan");
FirefoxOptions options = new FirefoxOptions();
options.setProfile(testprofile);
DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability("marionatte", false);
options.merge(dc);
WebDriver driver = new FirefoxDriver(options);
driver.get("https://www.google.com");
Update
I am not sure about your usecase and why you want to use Legacy Firefox Driver. But as per the GitHub discussion Unable to Start Firefox Using the Legacy Driver on a 3.5.3 Grid #jimevans clearly mentions :
The legacy Firefox driver won't work with Firefox 53 or so. You might get the browser to launch, but the language bindings will be entirely unable to communicate with the driver (because Firefox will refuse to load the browser extension that is the legacy Firefox driver).
#barancev also mentions :
A binding should not pass OSS capabilities in W3C-compliant parts of payload, in "capabilities" block. They are allowed in "desiredCapabilities" block only. Perhaps, Mozilla broke Selenium compatibility in Firefox 48 in release channel, but restored it in version 52 in esr channel. It was unexpected, but it's true.
It's all upto you to take a informed descission.

Is there a way to clear session data to ensure a clean session is initiated each time?

For IE you would use capabilities like this:
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);
and possibly in combination with
driver.manage().deleteAllCookies();
How could this be achieved using Chrome and ChromeDriver?
While we work with Internet Explorer Driver we use the field IE_ENSURE_CLEAN_SESSION
IE_ENSURE_CLEAN_SESSION
As per the JavaDocs IE_ENSURE_CLEAN_SESSION is the Capability that defines whether to clean or not browser cache before launching Internet Explorer by IEDriverServer and is configured as follows :
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);
Now let us have a look at GeckoDriver which follows the WebDriver Spec.
GeckoDriver / moz:profile / rust_mozprofile
If you have a closer look at the geckodriver logs closely you will observe that each time geckodriver is called a new moz:profile is scopped out and the details of rust_mozprofile occurs in the following line:
Marionette CONFIG Matched capabilities: {"browserName":"firefox","browserVersion":"56.0","platformName":"windows_nt","platformVersion":"6.2","pageLoadStrategy":"normal","acceptInsecureCerts":false,"timeouts":{"implicit":0,"pageLoad":300000,"script":30000},"rotatable":false,"specificationLevel":0,"moz:processID":5848,"moz:profile":"C:\\Users\\AtechM_03\\AppData\\Local\\Temp\\rust_mozprofile.OfFuR9ogm33d","moz:accessibilityChecks":false,"moz:headless":false}
This log clearly indicates that Marionette scoops out a new "moz:profile":"C:\\Users\\AtechM_03\\AppData\\Local\\Temp\\rust_mozprofile.OfFuR9ogm33d" and this configuration is handled by the WebDriver instance i.e. the GeckoDriver.
You can find a more detailed discussion on moz:profile in Is it Firefox or Geckodriver, which creates “rust_mozprofile” directory discussion.
ChromeDriver
ChromeDriver which is following the same WebDriver Spec does abides (will be abiding) by the same suite.
Incase you are using any stored FirefoxProfile or ChromeProfile, WebDriver will pick up the existing profile where the Stored Browser Configurations are picked up for reuse.
driver.manage().deleteAllCookies();
Irespective of New/Existing FirefoxProfile or ChromeProfile if you add the line :
driver.manage().deleteAllCookies();
Only the cookies gets deleted only to be get restored back to support the Active Browser Session

How to use already opened browser in webdriver using java

I have requirement that everytime i login, it asking security code in webapp. But it will ask only once since browser stored the cookies.but it is asking again and again in selenium webdriver since driver always opening new browser every time.
So i need to use already opened browser in selenium webdriver java. please help me with example code.
Finally i found solution that using google chrome option in java. It stores cookies in your temp file and reusing it everytime.
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
options.addArguments("test-type");
options.addArguments("start-maximized");
options.addArguments("user-data-dir=D:/temp/");
capabilities.setCapability("chrome.binary","res/chromedriver.exe");
capabilities.setCapability(ChromeOptions.CAPABILITY,options);
driver1 = new ChromeDriver(capabilities);

Starting chromedriver with saved passwords enabled

Everytime that I start the Chrome web browser, using chromedriver, it starts up clean with any custom settings disabled. I have a case where I am logged in on a website and I want to access the account to get some information. However, the newly opened browser is not logged into the account anymore. Even when I open a new browser manually I am still logged in on that same page. Is there a way to enable custom settings? Preferably in Java.
You can achieve this by using ChromeOptions as below :-
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
String chromeDirPath = "provided here a path where you want to custom chrome dir which could be use everty time you launch"
//ensure chromeDirPath exist in dir
ChromeOptions options = new ChromeOptions();
options.addArguments("--user-data-dir="+chromeDirPath);
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(capabilities);
driver.get("url");
Now it will maintain your custom browser setting at chromeDirPath
Hope it will help you.

Enabling popup windows in Chrome by Selenium

My apologies in advance if my question sounds primary, I am very new at QA and Selenium.
I am using Java and Selenium to write a test, at one of my test's step when I click on a button it is supposed to open another window but Chrome blocks the popup window, can I enable popup by Selenium?
Well, you need to initialize the ChromeDriver with a customized configuration which will disable the flag to block popups. From this site, the command line switch for it is disable-popup-blocking. So, using ChromeOptions and DesiredCapabilities, you add the desired config using the DesiredCapabilities.setCapability() function.
ChromeOptions options = new ChromeOptions();
options.addArguments("test-type");
options.addArguments("disable-popup-blocking");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(capabilities);
EDIT: Just found the same solution on this site.
There is also another option to enable popup windows. Because sometimes your company may block you from accessing any application in admin mode. If the above method fails to work, you can use the below codes to enable pop ups.
WebDriver driver = new ChromeDriver(options);
driver.manage().window().maximize();
driver.get("chrome://settings/content");
Thread.sleep(4000);
driver.switchTo().frame("settings");
Thread.sleep(2000);
driver.findElement(By.xpath("//input[#type='radio' and #name='popups']")).click();
Thread.sleep(4000);
driver.findElement(By.id("content-settings-overlay-confirm"));
Thread.sleep(4000);
Use the above code before starting your test.
If anyone is still encountering this problem, it's probably because they are on an old version of ChromeDriver. Popup blocking was disabled by default from version 21+
Reference: https://bugs.chromium.org/p/chromedriver/issues/detail?id=1291
For me worked this solution:
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-web-security");
ChromeDriver driver = new ChromeDriver(options);

Categories

Resources