5 Replies Last post: Jun 29, 2009 9:33 AM by Dmitri Tikhanski  
kirill Beginner 2 posts since
May 28, 2009
Currently Being Moderated

May 28, 2009 9:47 AM

How to access browser print menu

In my automation I need to access browser print menu to print web page as PDF.

Please let me know if it is possible.


Thank you,

Kirill.

Dmitri Tikhanski Beginner 24 posts since
Feb 24, 2009
Currently Being Moderated
1. May 28, 2009 11:41 AM in response to: kirill
Re: How to access browser print menu

It's impossible by means of Selenium. You can use native input (e.g.

 

selenium.keyDownNative("17"); // stands for control

selenium.keyDownNative("80"); // stands for "P"

selenium.keyUpNative("17");)

 

or create an instance of Java.awt.robot class, or access browser's context menu by native simulating right mouse click, or use WinAPI for these purposes - whatever.

 

Feel free to ask if you need more assistanse or code samples.

Dmitri Tikhanski Beginner 24 posts since
Feb 24, 2009
Currently Being Moderated
3. Jun 10, 2009 12:44 PM in response to: kirill
Re: How to access browser print menu

Hi Kirill,

 

Please find sample code (in Java) attached:

 

I use it for setting focus for browser as somewhere in my tests I have to use native input.

 

1. Download and import Nativecall library (it's free and allows access to WinApi functions) http://johannburkard.de/software/nativecall/

 

2. Use following code as below (I'll add comments to all functions)

 

NativeCall.init(); // - initialize NativeCall and load corresponding dll

 

IntCall hwnd = new IntCall("User32", "FindWindowA"); //- hwnd stands for window handle, FindWindowA is WinAPI function to get window handle by it's caption - in your case the title you dialog box. User32 stands for user32.dll containing this API function

 


// get window handle

 

int i=0;

 

i= i = hwnd.executeCall(new Object[]{null, "here comes your dialog caption}); // - get window handle

 

hwnd.destroy(); // - release resource as Java GC cannot reach it

 


IntCall setfg = new IntCall("User32", "SetForegroundWindow"); // initialize WinAPI function which brings window to front and sets it active

 

 

 

boolean success = setfg.executeBooleanCall(new Object[]{i});

 

setfg.destroy();

 

if (!success || i==0)
{
   System.out.println("Something is wrong"); }

 


Please feel free to contact me if you have further queries,

 

Regards,

 

Dmitri.

Ara Guru 218 posts since
May 22, 2009
Currently Being Moderated
4. Jun 29, 2009 9:19 AM in response to: Dmitri Tikhanski
Re: How to access browser print menu

Hi Dmitri,

 

This code works well. Now i am trying to click on Print OR Cancel buttons(Using sendMessage WinApi function). But it doesn't work. Is this correct WinApi function?

 

Thanks,

Ara

Dmitri Tikhanski Beginner 24 posts since
Feb 24, 2009
Currently Being Moderated
5. Jun 29, 2009 9:33 AM in response to: Ara
Re: How to access browser print menu

Hi Kirill,

 

You can try using PostMessage function (it's better than SendMessage being asynchronous and putting messages in queue. But you'll need to know hwnd of OK/Cancel button).

 

I believe that Your browser accepts hotkeys in the print dialog and You can easily reach them quite like as in my first response:

 

e.g.

 

selenium.keyDownNative("10"); - ten stands for "enter"

selenium.keyUpNative("10"); - ten stands for "enter"

 

elsewise You can try

selenium.keyPressNative("10"); - some browsers use different javascript events to track key presses.

 

Same for cancel:

 

escape keykode is 27, so it'll look like

 

selenium.keyPressNative("27");

 

btw: You can use KeyEvent class so you won't need to determine keycode any other way:

 

e.g. selenium.keyPressNative(String.valueOf(KeyEvent.VK_ENTER));

 

You will need to add following line into import section of Your class to access VK's definitions:

 

import java.awt.event.KeyEvent;

 

Hope this helps,

 

Dmitri

More Like This

  • Retrieving data ...