Sunday, August 19, 2012

Selenium Example Using Junit: Home Page Test


Junit example Using Selenium

import static org.junit.Assert.*;

import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class HomePageJunitTest {

Selenium selenium;

@Before
public void setUp() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*iexploreproxy", "https://www.google.co.in/");
selenium.start();
}

@Test
public void testJunit() throws Exception {
selenium.open("/");
for (int second = 0;; second++) {
if (second >= 60) fail("timeout");
try { if (selenium.isElementPresent("id=gbqfq")) break; } catch (Exception e) {}
Thread.sleep(1000);
}

selenium.type("id=gbqfq", "selenium hq");
Thread.sleep(2000);
selenium.click("//span[@class=\"gbqfi\"]");
for (int second = 0;; second++) {
if (second >= 60) fail("timeout");
try { if (selenium.isElementPresent("link=Selenium - Web Browser Automation")) break; } catch (Exception e) {}
Thread.sleep(1000);
}

boolean res=selenium.isElementPresent("link=Selenium - Web Browser Automation");
assertEquals(res, true);
boolean res2=selenium.isTextPresent("What is Selenium? Selenium automates browsers.");
assertEquals(res2, true);
}

@After
public void tearDown() throws Exception {
selenium.stop();
}
}

No comments:

Post a Comment

Thanks for your valuable comments