Automation Using Selenium Webdriver

Wednesday, 14 December 2016

Java – Remove Duplicate Number from Array

package JavaPrograms;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class RemoveduplicateArray {
//
//  Remove duplicate entries from an array of long integers
//
//
public static void main(String[] args)
{
//
// A long integer array with some duplicated values
//
long [] longArray = { 11,0, 11, 22, 44, 55, 66, 77, 88, 0, 99, 77, 66, 42, 55, 66, 99, 11};
System.out.println("Original array size = " + longArray.length);
System.out.println("Original array            : " +
Arrays.toString(longArray));
//
// Want to let Java convert the array to a Set, since
// the Set object won't have duplicate entries.
//
// But first...
//
// Since the Set constructor needs a List (not an array),
// "convert" the array to a List.
//
List<Long> longList = new ArrayList<Long>();
for (int i = 0; i < longArray.length; i++)
{
longList.add(longArray[i]);
}
// Now, instantiate a Set with its constructor that has a List argument
Set <Long> longSet = new HashSet<Long>(longList);
//
// Create an array of Long objects and use Set.toArray()
// to "convert" the Set to the array.
Long[] resultNoDups = new Long[longSet.size()];
longSet.toArray(resultNoDups);
// Finally, if we want an array of long ints rather
// than an array of Long objects, create the array
// and copy the values
long [] finalArray = new long[resultNoDups.length];
for (int i = 0; i < resultNoDups.length; i++) {
finalArray[i] = resultNoDups[i];
}
// Taa-daa
System.out.println("After removing duplicates : " +
Arrays.toString(finalArray));
System.out.println("Result array size = " + finalArray.length);
System.out.println();
               }
}

Monday, 12 December 2016

Reading Font Properties In Selenium WebDriver Using .getCssValue() Method



Sometimes you need to read font properties like font size, font color, font family, font background color etc.. during WebDriver test case execution. Selenium WebDriver Is very wast API and It has many built In methods to perform very small small operations on web page.Reading font property manually Is very simple task using firebug as shown In bellow given Image




If you wants to read above shown font property In selenium webdriver then you can do It using .getCssValue() Method. You can provide property name (Example : font-family, font-size, font-weight, etc.) with .getCssValue() method to read Its value.

Bellow given example will read values of font-size, color, font-family and text-align properties of "Example Login Page" text.

package Testing_Pack;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class fontTest { 
WebDriver driver = null;

    @BeforeTest
    public void setup() throws Exception {  
  driver = new FirefoxDriver();
         driver.manage().window().maximize();
         driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
         driver.get("http://any login page");
    }

 @Test
 public void readFontProperty(){
  //Locate text string element to read It's font properties.
  WebElement text = driver.findElement(By.xpath("//h1[contains(.,'Example Login Page')]"));
  
  //Read font-size property and print It In console.
  String fontSize = text.getCssValue("font-size");
  System.out.println("Font Size -> "+fontSize);
  
  //Read color property and print It In console.
  String fontColor = text.getCssValue("color");
  System.out.println("Font Color -> "+fontColor);
  
  //Read font-family property and print It In console.
  String fontFamily = text.getCssValue("font-family");
  System.out.println("Font Family -> "+fontFamily);
  
  //Read text-align property and print It In console.
  String fonttxtAlign = text.getCssValue("text-align");
  System.out.println("Font Text Alignment -> "+fonttxtAlign);
 }
}

Output of above example Is as bellow.
Font Size -> 26.4px
Font Color -> rgba(102, 102, 102, 1)
Font Family -> "Trebuchet MS",Trebuchet,Verdana,sans-serif
Font Text Alignment -> left


Open a link in a New tab

We use actions objects to open any hyperlink in a new tab. e.g.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.Test;
import java.util.concurrent.TimeUnit;

public class NewTab {
    public static void main(String[] args) {
       WebDriver driver = new FirefoxDriver();
        driver.get("http://www.flipcart.com/");
        driver.manage().window().maximize();
        Actions act = new Actions(driver);
        WebElement link = driver.findElement(By.linkText("Flipkart First"));
        act.moveToElement(link).contextClick().sendKeys("F").perform();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
         driver.close();
   
    }
}