Sometimes in integration testing you want to use automated testing of variables. In my case Im loading a complex flex application that takes 5 – 10 seconds to finish its loading process, and when its done it does a ExternalInterface.call() out to javascript and sets a public variable on the window.

Whether good or bad practice I don’t care, that is not the point of this post.

In the tests below were testing the url, we have security in place to always see if a deepLink that the user put in, is a real link, or if it requires login or not, and we need to verify that the ones publicly accessible work to our liking… This is nice because when we work on the navigation and security of those links later, it’s fairly complex, and we don’t want to have to worry about that breaking, so now all we have to do is run this test and we’ll know whats broke.

Here is an excerpt of the Selenium WebDriver 2.5 testing code.

BaseTest.java

package tests;

import java.util.concurrent.TimeUnit;

import junit.framework.Assert;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import com.gargoylesoftware.htmlunit.WebClient;
import org.openqa.selenium.JavascriptExecutor;

public class BaseTest {
   public WebDriver driver;
   public JavascriptExecutor js;
   public String baseUrl;
   public Long defaultTimeout;
   public Long defaultFlexTimeout;
   public StringBuffer verificationErrors = new StringBuffer();
   
   @BeforeMethod 
   public void oneTimeSetUp() {
       driver = new FirefoxDriver();
       js = (JavascriptExecutor) driver;
       baseUrl = "http://someWebSite/";
       defaultTimeout = new Long(5);
       defaultFlexTimeout = new Long(10);
       driver.manage().timeouts().implicitlyWait(defaultTimeout, TimeUnit.SECONDS);
   }

}

Then NavigationPublicTest.java

package tests.navigation;
import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;

import junit.framework.Assert;

import org.apache.http.conn.ssl.BrowserCompatHostnameVerifier;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;

import com.gargoylesoftware.htmlunit.javascript.background.JavaScriptExecutor;

import tests.BaseTest;

//@Test(groups="publicNavigation")
public class NavigationPublicTest extends BaseTest {
	
	@Test(groups="publicNavigation")
	public void testAllDeepLinksAllowAll() {
		deepLinkTest("#Test_One");
		deepLinkTest("#Test_Two");
		deepLinkTest("#Test_Three");
			
		//needs administration login, do later.
		//deepLinkTest("#Administration");
	}
	
	/**
	 * Test deep link of #Locate_Case, this should load the flex app, and not prompt login, and keep #Locate_Case on the url.
	 */
	public void deepLinkTest(String deepLink) {
		driver.get(this.baseUrl + deepLink);
		
		WebDriverWait wait = new WebDriverWait(driver, defaultFlexTimeout);
		
		// Wait for redirects to happen, look for text on page in order to move on.
		wait.until(new ExpectedCondition() {
			public Boolean apply(WebDriver webDriver) {
				System.out.println("waiting for flex app to load ...");
				
				Object isApplicationLoaded = js.executeScript("return window.isApplicationLoaded");
				
				return isApplicationLoaded != null;
			}
		});
		
		String url = driver.getCurrentUrl();
		String destinationUrl = this.baseUrl + deepLink;
		
		// wait 
		if( url.toString().equals( destinationUrl.toString() ) ) {
			System.out.println("The url is correct " + url);
		} else { 
			Assert.fail("The url is not correct, it should be: " + destinationUrl + " but is: " + url);
		}
		
	}
}