Saturday, July 5, 2014

How to verify the text present in my web page.

There could be two ways to verify text present in web page or not-
1) Find that element where that text is populating and use getText() method then verify.
2)1st Get the page source using getPageSource() method then verify.

Ex-

import java.util.concurrent.TimeUnit;

import junit.framework.Assert;

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

public class VerifyTextPresentInWebPage {
    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();
        driver.get("http://www.goibibo.com/");
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
       
        //verify 'International Flights' text is there in the web page or not
        String expText = "International Flights";
       
        //first way to verify by using locator and getText() method
        String actText = driver.findElement(By.xpath("//a[@class='hm_inactive']")).getText();
        if(actText.contains(expText)){
            System.out.println("1) Expected text '"+expText+"' present in the web page.");
        }else{
            System.out.println("1) Expected text '"+expText+"' is not present in the web page.");
        }
       
        //second way to verify by using getPageSource method
        String pageSource = driver.getPageSource();
        if(pageSource.contains(expText)){
            System.out.println("2) Expected text '"+expText+"' present in the web page.");
        }else{
            System.out.println("2) Expected text '"+expText+"' is not present in the web page.");
        }
        driver.close();
    }
}


Friday, July 4, 2014

Log4j

Log4j, you can always say yes you have used log4j to log the run time information. Interviewer never ask about the code but even if he ask you can easily say that it's one time code which is already there but yes i have worked on that. I know the logic but exact code i did not remember.

Note- code will post soon.

Maven Project

1) Maven Project- the main thing is that jar files is not required to download each time separately whenever there is any update in browser or in any other files. It will automatically download the jar files for your project thru POM.xml in which we add the dependency. (This you can tell in interview.) You can always say that yes you have worked in maven project. This is the main difference in java project and maven project. 

Note- Detail project will post soon.

Wednesday, June 25, 2014

Print the name of friends with the status like one is online, busy, idle or offline in gmail chat.

Note- Please give the gmail id and password while runtime. (after pressing ctrl+f11).

import java.util.List;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;

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

public class GmailOnlinePeople {

public static void main(String[] args) {
    WebDriver driver;
    Scanner in = new Scanner(System.in);
    System.out.println("Enter the gmail id: ");
    String emailId = in.next();
    System.out.println("Enter the pass: ");
    String pass = in.next();
 
    driver = new FirefoxDriver(); //open firefox browser
 
    //login to gmail
    driver.get("http://www.gmail.com");
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(40,TimeUnit.SECONDS);
    driver.findElement(By.name("Email")).sendKeys(emailId);
    driver.findElement(By.name("Passwd")).sendKeys(pass);
    driver.findElement(By.name("signIn")).click();
    String name="";
    //friends with available status
    try {
        List<WebElement> available = driver.findElements(By.xpath("//tr[td[img[contains(@alt,'Available')]]]//td[2]/span[1]"));
        System.out.println("number of friends with available status in the gmail chat: "+available.size());
        if(available.size()!=0){
            System.out.println("Name of the friends with Available status: ");
        }
        for (int i=0; i <available.size(); i++)
        {
            name = available.get(i).getAttribute("textContent");
            System.out.println((i+1)+") "+name);
        }
    } catch (NoSuchElementException e) {
        System.out.println("No one is there with available status.");
    }
   
  //friends with busy status in the gmail chat
    try {
        List<WebElement> busy = driver.findElements(By.xpath("//tr[td[img[@alt='Busy']]]//td[2]/span[1]"));
        System.out.println("number of friends with busy status in the gmail chat: "+busy.size());
        if(busy.size()!=0){
            System.out.println("Name of the friends with busy status: ");
        }
        for (int i=0; i <busy.size(); i++)
        {
            name = busy.get(i).getAttribute("textContent");
            System.out.println((i+1)+") "+name);
        }
    } catch (NoSuchElementException e) {
        System.out.println("No one is with busy status.");
    }
   
  //friends with idle status
    try {
        List<WebElement> idle = driver.findElements(By.xpath("//tr[td[img[@alt='Idle']]]//td[2]/span[1]"));
        System.out.println("number of friends with idle status in the gmail chat: "+idle.size());
        if(idle.size()!=0){
            System.out.println("Name of the friends with idle status: ");
        }
        for (int i=0; i <idle.size(); i++)
        {
            name = idle.get(i).getAttribute("textContent");
            System.out.println((i+1)+") "+name);
        }
    } catch (NoSuchElementException e) {
        System.out.println("No one is with idle status.");
    }
   
  //friends with offline status
    try {
        List<WebElement> offline = driver.findElements(By.xpath("//tr[td[img[@alt='Offline']]]//td[2]/span[1]"));
        System.out.println("number of friends offline in the gmail chat: "+offline.size());
        if(offline.size()!=0){
            System.out.println("Name of the friends offline: ");
        }
        for (int i=0; i <offline.size(); i++)
        {
            name = offline.get(i).getAttribute("textContent");
            System.out.println((i+1)+") "+name);
        }
    } catch (NoSuchElementException e) {
        System.out.println("No one is offline.");
    }
    driver.close();
}
}

