close download popup window java - java

I'm using Selenium Webdriver in Java and I want to verify if is possible to download one document. When I click a link, its shown pup up download window and I need verify text from the title and close it. But I cannot click at the popup window and I don't know XPath etc. http://postimg.org/image/si2eagaqr/
driver.close(); this statement is useless for me
Can anyone advise me? I will be very grateful.

You can configure Firefox the directly download the files - File types and download actions
If you don't want to hardcode the setting for your browser, you can setup a specific FF profile only for your tests, where you can configure where you want the files to be downloaded.
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);
I suggest you to use the first approach is much simpler.

Hello you can use robot for this purpose by pressing Enter please call the function below to press enter and it will press download button on UI . If this not works please let me know key sequence to perform this action .
public void pressEnter() throws AWTException, InterruptedException {
Thread.sleep(5000);
Robot rb=new Robot();
rb.keyPress(KeyEvent.VK_ENTER);
rb.keyRelease(KeyEvent.VK_ENTER); }

Related

What kind of window is this and how do I handle it with Selenium? Autoit?

I have a dialog that Windows Detective claims is a "MozillaDialogClass" window, I'm testing in Firefox right now of course.
Here is an image of the window.
AutoIt IDE doesn't recognize it. Windows Defender can't see any of it's properties so I have no idea what the name of it is or anything else about it. One of the developers says it's a browser specific window. (whatever the heck that is, if it is why can't anything see it?).
Anyone have any ideas on this one? I'm stumped.
The best way to handle this is to use SikuliX. Below is an example scenario using SikuliX. Basically, SikuliX automates anything you see on the screen of your desktop computer running Windows, Mac or some Linux/Unix. It uses image recognition powered by OpenCV to identify and control GUI components. This is handy in cases when there is no easy access to a GUI's internals or the source code of the application or web page you want to act on.
Below is a hello world example. This clicks on the spotlight icon on the screen, waits spotlight’s input window appears, and then types “hello world” and hits ENTER.
import org.sikuli.script.*;
public class TestSikuli {
public static void main(String[] args) {
Screen s = new Screen();
try{
s.click("imgs/spotlight.png", 0);
s.wait("imgs/spotlight-input.png");
s.type(null, "hello world\n", 0);
}
catch(FindFailed e){
e.printStackTrace();
}
}
}
You can find sikuli here
This is a native Dialog from your webbrowser. So it is not easy to handle this with Selenium. What do you want to test in it?
For some reading, see also this question:
https://sqa.stackexchange.com/questions/2197/how-to-download-a-file-using-seleniums-webdriver
Rather than handling the download window, you could setup Firefox to automatically download the file to a folder:
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("pdfjs.disabled", true); // disable the built-in viewer
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.dir", "C:\\Windows\\temp");
profile.setPreference("browser.download.panel.shown", false);
profile.setPreference("browser.helperApps.neverAsk.openFile", "");
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/vnd.ms-excel");
WebDriver driver = new FirefoxDriver(profile);
driver.get("http://www.exinfm.com/free_spreadsheets.html");
driver.findElement(By.linkText("Capital Budgeting Analysis")).click();
Or to automatically open a file with the default application:
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("pdfjs.disabled", true); // disable the built-in viewer
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.panel.shown", false);
profile.setPreference("browser.helperApps.neverAsk.openFile", "application/vnd.ms-excel");
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "");
WebDriver driver = new FirefoxDriver(profile);
driver.get("http://www.exinfm.com/free_spreadsheets.html");
driver.findElement(By.linkText("Capital Budgeting Analysis")).click();
Note that you need to update the MIME type (application/vnd.ms-excel here) by the one of your file.
The MIME type is the Content-Type from the response header.
And if you just want to close the dialog, then I would use AutoIt:
WinWait("[CLASS:MozillaDialogClass]", "", 10)
WinClose("[CLASS:MozillaDialogClass]")

How to make the screenshot of the entire display containing the browser using webdriver?

Does anybody know how to make full screenshot of display using webdriver?
In Selenium documentation I read that it is possible: (http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/TakesScreenshot.html)
..
The screenshot of the entire display containing the browser
..
I use Selenium Grid. My hub is on linux and node on windows. I tried to use Robot, however it takes screenshot on linux, however needs on windows
If you want a screenshot of your currently running browser instance then you can do it using following code:
public static void captureScreen(WebDriver driver, String screenshotFileName)
{
String screenshotsFile = screenshotsFolder+screenshotFileName+imageExtention;
try {
File screenShot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenShot, new File(screenshotsFile));
} catch (IOException e) {
}
}
But if you want to take screenshot of your active window (other than browser only) then you can use Robot class.
Edit:
Although it's too late for you, the link below contains your answer. I think it may be helpful for others who are searching for the same thing.
Screen shot issue in selenium webdriver
You need to use FirefoxDriver to screenshot whole page.
Chrome and IE doesn't support it.
hi try it like below it will take the complete webpage screen shot
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
//The below method will save the screen shot in defined drive with name "screenshot.png"
FileUtils.copyFile(scrFile, new File("yourPath\\screenshot.png"));

