Wednesday, February 3, 2016

JUNIT with SELENIUM

Sample Script to use JUNIT with Selenium

import org.junit.After;

import org.junit.Assert;


import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class junitsample {
        WebDriver ie = null;
        @Before
        public void initiation(){
       System.setProperty("webdriver.ie.driver", "D:\\Hemanth\\IEDriverServer_x64_2.50.0\\IEDriverServer.exe");
       ie= new InternetExplorerDriver();
        }
        @After
        public void last(){
               ie.close();
               System.out.println("completed");       
        }
        @Test
        public void testing(){
               ie.get("http://stlc4u.blogspot.in/");
                String s = ie.getTitle();
                System.out.println(s);
                ie.findElement(By.linkText("2014")).click();
                s = ie.getCurrentUrl();
                Assert.assertTrue(ie.getTitle().equals("Software Testing"));
                }
}
The “Before” method will be executed before each “Test” method and “After”method will be executed after each “Test” method. Assert is used for  verification purpose.here assert is verifying whether the “2014” link related page name is “Software testing”or not. Run this as JUNIT Test, the result would  mentioned below

If you change the Assert to “Software” instead of “Software Testing”, it will fail.

 

 
 
 

No comments:

Post a Comment