Find and Close a Dialog in Firefox using Selenium and C# - c#

Issue: My selenium script is not recognizing and closing a dialog box that pops up when I redirect to a URl that contains file to download. The attached image shows the dialog I am referring to.
I know this has been asked a million times and I have spent at least 24 hours researching and trying other suggestions posted across the web but with no success. I am hoping the attached image will clarify which Firefox dialog box I am referring too.
I have tried the following solutions
1. Creating IAlerts (Alert element is not found)
2. Searching by trying to find the element by xpath (xpath to cancel was not found)
3. WindowsHandler Method (was not able to figure out the window name)
Any help on this would be greatly appreciated. Thank You in advance!
FireFox Dialog

There are several types of "popups".
Javascript dialogs - These are NOT HTML dialogs but are instead Javascript created dialogs. These consist of alert(), confirm(), and prompt() and can be handled with Alert.
HTML dialogs - These take on various forms depending on how they are formatted. If you right-click on the dialog and see the typical context menu for your browser, then it's an HTML dialog. They are part of the DOM of the current page so you can use Selenium to access this just as you would any other page (e.g., no window handles needed).
Browser window - These are actually another browser window that pops up and may look like a dialog, depending on how it's formatted. It's different than an HTML dialog because it can be moved outside of the current browser page frame. You will need to use window handles to access these windows as you would another tab in the browser. Once you get ahold of the window handle, you can access the HTML of the page like any other page.
Browser dialog - these take on various forms but are akin to System dialogs. They are not made up of HTML so they cannot be accessed by Selenium. Other than using a library to access them, you can interact with them in a limited fashion by sending keys such as <SPACE>, <TAB>, <ENTER>, etc.
What you have pictured is a (4) browser dialog. They it takes on different forms depending on the browser you do the action on. Your best course of action is probably send keys. Sometimes, depending on the browser, you can specify settings to autodownload a file to a specific location so that the dialog doesn't come up. I've never used this so I can't direct you there.

I was able to find and close a download dialog box by checking the amount of open windows after the dialog appears. This solution worked for me.
Find a link to open a dialog and click it:
var link = MyBrowser.Driver.FindElement(OpenQA.Selenium.By.Id("Button"));
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
js.ExecuteScript("arguments[0].click();", link);
Get the new window count after the dialog opens:
var newWindowCount = MyBrowser.Driver.WindowHandles.Count;
Switch to the newly opened dialog and close it:
MyBrowser.Driver.SwitchTo().Window(MyBrowser.Driver.WindowHandles[newWindowCount-1]);
MyBrowser.Driver.Close();

Related

Avoid Pop-up blocker when redirecting to a page in a new tab

So, I have an a application that needs a report to be exported in PDF. And on button click the generated pdf should open in a new tab for the user to view the contents before downloading.
My problem is opening the page in a new tab gets a pop-up blocker warning, I already tried the following:
string url = "/Page2.aspx?id="+id+;
Response.Write("<script>");
Response.Write("window.open('"+url+"','_newtab')");
Response.Write("</script>");
.......
Page.ClientScript.RegisterStartupScript(this.GetType(),"OpenWindow","window.open('"+url+"','_newtab');",true);
...
ScriptManager.RegisterStartupScript(this, this.GetType(), "NewTab", "window.open('" +url+ "');", true);
and still no progress. The redirecting should be done in the server side since I've got to get some data and passed it together with the link.
Usually a browser will block a popup unless the popup is initiated by the user.
If a window opens directly as a result of an onclick it will generally open okay. If the button click indirectly opens the window, it is more likely to be blocked. So your injecting a script tag etc. is likely to be blocked as a webpage being spammy (no-one wants unsolicited popups to appear when they browse).
Putting the window.open directly into the onclick event is the most likely solution to work.
Ultimately however you cannot control the behaviour of the browser though so it may not be possible to do what you want.
Changing the button into a styled hyperlink would work (with a target of _blank or similar). It is not possible to tell a browser to open in a new tab vs a new window.

Internet Explorer PDF Reader

