Handling alert in Selenium RC


package com.softwaretesting_guru.help;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.server.SeleniumServer;

import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.SeleneseTestCase;


public class AlertTest extends SeleneseTestCase {
      SeleniumServer ss;

      @Before
      public void setUp() throws Exception {
            // Start Selenium Server
            SeleniumServer ss = new SeleniumServer();
            ss.start();
            // Start Selenium
            selenium = new DefaultSelenium("localhost", 4444, "firefox",
                        "https://irctc.co.in");
            selenium.start();

      }

      @After
      public void tearDown() throws Exception {

      }

      @Test
      public void test() throws InterruptedException {
            // open the browser
            selenium.open("/");
            // maxmize the browser
            selenium.windowMaximize();
            // click on login button
            selenium.click("id=button");
            // expected result user should get alert using selenium verify alert and
            // handel the alert
            // verifying the alert
            if (selenium.isAlertPresent()) { // handel the alert
                  selenium.getAlert();
                  System.out.println("Alert Present");

            } else {
                  System.out.println("There is no alert present test case fail");
            }
            // enter username
            selenium.type("name=userName", "selenium");
            // enter password
            selenium.type("name=password", "selenium");

            Thread.sleep(5000);
            // getting the alert text use below code
            // Store the alert text
            String str = selenium.getAlert();
            // print alert text
            System.out.println(str);
            // compare the str text and alert text
            String alert = "Enter Value for  UserName";
            if (alert.equals(str)) {
                  // handel alert
                  System.out.println("Alert Present");

            } else {
                  System.out.println("There is no alert");
            }

            Thread.sleep(5000);
      }

}
 
By: Manoj Kumar