Starting Selenium Testing with Java and JUnit

This is interesting way of testing web applications, most of the web applications are constantly changed due to new requirements wanted by your beloved business analyst, as most of these guys are technologically handicap changes are asked for even at the UAT stage. The victims of such foolish actions are the hardworking developers, who need to make sure the functionalities are coded and tested. The testing teams do not report bugs coming out of these changes as they do not complete the regression tests on the previously verified functionalities.
So to done away with those saturday evening call that things are not working when sanity done by the users after production deployment selenium can be very helpful. In this post i will let you show you how to setup the selenium testing framework on your machine and run your first JUnit test.
a) We need to download some files to get the framework up and running, click to this link to get to the selenium download page.
Selenium Download Jars
Selenium Download Jars
Download the three files and get the jars from them. Also get the hamcrest and junit.jar from the Junit download page. Make a user library in eclipse and name it appropriately.
In eclipse, create a Java project.
Project Structure in Eclipse for Selenium Java Junit
Project Structure in Eclipse for Selenium Java Junit
As you can see in the picture, the project is a simple Java project with a user library named as Selenium and the test file a class with junit tests. The Selenium lbrary contains all the jars present in downloaded files from the selenium download page.
Lets get to program.
  1. import java.io.File;
  2. import org.junit.After;
  3. import org.junit.Before;
  4. import org.junit.Test;
  5. import org.openqa.selenium.WebDriver;
  6. import org.openqa.selenium.ie.InternetExplorerDriver;
  7. public class SeleniumTest {
  8. WebDriver driver;
  9. @Before
  10. public void setup() throws Exception{
  11. File file = new File("G:/development/selenium/IEDriverServer.exe");
  12. System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
  13. driver = new InternetExplorerDriver();
  14. }
  15. @After
  16. public void tearDown() throws Exception{
  17. driver.quit();
  18. }
  19. @Test
  20. public void test(){
  21. driver.navigate().to("https://www.google.com");
  22. }
  23. }
You need to know that testing a web application is similar to JUnit tests, we get to a page and assert that certain elements on the web page have certain values at certain times. We can provide values to the page and observe its behaviour.
In the above program, we are using @Before to setup the driver, here Internet Explorer driver is being used. @After to breakaway the resources, using the quit method.
The actual test marked by @Test, the test simply gets us to the google.com.
When you run the test you will get these two pages coming one after another.
Internet Explorer Webdriver Server Page
Internet Explorer Webdriver Server Page
Internet Explorer Google Page
Internet Explorer Google Page
Thats it for this post, some more posts on this topic to follow. Leave comments if you want to.

Comments

  1. The Article on Starting Selenium Testing with Java and JUnit is nice. It give detail information about it .Thanks for Sharing the information Selenium Testing Testing.
    mobile application testing

    ReplyDelete

Post a Comment

Popular posts from this blog

Java Interview : Threads

Spring Framework Interview Notes : Part Two Wiring

Card Dealer In Java in Less than 5 minutes