Press ENTER key in Selenium WebDriver with java when element property not present

I am using selenium webdriver with Java to automate the webpages
When I enter the url, I am getting the authentication required dialog box
I am able to enter the username and password by configuring profile
but I am not able to click on OK button
Note: Not able to get the ok button property so am not able to
use the below code
import org.openqa.selenium.Keys
WebElement.sendKeys(Keys.RETURN);
Is there any other way to press on ok button through webdriver?
You need to handle it as an alert box,
wait for popup to appear and click OK.
Below code waits up to a maximum of 10 seconds for the popup to be present and then accepts it by clicking OK. Although wait is optional.
new WebDriverWait(driver, 10).until(ExpectedConditions.alertIsPresent());
driver.switchTo().alert().accept();
Handling credential boxes is not possible directly using Selenium You can use the JAVA AWT robot class to press enter. This class is available in the java API itself.
Robot r = new Robot();
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER);
Alternatively, you can use AutoIt or an image based testing tool like SIKULI http://www.sikuli.org.
Please note that when you are using these solutions, the workstation screen cannot be locked while running the test cases.
Try this code snippet:
driver.findElement(By.xpath("//body")).sendKeys(Keys.RETURN);
It will definitely work.

Checking for URL in JUnit

I'm using the selenium RC driver for JUnit testing my application. Here is the relevant code I am trying to write. It checks that if the user clicks the link, they are taken to the right place.
selenium.click("//td[#id='myLink']/a");
String myURL = selenium.getLocation();
However, when the webdriver simulates a "click," it instead opens up a new window. So now I have the first window open, and the page that I wanted to navigate to is open in a seperate window. Now, when I try to get the URL, selenium grabs the first URL, so it fails the test case. How can I make the page open in the same window? Or, alternatively, how can I get selenium to check the URL of the new window?
EDIT: Should also mention, both my first and second window have the same title, so I don't think I can use selenium.SelectWindow(Title); for this.
Well, with selenium RC being as fickle as it is, I found a work-around for this problem. Here is the brief overview of how I changed my code:
String targetURL = selenium.getAttribute("//td[#id='myLink']/a#href");
selenium.openWindow(targetURL, "myNewWindow");
Thread.sleep(3000); //wait a few seconds for my page to load
selenium.selectWindow("myNewWindow");
selenium.getLocation();
//do my checks
selenium.selectWindow("originalWindowName");
So I retrieve the href myself, open the window using the retrieved URL and with my own custom name. I then switch the context of the driver to the new window and perform my checks. Once I am done, I switch my context back to the original window, and continue with my tests.

Webdriver hangs in new window with “about:blank” in address bar, transitioning to file download dialog

I need some help for the following issue using Webdriver, Java, and Firefox.
In the testing, when clicking on a link,
1) it will often open a new window with a normal web page. OR
2) occasionally, it will open a new window with “about:blank” in address bar; after 20 to 60 seconds the new window will disappear and a file download window will appear.
Because the URL is rewritten for SEO, there is no way to check the URL before or after clicking on the link to determine whether the link connects to a normal web page or a downloadable file.
In both cases,
driver.getWindowHandles().size() == 2,
so I can switch to the new window successfully using the following statements in order to check whether a certain WebElement exists in new window.
for (String winHandle : driver.getWindowHandles()) {
driver.switchTo().window(winHandle);
}
But if the link connects to a downloadable file, the execution will hang for any of the following methods:
findElement(By.xpath(“//html”));
findElements(By.tagName(“body”));
getCurrentUrl();
getPageSource();
getTitle();
getWindowHandle();
getWindowHandles() always returns 2 while new window with “about:blank” in address bar presents before it is replaced by the file download dialog. It occasionally throws a NoSuchWindowException exception; but most of the time, it just hangs. I tried the Explicit and Implicit Waits to no avail.
Many thanks
Sam
Have you tried setting the following preference? (This will get rid of the download dialog all together)
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.manager.showWhenStarting",false);
FirefoxDriver driver = new FirefoxDriver(profile);

Categories

Resources