Tuesday, October 19, 2010

Selenium

If you are on this page its probably via google cos you need an answer to e selenium issue or you have decided to finally learn selenium. Either way you are welcome. Let me Assert() something very quickly. Selenium is very very easy. If  you are struggling with it. Then Ermm.. keep learning.. :-)


Here's my first snippet. How to copy data of a table in a web page using selenium,
Then sort the data by Name in alphabetic order
Also Sort by Date.

And finally to test the default sorting order by name and then by date


SELECT uid, md5(CONCAT('1', pass, login)) as hash FROM users WHERE mail = ;


package com.artizani.testsuite;


import java.util.*;
import java.text.*;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.openqa.selenium.*;

/**
 * This test will perform the following actions:
 *
 * <ul>
 * <li></li>
 * <li></li>
 * <li></li>
 * </ul>
 *
 * @author david salami
 * @
 */
public class SortAndFilterView extends BaseClass {


    private String[][] beforeTableData;
    private String[][] sortedTableData;
    private String[][] afterTableData;
    static int indexCount = 0;
    static int indexCounter = 0;

    public SortAndFilterView() {
    }

    public SortAndFilterView(String nameString) {
        super(nameString);
    }

    public void goToRab() throws Exception {

        driver.get("http://www.hotmail.com/");
        driver.findElement(By.xpath("//input[@id='edit-name']")).sendKeys("username");
        driver.findElement(By.xpath("//input[@id='edit-pass']")).sendKeys("password");
        driver.findElement(By.xpath("//input[@id='edit-submit']")).click();
     
        waitForElement("//li/a[@href='/rab/consultant_registrations']", 5000, 250);
        driver.findElement(By.xpath("//li/a[@href='/rab/consultant_registrations']")).click();

        Object[] vErrors = {};
        setVerificationErrors(vErrors);
    }

    public void clickToSortByName() throws Exception {


          int pageNum = 0;
          int rowNum = 0;
          int pageRowCount =0;
          int add = 0;
          int rowCount =0;
        

          for (int pageCount = 5; pageCount !=0; pageCount--) // checks for pagination link from 5 to 1
          {
          if(isElementPresent("//div[@id='content']//li/a[.='"+pageCount+"']"))
          {
         
          pageNum = pageCount;
          break;
          }
          else {pageNum=1;}
          }
         
          if (pageNum > 1){

          driver.findElement(By.xpath("//div[@id='content']//li/a[.='"+pageNum+"']")).click();
          rowCount = getXPathCount("//div[@id='edit-objects-wrapper']/table[2]/tbody/tr") -1;
          rowNum = (((pageNum -1) * 10) + rowCount);
          }
          else{
          rowNum = getXPathCount("//div[@id='edit-objects-wrapper']/table[2]/tbody/tr");
          }
    
        beforeTableData = new String[rowNum][6]; //Declare the 2D array size
        sortedTableData = new String[rowNum][6]; //Declare the 2D array size
        afterTableData = new String[rowNum][6]; //Declare the 2D array size

            //  Note the Table has 6 rows. selenium count table  rows from 1 an d not 0

        driver.findElement(By.xpath("//li/a[@href='/rab/consultant_registrations']")).click();
        for (int x =0; x<=pageNum; x++)
        {
          pageRowCount = getXPathCount("//div[@id='edit-objects-wrapper']/table[2]/tbody/tr");
          int y=2;
                  for (int counter = 0; counter <= pageRowCount-2; counter++) {
                       if (!waitForElement("//div[@id='edit-objects-wrapper']/table[2]/tbody/tr[2]", 5000, 250)) {
                            break;
                        }
                               for (int j = 2; j <8; j++) {
                                        beforeTableData[indexCount][j-2] = driver.findElement(By.xpath("//div[@id='edit-objects-wrapper']/table[2]/tbody/tr["+y+"]/td[" +j+ "]")).getText();
                                    }
                                    y++;
                                    if (y>11){y=2;}
                  
                                    indexCount++;
                  }
                     if(pageNum ==1 || x==pageNum)
                     {
                     break;
                     }
                     int pNumber = x+2;
                     driver.findElement(By.xpath("//div[@id='content']//li/a[.='"+pNumber+"']")).click();
                     add = indexCount;
                     x++;
          
        }
        indexCount = 0;
        driver.findElement(By.xpath("//li/a[@href='/rab/consultant_registrations']")).click();
        driver.findElement(By.linkText("Display name")).click();
        pause(1000);

        for (int x =0; x<=pageNum; x++)
        {
          pageRowCount = getXPathCount("//div[@id='edit-objects-wrapper']/table[2]/tbody/tr");
          int y=2;
                  for (int counter = 0; counter <= pageRowCount-2; counter++) {
                       if (!waitForElement("//div[@id='edit-objects-wrapper']/table[2]/tbody/tr[2]", 5000, 250)) {
                            break;
                        }
                                 for (int j = 2; j <8; j++) {
                                        afterTableData[indexCount][j-2] = driver.findElement(By.xpath("//div[@id='edit-objects-wrapper']/table[2]/tbody/tr["+y+"]/td[" +j+ "]")).getText();
                                    }
                                    y++;
                                    if (y>11){y=2;}
                       
                                    indexCount++;
                  }
                     if(pageNum ==1 || x==pageNum)
                     {
                     break;
                     }
                     int pNumber = x+2;
                     driver.findElement(By.xpath("//div[@id='content']//li/a[.='"+pNumber+"']")).click();
                     add = indexCount;
                     x++;

        }


        System.out.print("================\n");
        printArray(beforeTableData);
        System.out.print("================\n");
        printArray(afterTableData);
        Arrays.sort(beforeTableData, new NameComparator());
        System.out.print("================\n");
        System.out.print("================\n");
        printArray(beforeTableData);

        if (beforeTableData.length == afterTableData.length && afterTableData.length > 0) {
            for (int i = 0; i < beforeTableData.length; i++) {
                for (int j = 0; j < 5; j++) {
                    if (!(beforeTableData[i][j] == null) || !(afterTableData[i][j] == null)) {

                        if (!beforeTableData[i][j].equals(afterTableData[i][j])) {

                            assertFalse(true);
                        }
                    }
                }
            }

        }
    }

    public void clickToSortByDate() throws Exception {




          int pageNum = 0;
          int rowNum = 0;
          int pageRowCount =0;
          int add = 0;
          int rowCount =0;


          for (int pageCount = 5; pageCount !=0; pageCount--)
          {
          if(isElementPresent("//div[@id='content']//li/a[.='"+pageCount+"']"))
          {

          pageNum = pageCount;
          break;
          }
          else {pageNum=1;}
          }

          if (pageNum > 1){

          driver.findElement(By.xpath("//div[@id='content']//li/a[.='"+pageNum+"']")).click();
          rowCount = getXPathCount("//div[@id='edit-objects-wrapper']/table[2]/tbody/tr") -1;
          rowNum = (((pageNum -1) * 10) + rowCount);
          }
          else{
          rowNum = getXPathCount("//div[@id='edit-objects-wrapper']/table[2]/tbody/tr");
          }

        beforeTableData = new String[rowNum][6];
        sortedTableData = new String[rowNum][6];
        afterTableData = new String[rowNum][6];

        driver.findElement(By.xpath("//li/a[@href='/rab/consultant_registrations']")).click();
        for (int x =0; x<=pageNum; x++)
        {
          pageRowCount = getXPathCount("//div[@id='edit-objects-wrapper']/table[2]/tbody/tr");
          int y=2;
                  for (int counter = 0; counter <= pageRowCount-2; counter++) {
                       if (!waitForElement("//div[@id='edit-objects-wrapper']/table[2]/tbody/tr[2]", 5000, 250)) {
                            break;
                        }
                               for (int j = 2; j <8; j++) {
                                        beforeTableData[indexCount][j-2] = driver.findElement(By.xpath("//div[@id='edit-objects-wrapper']/table[2]/tbody/tr["+y+"]/td[" +j+ "]")).getText();
                                    }
                                    y++;
                                    if (y>11){y=2;}

                                    indexCount++;
                  }
                     if(pageNum ==1 || x==pageNum)
                     {
                     break;
                     }
                     int pNumber = x+2;
                     driver.findElement(By.xpath("//div[@id='content']//li/a[.='"+pNumber+"']")).click();
                     add = indexCount;
                     x++;

        }
        indexCount = 0;
        driver.findElement(By.xpath("//li/a[@href='/rab/consultant_registrations']")).click();
        driver.findElement(By.linkText("Date")).click();
        pause(1000);

        for (int x =0; x<=pageNum; x++)
        {
          pageRowCount = getXPathCount("//div[@id='edit-objects-wrapper']/table[2]/tbody/tr");
          int y=2;
                  for (int counter = 0; counter <= pageRowCount-2; counter++) {
                       if (!waitForElement("//div[@id='edit-objects-wrapper']/table[2]/tbody/tr[2]", 5000, 250)) {
                            break;
                        }
                                 for (int j = 2; j <8; j++) {
                                        afterTableData[indexCount][j-2] = driver.findElement(By.xpath("//div[@id='edit-objects-wrapper']/table[2]/tbody/tr["+y+"]/td[" +j+ "]")).getText();
                                    }
                                    y++;
                                    if (y>11){y=2;}

                                    indexCount++;
                  }
                     if(pageNum ==1 || x==pageNum)
                     {
                     break;
                     }
                     int pNumber = x+2;
                     driver.findElement(By.xpath("//div[@id='content']//li/a[.='"+pNumber+"']")).click();
                     add = indexCount;
                     x++;

        }


//        System.out.print("======DATE==========\n");
//        printArray(beforeTableData);
//        System.out.print("================\n");
//        printArray(afterTableData);
        Arrays.sort(beforeTableData, new DateComparator());
//        System.out.print("================\n");
//        System.out.print("================\n");
//        printArray(beforeTableData);

        if (beforeTableData.length == afterTableData.length && afterTableData.length > 0) {
            for (int i = 0; i < beforeTableData.length; i++) {
                for (int j = 0; j < 5; j++) {
                    if (!(beforeTableData[i][j] == null) && (afterTableData[i][j] == null)) {
                        if (!beforeTableData[i][j].equals(afterTableData[i][j])) {
                            assertFalse(true);
                        }
                    }
                }
            }

        }
    }

    public void defaultViewByDate() throws Exception {

        driver.findElement(By.xpath("//li/a[@href='/xxx']")).click();
        waitForElement("//li/a[@href='/xxx/registrations']", 5000, 250);
        driver.findElement(By.xpath("//li/a[@href='/xxx/registrations']")).click();
        int rowNum = getXPathCount("//div[@id='edit-objects-wrapper']/table[2]/tbody/tr");
        beforeTableData = new String[rowNum][6];
        sortedTableData = new String[rowNum][6];
        afterTableData = new String[rowNum][6];

        for (int i = 2; i <= rowNum; i++) {

            if (!waitForElement("//div[@id='edit-objects-wrapper']/table[2]/tbody/tr[2]", 5000, 250)) {
                break;
            }
            for (int j = 2; j < 8; j++) {
                beforeTableData[i - 2][j - 2] = driver.findElement(By.xpath("//div[@id='edit-objects-wrapper']/table[2]/tbody/tr[" + i + "]/td[" + j + "]")).getText();
            }
        }

        for (int i = 2; i <= rowNum; i++) {
            if (!waitForElement("//div[@id='edit-objects-wrapper']/table[2]/tbody/tr[2]", 5000, 250)) {
                break;
            }

            for (int j = 2; j < 8; j++) {
                afterTableData[i - 2][j - 2] = driver.findElement(By.xpath("//div[@id='edit-objects-wrapper']/table[2]/tbody/tr[" + i + "]/td[" + j + "]")).getText();
            }
        }
//        System.out.print("======DefaultView==========\n");
//        printArray(beforeTableData);
//        System.out.print("================\n");
//        afterTableData =  beforeTableData;
//        printArray(afterTableData);
        Arrays.sort(beforeTableData, new ReverseDateComparator());
//        System.out.print("================\n");
//        System.out.print("================\n");
//        printArray(beforeTableData);


        if (beforeTableData.length == afterTableData.length && afterTableData.length > 0) {
            for (int i = 0; i < beforeTableData.length; i++) {
                for (int j = 0; j < 5; j++) {
                    if (!(beforeTableData[i][j] == null) || !(afterTableData[i][j] == null)) {
                        if (!beforeTableData[i][j].equals(afterTableData[i][j])) {
                            assertFalse(true);
                            System.out.print("==ASSERT===FAILURE=====\n");
                        }
                    }
                }
            }

        }
    }