Login/logout scenario for linkedin.

Note: Please give the your user id and password in highlighted place.

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 LinkedIn {
public static void main(String[] args) throws InterruptedException {
WebDriver driver=new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.linkedin.com/");
driver.findElement(By.id("session_key-login")).sendKeys("give_ur_userid_here");
driver.findElement(By.id("session_password-login")).sendKeys("give_ur_pass_here");
//driver.findElement(By.xpath("//input[@id='signin']")).click();
Actions act=new Actions(driver);
WebElement sign = driver.findElement(By.id("signin"));
act.moveToElement(sign).doubleClick().perform();
WebElement network = driver.findElement(By.xpath("//li[@class='nav-item']/a[contains(text(),'Connections')]"));
act.moveToElement(network).perform();
Thread.sleep(1000);
WebElement addConn = driver.findElement(By.xpath("//ul[@class='sub-nav']//a[contains(text(),'Add Connections')]"));
act.moveToElement(addConn).click().perform();
WebElement img = driver.findElement(By.xpath("//img[@class='img-defer nav-profile-photo']"));
act.moveToElement(img).perform();
WebElement signOut = driver.findElement(By.xpath("//a[contains(text(),'Sign Out')]"));
act.moveToElement(signOut).click().perform();

}
}

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 LinkedIn {
public static void main(String[] args) throws InterruptedException {
WebDriver driver=new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.linkedin.com/");
driver.findElement(By.id("session_key-login")).sendKeys("give_ur_userid_here");
driver.findElement(By.id("session_password-login")).sendKeys("give_ur_pass_here");
//driver.findElement(By.xpath("//input[@id='signin']")).click();
Actions act=new Actions(driver);
WebElement sign = driver.findElement(By.id("signin"));
act.moveToElement(sign).doubleClick().perform();
WebElement network = driver.findElement(By.xpath("//li[@class='nav-item']/a[contains(text(),'Connections')]"));
act.moveToElement(network).perform();
Thread.sleep(1000);
WebElement addConn = driver.findElement(By.xpath("//ul[@class='sub-nav']//a[contains(text(),'Add Connections')]"));
act.moveToElement(addConn).click().perform();
WebElement img = driver.findElement(By.xpath("//img[@class='img-defer nav-profile-photo']"));
act.moveToElement(img).perform();
WebElement signOut = driver.findElement(By.xpath("//a[contains(text(),'Sign Out')]"));
act.moveToElement(signOut).click().perform();

}
}

Tuesday, June 24, 2014

How to get the screen resolution and size ?

import java.awt.Dimension;
import java.awt.Toolkit;

public class ScreenResolution {
    public static void main(String[] args)
    {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); //get the dimension of screen
        System.out.println("Screen Widht: " + screenSize.getWidth());
        System.out.println("Screen Height: " + screenSize.getHeight());
       
        int screenResolution = Toolkit.getDefaultToolkit().getScreenResolution(); //get the resolution
        System.out.println("Screen resolution is : "+screenResolution);
    }
}


Output-

Screen Widht: 1366.0
Screen Height: 768.0
Screen resolution is : 96

Sunday, June 22, 2014

How to print the dorpdown values which are hidden ?

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 MakeMyTrip {
    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();
        driver.get("http://www.makemytrip.com/");
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.findElement(By.xpath("//span[input[@tabindex='4']]//a")).click();
        List<WebElement> cities = driver.findElements(By.xpath("//li[@class='ui-menu-item']"));
        System.out.println("Number of cities : "+cities.size()); //this will take only those which are visible in 1st click
        for(int i=0; i<cities.size(); i++){
            System.out.println(i+1+") city name: "+cities.get(i).getText());
        }
        driver.close();
    }
}


