Pages

Friday, 16 December 2011

Regular Expression Example

Regular expression patterns in Selenese need to be prefixed with either regexp: or regexpi:. The former is case-sensitive; the latter is case-insensitive.

package qtt.selenium;
import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.server.SeleniumServer;
import java.util.regex.Pattern;

public class RegularExp extends SeleneseTestCase {
    SeleniumServer ss;
    @Before
    public void setUp() throws Exception {
        ss= new SeleniumServer();
        ss.start();
        selenium = new DefaultSelenium("localhost", 4444, "*chrome", "
http://wwp.greenwichmeantime.com/time-zone/asia/india/time/");
        selenium.start();
    }

    @Test
    public void testRegEx() throws Exception {
        selenium.open("
http://wwp.greenwichmeantime.com/time-zone/asia/india/time/");
        selenium.windowMaximize();
        selenium.selectFrame("I_asia_kolkata");
        verifyTrue(selenium.isTextPresent("Friday, 18 November\n1:01:42 p.m."));
        verifyTrue(selenium.isTextPresent("regexpi:[0-9]{1,2}:[0-9]{2}:[0-9]{2} [a,p].m"));
    }

    @After
    public void tearDown() throws Exception {
        selenium.stop();
        ss.stop();
    }
}

Regular expression  used:

[0-9]{1,2} 1 or 2 digits (for the hour of the day)
: The character : (no special characters involved)
[0-9]{2} 2 digits (for the minutes)
: The character : (no special characters involved)
[0-9]{2}  2 digits (for the seconds)followed by space
[a,p]m “a” or “p” followed by “m” (am or pm)

No comments:

Post a Comment