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:


New Test
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 {"");"link=Simple Window Modal");"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

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

// Adding sleep to view the window for 5 sec

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

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

No comments:

Post a Comment