As a test automation engineer using Selenium, one of the most frustrating issues you may encounter is your test running into an access denied page. This usually occurs when your test tries to access a part of the application that requires login or other credentials. When an access denied page appears, it breaks the flow of your test and leads to failures.
There are several strategies you can employ to avoid and handle access denied pages in your Selenium tests:
Log Into the Application Before Testing
The simplest solution is to have your test log into the application at the start, before attempting any other actions. This will ensure your tests run in an authenticated state and can access restricted pages:
// Log into application
driver.get("http://www.myapp.com");
driver.findElement(By.id("username")).sendKeys("test");
driver.findElement(By.id("password")).sendKeys("testpass");
driver.findElement(By.cssSelector("button[type='submit']")).click();
// Continue with testing steps...
Logging in upfront eliminates many access denied errors.
Check for Access Denied Pages
Even when logged in, some parts of an application may have additional access restrictions. Your test may unintentionally navigate to such pages.
A best practice is to regularly check for access denied pages as your test runs:
// Perform test step
// Check if access was denied
if(driver.getPageSource().contains("Access denied")) {
// Handle access denied page
// ...
} else {
// Continue testing
}
This allows your test to detect access issues and handle them appropriately.
Refresh Tokens to Maintain Session
For applications with strict session timeouts, you may encounter access issues if your test runs too long. The session and credentials may expire mid-test.
You can keep the session alive by periodically refreshing tokens:
// Log in
// Save auth token
String authToken = driver.manage().getCookieNamed("authToken").getValue();
// Later in test...
// Check if session expired
if(driver.getCurrentUrl().contains("sessionExpired")) {
// Refresh session
driver.manage().addCookie(new Cookie("authToken", authToken));
driver.navigate().refresh();
}
This reuses the old auth token to refresh the session, avoiding denied pages.
Handle Access Denied Pages
Sometimes access issues are hard to prevent entirely. When your test does hit a denied page, you want to handle it gracefully.
Here are some approaches:
Log and continue: Record the failure, then redirect the test to a valid page to continue other steps:
if(driver.getPageSource().contains("Access denied")) {
System.out.println("Hit access denied page");
driver.get("http://www.myapp.com/home");
}
Retry with alternate credentials: Try logging in with other user accounts that may have required access:
driver.findElement(By.id("username")).sendKeys("altUser");
driver.findElement(By.id("password")).sendKeys("password");
Report issues: Raise defects highlighting unintentional access restrictions that need resolution:
System.out.println("Section restricts access for valid user - DEFECT");
With robust access denied handling, your tests can recover and provide useful feedback.
Key Takeaways
Here are some key tips for dealing with access issues in Selenium:
By anticipating and managing access issues, you can create reliable Selenium tests.
Focusing on robust authentication, session handling, and failure strategies will help your test automation smoothly drive applications with strict access controls.