Selenium: Unable to create folder and file for download in the Jenkins pipeline - selenium

The test is to download a file by clicking the link for download template. When I execute the script in my local machine, it is working perfectly. Able to create the download folder and the file downloaded is stored in the newly created "download" folder.
But when I integrate it to the jenkins pipeline, there is no folder created and no file downloaded.
Note: In the jenkins pipeline, the script is executed using chrome in the selenium grid. Please refer to the configuration in the chrome driver.
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", System.getProperty("user.dir") + "\\src\\main\\resources\\downloads");
ChromeOptions options = new ChromeOptions();
HashMap<String, Object> chromeOptionsMap = new HashMap<String, Object>();
options.setExperimentalOption("prefs", chromePrefs);
options.addArguments("--test-type");
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(ChromeOptions.CAPABILITY, chromeOptionsMap);
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
driver = new RemoteWebDriver(new URL(seleniumGridUrl), cap);

If you are not going to check the contents of the downloaded file in your test, I'd like to suggest using a kind of by-pass approach. Using HTTP library, you may just execute Head http call to the resource, you need to download. In the response you'll get the file type and its size. In your test you simply assert against them to validate correctness of accessed for downloading file.
I think the issue you get is connected somehow with the permissions, that does not allow creating folders / files on the environment, where test is executed.

Related

How to enable save passwords using selenium chromedriver

when I start chromedriver by default password save options are disabled (I can see options to save password are enabled in chrome config), but when I log in any website, chromedriver never ask me to save the password.
For my tests I need to save passwords, I have tried some options but nothing works.
I used same profile to open chromedriver, so I don't use the temporary profiles that chromedriver creates every time I launch it.
(sorry about my english)
Selenium Java
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("credentials_enable_service", true);
prefs.put("profile.password_manager_enabled", true);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", prefs);
driver = new ChromeDriver(options);
Selenium Python
var options = new ChromeOptions();
options.AddUserProfilePreference("credentials_enable_service", true);
options.AddUserProfilePreference("profile.password_manager_enabled", true);
var driver = new ChromeDriver(options);

Accept microphone and camera permissions on Edge using selenium

I am running selenium scripts on Edge browser. one of the functionality requires to initiate a audio or video call between two windows. In chrome, we can use 'use-fake-ui-for-media-stream' in chrome options. Is there anything similar for Edge. If there isn't, is there a way to accept these alerts at runtime. I have tried -
driver.switchTo().alert().accept(),
but this also doesn't work, and throws error saying no such alert present
Edited
I am using Edge chromium and java selenium and have set properties as below in code. Still permission pop up shows when script runs
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.default_content_settings.popups", 0);
prefs.put("download.default_directory", fileDownloadLocation);
EdgeOptions options= new EdgeOptions();
options.setCapability("prefs", prefs);
options.setCapability("allow-file-access-from-files", true);
options.setCapability("use-fake-device-for-media-stream", true);
options.setCapability("use-fake-ui-for-media-stream", true);
DesiredCapabilities capabilities = DesiredCapabilities.edge;
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS,true);
capabilities.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS,true);
System.setProperty("webdriver.edge.driver", getDriverPath("EDGE"));
driver = new EdgeDriver(options);
driver.manage().window().maximize();
I suggest you make a test with the sample code below. I tried to test with the Edge Chromium browser and it looks like it is not asking the permission popup.
JAVA code:
public static void main(String[] args)
{
System.setProperty("webdriver.edge.driver","\\msedgedriver.exe");
EdgeOptions op=new EdgeOptions();
op.addArguments("use-fake-device-for-media-stream");
op.addArguments("use-fake-ui-for-media-stream");
WebDriver browser = new EdgeDriver(op);
browser.get("https://your_URL_here...");
}
In Selenium 3.141 Version we dont have addArguments() method but in Selenium 4.0.0 alpha version we have addArguments() method
EdgeOptions edgeOpts = new EdgeOptions();
edgeOpts.addArguments("allow-file-access-from-files");
edgeOpts.addArguments("use-fake-device-for-media-stream");
edgeOpts.addArguments("use-fake-ui-for-media-stream");
edgeOpts.addArguments("--disable-features=EnableEphemeralFlashPermission");
driver = new EdgeDriver(edgeOpts);

