HtmlUnit javascript event - htmlunit

I successfull done login with HtmlUnit on SITE, run a form search and got a page with results.
Now in real world, to get the whole page result I need to scroll the page more times .
But I am using HtmlUnit ,then I have done :
List<DIV> res=page.getByXPath(...) // got 13 items
ScriptResult res=page.executeJavaScript("window.scrollBy (0,2000);");
res=page.getByXPath(...) // would get other items,same query as the first
The problem is that the query on page , after the above code is returning the same items as before run JS , the XPath query return same result items .
Instead ,on browser, the scroll work well and return the seconf train of result items.
Then , does HtmlUnit is not designed for this ? a bug ? or there is another HtmlUnit trick?
I tried also
ScriptResult res =page.getBody().fireEvent("scroll");
thanks

As HtmlUnit is a headless browser, the scrolling support is still weak. Please open an issue at gitub about this (including sample code) and i will try to improve the support if possible.

Related

How does capybara/selenium grab current URL? Issue with single page site

I am using ruby and capybara(which leverages selenium) to automate walking through a website. After navigating to a new page I verify that the new page URL is what i'm expecting. My issue comes when I walk through an order funnel that is a single page but loads different views.
Some code...
I create my session instance then have additional code opening the browser and walking to a certain point in the website that I wont include
$session = Capybara::Session.new(:selenium)
My line for checking the browser URL without search params ie: everything after '?'
if url == $session.current_url.to_s.split("?")[0]
urlCorrect = true
end
This code works fine when my URL is
https://www.homepage.com
Then I click on a link that takes me to my order funnel ... https://www.homepage.com/order#/orderpage1?option1=something&option2=somethingelse
My function still matches the expected URL. But the issue comes when I move to the second page of the order funnel :
https://www.homepage.com/order#/orderpage2?option1=something&option2=somethingelse
My capybara code to get current url still returns the URL from orderpage1. Im guessing its because there is no postback when moving from orderpage1 to orderpage2 but i dont know how to force a postback or tell capybara to re-grab the url
Any advice will be appreciated.
Thanks
Quick Edit: I forgot to mention this behavior is only in IE. Chrome and Firefox both work correctly with the exact same code
Capybara grabs the current_url by querying the browser - it doesn't cache it. The issue you're probably running into is that clicking the link to move to the next page doesn't wait for the page change to happen, so if you call current_url before the page load has happened you'll still get the original url. There are two solutions for that - 1. use capybara to look for content that doesn't appear until the new page is loaded ( have_content ), 2. use the has_current_path? method which will wait/retry for a period of time until the current_path/url match
$session.has_current_path?('expected path')
There are options if you want to match against the full url, and you can use a regex to match as well - http://www.rubydoc.info/gems/capybara/Capybara/SessionMatchers#has_current_path%3F-instance_method
Thanks to Tom Walpole for finding the bug report for this issue. This link sums up the root of the issue and provides a few workarounds if anyone else is encountering this issue.
https://github.com/angular/protractor/issues/132

How a verify a page in transition using selenium

I clicked on a button on some page which redirects me to some other page.but between these pages there is one page which comes only for 1 or 2 seconds. I have to verify that page.I am using selenium.
Any suggestions?
What you could do here, is do a simple waitForElement subroutine.
Ideally, when your test framework cannot find an element to operate with, it will fail.
So we can assume, that when your test gets past waitForElement(theInterimElement) then your interim element has appeared, and we can continue.
Check the url of the page is the url of the desired page and wait while this is not true.
while (!webDriver.Url.Contains(desiredUrl.ToString()))
Thread.Sleep(50);

jquery ajax search form

I have a REST Url http://localhost/issues presenting me a search form for querying issues of a bug tracker system. The search form has a textfield with name="f". The action of the form is action="/issues" and the method=GET. So if I type "foobar" into the textfield, I am getting redirected to http://localhost/issues?f=foobar.
To prevent page reload, I use AJAX with jquery. So I used $(document).on('submit',...) to bind an AJAX query function to the submit function of the form. I used firebug to proove, that jquery sends the request and no page reload is taken. I also have a function to fill results of the response into my site presented in the browser. Everything works fine for the first search attempt.
But if I type another search string "anotherfoobar" into the textfield and resend the request, the request URL is http://localhost/issues?f=foobar&f=anotherfoobar, so the search parameter is only appended to the URL, not updatedas I thought it would happen.
I've read about "hashing" the URL parameters to prevent this behaviour here on stackoverflow, but do not fully understand what and why this is happening and what "hashing" means exactly. Perhaps it's not the right solution for me. So I kindly ask someone to explain me how I can solve this.
I've solved it! Don't ask how exactly, what I did was the following:
The AJAX response is the whole Web page as I would get it with a page reload. with jquery I select a div which includes the search form AND the results.
I simply defined a div only for the results, and update only the content of the results div. After doing so, everything works fine, and the results div updates with multiple search attempts.
Another case of not knowing what I do, but I do it fine ;)
Perhaps someone still can explain me my error so wisdom can rise.

Page does not change after I set an option button with HtmlUnit

I am using HtmlUnit to navigate through the Web of knowledge web page. I am using the code below to set an option button so that results on the page would be sorted appropriately. Unfortunately, nothing happens when I execute the code. Results on the page remains sorted in the same way as they ware before.
HtmlSelect ssort = (HtmlSelect) pageX.getFirstByXPath("//*[#id=\'topNavBar\']/tbody/tr/td[3]/form/select");
HtmlOption optionA = ssort.getOptionByValue("LC.D;PY.D;AU.A;SO.A;VL.D;PG.A");
ssort.setSelectedAttribute(optionA, true);
ssort.click();
I debuged the code and there is no errors. Do you have any idea what am I doing wrong?
As a general rule, functions like the .click() and .setSelectedAttribute(HTMLOption, boolean)(See JavaDoc) will return a HTMLPage that in most cases is the same as the current one, however in your case it will return a different HTMLPage. So to capture the new page you just need to assign the return value to a HTMLPage.
N.B: You could also use getCurrentWindow() on the WebClient instance.

How to handle google search ajax data using selenium

How to handle google search ajax data using selenium (Type some string in google search, do not press enter key and check matching string data in search text box) ? How to get this data using selenium RC/Webdriver ?
If you know that the action of some event (e.g. sendKeys, onClick, whatever) is going to trigger some event - like an Ajax request - you should be using waitFor until your condition is met (see the Advanced Usage guide).
To avoid supplying a brittle timeout threshold on your test, you may want to call this in a Poll implementation, say once every 500ms 10 times for example, and then pass/fail accordingly
FitLibraryWeb has some nice, clean abstractions for this, but you'd need to use Fitnesse of course
Thank u friends .. I am able to run a google search from selenium example code using firefox driver : http://seleniumhq.org/docs/03_webdriver.html#introducing-the-selenium-webdriver-api-by-example
Now i am trying to click on 'Change Location' available in left side pane after getting the search result but no luck. Code sample :
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
WebElement element = driver.findElement(By.name("q"));
element.sendKeys("Cheese!");
element.submit();
WebElement expandLocation = driver.findElement(By.id("expand_location_link"));
expandLocation.click();
Debug info:
In view source i am not able to see the element expand_location_link but is visible in element inspect section of firefox/chrome or after saving the page from browser to local system.

Resources