Sunday, April 3, 2011

Handling JQuery Modal window using Selenium IDE/RC and Selenium 2

For a simple modal window there is nothing out of ordinary that needs to be done in Selenium. I am sharing sample code for automating modal window as below:


IDE:

New Test
openhttp://www.queness.com/resources/html/modal/jquery-modal-window.html
clicklink=Simple Window Modal
clicklink=Close it

RC (Java-TestNG):

package com.example.tests;

import com.thoughtworks.selenium.*;
import org.testng.annotations.*;
import static org.testng.Assert.*;
import java.util.regex.Pattern;

public class TC_ModalWin extends SeleneseTestCase {
@Test public void testTC_ModalWin() throws Exception {
selenium.open("http://www.queness.com/resources/html/modal/jquery-modal-window.html");
selenium.click("link=Simple Window Modal");
selenium.click("link=Close it");
}
}

Selenium 2 (Using Firefox Driver):

package script;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
//import org.openqa.selenium.RenderedWebElement;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class WebDriverModalWin {
public static void main(String[] args) throws Exception {

WebDriver driver = new FirefoxDriver();

// Go to the Sample Modal Page
driver.get("http://www.queness.com/resources/html/modal/jquery-modal-window.html");

// Select the link "Simple Window Modal"
WebElement query = driver.findElement(By.linkText("Simple Window Modal"));
query.click();

// Adding sleep to view the window for 5 sec
Thread.sleep(5000);

WebElement element2 = driver.findElement(By.linkText("Close it"));
element2.click();

System.out.println("Modal Window Opened and Closed Successfully!");
}
}



No comments:

Post a Comment