Selenium Webdriver – Java with JUnit

By | 03/05/2015

you can find a basic simple Java class written in JUnit format for Selenium Webdriver. JUnit has some advantages such as Assertions etc. in this post, you see without Assertion. i will write a post how to use Assertion soon .

 

import org.junit.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;

public class SimpleSeleniumJavaJunit {
private WebDriver driver;

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

@Test
public void testJunitwebdriver() throws Exception {
driver.get("http://www.mahsumakbas.net/");

String element = driver.findElement(By.xpath("//a[@class='site-description']")).getText();

System.out.println("Page title is: " + driver.getTitle());

System.out.println("Site Description is: " + element);
}

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

}

 

after run as JUnit test, you should see following output:

Page title is: Software Test Engineering – Test Automation | A Blog For Software Testing and Automation
Site Description is: A Blog For Software Testing and Automation

Leave a Reply

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

*