Skip to content

Commit

Permalink
implement getting context from Firefox driver
Browse files Browse the repository at this point in the history
  • Loading branch information
titusfortner committed Sep 28, 2021
1 parent c66b1ea commit b8caa25
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 18 deletions.
18 changes: 14 additions & 4 deletions java/src/org/openqa/selenium/firefox/AddHasContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@
@AutoService({AdditionalHttpCommands.class, AugmenterProvider.class})
public class AddHasContext implements AugmenterProvider<HasContext>, AdditionalHttpCommands {

public static final String CONTEXT = "context";
public static final String SET_CONTEXT = "setContext";
public static final String GET_CONTEXT = "getContext";

private static final Map<String, CommandInfo> COMMANDS = ImmutableMap.of(
CONTEXT, new CommandInfo("/session/:sessionId/moz/context", HttpMethod.POST));
SET_CONTEXT, new CommandInfo("/session/:sessionId/moz/context", HttpMethod.POST),
GET_CONTEXT, new CommandInfo("/session/:sessionId/moz/context", HttpMethod.GET));

@Override
public Map<String, CommandInfo> getAdditionalCommands() {
Expand All @@ -63,8 +65,16 @@ public void setContext(FirefoxCommandContext context) {
Require.nonNull("Firefox Command Context", context);

executeMethod.execute(
CONTEXT,
ImmutableMap.of(CONTEXT, context));
SET_CONTEXT,
ImmutableMap.of("context", context));
}

@Override public FirefoxCommandContext getContext() {
String context = (String) executeMethod.execute(
GET_CONTEXT,
null);

return FirefoxCommandContext.fromString(context);
}
};
}
Expand Down
11 changes: 11 additions & 0 deletions java/src/org/openqa/selenium/firefox/FirefoxCommandContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,15 @@ public enum FirefoxCommandContext {
public String toString() {
return String.valueOf(text);
}

public static FirefoxCommandContext fromString(String text) {
if (text != null) {
for (FirefoxCommandContext b : FirefoxCommandContext.values()) {
if (text.equalsIgnoreCase(b.text)) {
return b;
}
}
}
return null;
}
}
5 changes: 5 additions & 0 deletions java/src/org/openqa/selenium/firefox/FirefoxDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,11 @@ public void setContext(FirefoxCommandContext commandContext) {
context.setContext(commandContext);
}

@Override
public FirefoxCommandContext getContext() {
return context.getContext();
}

private static Boolean forceMarionetteFromSystemProperty() {
String useMarionette = System.getProperty(SystemProperty.DRIVER_USE_MARIONETTE);
if (useMarionette == null) {
Expand Down
7 changes: 7 additions & 0 deletions java/src/org/openqa/selenium/firefox/HasContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,11 @@ public interface HasContext {
*/
void setContext(FirefoxCommandContext context);

/**
* Current context commands are operating on.
*
* @return {@link FirefoxCommandContext} value currently operating on commands
*/
FirefoxCommandContext getContext();

}
18 changes: 4 additions & 14 deletions java/test/org/openqa/selenium/firefox/FirefoxDriverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -516,21 +516,11 @@ public void canTakeFullPageScreenshot() {

@Test
public void canSetContext() {
FirefoxOptions options = new FirefoxOptions();
String dir = "foo/bar";
options.addPreference("browser.download.dir", dir);

// Need to set the preference to be able to read it
WebDriver driver = new WebDriverBuilder().get(options);
HasContext context = (HasContext) driver;

try {
((HasContext) driver).setContext(FirefoxCommandContext.CHROME);
String result = (String) ((JavascriptExecutor) driver)
.executeScript("return Services.prefs.getStringPref('browser.download.dir')");
assertThat(result).isEqualTo(dir);
} finally {
driver.quit();
}
assertThat(context.getContext()).isEqualTo(FirefoxCommandContext.CONTENT);
context.setContext(FirefoxCommandContext.CHROME);
assertThat(context.getContext()).isEqualTo(FirefoxCommandContext.CHROME);
}

private static class CustomFirefoxProfile extends FirefoxProfile {}
Expand Down

0 comments on commit b8caa25

Please sign in to comment.