Automate PDF download [duplicate]

This question already has an answer here:
How to download files using GeckoDriver Firefox and Selenium?
(1 answer)
Closed 3 years ago.
I have a requirement to download a PDF file using Selenium Webdriver (#Java). Steps:
Save Product
Click on Print
New window with url opens
Save PDF.
Can anybody please help me in automating the above steps.
For downloads, I put the follwing code before instantiating the Chrome browser
HashMap<String, Object> chromePrefs = new HashMap<>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", "YouSaveDirectoryHere");
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
options.merge(cap);
This automatically downloads the file without a popup when clicking on the download button.
Hope it helps!

Getting java.lang.NullPointerException in profile.setPreference("browser.popups.showPopupBlocker", false);

System.setProperty("webdriver.gecko.driver","C:\\geckodriver.exe");
ProfilesIni profile2 = new ProfilesIni();
FirefoxProfile profile3 = profile2.getProfile("AutoProfile");
profile3.setPreference("browser.popups.showPopupBlocker", false);
profile3.setPreference("browser.download.dir", "D:\\WebDriverDownloads");
profile3.setPreference("browser.download.folderList", 2);
profile3.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;");
profile3.setPreference( "browser.download.manager.showWhenStarting", false );
profile3.setPreference( "pdfjs.disabled", true );
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.setProfile(profile3);
WebDriver driver = new FirefoxDriver(firefoxOptions);
The most probhable reason you are seeing a java.lang.NullPointerException is because the Firefox Profile which you are trying to use i.e. AutoProfile doesn't exist (yet to be created) on your local system.
Solution
Before you start using the Firefox Profile AutoProfile ensure that this Firefox Profile exists in your local system. You can create a Firefox Profile manually following the instructions at Creating a new Firefox profile on Windows.
To configure auto downloads setPreference("browser.popups.showPopupBlocker", false) is not required so you can remove it.
Execute your #Test.
Here you can find a detailed discussion on how to use a new/existing Firefox Profile for your tests

Is there any egde webdriver documentation about preferences?

I am currently trying to create cross-browser automated tests with Geb but I cannot find any documentation about preferences with Edge. What I am trying to do is to set up my Edge environment to automatically download documents and save them in downloads/edge.
I have done this way for chrome and firefox:
customChrome {
driver = {
System.setProperty("webdriver.chrome.driver", new File ("Drivers/chromedriver_win32/chromedriver.exe").getAbsolutePath())
Map<String, Object> chromePrefs = new HashMap<String, Object>()
chromePrefs.put("download.default_directory", new File("downloads/chrome").getAbsolutePath())
chromePrefs.put("download.prompt_for_download", false)
chromePrefs.put("plugins.always_open_pdf_externally", true)
ChromeOptions opt = new ChromeOptions()
opt.setExperimentalOption("prefs", chromePrefs)
new ChromeDriver(opt)
}
}
customFF {
driver = {
FirefoxProfile myProfile = new FirefoxProfile()
myProfile.setPreference("browser.helperApps.alwaysAsk.force", false)
myProfile.setPreference("browser.download.manager.showWhenStarting", false)
myProfile.setPreference("browser.download.folderList", 2)
myProfile.setPreference("browser.download.dir", new File("downloads/firefox").getAbsolutePath()) // my downloading dir
myProfile.setPreference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", false)
myProfile.setPreference("browser.download.useDownloadDir", true)
myProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf, image/jpeg")
myProfile.setPreference("pdfjs.disabled", true)
System.setProperty("webdriver.gecko.driver", new File("Drivers/GeckoDriver/geckodriver.exe").getAbsolutePath())
new FirefoxDriver(myProfile)
}
}
I used this source file to get chrome's prefs and this webpage for getting those of firefox but I cannot find anything similar for Edge. Does Microsoft provides any info about this ?
Please share any info you may have.
I'm not sure if things have changed since, but according to this answer
IE doesn't use profiles and is therefore unable to download files to a specific location.