New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to get current "User-Agent" for browser session. Related to #639 #657

Closed
wants to merge 4 commits into
base: master
from
Jump to file or symbol
Failed to load files and symbols.
+72 −10
Diff settings

Always

Just for now

Next

Add ability to get UserAgent.

Set User-Agent for HttpGet in DownloadFileWithHttpRequest.
  • Loading branch information...
rosolko committed Dec 28, 2017
commit ee488d3e3f8a6c638f97667b533d4d2b066c6050
@@ -142,7 +142,7 @@ private static void mockModalDialogs() {
* Open a web page using Basic Auth credentials and create PageObject for it.
* @return PageObject of given class
*/
public static <PageObjectClass> PageObjectClass open(String relativeOrAbsoluteUrl,
public static <PageObjectClass> PageObjectClass open(String relativeOrAbsoluteUrl,
String domain, String login, String password,
Class<PageObjectClass> pageObjectClassClass) {
open(relativeOrAbsoluteUrl, domain, login, password);
@@ -603,10 +603,10 @@ private static void checkDialogText(String expectedDialogText, String actualDial
/**
* Switch to window/tab/frame/parentFrame/innerFrame/alert.
* Allows switching to window by title, index, name etc.
*
*
* Similar to org.openqa.selenium.WebDriver#switchTo(), but all methods wait until frame/window/alert
* appears if it's not visible yet (like other Selenide methods).
*
*
* @return SelenideTargetLocator
*/
public static SelenideTargetLocator switchTo() {
@@ -646,12 +646,12 @@ public static WebElement getFocusedElement() {
/**
* Create a org.openqa.selenium.support.ui.FluentWait instance with Selenide timeout/polling.
*
* Sample usage:
*
* Sample usage:
* {@code
* Wait().until(invisibilityOfElementLocated(By.id("magic-id")));
* }
*
*
* @return instance of org.openqa.selenium.support.ui.FluentWait
*/
public static FluentWait<WebDriver> Wait() {
@@ -715,7 +715,7 @@ else if (errors instanceof Map) {
} catch (WebDriverException | UnsupportedOperationException cannotExecuteJs) {
log.warning(cannotExecuteJs.toString());
return emptyList();
}
}
}
private static List<String> errorsFromList(List<Object> errors) {
@@ -822,6 +822,15 @@ public static void clearBrowserLocalStorage() {
executeJavaScript("localStorage.clear();");
}
/**
* Get current user agent from browser session
*
* @return browser user agent
*/
public static String getUserAgent() {
return executeJavaScript("return navigator.userAgent;");
}
private static List<LogEntry> getLogEntries(String logType, Level logLevel) {
try {
return getWebDriver().manage().logs().get(logType).filter(logLevel);
@@ -38,15 +38,16 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static com.codeborne.selenide.Selenide.getUserAgent;
import static com.codeborne.selenide.impl.Describe.describe;
import static org.apache.commons.io.FileUtils.copyInputStreamToFile;
import static org.apache.http.client.protocol.HttpClientContext.COOKIE_STORE;
public class DownloadFileWithHttpRequest {
private static final Logger log = Logger.getLogger(DownloadFileWithHttpRequest.class.getName());
public static boolean ignoreSelfSignedCerts = true;
public File download(WebElement element) throws IOException {
String fileToDownloadLocation = element.getAttribute("href");
if (fileToDownloadLocation == null || fileToDownloadLocation.trim().isEmpty()) {
@@ -83,7 +84,8 @@ protected HttpResponse executeHttpRequest(String fileToDownloadLocation) throws
.setCookieSpec(CookieSpecs.STANDARD)
.build()
);
httpGet.setHeader("User-Agent", getUserAgent());
HttpContext localContext = new BasicHttpContext();
localContext.setAttribute(COOKIE_STORE, mimicCookieState());
@@ -0,0 +1,51 @@
package integration;
import com.codeborne.selenide.Configuration;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.junit.Test;
import static com.codeborne.selenide.Selenide.getUserAgent;
import static com.codeborne.selenide.Selenide.open;
import static com.codeborne.selenide.WebDriverRunner.isHtmlUnit;
import static integration.UserAgentTest.CaseInsensitiveSubstringMatcher.containsIgnoringCase;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.text.IsEmptyString.isEmptyOrNullString;
import static org.junit.Assert.assertThat;
import static org.junit.Assume.assumeFalse;
public class UserAgentTest extends IntegrationTest {
@Test
public void currentUserAgentTest() {
assumeFalse(isHtmlUnit());
open("/start_page.html");
String userAgent = getUserAgent();
assertThat(userAgent, not(isEmptyOrNullString()));
assertThat(userAgent, containsIgnoringCase(Configuration.browser));
}
public static class CaseInsensitiveSubstringMatcher extends TypeSafeMatcher<String> {
private final String subString;
private CaseInsensitiveSubstringMatcher(final String subString) {
this.subString = subString;
}
static Matcher<String> containsIgnoringCase(final String subString) {
return new CaseInsensitiveSubstringMatcher(subString);
}
@Override
protected boolean matchesSafely(final String actualString) {
return actualString.toLowerCase().contains(this.subString.toLowerCase());
}
@Override
public void describeTo(final Description description) {
description.appendText("containing substring \"" + this.subString + "\"");
}
}
}
ProTip! Use n and p to navigate between commits in a pull request.