Writing Selenium test case using JUnit

Learn how to write selenium test case using JUnit. In this example learn how to navigate through the pages and also validate page load properly happened or not. Junit Assert methods are used to validate the expected results in the test case.

If you feel this video is useful, Please share it with your friends on Facebook, twitter Google plus etc.

Code developed in this tutorial provided below:

import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

/**
 * @author Niranjan
 *
 */
public class DigitalAppTestCases {

	static ChromeDriver driver = null;
	static WebDriverWait wait = null;
	
	/**
	 * This method called once for entire class test cases.
	 * @throws java.lang.Exception
	 */
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		// Replace the path of chrome driver according to your setup
		System.setProperty("webdriver.chrome.driver", "/Users/Niranjan/My Briefcase/My Development/eclipse_learning/SeleniumLearning/lib/webdrivers/chromedriver");
		
		driver = new ChromeDriver();
	}

	/**
	 * This method will be called every time before each test case.
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {
		driver.get("http://www.digitalappconsultancy.com");
	}

	@Test
	public void testHomePage() {
		driver.get("http://www.digitalappconsultancy.com");
		wait = new WebDriverWait(driver, 2);
		wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("page")));
		Assert.assertTrue(driver.getTitle().equals("Home - Digital App Consultancy"));
	}
	
	@Test
	public void testTechBlogPage() {
		driver.get("http://digitalappconsultancy.com/site/blog/");
		wait = new WebDriverWait(driver, 2);
		wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("page")));
		Assert.assertTrue(driver.getTitle().equals("Tech Blog - Digital App Consultancy"));
	}
	
	@Test
	public void testTechTutorialsPage() {
		driver.get("http://digitalappconsultancy.com/site/nirutechtutorials/");
		wait = new WebDriverWait(driver, 2);
		wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("page")));
		Assert.assertTrue(driver.getTitle().equals("Tech Tutorials - Digital App Consultancy"));
	}
	
	@Test
	public void testMobileAppsPage() {
		driver.get("http://digitalappconsultancy.com/site/mobileapps/");
		wait = new WebDriverWait(driver, 2);
		wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("page")));
		Assert.assertTrue(driver.getTitle().equals("Mobile Apps - Digital App Consultancy"));
	}
	
	@Test
	public void testContactIsPage() {
		driver.get("http://digitalappconsultancy.com/site/contact/");
		wait = new WebDriverWait(driver, 2);
		wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("page")));
		Assert.assertTrue(driver.getTitle().equals("Contact - Digital App Consultancy"));
	}

	@AfterClass
	public static void cleanUp(){
		driver.quit();
	}
}

For further reading refer Java Selenium Programming Category posts.

Learning Selenium Java test case using JUnit
Tagged on:     

Leave a Reply

Your email address will not be published. Required fields are marked *