What is the best way to test download using selenium webdriver? - selenium-webdriver

I am new to selenium, I understand two ways to test download file
1) simply call web element click
2) taking download link and test whether file exist or not
my question is which is better way or is there any another method???

You have to be more specific about your question, but as far as Download functionality is concerned in a browser, you set a Browser Profile, so that it would download the files automatically in the desired location. you can use this 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.yourpage.com/");

Related

Not able to save firefox profile plugin activation changes using selenium webdriver

When i am opening my same firefox profile through selenium webdriver, each time I have to activate shockwave player manually and changes are not getting saved in firefox profile.
Is there any way to activate plugins through script automatically or save changes in firefox profile for later use.
ProfilesIni profile = new ProfilesIni();
System.setProperty("webdriver.gecko.driver","C:\\Users\\Test-pc1\\geckodriver-v0.16.1-win64\\geckodriver.exe");
FirefoxProfile myprofile = profile.getProfile("SeleniumTest");
WebDriver driver = new FirefoxDriver(myprofile);
myprofile.setPreference("dom.ipc.plugins.enabled.Shockwaveflash.exe", "true");
I hope it will help you to achieve, what you want.
http://toolsqa.com/selenium-webdriver/custom-firefox-profile/ You can refer this.
Once you created your FF profile then, launch it and click on Option menu in right top corner,Select Options.
Select Application, And select the application and say savefile
Once you done, it then run you test with this profile.As mention in the link above.

Windows Popup appears while saving data in Chrome

I am creating some data and click on Save button. Manually when i do it there is no popup at all. But in automation browser of chrome i could see a popup appears asking to stay in page or leave page. How can i avoid the popup in automation script?
As per my knowledge, we have to create profile for chrome driver. I used the below whicc is not working for me. Can someone plssss plssss help me out on this
String userProfile= "C:\\Users\\user_name\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\";
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir="+userProfile);
options.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(options);
driver.get("http://www.google.com");
is there any specific reason that you are using ChormeOptions?
You can simply use the below code, and it'll work fine and no popups.
System.setProperty("webdriver.chrome.driver","pathToChromeDriver");
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com");

How to open a URL in Selenium webdriver other than driver.get() and navigate()?

I'm using Selenium webdriver using Java and i know how to open a browser in 2 ways:
driver.get("some url")
driver.navigate().GoToUrl("some url")
Is there any option available to open a browser other than this?
You can use Java script, there is a command window.location='url' which can help you to achieve this.
String url = "https://www.google.com";
String script = "window.location = \'"+url+"\'";
System.setProperty("webdriver.ie.driver", "Drivers\\IEDriverServer.exe");
WebDriver driver= new FirefoxDriver();
((JavascriptExecutor) driver).executeScript(script);
But again this is NOT a recommended method.
Difference between get() and this command is that, get() would wait for your page to load but NOT Javascript, it would just do what you command and that's it. You would need to separately manage waits then.
So if possible use the traditional methods. :)

Selenium Webdriver: download files on firefox using DesiredCapabilities class

Is there an alternative to firefox profile to download files, i.e. through desiredcapability on selenium webdriver?
I want to download files from a website but through desiredcapabilities.
So one piece of code is applicable accross all the browsers.
In theory, you could create a AugmenterProvider and pass it to the Augmenter class but I have not done so and its beyond my skill.

How to create a new tab or window using Selenium Webdriver in Chrome browser

I am trying to add a new tab using Webdriver 2.0 in Chrome but couldn't get the result.I have followed few answers provided in different forums.As I am very new to java and the answers available are more specific to Java scripting,I have posted this query to get a simple solution if possible.
E.g : The following statement is not triggering any action but the result in Selenium shows pass. Please advise.
driver.findElement(By.tagName("body")).sendKeys(Keys.CONTROL +"t");
When you can open a chrome window using
WebDriver driver = new ChromeDriver();
You can simply open a new window using
WebDriver driver2=new ChromeDriver();
You can access driver and driver2 in parallel or sequential.
To close a window do driver.close();
With the latest versions of selenium you can create a new tab, the line in the official github repo.
In C# is something like this:
ChromeDriver driver = new ChromeDriver();
driver.SwitchTo().NewWindow(WindowHint.Tab);
here is the junit test of java for creating a new tab

Resources