Pages

Search This Blog

Saturday, May 22, 2010

Selenium Regular Expression and pattern matching

Like locators, patterns are a type of parameter frequently required by Selenese commands. Examples
of commands which require patterns are verifyTextPresent, verifyTitle, verifyAlert, assertConfirmation,
verifyText, and verifyPrompt.Patterns allow one to describe, via the use of special characters, what text is expected rather
than having to specify that text exactly

  • Global Patterns

    * which translates to “match anything,” i.e., nothing, a single character, or many characters.
    [ ] (character class) which translates to “match any single character found inside the square
    brackets.” A dash (hyphen) can be used as a shorthand to specify a range of characters
    (which are contiguous in the ASCII character set). A few examples will make the functionality
    of a character class clear:
    [aeiou] matches any lowercase vowel
    [0-9] matches any digit
    [a-zA-Z0-9] matches any alphanumeric character
    In most other contexts, globbing includes a third special character, the ?. However, Selenium globbing
    patterns only support the asterisk and character class.
    To specify a globbing pattern parameter for a Selenese command, one can prefix the pattern with a glob:
    label. However, because globbing patterns are the default, one can also omit the label and specify just
    the pattern itself.
    Below is an example of two commands that use globbing patterns. The actual link text on the page
    being tested was “Film/Television Department”; by using a pattern rather than the exact text, the click
    command will work even if the link text is changed to “Film & Television Department” or “Film and
    Television Department”. The glob pattern’s asterisk will match “anything or nothing” between the word
    “Film” and the word “Television”.
    click link=glob:Film*Television Department
    verifyTitle glob:*Film*Television*
    The actual title of the page reached by clicking on the link was “De Anza Film And Television Department
    - Menu”. By using a pattern rather than the exact text, the verifyTitle will pass as long as
    the two words “Film” and “Television” appear (in that order) anywhere in the page’s title. For example,
    if the page’s owner should shorten the title to just “Film & Television Department,” the test would still
    pass. Using a pattern for both a link and a simple test that the link worked (such as the verifyTitle
    above does) can greatly reduce the maintenance for such test cases.

    Some examples for global pattern

    Lets say your element looks like

    <input class="foo" value="the text value" name="theText">

    verifyEquals("*text*", selenium.getValue("theText"));

    <input type="hidden" value="the hidden value" name="theHidden">

    verifyEquals("* hidden value", selenium.getValue("theHidden"));

    If you have drop down combo box then you can use global pattern matching .

    <select id="theSelect">
    <option id="o1" value="option1">first option</option>
    <option selected="selected" id="o2" value="option2">second option</option>
    <option id="o3" value="option3">third,,option</option>
    </select>

    select the second option and verify like this

    assertEquals("second *", selenium.getSelectedLabel("theSelect"));
    or,
    String[] arr = {"first*", "second*", "third*"};

    verifyEquals(arr, selenium.getSelectOptions("theSelect"));

  • The question mark ? indicates there is zero or one of the preceding element. For example, colo?r matches both "color" and "colour".

    If your element is like
    <input class="foo" value="the text value" name="theText">

    verifyEquals("?oo", selenium.getAttribute("theText@class"));


  • Regular expression
    Regular expression patterns are the most powerful of the three types of patterns that Selenese supports.
    Regular expressions are also supported by most high-level programming languages, many text editors and a host of tools, including the Linux/Unix command-line utilities grep, sed, and awk.

    regexp (wildcard) matching


    <input class="foo" value="the text value" name="theText">

    verifyEquals("regexp:^[a-z ]+$", selenium.getValue("theText"));

    <input type="hidden" value="the hidden value" name="theHidden">

    verifyEquals("regexp:dd", selenium.getValue("theHidden"));


    <select id="theSelect">
    <option id="o1" value="option1">first option</option>
    <option selected="selected" id="o2" value="option2">second option</option>
    <option id="o3" value="option3">third,,option</option>
    </select>

    assertEquals("regexp:second .*", selenium.getSelectedLabel("theSelect"));

  • verify Attributes using regular expression

    <input class="foo" value="the text value" name="theText">

    verifyEquals("regexp:^f", selenium.getAttribute("theText@class"));

    verifyEquals("regex:^[a-z ]+$", selenium.getValue("theText"));

    <select id="theSelect">
    <option id="o1" value="option1">first option</option>
    <option selected="selected" id="o2" value="option2">second option</option>
    <option id="o3" value="option3">third,,option</option>
    </select>

    assertEquals("exact:second option", selenium.getSelectedLabel("theSelect"));

    String[] arr = {"regexp:^first.*?", "second option", "third*"};
    verifyEquals(arr, selenium.getSelectOptions("theSelect"));
  • How to test element is editable in selenium

    We can verify weather an element is editable or not using isEditable selenium command.

    If your element is in below format
    
    <input value="black" name="plain_text">
    
    
    then we can use selenium isEditable to find this element is editable or not
    
    assertTrue(selenium.isEditable("plain_text"));
    
    <select name="plain_select"><option>Niraj</option></select>
    
    assertTrue(selenium.isEditable("plain_select"));
    
    <input disabled="yup" value="black" name="disabled_text">
    
    assertTrue(!selenium.isEditable("disabled_text"));
    
    <select disabled="yup" name="disabled_select"><option>Niraj</option></select>
    
    assertTrue(!selenium.isEditable("disabled_select"));
    
    

    How to test based on attributes of element using CSS

    We can test based on attributes of elements by using css. We can use regular expression in css attributes to identify the element.
    If your element is in this css format.
    
    <div id="css3test"><a name="foobar">foobar</a><a name="barfoo">barfoo</a><a id="foobar" name="foozoobar">foozoobar</a></div>
    
    
  • How to store the text of and element which css attributes start with some specific letters.

    Ex. in above element how to store the text whose element name start with "foo"
    Selenium IDE
    
    
    <tr>
    <td>storeText</td>
    <td>css=a[name^="foo"]</td>
    <td>str</td>
    </tr>
    
    
    Selenium RC
    
    String str = selenium.getText("css=a[name^=\"foo\"]");
    
    
    This will store the "foobar" in str variable because element <a name="foobar">foobar</a> name start with "foo"

  • How to store the text of and element which css attributes ends with some specific letters.

    Ex. in above element how to store the text whose element name ends with "foo"

    Selenium IDE
    
    <tr>
    <td>storeText</td>
    <td>css=a[name$="foo"]</td>
    <td>str</td>
    </tr>
    
    
    Selenium RC
    
    String str = selenium.getText("css=a[name$=\"foo\"]");
    
    
    This will store the "barfoo" in str variable because element <a name="barfoo">barfoo</a> name ends with "foo"

  • How to store the text of and element which css attributes starts with any letters and ends with any letters but containing some specific letters in middle.

    Ex. in above element how to store the text whose element name ends with any letter and ends with any letter but containing some specific letters "zoo"

    Selenium IDE
    
    <tr>
    <td>storeText</td>
    <td>css=a[name*="zoo"]</td>
    <td>str</td>
    </tr>
    
    

    Selenium RC
    
    String str = selenium.getText("css=a[name*=\"zoo\"]");
    
    
    This will store the "foozoobar" in str variable because element <a name="foozoobar">foozoobar</a> name contains "zoo" in the middle.

    There is another method to store
    
    String str = selenium.getText("css=a:contains(\"zoo\")");
    
    

  • How to identify element using more than one css attributes.

    Lets say your element is in this format.
    
    <a alt="foo" class="a2" href="#id2" name="name1" id="id2">this is the <b>second</b> <span selenium:foo="bar">element</span></a>
    
    
    then if you want to store "this is the second element" in a variable called str the

    Selenium IDE
    
    <tr>
    <td>storeText</td>
    <td>css=a[name*="name"][alt="foo"]</td>
    <td>str</td>
    </tr>
    
    
    Selenium RC
    
    String str = selenium.getText("css=a[name*=\"name\"][alt=\"foo\"]");
    
    
    If you want to store only "element" from above element then use below code
    Selenium IDE
    
    <tr>
    <td>storeText</td>
    <td>css=a[name*="name"][alt="foo"]>span</td>
    <td>str</td>
    </tr>
    
    
    Selenium RC
    
    String str = selenium.getText("css=a[name*=\"name\"][alt=\"foo\"]>span");
    
    
  • How to test Pseudo-classes : Pseudo-elements and pseudo-classes

    Lets say your element looks like
    
    <div id="structuralPseudo">
    <span>span1</span>
    <span>span2</span>
    <span>span3</span>
    <span>span4</span>
    <div>div1</div>
    <div>div2</div>
    <div>div3</div>
    <div>div4</div>
    </div>
    
    
    to identify the element within the element we will use
    
    css=div#structuralPseudo :nth-child(n)
    
    

    1. To identify first element in pseudo element id="structuralPseudo and store its text.
    
    String str = selenium.getText("css=div#structuralPseudo :nth-child(n)");
    
    
    this will store "span1" in str variable.

    2. To identify second element in pseudo element id="structuralPseudo and store its text.
    
    String str = selenium.getText("css=div#structuralPseudo :nth-child(2n)");
    
    
    this will store "span2" in str variable.

    3. To identify third element in pseudo element id="structuralPseudo and store its text.
    
    String str = selenium.getText("css=div#structuralPseudo :nth-child(3n)");
    
    
    this will store "span3" in str variable.
    ...
    ...
    ...

    8.. To identify the last element in pseudo element id="structuralPseudo and store its text.
    
    String str = selenium.getText("css=div#structuralPseudo :nth-child(3n)");
    
    
    this will store "div4" in str variable.


    9. But you can access first and last child directly

    to access first element
    
    String str = selenium.getText("css=div#structuralPseudo :first-child");
    
    
    this will store "span1" in str variable
    To access last element
    
    String str = selenium.getText("css=div#structuralPseudo :last-child");
    
    
    this will store "div4" in str variable
    For more details about css selector please visit .

    http://www.w3.org/TR/CSS2/selector.html

    
    <input type="text" disabled="true" value="disabled" name="disabled">
    
    assertTrue(selenium.isElementPresent("css=input[type=\"text\"]:disabled"));
    
    <input type="checkbox" checked="true" value="checked" name="checked">
    
    assertTrue(selenium.isElementPresent("css=input[type=\"checkbox\"]:checked"));
    
    

  • Friday, May 21, 2010

    How to test the immediate element using CSS


    We can test the elements and their immediate elements using CSS.
    If your element is in this format.
    
    <span id="firstChild">first child <a>second child</a></span>
    
    
  • How to store "first child" in a variable called str then use
    
    String str = selenium.getText("css=div#combinatorTest > span");
    
    
  • If you need to store the text of immediate element <a>second child</a> in a variable called str then use

    
    String str = selenium.getText("css=div#combinatorTest > span>a");
    
    
  • If you element has many value in its class then you can identify the element or you can store the element text in a variable

    <a class="class1 class2 class3">this is the element</a>

    You can use below code for verifying the element or storing the text in the element.
    Verifying element in RC
    
    verifyTrue(selenium.isElementPresent("css=a[class~=\"class2\"]"));
    
    
    Verifying element in IDE
    
    <tr>
    <td>verifyElementPresent</td>
    <td>css=a[class~="class2"]</td>
    <td></td>
    </tr>
    
    

    storing element text in a variable using RC
    
    String str = selenium.getText("css=a[class~=\"class3\"]");
    
    
    storing element text in a variable using IDE
    
    <tr>
    <td>storeText</td>
    <td>css=a[class~="class3"]</td>
    <td>str</td>
    </tr>
    
    
  • Testing preceding combinator elements

    
    <div id="combinatorTest">this is the parent element. <span id="firstChild">this is a child element <a>and grandson element</a></span>, <span>another child element</span>, <span>last child element<span></span></span></div>
    
    
    How to store the text of preceding combinator element in RC

    to store "another chile element"
    
    String str = selenium.getText("css=span#firstChild + span");
    
    
    to store "last child element"
    
    String str = selenium.getText("css=span#firstChild + span + span");
    
    
    How to store the text of preceding combinator element in IDE

    to store "another chile element"
    
    <tr>
    <td>storeText</td>
    <td>css=span#firstChild + span</td>
    <td>str</td>
    </tr>
    
    
    to store "last child element"
    
    <tr>
    <td>storeText</td>
    <td>css=span#firstChild + span + span </td>
    <td>str</td>
    </tr>
    
    
  • Thursday, May 20, 2010

    How to make selenium to wait for an element every second

    Sometimes waitForPageToLoad() and pause fails then to handle this condition we just need to wait for an element we are waiting to load on the page. Once its loaded our execution should proceed.

    
    boolean Condition = false;
    for (int second = 0; second < 60; second++) {
    try {
    
    if ((selenium.isElementPresent("//a[@id='overridelink']"))) {
    selenium.click("//a[@id='overridelink']");
    Condition = true;
    break;
    selenium.waitForPageToLoad(MAX_WAIT_TIME_IN_MS);
    
    }
    
    }
    catch (Exception ignore) {
    }
    pause(1000);
    }
    assertTrue(Condition);
    
    

    How to compare two variables in selenium

    We can compare two variable and store the result as Boolean (true or false ) in a Boolean variable. Using javascript we can compare
    
    <tr>
    <td>store</td>
    <td>5</td>
    <td>a</td>
    </tr>
    <tr>
    <td>store</td>
    <td>2</td>
    <td>b</td>
    </tr>
    <tr>
    <td>store</td>
    <td>javascript{var bool=(storedVars['a']==storedVars['b']);bool;}</td>
    <td>bool</td>
    </tr>
    
    

    Wednesday, May 19, 2010

    How to handle SSL certificate issue in Selenium / firefox

    Selenium creates a new and clean profile each time it runs a test, so it won't keep any cookies of the previous session and it wont use any browser plug-in however you can force selenium to use your own profile.

    Steps
    1. Close all instances of firefox browser.
    2. Create a new firefox profile : Go to Run and type firefox -p and press OK . This will open a dialog bod where you create new firefox profile, have a name "nk" and save it in C: drive.
    3. Install all plug-ins in this profile ; For SSL certificate issue install SSL security bypass

    https://addons.mozilla.org/en-US/firefox/addon/10246/

    4. Now you write you test cases like below

    import org.openqa.selenium.server.RemoteControlConfiguration;
    import org.openqa.selenium.server.SeleniumServer;
    import org.testng.annotations.*;
    import com.thoughtworks.selenium.*;
    import java.io.File;

    public class firefoxprofile extends SeleneseTestCase {

    private SeleniumServer seleniumServer;
    private Selenium selenium;
    public static final String MAX_WAIT_TIME_IN_MS="60000";

    @BeforeClass
    public void setUp() throws Exception {

    File template = new File("C:\\nk");
    RemoteControlConfiguration rc = new RemoteControlConfiguration();
    rc.setSingleWindow(true);
    rc.setFirefoxProfileTemplate(template);
    seleniumServer = new SeleniumServer(rc);
    selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.google.com/");
    seleniumServer.start();
    selenium.start();

    }


    @Test
    public void googling() {
    selenium.open("/");
    selenium.waitForPageToLoad(MAX_WAIT_TIME_IN_MS);
    selenium.type("q", "http://automationtricks.blogspot.com");
    selenium.click("btnG");
    selenium.waitForPageToLoad(MAX_WAIT_TIME_IN_MS);
    assertTrue(selenium.isTextPresent("http://automationtricks.blogspot.com"));
    }


    @AfterTest
    public void tearDown() throws InterruptedException{
    selenium.stop();
    seleniumServer.stop();

    }
    }

    How to run test cases in specified browser profile

    Selenium creates a new and clean profile each time it runs a test, so it won't keep any cookies of the previous session and it wont use any browser plug-in however you can force selenium to use your own profile.

    Steps
    1. Close all instances of firefox browser.
    2. Create a new firefox profile : Go to Run and type firefox -p and press OK . This will open a dilog bod where you create new firefox profile, have a name "nk" and save it in C: drive.
    3. Start the browser: Go to run and type firefox -p that will open a dialog box. Select the profile "NK" which you created in the 2nd step.
    4. Install all plug-ins in this profile ; like SSL security bypass

    https://addons.mozilla.org/en-US/firefox/addon/10246/

    5. Now you write you test cases like below
    
    import org.openqa.selenium.server.RemoteControlConfiguration;
    import org.openqa.selenium.server.SeleniumServer;
    import org.testng.annotations.*;
    import com.thoughtworks.selenium.*;
    import java.io.File;
    
    public class firefoxprofile extends SeleneseTestCase {
    
    private SeleniumServer seleniumServer;
    private Selenium selenium;
    public static final String MAX_WAIT_TIME_IN_MS="60000";
    
    @BeforeClass
    public void setUp() throws Exception {
    
    File template = new File("C:\\nk");
    RemoteControlConfiguration rc = new RemoteControlConfiguration();
    rc.setSingleWindow(true);
    rc.setFirefoxProfileTemplate(template);
    seleniumServer = new SeleniumServer(rc);
    selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.google.com/");
    seleniumServer.start();
    selenium.start();
    
    }
    
    
    @Test
    public void googling() {
    selenium.open("/");
    selenium.waitForPageToLoad(MAX_WAIT_TIME_IN_MS);
    selenium.type("q", "http://automationtricks.blogspot.com");
    selenium.click("btnG");
    selenium.waitForPageToLoad(MAX_WAIT_TIME_IN_MS);
    assertTrue(selenium.isTextPresent("http://automationtricks.blogspot.com"));
    }
    
    
    @AfterTest
    public void tearDown() throws InterruptedException{
    selenium.stop(); 
    seleniumServer.stop();
    
    }
    }
    
    

    Tuesday, May 18, 2010

    How to identify dynamic element in selenium

    Many web sites create dynamic element on their web pages where Ids of the elements gets generated dynamically. Each time id gets generated differently. So to handle this situation we use some JavaScript functions.

    starts-with

    if your dynamic element's ids have the format <button id="continue-12345" /> where 12345 is a dynamic number you could use the following
    
    XPath: //button[starts-with(@id, 'continue-')]  
    
    
    contains

    Sometimes an element gets identfied by a value that could be surrounded by other text, then contains function can be used.
    To demonstrate, the element <input class="top suggest business"> can be located based on the ‘suggest’ class without having to couple it with the ‘top’ and ‘business’ classes using the following
    
    XPath: //input[contains(@class, 'suggest')].
    
    

    How to use Css instead of Xpath in selenium

    Sometimes selenium gets confused to identify an element on the web page because more than one elements have the same name and ids so to handle this situation we instructs selenium to identify the object using CSS instead of Xpath.

    
    <tr>
    <td>open</td>
    <td>http://economictimes.indiatimes.com/</td>
    <td></td>
    </tr>
    <tr>
    <td>type</td>
    <td>css=input[style='width: 120px; font-size: 12px;']</td>
    <td>Business</td>
    </tr>
    
    
    In selenium RC you can use below code
    
    selenium.open("http://economictimes.indiatimes.com/");
    
    selenium.type("css=input[style='width: 120px; font-size: 12px;']", "Business");
    
    
    There is another way to use css instead of Xpath

    Try below code for clicking on search button on www.google.com

    
    <tr>
    <td>type</td>
    <td>q</td>
    <td>www.automationtricks.blogspot.com</td>
    </tr>
    <tr>
    <td>click</td>
    <td>css=span.ds > span.lsbb > input.lsb</td>
    <td></td>
    </tr>
    
    

    Monday, May 17, 2010

    When selenium.waitForPageToLoad is not working

    When selenium.waitForPageToLoad() is not working then we can use alternate solution.

    
    boolean Condition = false;
    for (int second = 0; second < 60; second++) {
    try {
    if ((selenium.isElementPresent("xpath of your element on which you want to click"))) {
    
    selenium.click("xpath of your element on which you want to click");
    Condition = true;
    break;
    }
    }
    catch (Exception ignore) {
    }
    pause(1000);
    }
    assertTrue(Condition);
    
    

    How to setup Selenium RC with TestNG in eclipse

    TestNG is a framework to work in JAVA. Junit and TestNG almost same on the surface but there are major differences between their frameworks and their nature of testing. JUnit is designed to do the unit testing, and it's doing it very well while the TestNG has designed to do the functionality testing at high level. So there are many features available in the TestNG that you will not find in JUnit.


    TestNG Annotation


    @BeforeTest: The annotated method will be run before any test method belonging to the classes inside the tag is run.

    @AfterTest: The annotated method will be run after all the test methods belonging to the classes inside the tag have run.

    @BeforeClass: The annotated method will be run before the first test method in the current class is invoked.

    @AfterClass: The annotated method will be run after all the test methods in the current class have been run.

    @Parameters: Describes how to pass parameters to a @Test method.


    Test case

    goolgesearch.java
    
    import org.openqa.selenium.server.RemoteControlConfiguration;
    import org.openqa.selenium.server.SeleniumServer;
    import org.testng.annotations.*;
    import com.thoughtworks.selenium.*;
    
    public class googlesearch extends SeleneseTestCase{
    
    Selenium selenium;
    public static final String MAX_WAIT_TIME_IN_MS="60000";
    private SeleniumServer seleniumServer;
    
    
    @BeforeClass
    @Parameters({"selenium.host","selenium.port","selenium.browser","selenium.url"})
    public void setUp(String host, String port, String browser , String url ) throws Exception {
    RemoteControlConfiguration rc = new RemoteControlConfiguration();
    
    rc.setSingleWindow(true);
    
    seleniumServer = new SeleniumServer(rc);
    selenium = new DefaultSelenium(host, Integer.parseInt(port), browser, url);
    seleniumServer.start();
    selenium.start();
    
    }
    
    @Test
    @Parameters({"search","expected"})
    public void googling(String search, String expected) {
    selenium.open("/");
    selenium.waitForPageToLoad("6000");
    selenium.type("q", search);
    selenium.click("btnG");
    selenium.waitForPageToLoad(MAX_WAIT_TIME_IN_MS);
    assertTrue(selenium.isTextPresent(expected));
    
    }
    @AfterTest
    public void tearDown() throws InterruptedException{
    selenium.stop(); 
    seleniumServer.stop();
    
    }
    }
    
    

    TestNG Suite

    googlesearchsuite.xml
    
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
    <suite name="googlesearch" verbose="3">
    <parameter name="selenium.host" value="localhost"></parameter>
    <parameter name="selenium.port" value="4444"></parameter>
    <parameter name="selenium.browser" value="*iexplore"></parameter>
    <parameter name="selenium.url" value="http://www.google.com"></parameter>
    
    <test name="googlesearch">
    <parameter name="search" value="automationtricks.blogspot.com"></parameter>
    <parameter name="expected" value="automationtricks.blogspot.com"></parameter>
    <classes>
    <class name="googlesearch"></class>
    </classes>
    </test>
    </suite>
    
    

    In this method you dont have to start your Selenium RC server through command line . The java code will start the server , run the test and stop the server.
    Before you run the Test , make sure all path and build path are correct.

  • Install TestNG plug-in in your eclipse.
  • Set the java build path and pointing to RC sever.
    Go to Java project and right click on it then select properties then select java build path then select library tab and select external jar pointing you selenium RC server.
  • Run the test case as TestNG test suite.
    Go to googlesearch.java page right click on the page the click on Run as... and select the suite in the dialog box which you created above.

  • How to pass parameters in selenium RC using TestNG

    We can parametrize our test cases using TestNG in Selenium RC.

    
    import org.openqa.selenium.server.RemoteControlConfiguration;
    import com.thoughtworks.selenium.*;
    import org.openqa.selenium.server.*;
    import org.testng.annotations.*;
    
    import java.io.*;
    import java.sql.*;
    
    
    
    public class TestExcel extends SeleneseTestBase {
    
    @DataProvider(name="DP1")
    public Object[][] createData(){
    Object[][] retObjArr = {{"testuser1","password1"},
    {"testuser2","password2"},
    {"testuser3","password3"},
    {"testuser4","password4"},
    {"testuser5","password5"},
    };
    return(retObjArr);
    }
    
    
    private SeleniumServer seleniumServer;
    Selenium selenium;
    
    @BeforeClass
    public void setUp()throws Exception{
    
    RemoteControlConfiguration rc = new RemoteControlConfiguration();
    rc.trustAllSSLCertificates();
    seleniumServer = new SeleniumServer(rc);
    selenium = new DefaultSelenium("localhost",4444,"*iexplore","http://www.yahoo.com");
    seleniumServer.start();
    selenium.start();
    }
    
    @Test (dataProvider = "DP1")
    public void testEmployeeData(String username, String password){
    selenium.open("https://login.yahoo.com/config/mail?.src=ym&.intl=us/");
    selenium.type("username", username);
    selenium.type("passwd",password);
    selenium.click(".save");
    selenium.waitForPageToLoad("30000");
    assertTrue(selenium.isTextPresent("Hi,"+username));
    selenium.click("_test_sign_out");
    selenium.waitForPageToLoad("30000");
    
    }
    @AfterTest
    public void tearDown() throws InterruptedException{
    selenium.stop();
    seleniumServer.stop();
    
    }
    
    

    How to pass parameters to JUNIT or TestNG test case.

    You can parametrize your test cases using excel sheet. With the help you TestNG we can pass different set of data to our test cases by following steps

    1. create a data file excel rename column as username and fill below data like
    
    username  
    test1         
    test2
    test3
    test4
    
    

    2. create a dsn through control pannel--> administrative tool--> Data source (ODBC) --> select system dsn --> click add
    then select "dirver do microsoft excel" select workbook your data file which you created above.

    now your data source and provider is ready now connect this in your test cases using java class for

    Class.forName("sun.jdbc.odbc.
    JdbcOdbcDriver");


    3. Write the test cases
    
    import org.openqa.selenium.server.RemoteControlConfiguration;
    import com.thoughtworks.selenium.*;
    import org.openqa.selenium.server.*;
    import org.testng.annotations.*;
    public class TestExcel extends SeleneseTestBase {
    @BeforeClass
    public void setUp()throws Exception{
    
    RemoteControlConfiguration rc = new RemoteControlConfiguration();
    rc.trustAllSSLCertificates();
    seleniumServer = new SeleniumServer(rc);
    selenium = new DefaultSelenium("localhost",4444,"*iexplore","http://www.yahoo.com");
    seleniumServer.start();
    selenium.start();
    }
    @Test
    public  void testread()throws Exception{
    // Connection connection = null;
    
    
    try{
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection( "jdbc:odbc:nk" );
    // Connection con = DriverManager.getConnection( "jdbc:odbc:nk" ); here you write your driver which you created using ODBC connecting excel workbook.
    
    Statement st = con.createStatement();
    ResultSet rs = st.executeQuery( "Select * from [Sheet1$]" );
    
    ResultSetMetaData rsmd = rs.getMetaData();
    int numberOfColumns = rsmd.getColumnCount();
    
    while (rs.next()) {
    
    for (int i = 1; i <= numberOfColumns; i++) {
    if (i > 1) System.out.print(", ");
    String columnValue = rs.getString(i);
    System.out.print(columnValue);
    selenium.open("/");
    
    pause(5000);
    selenium.click("link=Sign in");
    selenium.waitForPageToLoad("30000");
    
    
    try {
    if (selenium.isElementPresent("//a[@id='overridelink']"))
    selenium.click("//a[@id='overridelink']");
    }
    catch (Exception e) {}
    pause(30000);
    selenium.type("userid", columnValue);
    selenium.type("pass","password");
    selenium.click("//button[@class='bfbt' and @type='submit']");
    pause(8000);
    String wc = selenium.getTable("//div[@id='dynamicmenu-hdrCtr']/table[1].0.1");
    System.out.print(wc);
    
    selenium.click("link=Sign out");
    selenium.waitForPageToLoad("20000");
    selenium.isTextPresent("Welcome!");
    
    }
    System.out.println("");   
    
    }
    
    st.close();
    con.close();
    
    } catch(Exception ex) {
    System.err.print("Exception: ");
    System.err.println(ex.getMessage());
    }
    }
    }
    
    

    How to use GOtoIf and while in selenium IDE

    Selenium IDE has some programing capacity upto some extend. There we can use If conditions and loop statements.

    How to use GOtoIf and while
    
    <tr>
    <td>store</td>
    <td>1</td>
    <td>looptimes</td>
    </tr>
    
    <tr>
    <td>while</td>
    <td>storedVars.looptimes < 11</td>
    <td></td>
    </tr>
    
    <tr>
    <td>store</td>
    <td>javascript{storedVars.looptimes++;}</td>
    <td></td>
    </tr>
    
    <tr>
    <td>gotoIf</td>
    <td>javascript{(storedVars.looptimes==5);}</td>
    <td>lbltest</td>
    </tr>
    
    <tr>
    <td>open</td>
    <td>http://www.yahoo.com</td>
    <td></td>
    </tr>
    
    <tr>
    <td>echo</td>
    <td>${looptimes}</td>
    <td></td>
    </tr>
    
    <tr>
    <td>endWhile</td>
    <td></td>
    <td></td>
    </tr>
    
    <tr>
    <td>label</td>
    <td>lbltest</td>
    <td></td>
    </tr>
    
    
    If your selenium IDE does not support while or if you don't have while js in your extension Please download below js and add in your extension.


    Click here to download while.js

    Start selenium server through eclipse

    You can start selenium RC server using JAVA code in eclipse.
    
    import com.thoughtworks.selenium.*;
    import org.openqa.selenium.server.SeleniumServer;
    import org.openqa.selenium.server.RemoteControlConfiguration;
    
    
    public class Cartpayment extends SeleneseTestCase {
    
    Selenium selenium;
    public static final String MAX_WAIT_TIME_IN_MS="60000";
    private SeleniumServer seleniumServer;
    
    
    
    
    public void setUp() throws Exception {
    RemoteControlConfiguration rc = new RemoteControlConfiguration();
    rc.setAvoidProxy(true);
    rc.setSingleWindow(true);
    rc.setReuseBrowserSessions(true);
    
    seleniumServer = new SeleniumServer(rc);
    selenium = new DefaultSelenium(localhost, 4444, *firefox, "http://www.yahoo.com");
    seleniumServer.start();
    selenium.start();
    }
    
    
    

    How to avoid invalid certificate in selenium RC (IE)

    If you are running selenium RC test with IE a site which already has invalid certificate then IE will give a warning for "invalid certificate". You can have selenium
    automatically click on "Continue" link in it(which has ID of overridelink) by adding
    below command in your IE specific test case.

    if ((selenium.isElementPresent("//a[@id='overridelink']"))) {
    selenium.click("//a[@id='overridelink']");
    //selenium.waitForPageToLoad(MAX_WAIT_TIME_IN_MS);
    selenium.waitForPageToLoad("30000");
    }