I apologize for the length of this question, but some background explanation is required.
Background:
I'm working on an internal web application that will display a pdf in an iframe. Adobe Reader X offers a "Read Mode" for displaying the pdf in the browser. This removes the shell and adds a menu box to the bottom of the pdf when the mouse is over the "middle center". In versions prior to X you could use JavaScript to modify the toolbar menu items.
What I Need:
I need to display the pdf in "Read Mode" AND remove all menu items. The computers that are to run this app are basically a kiosk and are literally a computer, monitor and mouse. No printers, no keyboard, no other interactivity.
My question is this:
How can I load a pdf for reading in internet explorer, and disable all interaction except scrolling. That is, no menus, no context menu, and no "shadow bar" when the bottom center is moused over. I've been searching for alternatives to Adobe, and there are some great ones, but the ones I've found that have a browser plugin, do not have a plugin for IE.
If someone know a way to just load the PDF Contents that would be an even better route.
More Information:
If it helps, while this is being built for Internet Explorer, the web-page will be loaded into a windows form that is just a full-screen browser control.
Other things I've tried:
I have also tried positioning a transparent iFrame on top of the pdf iFrame, covering only the displayed content, but leaving the scrollbars intact. This worked perfect if I loaded anything BUT a pdf. If I loaded any other website then it worked as expected (except the scroll wheel of course doesn't scroll the webpage). If a pdf was loaded, then all that would appear is a white square. Use the dev toolbar to remove the transparent iFrame and the PDF looks good.
I've found a good partial solution. A product called PDFObject.
Using it, I can add the following JavaScript to my page:
function embedPDF() {
var myPDF = new PDFObject({
url: 'PathToPdf.pdf',
pdfOpenParams: {
scrollbars: '1',
toolbar: '0',
statusbar: '0',
messages: '0',
navpanes: '0'
}
}).embed('DivToLoadPdfInto');
}
window.onload = embedPDF;
This will embed the pdf into a div and remove the toolbar, statusbar, message, navpanes, but still allow scrollbars. This prevents the shadow box menu from appearing, which is great!
Users can still right-click on the pdf so I'll try the standard Right-Click blockers in JavaScript and edit if I can get something to work.
Edit:
A good resource for URL Parameters can be found here.
Ok, so I have come up with a final solution.
Needs:
Load PDF in iFrame in "Reader Mode" but with no shadow bar
PDF Context Menu should be disabled
Addressing the first need is easy with some caveats. Set the source of the iFrame to "pathToPdf/document.pdf#toolbar=0&navpanes=0". The caveats here are that (in IE at least) once this breaks, then the browser needs to be closed and re-opened to work again. So, what can break it?
User right clicks and opens the navigation pane buttons.
User presses the escape button on the keyboard.
The webpage is loaded into the browser control of a windows form, and luckily once broken there it works fine without closing and reopening. Could still be a problem though for the PDF being viewed. I don't have to worry about the escape key, because the app is running on a KIOSK(basically) and there is no keyboard, only a mouse.
Disabling the context menu has proved to be the most difficult thing to do. The web browser control has a "IsWebBrowserContextMenuEnabled" property that when set to "False" will disable the context menu for the browser. Probably because the adobe reader is a plugin, this does not effect the PDF. With Reader X there is no registry setting (that I could find) or JavaScript method, or general setting that will disable context menu.
I was ready to give up when I remembered something. This is a kiosk, and there is no need for the right mouse button. So the solution; modify the registry and turn off the right click for the internal OS. To do that simply open regedit and goto:
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer
Find (or create if it is not there) a DWORD key named "NoViewContextMenu".
A value of 1 will disable right clicking, 0 will enable.
Summary:
Use "pathToPdf/document.pdf#toolbar=0&navpanes=0"
Disable right click in windows registry
Edit:
The registry hack only disables the right mouse button in windows explorer. We are having no luck in finding a good software solution, so we are left with either writing our own custom mouse drivers or simply popping open the mice and removing the physical trigger mechanism.

How can i automate sharepoint people picker with watin

I'm new in automated testing (Watin for IE) and i came across with some issues automating the People picker of sharepoint. I can't take the controls within the pop up because the pop up blocks main page (blocking also Intenet Explorer Developer Tools). Someone knows how can i do it or at least the name of the controls for IE?
Thanks
If it is a modal dialog that is getting you bothered, you can try this.
Open the SharePoint site in Firefox and then open the People-Picker. In firefox you would have the URL displayed for the modal dialog too. And from there you use the URL to open it in a new browser tab directly and use a web development tools plugin to view the DOM.

How to complete some task on click of a button and then navigate to a different page?

I am developing a winform app which is controlling a website. On click of a button in winform app i want to fill a form, click the submit button on the web page upon which a new page will open , on that page i have to click on a link.
How to achieve this. i have tried many things but did not got any proper solution
If you're looking for a web automation tool, use Watin. It allows you to automate via code the opening of a browser, the clicking of buttons, and the fillout of form fields. However, that is more of a testing library.
Honestly though, your choice of solution sounds odd. You're trying to get a client application to make requests through a web page, instead of directly requesting whatever resource yourself from the client application. It's hard to give you a direct solution without a good understanding of the problem space.

How can I disable a label when the IE download window appears?

I have an ASP.NET WebForm.
When users click on a link, it calls another page. That page, in turn, references an assembly and initiates a download, which takes some time to appear as it is rendered on the fly.
The issue:
I want to display a popup message: "Please wait download initiated" when users click the download button. When the Internet Explorer download window appears, I want to disable the original popup.
AFAIK, Elad is true. But you can try one thing. If your are throwing content from page, then just before writing content do the stuffs you want

Categories