Sunday, August 19, 2012

Selenium Using testNG example: Home Page Test


Selenium Example Using testNG.

import com.thoughtworks.selenium.*;
import org.testng.annotations.*;

public class HomePageTest extends SeleneseTestCase {

@BeforeMethod
public void setUp() throws Exception {

selenium = new DefaultSelenium ("localhost", 4444, "*iexploreproxy", "https://www.google.co.in/");
selenium.start();
}

@Test
public void testHomePageTest() 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", "seleniumhq");
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);
}

verifyTrue(selenium.isElementPresent("link=Selenium - Web Browser Automation"));
verifyTrue(selenium.isTextPresent("What is Selenium? Selenium automates browsers."));
}

@AfterMethod
public void tearDown() {
selenium.close();
selenium.stop();
}
}

1 comment:

  1. Hello,

    you do not need to have try/catch statements. Selenium has some features which waits till some element is present in the DOM.

    ReplyDelete

Thanks for your valuable comments