Html Unit not being able to find content created by JS - java

I am using the JQuery UI datepicker plugin to dynamically change some page content. I get HtmlUnit to click on the text input, which should make the datepicker popup appear. However when I call driver.findElement(By.id("ui-datepicker-month")) I get an exception - it seems anything which is newly added to the DOM by JS does not become visible to HtmlUnit.
I'm not sure if there is any way around this, but it is worth noting when I use Chromedriver I don't have this problem - I am able to find the new elements. Also the getSource() method only ever returns the original source code with HtmlUnit whereas with Chromedriver it returns the updated source.
Is there any way to support the JS using Html Unit?

Related

Is there any way to verify PDF zoom in / out buttons using selenium?

I want to verify PDF zoom in and out buttons but seems not able to get it done. I tried to open PDF in new tab and then verify but that is also not working.
I can see some tags and divs when inspect.
I tried diff. xpaths like :
//paper-fab[#aria-label='Zoom in']
//paper-feb
But it always returns ZERO when I check with list webelement.
Here is example URL :
https://pdfobject.com/static.html
I suggest you use Sikuli:
Sikuli automates anything you see on the screen. It uses image recognition to identify and control GUI components. It is useful when there is no easy access to a GUI's internal or source code.
I think there is no way to do what you are wanting with just Selenium. If you need to integrate Selenium and Sikuli, you can see this post on SOF: Calling to a Sikuli script from Python (Selenium) . It can give some ideas for you. Yet, maybe you can read a bit about the Robot class.
I hope this helps.

Accessing a pdf opened in a new browser window with Selenium and JAVA

Language - JAVA
IDE - Eclipse
Tool - Selenium Web Driver
I have a test scenario where clicking on a link opens a new window with a pdf content being shown. The PDF shows up a form with Save and Cancel button. We don't get any element identifier using firebug for elements shown on the new window. How can I write a script to tell the driver to identify fields in the pdf and input something there followed by clicking on Save button.
Not sure if you can identify fields in a pdf using webdriver, since, in most cases, the pdf would be embedded in the browser and the fields in the pdf are not Html components.
One thing you might want to check out is using AutoIt scripting and calling this script from your host program, in your case, java program. I have not tried using AutoIt for pdf but used it for other purposes and found that it is not difficult to learn and use.
You can call AutoIt executable from java program as follows:
Runtime.getRuntime().exec("checkPDF.exe");
Since it is an executable, you might have portability issues if you plan on running your webdriver script on other platforms.
Not going to work with Selenium. PDFs are usually displayed using native desktop applications/browser plugins and that is nothing Selenium can handle in general. BTW: what do you intend to test in a PDF formular? Seems pointless to me (at first sight).
What you CAN do is: take a desktop screenshot using AWT. And you should be able to use native desktop methods to simulate mouse clicks and key presses. But that is, well, not so nice [tm] because you may need to record the click coordinates and re-record them every time the PDF changes.
Maybe related: Interacting with a PDF popup in Selenium

Download web page using Java after javascript executes

I have a problem that doesn't seem to be answered clearly in StackOverflow. I want to download a page using Java and retrieve some data from it in order to give some values to an application that I develop. This page is a betting site so it contains javascrit methods to change the betting values.
In order to do some tests I downloaded the page manually using Ctrl-S and then I made a programm (with FileReader, BufferedReader, etc...) which retrieves the data. This worked perfectly. So I would make a bash script to be executed in order to download the page every time when the user opens my application.
After that I searched for methods who download the page programmatically (I used Jsoup, URL, ...). What I noticed is that the javascript variable values couldn't be printed because the javascript code wasn't executed.
What i want to know is that if there is some way to download programmatically the executed website (download the instance of the javascript values) without having to make some bash script to do it every time before someone opens my app.
Try HtmlUnit. It is used for automatic testing but should fit your purpose well too!

SWT Browser -- get selected text

I am using a Browser view in my application and i need to get the text selected by user. I tried creating mouse event but nothing is working. I am using following link to create the browser:
http://180.179.103.253/q3/twapp/process.php?mdl=onetimeurl&id=1061&deviceid=9d2aefc2-a386-4afc-b307-44f41ec8311d&test=1
I also checked in documentation, there is no method like getSelectionText() in SWT browser.
you could achieve this by executing java script function in the browser from your View.Write a java script to get the selection the text and execute it in the browser.
Look at the Browser API:
browser.execute(script);
browser.evaluate(script);
Look at BrowserFunction interface for java-javascript call back mechanism

HtmlUnit stops JavaScript execution after a window.open

I've recently updated from HTMLUnit 2.4 to 2.5 (we'd go for the latest version but there is a lot of code to refactor due to the deprecated APIs). I'm now having a problem with some JavaScript that opens a window.
The page under test, is a 'Please wait while loading screen' for reports. The page opens a new window then redirects back to page that originally launched the print.
So the JavaScript looks something like:
window.open(url,'report_window');
document.location.href = original_url;
With HtmlUnit 2.4 the script would continue executing and if I grabbed the original Window object it would have performed the redirect. However, after upgrading to HtmlUnit 2.5, the original window is still on the 'Please wait' page - the redirect is never executed. It appears as though the JavaScript stopped executing after the call to window.open.
I have confirmed the page behaves correctly if I test manually. I've also tried different JavaScript after the window.open call to confirm that that particular call is not the issue.
Is anyone aware of an issue like this and any potential workarounds? We have to stay on HtmlUnit 2.5 because of jQuery compatibility.
I was able to fix this issue by removing the CurrentWindowTracker object from the web window listeners on the WebClient object. Unfortunately, this field is completely encapsulated, so I had to retrieve it via reflection.
Field windowListeners = WebClient.class
.getDeclaredField("webWindowListeners_");
windowListeners.setAccessible(true);
Collection webWindowListeners = (Collection) windowListeners
.get(webClient);
webWindowListeners.clear();

Categories