Skip to content

Commit 9985d1f

Browse files
committed
Using baseUrl passed in the command line to open relative URLs
1 parent ba56ad1 commit 9985d1f

3 files changed

Lines changed: 24 additions & 22 deletions

File tree

java/server/src/org/openqa/selenium/server/htmlrunner/CoreTestCase.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ public CoreTestCase(String url) {
5252
this.url = Preconditions.checkNotNull(url);
5353
}
5454

55-
public void run(Results results, WebDriver driver, Selenium selenium) {
55+
public void run(Results results, WebDriver driver, Selenium selenium, URL baseUrl) {
5656
String currentUrl = driver.getCurrentUrl();
5757
if (!url.equals(currentUrl)) {
5858
driver.get(url);
5959
}
6060

6161
// Grabbing the steps modifies the underlying HTML...
62-
List<LoggableStep> steps = findCommands(driver);
62+
List<LoggableStep> steps = findCommands(driver, baseUrl);
6363
// ... which we now grab so we can process it later.
6464
String rawSource = getLoggableTests(driver);
6565

@@ -96,17 +96,18 @@ private String getLoggableTests(WebDriver driver) {
9696
"return trElement.outerHTML;"));
9797
}
9898

99-
private List<LoggableStep> findCommands(WebDriver driver) {
100-
// Figure out the base url, if there is one.
101-
List<WebElement> allLinks = driver.findElements(By.xpath("//head/link[@rel='selenium.base']"));
102-
// Only use the first one (if there's one at all)
103-
URL baseUrl = null;
104-
if (!allLinks.isEmpty()) {
105-
String href = allLinks.get(0).getAttribute("href");
106-
try {
107-
baseUrl = new URL(href);
108-
} catch (MalformedURLException e) {
109-
throw new SeleniumException("Base URL for test cannot be parsed: " + href);
99+
private List<LoggableStep> findCommands(WebDriver driver, URL baseUrl) {
100+
if (baseUrl == null) {
101+
// Figure out the base url, if it is not specified and there is one in the test case file.
102+
List<WebElement> allLinks = driver.findElements(By.xpath("//head/link[@rel='selenium.base']"));
103+
// Only use the first one (if there's one at all)
104+
if (!allLinks.isEmpty()) {
105+
String href = allLinks.get(0).getAttribute("href");
106+
try {
107+
baseUrl = new URL(href);
108+
} catch (MalformedURLException e) {
109+
throw new SeleniumException("Base URL for test cannot be parsed: " + href);
110+
}
110111
}
111112
}
112113

java/server/src/org/openqa/selenium/server/htmlrunner/CoreTestSuite.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.openqa.selenium.WebDriver;
2626
import org.openqa.selenium.WebElement;
2727

28+
import java.net.URL;
2829
import java.util.List;
2930

3031
public class CoreTestSuite {
@@ -35,7 +36,7 @@ public CoreTestSuite(String url) {
3536
this.url = url;
3637
}
3738

38-
public Results run(WebDriver driver, Selenium selenium) {
39+
public Results run(WebDriver driver, Selenium selenium, URL baseUrl) {
3940
if (!url.equals(driver.getCurrentUrl())) {
4041
driver.get(url);
4142
}
@@ -69,7 +70,7 @@ public Results run(WebDriver driver, Selenium selenium) {
6970
Results results = new Results(rawSuite);
7071

7172
for (String testUrl : allTestUrls) {
72-
new CoreTestCase(testUrl).run(results, driver, selenium);
73+
new CoreTestCase(testUrl).run(results, driver, selenium, baseUrl);
7374
}
7475

7576
return results;

java/server/src/org/openqa/selenium/server/htmlrunner/HTMLLauncher.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public class HTMLLauncher {
7777
* Launches a single HTML Selenium test suite.
7878
*
7979
* @param browser - the browserString ("*firefox", "*iexplore" or an executable path)
80-
* @param browserURL - the start URL for the browser
80+
* @param startURL - the start URL for the browser
8181
* @param suiteURL - the relative URL to the HTML suite
8282
* @param outputFile - The file to which we'll output the HTML results
8383
* @param timeoutInSeconds - the amount of time (in seconds) to wait for the browser to finish
@@ -86,7 +86,7 @@ public class HTMLLauncher {
8686
*/
8787
public String runHTMLSuite(
8888
String browser,
89-
String browserURL,
89+
String startURL,
9090
String suiteURL,
9191
File outputFile,
9292
long timeoutInSeconds,
@@ -107,10 +107,10 @@ public String runHTMLSuite(
107107
WebDriver driver = null;
108108
try {
109109
driver = createDriver(browser);
110-
URL suiteUrl = determineSuiteUrl(browserURL, suiteURL);
110+
URL suiteUrl = determineSuiteUrl(startURL, suiteURL);
111111

112112
driver.get(suiteUrl.toString());
113-
Selenium selenium = new WebDriverBackedSelenium(driver, browserURL);
113+
Selenium selenium = new WebDriverBackedSelenium(driver, startURL);
114114
selenium.setTimeout(String.valueOf(timeoutInMs));
115115
if (userExtensions != null) {
116116
selenium.setExtensionJs(userExtensions);
@@ -119,7 +119,7 @@ public String runHTMLSuite(
119119
if (allTables.isEmpty()) {
120120
throw new RuntimeException("Unable to find suite table: " + driver.getPageSource());
121121
}
122-
Results results = new CoreTestSuite(suiteUrl.toString()).run(driver, selenium);
122+
Results results = new CoreTestSuite(suiteUrl.toString()).run(driver, selenium, new URL(startURL));
123123

124124
HTMLTestResults htmlResults = results.toSuiteResult();
125125
try (Writer writer = Files.newBufferedWriter(outputFile.toPath())) {
@@ -143,7 +143,7 @@ public String runHTMLSuite(
143143
}
144144
}
145145

146-
private URL determineSuiteUrl(String browserUrl, String suiteURL) throws IOException {
146+
private URL determineSuiteUrl(String startURL, String suiteURL) throws IOException {
147147
if (suiteURL.startsWith("https://") || suiteURL.startsWith("http://")) {
148148
return verifySuiteUrl(new URL(suiteURL));
149149
}
@@ -184,7 +184,7 @@ private URL determineSuiteUrl(String browserUrl, String suiteURL) throws IOExcep
184184
}
185185

186186
// Well then, it must be a URL relative to whatever the browserUrl. Probe and find out.
187-
URL browser = new URL(browserUrl);
187+
URL browser = new URL(startURL);
188188
return verifySuiteUrl(new URL(browser, suiteURL));
189189
}
190190

0 commit comments

Comments
 (0)