Lastwall
Lastwall

Reputation: 239

Selenium WebDriver using JavaScript to change self.location, query string not sent

I am using Selenium.Net to test an ASP.Net application.

There is an button ASP.Net button on the page that calls a JavaScript. This JavaScript changes the self.location and adds a query string.

When I test it through Selenium, the query string is not present which causes the test to fail (the query string is read by the page being redirected to). The page location is updated correctly though. When I do this manually the query string is added correctly.

Here is the button and it's JavaScript:

<asp:Button ID="btnDoSearch" runat="server" CausesValidation="false" OnClientClick="QuickSearch();return false;" />

<script type="text/javascript">
    function QuickSearch() {
        var txtValueToSearch = $find("<%= QuickSearchTextbox.ClientID %>");                        
        self.location = "http://baseurl.com/Pages/QuickSearch.aspx?s=" + encodeURIComponent(txtValueToSearch.get_value());
    }
</script>

Adding an alert in the QuickSearch function returns the correct value.

Here is the relevant part of the test:

driver.FindElement(By.Id("txtValueToSearch")).SendKeys("test");
driver.FindElement(By.Id("btnDoSearch")).Click();        
wait.Until(ExpectedConditions.ElementExists(QuickSearchPage.SearchResults));

When the test is run, the word "test" is correctly sent to the search text box. The button is correctly pressed. The page QuickSearch.aspx pages load. But without the ?s= query string.

I have tried using Actions instead of the driver and also to add explicit waits and Thread.sleep but it did not correct the problem.

Upvotes: 1

Views: 136

Answers (1)

Buaban
Buaban

Reputation: 5137

You can get text from property 'value'. See code below:

function QuickSearch() {
        var txtValueToSearch = document.getElementById('txtValueToSearch');                        
        self.location = "http://baseurl.com/Pages/QuickSearch.aspx?s=" + encodeURIComponent(txtValueToSearch.value);
}

Upvotes: 1

Related Questions