Wednesday 30 December 2015

hash map and hash table difference

Difference between HashMap and HashTable / HashMap vs HashTable 

1. Synchronization or Thread Safe :  This is the most important difference between two . HashMap is non synchronized and not thread safe.On the other hand, HashTable is thread safe and synchronized.
When to use HashMap ?  answer is if your application do not require any multi-threading task, in other words hashmap is better for non-threading applications. HashTable should be used in multithreading applications.

2. Null keys and null values :  Hashmap allows one null key and any number of null values, while Hashtable do not allow null keys and null values in the HashTable object.



3. Iterating the values:  Hashmap object values are iterated by using iterator .HashTable is the only class other than vector which uses enumerator to iterate the values of HashTable object.



HashMapHashtable



SynchronizedNoYes



Thread-SafeNoYes



Null Keys and Null valuesOne null key ,Any null valuesNot permit null keys and values



Iterator typeFail fast iteratorFail safe iterator



PerformanceFastSlow in comparison



Superclass and LegacyAbstractMap , NoDictionary , Yes

Tuesday 29 December 2015

snapshot for reporting

 // Function call when test step FAILS
    public static void takeSnapshot(String msg,int status,WebDriver driver)
    {
        universalImageCounter++;
            String tempImgFile=IMG_PATH_UNDER_PARENT+"screenshot_"+(universalImageCounter)+".png";           
            try
            {
                File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
                FileUtils.copyFile(srcFile, new File(tempImgFile));
                addToReport("###"+msg, status);
            }
            catch(Exception ex)
            {
                    addToReport("Error ID 1: Issue while taking snapshot.",FAIL);
            }
    }

Swipr issue while X and Y axis distance less than 50

try{
    List<WebElement> el=driver.findElements(xpath);
    System.out.println("No of elements: "+el.size());
    int a,b,c;
    element=(MobileElement)el.get(0);
    int topY = element.getLocation().getY();
    int bottomY = topY + element.getSize().getHeight();
    int centerX = element.getLocation().getX() + (element.getSize().getWidth()/2);
   
    /*if(topY<0){
        element=(MobileElement)el.get(1);
    topY = element.getLocation().getY();
    bottomY = topY + element.getSize().getHeight();
    centerX = element.getLocation().getX() + (element.getSize().getWidth()/2);
    }*/
    if(topY<0)
        topY=50;
    if(bottomY>driver.manage().window().getSize().height)
    bottomY=driver.manage().window().getSize().height-50;
//    System.out.println("topY="+topY);
//    System.out.println("bottomY="+bottomY);
//    System.out.println("centerX="+centerX);
    driver.swipe(centerX, bottomY, centerX, topY, duration);
    if (bottomY-topY<=50){
        bottomY=driver.manage().window().getSize().height-50;
        topY=bottomY-50;
    }   
    driver.swipe(centerX, bottomY, centerX, topY, duration);
       
   
    }catch(Exception e){
        System.out.println("Exception occurred while swipeing up");
    }

Wednesday 2 December 2015

enhanced for loop

enhanced for loop is a simpler way to do this same thing. (The colon in the syntax can be read as "in.")
for (int myValue : myArray) {
    System.out.println(myValue);
}
The enhanced for loop was introduced in Java 5 as a simpler way to iterate through all the elements of a Collection (Collections are not covered in these pages). It can also be used for arrays, as in the above example, but this is not the original purpose.
Enhanced for loops are simple but inflexible. They can be used when you wish to step through the elements of the array in first-to-last order, and you do not need to know the index of the current element. In all other cases, the "standard" for loop should be preferred.
Two additional statement types, break and continue, can also control the behavior of enhanced for loops.

Comparing Array and list of array

 public Mobile_AboutMePage verifyaboutMeScreen() throws IOException, InterruptedException{
       
        //expected Array list
        String[] expectedLabels={"Basic Info","More about Myself, Partner and Family", "Family","Religious Background","Family","Astro Details","Location, Education & Career","Lifestyle","Partner Preferences","Partner Location","Partner Education & Career","Partner Lifestyle & Appearance"};
        //created actual label list from find element
        List<String> actualLabels=new ArrayList<String>();
        try{
            List<WebElement> lable=driver.findElements(TestUtils.GetPropertiesWeb("AboutMe_BasicInfo"));
            System.out.println(lable.size());
           
           
       
            //for loop -to check every element(e1) from list (lable)
            for(WebElement el:lable){
                if(!el.getText().equals(""))
                    //adding to arraylist
                    System.out.println(actualLabels.add(el.getText()));
            }
           
            for(int i=0;i<expectedLabels.length;i++){
                // comparing 2 list
                if(actualLabels.contains(expectedLabels[i]))
                {
                    System.out.println(lable.get(i).getText());
                    System.out.println( "matching with expected text" );
                    Reports.addToReport("In Basic Info Section: "+expectedLabels[i]+" Displayed<>ASSERT-PASS<>"+Page,PASS);
                }
                else{
                   
                    System.out.println(lable + "not matching with expected text" );
                    Reports.takeSnapshot("Element not found: Basic Info Locator<>ASSERT-FAIL<>"+Page+"<>*",FAIL, driver);
                }
            }
               
        }catch(Exception e){
            System.out.println("Lable locator not found");
            Reports.takeSnapshot("Element not found: Basic Info Locator<>ASSERT-FAIL<>"+Page+"<>*",FAIL, driver);
        }
        return this;
    }

Reading data from excel

//This function reads data from excel and returns as hash table
    public static Object[][] getData(String sheetName,Xls_Reader xls){
        int rowCount = xls.getRowCount(sheetName)-1;
        int columnCount=xls.getColumnCount(sheetName);
        System.out.println("rowCount="+rowCount);
        int c=0;
        for(int k=2;k<rowCount+2;k++){
            if(!xls.getCellData(sheetName, 0, k).equalsIgnoreCase("Y"))
                c++;
        }
        int d=0;
        int row=rowCount-c;
        System.out.println(row+"--"+columnCount);
        Object[][] data = new Object[row][1];
        Hashtable<String,String> table=null;
        for(int i=2;i<=rowCount+1;i++){
            if(xls.getCellData(sheetName, 0, i).equalsIgnoreCase("Y")){
            table=new Hashtable<String,String>();
              for(int j=0;j<columnCount;j++){
                 String key=xls.getCellData(sheetName, j, 1).trim();
                 String val=xls.getCellData(sheetName, j, i).trim();//.split(",")[0];
                 //System.out.println(key+"--"+val);
                 table.put(key, val);
            }
              //System.out.println(d);
              data[d][0]=table;
              d++;
        }
        }
        return data;
    }

for selendroid switch to webview

 public void switchToWebviewSelendroid() throws IOException, InterruptedException{
          //Thread.sleep(30000L);
          try{
          Set<String> contextNames = driver.getContextHandles();
          for (String contextName : contextNames) {
              System.out.println(contextNames); //prints out something like NATIVE_APP \n WEBVIEW_1
            
          }
          //driver.context("WEBVIEW_com.shaadi.android");
          driver.switchTo().window("WEBVIEW_0");
          }catch(Exception e){
              e.printStackTrace();
          }
      }