output-

Number of cities : 47
1) city name: New Delhi, India (DEL)
2) city name: Mumbai, India (BOM)
3) city name: Bangalore, India (BLR)
4) city name: Goa, India (GOI)
5) city name: Chennai, India (MAA)
6) city name: Kolkata, India (CCU)
7) city name: Hyderabad, India (HYD)
8) city name: Pune, India (PNQ)
9) city name: Ahmedabad, India (AMD)
10) city name: Cochin, India (COK)
11) city name: Jaipur, India (JAI)
12) city name: Dubai, UAE (DXB)
13) city name: Singapore, Singapore (SIN)
14) city name: Bangkok, Thailand (BKK)
15) city name: New York, US - All Airports (NYC)
16) city name: Kuala Lumpur, Malaysia (KUL)
17) city name: London, UK - All Airports (LON)
18) city name: Hong Kong, China (HKG)
19) city name: Doha, Qatar (DOH)
20) city name: Colombo, Sri Lanka (CMB)
21) city name: Agartala, India (IXA)
22) city name: Agatti Island, India (AGX)
23) city name: Ahmedabad, India (AMD)
24) city name: Aizawl, India (AJL)
25) city name: Allahabad, India (IXD)
26) city name: Amritsar, India (ATQ)
27) city name: Aurangabad, India (IXU)
28) city name: Bagdogra, India (IXB)
29) city name: Bangalore, India (BLR)
30) city name: Belgaum, India (IXG)
31) city name: Bellary, India (BEP)
32) city name: Bhavnagar, India (BHU)
33) city name: Bhopal, India (BHO)
34) city name: Bhubaneshwar, India (BBI)
35) city name: Bhuj, India (BHJ)
36) city name: Calicut, India (CCJ)
37) city name: Chandigarh, India (IXC)
38) city name: Chennai, India (MAA)
39) city name: Cochin, India (COK)
40) city name: Coimbatore, India (CJB)
41) city name: Dehradun, India (DED)
42) city name: Dharamshala, India (DHM)
43) city name: Dibrugarh, India (DIB)
44) city name: Dimapur, India (DMU)
45) city name: Diu, India (DIU)
46) city name: Gaya, India (GAY)
47) city name: Goa, India (GOI)

Saturday, June 21, 2014

How to Select Last value from the dropdown without using getOptions() ?

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 SelectLastOption
    {
    public static void main(String [] args) throws InterruptedException
    {
    WebDriver driver=new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.manage().window().maximize();
    driver.get("http://www.naukri.com/");
    driver.findElement(By.xpath("//li[p[text()='Job Category']]//span")).click();//click on job category dropdown
       
    List<WebElement> allOptions = driver.findElements(By.xpath("//select[@id='fareaSL']//option")); //get all the options from the dropdown
    System.out.println("last option is"+allOptions.get(allOptions.size()-1).getText()); //print last option
   
    Actions act = new Actions(driver);
    act.doubleClick(allOptions.get(allOptions.size()-1)).perform(); //its not always mandatory to use double click.
   
    //to confirm what value has been selected inside box
   
    String value = driver.findElement(By.xpath("//li[p[text()='Job Category']]//span/input")).getAttribute("value");
    System.out.println("the option has been selected is "+value);
}
}

How to select desired date from the calendar pop-up ?

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 CalenderPopUp {
   
     public static void main(String[] args) {
     int day, month, year;
   
     String futrDate = "td_"+2015+"_"+06+"_"+24; //this is the 'id' format in the calendar popup in html code, ex- id=td_2015_6_24
   
     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();
     String monthYear="";
     //this loop will execute untill your required month and year will not appear in the calendar
     //here you can also pass the input from your and by making code as common
     while(!(monthYear.equals("June 2015"))){
         driver.findElement(By.xpath("//a[@class='js_btnNext sprite nextBtn']")).click(); //this will click on next button for month
         monthYear = driver.findElement(By.xpath("//span[@class='js_monthTitle']")).getText(); // this is to get the month and year from calendar pop up
     }
   
     driver.findElement(By.xpath("//td[@id='"+futrDate+"']")).click(); //this will select the date
     }
}

How to handle multiples windows if they open at different places not all together.

How to handle multiples windows if they open at different places not all together.