8

driver.Navigate().GoToUrl("/") sets the location to "/" instead of "http://www.domain.com/"

another example would be

driver.Navigate().GoToUrl("/view1") sets the location to "/view1" instead of "http://www.domain.com/view1"

Either example would cause the browser to return with address isn't valid.

ton.yeung
  • 4,169
  • 5
  • 38
  • 68
  • testing routes. a relative path from the root is shorter than localhost/whatever and its less to type too. I've made the root itself a global constant, but its still a pain. – ton.yeung May 16 '13 at 00:12

3 Answers3

5

you can use a Java URI to calculate a path relative to the current uri or domain:

import java.net.URI;

driver.get("http://example.org/one/");

// http://example.org/one/two/
driver.get(new URI(driver.getCurrentUrl()).resolve("two/").toString());

// http://example.org/one/two/three/?x=1
driver.get(new URI(driver.getCurrentUrl()).resolve("three/?x=1").toString());

// http://example.org/one/two/three/four/?y=2
driver.get(new URI(driver.getCurrentUrl()).resolve("./four/?y=2").toString());

// http://example.org/one/two/three/five/
driver.get(new URI(driver.getCurrentUrl()).resolve("../five/").toString());

// http://example.org/six
driver.get(new URI(driver.getCurrentUrl()).resolve("/six").toString());

If you're able to calculate the url without using getCurrentUrl(), it might make your code more readable.

Alexander Taylor
  • 13,171
  • 10
  • 56
  • 75
3

The solution now is to use:

driver.Navigate().GoToRelativePath("/view1");

and you will be navigated within the same domain.

UPDATE: This was valid in Selenium WebDriver 2.42 but does not seem to be listed in 3.1 the solution would be

driver.Navigate().GoToUrl(baseUrl + "/view1")

Michael Nakayama
  • 618
  • 2
  • 7
  • 20
0

This is probably the shortest way to navigate to a specific url when they all have the same domain:

private String baseUrl = "http://www.domain.com/";

[...]

driver.get(baseUrl + "url");

driver.get(String url) is equivalent to driver.navigate().to(String url).

Marco
  • 458
  • 7
  • 22