Wednesday, 13 January 2016

How to count the number of checkboxes checked in selenium webdriver ? or How to select multiple check box and verify ?

//How to count the number of checkboxes checked in selenium webdriver ? or How to select multiple check box and verify ?


import java.util.List;
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;

public class Count_Checkbox_Checked {
    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("http://www.gsmarena.com/samsung-phones-9.php");
        List<WebElement> checkBoxes = driver.findElements(By.xpath("//input[@type='Checkbox']"));
       // for(int i=0; i<checkBoxes.size(); i=i+2){
        for(int i=0; i<checkBoxes.size(); i=i+4)
        {
       
            checkBoxes.get(i).click();
        }
        int checkedCount=0, uncheckedCount=0;
        for(int i=0; i<checkBoxes.size(); i++){
            System.out.println(i+" checkbox is selected "+checkBoxes.get(i).isSelected());
            if(checkBoxes.get(i).isSelected()){
                checkedCount++;
            }else{
                uncheckedCount++;
            }
        }
        System.out.println("number of selected checkbox: "+checkedCount);
        System.out.println("number of unselected checkbox: "+uncheckedCount);
    }
}


Online shopping Click mobiles and it's looping the name

//Click mobiles and it's looping the name

import java.awt.AWTException;
import java.util.List;
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.openqa.selenium.interactions.Actions;

public class Cart_Content_Print_Name {
    public static void main(String[] args) throws AWTException {
        WebDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        driver.manage().window().maximize();
        driver.get("http://shopping.rediff.com/shopping/index.html");
        WebElement mobiles = driver.findElement(By.xpath("//span[text()='Mobiles']"));
        Actions act = new Actions(driver);
        act.moveToElement(mobiles).perform();
        act.moveToElement(driver.findElement(By.xpath("//a[text()='Mobile Handsets']"))).click().perform();
       
       
        List<WebElement> sets = driver.findElements(By.xpath("//div[@class='div_grid_display_big']"));
             
       
        for(int i=0; i<3; i++){
            sets.get(i).findElement(By.tagName("a")).click();
            driver.findElement(By.xpath("//input[@value='Buy now']")).click();
            if(i<2){
                driver.get("http://shopping.rediff.com/categories/mobile-handsets--new-/cat-8344/?sc_cid=fixcat_mobile%20handsets");
                sets = driver.findElements(By.xpath("//div[@class='div_grid_display_big']"));
            }
        }
       
            driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@name='cartframe']")));   
           
           List<WebElement> cart = driver.findElements(By.xpath("//tr[@class='selected_graybg']//table[@style='width:800px;padding:8px 0px 0px 0px;']"));
            for(int j=0; j<cart.size(); j++){
                List<WebElement> rowstest=cart.get(j).findElements(By.tagName("tr"));
                for(WebElement r:rowstest)
                {
                    List<WebElement> cols=r.findElements(By.tagName("td"));
                    for(int k=0; k<cols.size(); k++)
                    {
                        if(k==0){
                            System.out.print(cols.get(k).findElement(By.tagName("a")).getText()+"  ");
                        }else if(k==1){
                            System.out.print(cols.get(k).findElement(By.tagName("input")).getAttribute("value")+"  ");
                        }else{
                            System.out.print(cols.get(k).getText()+"  ");
                        }   
                    }
                    System.out.println();
               }
            }
        }
       
}

Handling calender popups or Date pickers



import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

    public class Calender_Popup_Or_Date_Picker {
    public static void main(String[] args) {
        int day, month, year;
          GregorianCalendar date = new GregorianCalendar();
          day = date.get(Calendar.DAY_OF_MONTH);
          month = date.get(Calendar.MONTH)+1;
          year = date.get(Calendar.YEAR);
          String today = "a_"+year+"_"+month+"_"+day;
          System.out.println(today);
    WebDriver driver = new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    driver.get("http://www.yatra.com/");
    driver.findElement(By.id("BE_flight_depart_date")).click();
    driver.findElement(By.xpath("//a[@id='"+today+"']")).click();
   
    }

    }

Sunday, 10 January 2016

How to read all data from CSV file



import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

import au.com.bytecode.opencsv.CSVReader;

