Skip to content
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 from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 16 additions & 7 deletions src/main/java/com/codeborne/selenide/Selenide.java
Expand Up @@ -142,7 +142,7 @@ public static <PageObjectClass> PageObjectClass open(URL absoluteUrl,
* 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);
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -646,12 +646,12 @@ public static <PageObjectClass, T extends PageObjectClass> PageObjectClass page(

/**
* 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() {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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());

Expand Down
29 changes: 29 additions & 0 deletions src/test/java/integration/UserAgentTest.java
@@ -0,0 +1,29 @@
package integration;

import com.codeborne.selenide.Configuration;
import org.apache.commons.lang3.StringUtils;
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 org.hamcrest.CoreMatchers.not;
import static org.hamcrest.text.IsEmptyString.isEmptyOrNullString;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeFalse;

public class UserAgentTest extends IntegrationTest {
@Test
public void currentUserAgentTest() {
assumeFalse(isHtmlUnit());

open("/start_page.html");
String userAgent = getUserAgent();
String browser = Configuration.browser;

assertThat(userAgent, not(isEmptyOrNullString()));
assertTrue(String.format("Current user agent [%s] should belong to '%s' browser", userAgent, browser),
StringUtils.containsIgnoreCase(userAgent, browser));
}
}