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.
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.
Thank you Dmitri for your reply.
I tried to use the "Native" functions, and I have one problem when focus is not on the object that is keystrokes is intend to go to. How can I obtain focus of desired object (Windows dialog box) ?
Also,
Please send me a code samples that you have, so I could look at them.
Thank you very much for your help.
Kirill.
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.
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
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
