Monday, December 2, 2013

Selenium: WebDriver with JUNIT

 WebDriver with JUNIT


import java.util.concurrent.TimeUnit;

import junit.framework.Assert;

import  org.junit.Assert.*;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;


public class WebDriverJunit {

public static WebDriver oBrowser;

@BeforeClass
public static void Selenium_init()
{
oBrowser=new FirefoxDriver();
oBrowser.manage().window().maximize();
oBrowser.manage().timeouts().pageLoadTimeout(15, TimeUnit.SECONDS);
oBrowser.get("http://www.bing.com");
}

@AfterClass
public static void Selenium_End()
{
oBrowser.quit();
}

@Test
public void Validate_Edit()
{
WebElement oEdit;

try
{
oEdit=oBrowser.findElement(By.id("sb_form_q"));
Assert.assertEquals("", oEdit.getText());
}
catch(Exception e)
{
Assert.fail(e.getMessage());
}
}

@Test
public void Search()
{
WebElement oEdit,oButton;
String sPageSource;

try
{
oEdit=oBrowser.findElement(By.id("sb_form_q"));
oButton=oBrowser.findElement(By.id("sb_form_go"));
oEdit.sendKeys("Selenium");
oButton.click();
sPageSource=oBrowser.getPageSource();
oBrowser.navigate().back();
if (sPageSource.lastIndexOf("Selenium")<100)
{
Assert.fail("No Proper result");
}
}
catch(Exception e)
{
Assert.fail(e.getMessage());
}
}
}

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...