I'm trying to print from a Silverlight application.
Printing works when I run the application Out Of Browser. But when I run it in the web browser, the Print dialog is shown but nothing happens when I click the buttons (Print, Cancel, or even close the window). I tried running without debugger attached (deployed to IIS) but it's the same thing.
The code:
private void PrintLabel()
{
PrintDocument printDocument = new PrintDocument();
printDocument.PrintPage += new EventHandler<PrintPageEventArgs>(printDocument_PrintPage);
printDocument.Print("Label for " + this.tbSerialNo.Text);
}
void printDocument_PrintPage(object sender, PrintPageEventArgs e)
{
e.PageVisual = this.csLabel;
}
Am I missing something?
This sounds like a browser-specific issue I have seen in the past. Are you running this in Firefox 3.6 or later? If so, this Firefox bug might be of some interest to you since the print dialog is a popup window as well.
You can fix this popup issue by setting dom.ipc.plugins.enabled to false in Firefox's about:settings menu. However, this sometimes causes mouse oddities, as described here.
If this is the case(and it sounds like it is), printing does work in Out-Of-Browser mode since its really just running Silverlight in an IE process without any toolbars.
Related
When opening the print page in Firefox a print dialog is triggered. As is mentioned in the answer linked here (How to handle print dialog in Selenium?), Selenium's WebDriver cant handle these sorts of browser (or OS) dialogs.
Im using Selenium with Java to test a particular website. So I included the following code (as was suggested by the answer linked above) which employed the use of Java Robot to get rid of the print dialog.:
// press Escape programatically - the print dialog must have focus, obviously.
Robot r = new Robot();
r.keyPress(KeyEvent.VK_ESCAPE);
r.keyRelease(KeyEvent.VK_ESCAPE);
This answer made sense to me because when I manually pressed the escape key, the print dialog was dismissed.
Unfortunately, the code mentioned above did not work for me i.e. the print dialog remained there. As suggested by some other posts, I made sure to wait until the print dialog appeared.
While running the automated test code I'm not entirely sure whether "the print dialog was focused" (as is mentioned in the comment in the code given above). I tried multiple methods to get it "in focus" such as switching to the last web handle, switching to the browser alert etc and all of those threw errors. This further confirmed to me the idea that selenium was unable to handle this dialog. I do suspect that the lack of "focus" on the print dialog is where I'm running into the issue.
I want to be able to dismiss this modal because I open the print page multiple times during my testing run which means that multiple print dialogs open. Once the testing code is completed, the browser quits and only one of these dialogs is closed. Could you please help me figure out what I'm doing wrong?
Im using the following:
Geckodriver version: geckodriver-v0.26.0-macos
Java: 1.8.0_191
MacOS: Version 10.15.5 (19F101)
Firefox version: 78.0.2
Please do let me know what other information I could include to help you answer this. Thanks! I would have commented on the thread I mentioned, but Im new to StackOverflow and you need 50 reputation points to make a comment :(
I am using selenium automation in Safari. It seems there is an issue in using driver.navigate.back()
So I used:
((JavascriptExecutor)driver).executeScript("history.back")
((JavascriptExecutor)driver).executeScript("history.go(-1)")
But this code is going to the previous page and not actually clicking the browser back button. I confirmed by doing manually. If I do manually in my application, it will pop up a dialog saying "do you want to leave this page?" But with this code it's not happening.
Based on this open issue, it seems Safari reloads an old page. It doesn't fully initialize the extension for the page.
Hence Selenium does not support it:
safaridriver.inject.commands.unsupportedHistoryNavigation = function() {
throw Error('Yikes! Safari history navigation does not work. We can ' +
'go forward or back, but once we do, we can no longer ' +
'communicate with the page...');
};
I am automating using selenium 2.0, my application launches the login page by default in a new window, hence my application has by default two windows. These two windows will remain open always. In this case I could switch between the windows without any problem. The below code is executed without any errors.
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
The problem starts while clicking the menu options a pop up window launches to search the records. Here, I need to switch between these three windows. I tried the below piece of code. It returns only the first two window handles.
Set availableWindows = driver.getWindowHandles();
This popup window is coded in such a way that, "In a .jsp file it is parameterised as window.open()".
Please let me know, if some one could help me on this?
If you're only seeing 2 window in getWindowHandles(), then the popup is probably a iframe. In this event, use driver.switchTo().frame() to switch focus to that frame instead of looking for an entirely new window.
Here's the documentation on the switch method: http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/WebDriver.html#switchTo()
One probable solution is to use JavascriptExecutor.executeScript() method to run a javascript code and handle the pop up window without switching into pop up window.
For example, from parent window of pop up window, run a javascript code which is something like this.
JavascriptExecutor exec = (JavascriptExecutor)driver;exec.executeScript("var popup = <<popupopener function>>; //operate on popup object to manipulate the markup of pop up window");
This may be a noob question, but I don't know how to determine what type of popup I am interacting with so I can have enough information to automate it using selenium webdriver.
I assumed it was just a popup, but when using (what seems to be) the standard way of dealing with popups my Selenium JUnit test freezes the instance the button is clicked.
String mainWindowHandle = driver.getWindowHandle();
//P2: Click on the element that opens the popup
driver.findElement(By.xpath("//a[contains(text(),'Login')]")).click();
Set s=driver.getWindowHandles();
Iterator ite=s.iterator();
while(ite.hasNext()){
System.out.println("Current ite = " + ite.next().toString());
String popupHandle=ite.next().toString();
if(!popupHandle.contains(mainWindowHandle)){
driver.switchTo().window(popupHandle);
System.out.println("reached popup window? Now perform the login procedure before returning to the original window");
driver.switchTo().window(mainWindowHandle);
}
}
This code was described here and was the accepted answer. I have also seen many other similar examples of this code, however in my application the selenium test hangs on line
driver.findElement(By.xpath("//a[contains(text(),'Login')]")).click();
It just waits for me to enter login info, and until I do that manually the test hangs. If I do enter the info the test continues. I want to have this code finding the handle of the popup window (and eventually) populating it with username and pass and clicking on 'ok'. Though could it be possible that I am not dealing with a traditional popup? How could I tell?
This login button is the following element in the html Login
I faced the same issue while using selenium RC. I have used try catch block as shown below which helped me in solving the problem.
hope it will be useful.
//webdriver code till clicking the pop up link
try {
selenium.waitForPopUp("myWindow","3000");
}
catch(Exception e){
selenium.selectPopUp("myWindow");
selenium.windowFocus();
//activity that needs to be performed on the pop up window***
//code that takes back the focus to main window
String[] winFocus;
String winTitle;
winFocus = selenium.getAllWindowTitles();
winTitle = winFocus[0];
selenium.selectWindow(winTitle);
}
Here is the code that i'm using:
System.Windows.Browser.HtmlPopupWindowOptions pop = new System.Windows.Browser.HtmlPopupWindowOptions();
pop.Directories = false;
pop.Menubar = false;
pop.Status = false;
pop.Toolbar = false;
System.Windows.Browser.HtmlPage.Window.Navigate(new Uri(#"http://" + App.Host + App.VirtualDirectory+ "/Print.aspx?ID=" + ID, UriKind.Absolute), "_blank", "location=no, toolbar=no, status=no");
What happens is in production only (Works locally and in a testing environment) the window displays and then immediately closes. Again this works fine locally in my development environment and then on an internal testing environment.
The page being called creates a dynamic PDF to be displayed to the screen. Here is the code that modifies the response object:
context.Response.ClearHeaders();
context.Response.ClearContent();
context.Response.AddHeader("Content-Disposition", "attachment;filename=print.pdf");
Then writes the actual PDF object to the output stream.
If I open the URL in a new Tab in IE I can view the document as intended, it's only through the print button inside Silverlight and only in IE. I have tried this in Chrome and it works fine, haven't tested Firefox.
There is no pop-up blocker involved, the window shows up then disappears. I have checked security settings and added the domain to my list of trusted sites. Looking for any other suggestions.
I'm answering my own question because I've found a suitable workaround.
I replaced the line
context.Response.AddHeader("Content-Disposition", "attachment;filename=print.pdf");
With
context.Response.ContentType = "application/pdf";
So instead of presenting the user with an Open/Save dialog it opens directly in the browser if they have a pre-defined PDF viewer. They can then print or save the file from Internet Explorer.