Redirection using Selenium WebDriver(HTMLUnitDriver)

14,404 views
Skip to first unread message

SUBRAMANYESWARA RAO BHAVIRISETTY

unread,
Dec 20, 2014, 11:44:55 AM12/20/14
to webd...@googlegroups.com
Hi,

  I have the following test case which needs to be automated.

1> We have html page wherein user gives username and password and then clicks on "signin".
2> When we click on "signin", server sends call back url with 302 response.
3> Browser follows 302 and goes to the callback url after which some other actions happen.

I was trying to automate the above scenario using selenium webdriver HTMLUnitDriver. Sorry if I am asking very basic question, I am newbie to Webdriver.

      
        driver.get(url);
        //driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        System.out.println("Source after initial implicit wait is " + driver.getPageSource());
        driver.findElement(By.name("username")).sendKeys("testuser");
        driver.findElement(By.name("passwd")).sendKeys("password");

        driver.findElement(By.name("signin")).submit();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

In the above code, when I do submit, it should take me to the redirected url as I observed with the browser but Webdriver always lands me on the same page without following the redirect/callback url.

As per the documentation, get is supposed to follow redirect urls.

public void get(java.lang.String url)
Description copied from interface: WebDriver
Load a new web page in the current browser window. This is done using an HTTP GET operation, and the method will block until the load is complete. This will follow redirects issued either by the server or as a meta-redirect from within the returned HTML. Should a meta-redirect "rest" for any duration of time, it is best to wait until this timeout is over, since should the underlying page change whilst your test is executing the results of future calls against this interface will be against the freshly loaded page. Synonym forWebDriver.Navigation.to(String).

Can anyone please let me know what I could be doing wrong or if there's any basic problem with my approach?
Thanks in advance.

~Subramanyam

darrell

unread,
Dec 25, 2014, 12:15:05 PM12/25/14
to webd...@googlegroups.com
You should be able to simulate a human using the browser. You should not need to do anything differently then when testing it manually. The only thing to remember is that Selenium is a LOT faster than a human. So I might think the test case is:

- go to URL
- see the final page

but what is really happening is:

- go to URL
- page returns a 302 redirect
- browser goes to the new URL
- we wait for the new page to load
- we see something on the page which lets us know the final page is loaded
- see the final page

So in Selenium you need to wait for the redirecting to happen. You don't need to do anything to make the redirect happen. That should just happen automatically. However, I often wait until I see something on the screen to tell me the final page has finished loading. What you can use is WebDriverWait to wait for an element to be visible, e.g. let's say the element we are waiting for us located by the CSS selector "div#someid" then we'd have:

    WebDriverWait wdw = new WebDriverWait(driver, 30); // wait up to 30 seconds
    wdw.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div#someid")));

This will wait until the element located by By.cssSelector("div#someid") appears on the screen or until 30 seconds. If it does not appear after 30 seconds your test will fail.

Krishnan Mahadevan

unread,
Dec 30, 2014, 4:00:34 AM12/30/14
to webdriver
You can try one of the following :

1. Instantiate your WebDriver as below and see if that works :
HtmlUnitDriver driver= new HtmlUnitDriver();
driver.setJavascriptEnabled(true);

or

HtmlUnitDriver driver = new HtmlUnitDriver(true);

This would ensure that all redirects triggered by Javascript are automatically taken care of by HtmlUnitDriver

2. You can consider sub-classing HtmlUnitDriver and then setup the redirection enabling part in the underlying webClient by invoking the 
getWebClient().getOptions().setRedirectEnabled(true);

Please check which of them works for you.



Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to webdriver+...@googlegroups.com.
To post to this group, send email to webd...@googlegroups.com.
Visit this group at http://groups.google.com/group/webdriver.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages