Wednesday, October 27, 2010

Preference Over Logic

I sometimes wonder why I reason the way I do now and how far I think my faculties have developed or regressed form the days I saw things the way they were. I remember sitting in my Bedroom in skeltons lane , Leyton at 17 writing out a potential cure for cancer, all types of pain physical and otherwise and all those "nice to have" that would make the world a happier place. Dreaming of everybody singing "kumba ye". Utopia for me was a reality, I lived with an unbreakable assurance that no matter what life handed me I would always overcome it without much hassle, scars or stories. Indeed I had hoped for harder situations because of my innate confidence in Religion and in Self.. Well that was the beginning of my formal reasoning experience. Being the first child. I didn't have much friend to play with lots of toys, but like most little boys I got bored of those toys real quick ( can still be a little boy at time btw :-) ). I spent a lot of my childhood looking at the wall of my bedroom watching imaginary films, seeing myself flying lego helicopters that actually lift off the ground, riding real motorbikes , shooting real guns and most especially taking out /or bringing to book all the baddies :-) (boy those were some days). Well what has all these background got to do with my essay. It may be a invaluable background or basis to understanding things better, if you are the type that like to look deeper.


The background for which I'm setting all these is the familiar
west Vs rest of the world, by rest of the world i refer to all hues of conservative all over the world from the Mullahs of Kandahar to the Hawks like Dick "the" Cheney and Donald"Donnie" Rumsfeld. remember the famous quote "your either with usu or against us" - a good primer for you cannot or should not bother about thinking and making your own mind up. If you have not listened to Chimamanda single story now is a good time to do so

http://www.ted.com/talks/chimamanda_adichie_the_danger_of_a_single_story.html

I will articulate my opinion from FACTS and cloth them in plain idioms, excluding all motives founded on ignorance, sophism and all manner of demagogue......

Article to follow Shortly......


1. Origin Of Logic

a) Pseudo-logic and its accomplice pseudo-science

b) Illogicality - the in-consequence of preferences

2 Nature Of Preference

a). Necessity

b). Illogicality - the in-consequence of preferences

    c) Important role of ignorance and uncertainty

    3. Views

a) The progressive view -

    b) The conservative view -

    c) The God People -

( cost of disharmony/ consequence / the unexpected / the predictable – what's missing, what's ignored , or ration, repression, self preserving, harmonizing views, paranoia and irrational)

  1. The Balanced Balance

    a) Good Judgement

    b) Good Reason

    5. Morality - environment, belief Age (times) and uncertainty - Factorising the common into sense

      6. Rhetoric and Sorphimist.

7. The undeniable Core

    • Essence or the Koko

    • What is, before how is, how isn't ( the validity )

    • Make Up your mind

    • Trust Me



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());
    }
}


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.. :-)

Preference Over Logic

Tuesday, August 14, 2007

MBNG Winner