public class ReadCSVData
{

public static void main(String[] args)
{
    String filename = "D:\\selenium_NEW_PRACTICE\\test_data\\test_csv_data.csv";
    ReadCSVData parseCSVFile = new ReadCSVData();

    System.out.println("Starting to parse CSV file using opencsv");
    parseCSVFile.parseUsingOpenCSV(filename);
}

private void parseUsingOpenCSV(String filename)
{
CSVReader reader;
try
{
    reader = new CSVReader(new FileReader(filename));
    String[] row;
    List<?> content = reader.readAll();

    for (Object object : content)
    {
        row = (String[]) object;
        for (int i = 0; i < row.length; i++)
        {
            // display CSV values
        System.out.println("Cell column index: " + i);
        System.out.println("Cell Value: " + row[i]);
        System.out.println("-------------");
        }
    }
}
catch (FileNotFoundException e)
{
    System.err.println(e.getMessage());
}
catch (IOException e)
{
    System.err.println(e.getMessage());
}
}
}

Friday, 8 January 2016

Encapsulation in java

Encapsulation :

 - Definition:
    : Encapsulation is a process of wrapping code/data(variables) and data together into a single unit.
    : Hiding the implementation and showing functionality
 - We can create a fully encapsulated class in java by making all the data memebers of class as "Private" . Ex: private String name;
 - By using only setter and getter methods you can make changes.
 - Example for Encapsulation : Capsule(medicine/capsule mixed with several medicines).
 - Ex: Suppose if u want to set the value of ID greater than 100 only, then u can write the
       logic inside the setter method.


Example Code:

Thursday, 7 January 2016

Fibonacci series code in java

package java_interview;

public class Fibonnacy_Test
{
    public static void main(String[] args) {
         int a=0;
         int b=1;       
    for(int i=0;i<10;i++)
    {       
        a=a+b;       
        b=a-b;
        System.out.println("printing fibonacy series 0 1 1 2 3 5 8 values : " + b);
    }
    }
}

How to Open link in new tab and close tab in selenium

package abcpack;

import static org.testng.Assert.assertEquals;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class NewTab {
    WebDriver d;
    @Test
    public void testNewTab() throws Exception
    {
        //Load the web page
        d.get("https://www.google.co.in/?gws_rd=ssl");
        assertEquals("Google",d.getTitle());
        Actions a=new Actions(d);
        WebElement link=d.findElement(By.linkText("Gmail"));
        a.moveToElement(link).keyDown(Keys.CONTROL).click(link).build().perform();
        Thread.sleep(5000);
    }
    @BeforeMethod
    public void setUp()
    {
        //Launch browser
        d=new FirefoxDriver();
        //System.setProperty("webdriver.ie.driver","F:\\Selenium_Scripts_Aug15\\Lib\\IEDriverServer.exe");
        //d=new InternetExplorerDriver();
        //System.setProperty("webdriver.chrome.driver","F:\\Selenium_Scripts_Aug15\\Lib\\chromedriver.exe");
        //d=new ChromeDriver();
        d.manage().window().maximize();
        d.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
    }
    @AfterMethod
    public void tearDown()
    {
        //Close browser
        d.quit();
    }
}
++++++++++++++
package All_Types_Run;

import java.awt.AWTException;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class Close_Or_Open_New_Tab {
    public static void main(String[] args) throws AWTException, InterruptedException {
       
        WebDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("http://selenium-makeiteasy.blogspot.in/search/label/Interview%20Ques-Ans%20for%20Automation%20Tester");
        Actions act = new Actions(driver);
        WebElement link = driver.findElement(By.xpath("//a[text()='How to improve performance of the script in selenium webdriver?']"));
        act.moveToElement(link).contextClick().sendKeys("T").perform(); //open link in new tab
        act.sendKeys(Keys.chord(Keys.CONTROL,Keys.TAB)).perform();//switch to new tab by pressing control+tab
        Thread.sleep(3000); // simply wait for 3sec to see new tab open or not
        act.sendKeys(Keys.chord(Keys.CONTROL,Keys.F4)).perform(); //press control+f4 to close tab
        driver.findElement(By.xpath("//a[text()='Show all posts']")).click(); //click on a 'Show all posts' link of main tab to confirm we are in main tab
    }
}