    // To test sorted array
    public static void printArray(String[][] array) {
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.print(array[i][j]);
                System.out.print("\t");
            }

            System.out.println();
        }
    }

    class NameComparator implements Comparator {

        public int compare(Object obj1, Object obj2) {
            int result = 0;

            String[] str1 = (String[]) obj1;
            String[] str2 = (String[]) obj2;

            /* Sort on first element of each array (last name) */
            if ((result = str1[0].compareTo(str2[0])) == 0) {
                /* If same display name, sort on second element (first name) */
                result = str1[2].compareTo(str2[2]);
            }

            return result;
        }
    }

    class ReverseDateComparator implements Comparator {

        public int compare(Object obj1, Object obj2) {
            try {
                DateFormat df = new SimpleDateFormat("dd/MM/yy");
                String[] str1 = (String[]) obj1;
                String[] str2 = (String[]) obj2;
                /* Sort on 3rd element of each array (date) */
                Date a = new Date();
                Date b = new Date();

                a = df.parse(str1[2]);
                b = df.parse(str2[2]);

                if (a.compareTo(b) == 0) {
                    if (str1[0].compareTo(str2[0]) == 0) {
                        return 1;
                    }
                } else {
                    return 0;
                }

            } catch (ParseException ex) {
                Logger.getLogger(SortAndFilterView.class.getName()).log(Level.SEVERE, null, ex);

            }
            return 0;
        }
    }

    class DateComparator implements Comparator {

        public int compare(Object obj1, Object obj2) {
            int result = 0;
            try {
                DateFormat df = new SimpleDateFormat("dd/MM/yy");
                String[] str1 = (String[]) obj1;
                String[] str2 = (String[]) obj2;
                /* Sort on 3rd element of each array (date) */
                Date a = new Date();
                Date b = new Date();

                a = df.parse(str1[2]);
                b = df.parse(str2[2]);

                if (b.compareTo(a) == 0) {
                    result = str1[0].compareTo(str2[0]);
                    return result;
                } else {
                    return 1;
                }

            } catch (ParseException ex) {
                Logger.getLogger(SortAndFilterView.class.getName()).log(Level.SEVERE, null, ex);

            }
            return 0;
        }
    }

    @annotation (author = "dsalami", story = {15691})
    public static Test suite() {
        TestSuite suite = new TestSuite();
        suite.addTest(new SortAndFilterView("goTo"));
        //suite.addTest(new SortAndFilterView("clickToSortByName"));
        suite.addTest(new SortAndFilterView("clickToSortByDate"));
        //suite.addTest(new SortAndFilterView("defaultViewByDate"));
        suiteCount = suite.testCount();
        return suite;
    }

    public static void main(String[] args) {
        run(suite());
    }
}


0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home