Skip to main content

_java

Example 1

First let's import our requisite classes (for annotations (e.g., org.junit.After, etc.), driving the browser with Selenium (e.g., org.openqa.selenium.WebDriver, etc.), and matchers for our assertions (e.g., org.hamcrest.CoreMatchers, etc.)) and start our class with some setup and teardown methods.

// filename: WorkWithBasicAuth.java
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;

public class WorkWithBasicAuth {
WebDriver driver;

@Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
}

@After
public void tearDown() throws Exception {
driver.quit();
}
// ...

Now to add our test.

// filename: WorkWithBasicAuth.java
// ...
@Test
public void workWithBasicAuthTest() {
driver.get("http://admin:admin@the-internet.herokuapp.com/basic_auth");
String pageMessage = driver.findElement(By.cssSelector("p")).getText();
assertThat(pageMessage, containsString("Congratulations!"));
}

}

In the test we're loading the page by passing in the username and password in the front of the URL (e.g., http://admin:admin@). Once it loads we grab text from the page to make sure we ended up in the right place.

Example 2

Alternatively, we could have accessed this page as part of the test setup (after creating an instance of Selenium). This would have cached the Basic Auth session in the browser, enabling us to visit the page again without having to specify credentials. This is particularly useful if you have numerous pages behind Basic Auth.

Here's what the script would look like.

// filename: WorkWithBasicAuth2.java
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;

public class WorkWithBasicAuth2 {
WebDriver driver;

@Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
driver.get("http://admin:admin@the-internet.herokuapp.com/basic_auth");
}

@After
public void tearDown() throws Exception {
driver.quit();
}

@Test
public void workWithBasicAuthTest2() {
driver.get("http://the-internet.herokuapp.com/basic_auth");
String pageMessage = driver.findElement(By.cssSelector("p")).getText();
assertThat(pageMessage, containsString("Congratulations!"));
}

}

NOTE: If your application serves both HTTP and HTTPS pages from behind Basic Auth then you will need to load one of each type before executing your test steps. Otherwise you will get authorization errors when switching between HTTP and HTTPS because the browser can't use Basic Auth credentials interchangeably (e.g. HTTP for HTTPS and vice versa).

Expected Behavior

When you save the first example and run it here is what will happen:

  • Open the browser
  • Visit the page using Basic Auth
  • Get the page text
  • Assert that the text is what we expect
  • Close the browser

And when you save the second example and run it here is what will happen:

  • Open the browser
  • Visit the page using Basic Auth in the setup
  • Navigate to the Basic Auth page (without providing credentials)
  • Get the page text
  • Assert that the text is what we expect
  • Close the browser

Summary

Hopefully this tip will help save you from getting tripped by Basic Auth when you come across it. Thanks to Roman Isko for contributing the initial Java code for this tip.

Happy Testing!