Showing posts with label Example with Real Scenario. Show all posts
Showing posts with label Example with Real Scenario. Show all posts

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


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

}
}

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 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
     }
}

Wednesday, June 18, 2014

Login to http://www.makemytrip.com/

Note- Here we need to take care while writing the xpath for password field because that has some hidden html code.

import java.util.concurrent.TimeUnit;

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

public class SuccessLogin{
public static void main(String[] args){

WebDriver driver =new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.makemytrip.com");
driver.findElement(By.id("login_dropOpenItem")).click();
driver.findElement(By.id("username")).sendKeys("rishi.vg1@gmail.com");
driver.findElement(By.xpath("//input[@id='password_text']")).sendKeys("somepassword");
driver.findElement(By.id("login_btn")).click();
}
}

Tuesday, June 17, 2014

Select value from dropdown without using Select class. Good example.

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class SelectFromDropDown {
    public static void main(String[] args) {
        WebDriver driver=new FirefoxDriver();
        driver.get("https://makemytrip.com/");
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.findElement(By.xpath("//div//span[2][contains(@class,'flL travelers rght_space room_sec1')]")).click();
        Actions act = new Actions(driver);
        act.sendKeys(Keys.chord(Keys.DOWN,Keys.DOWN)).perform(); //press down key two times to select 3
    }
}

Monday, June 16, 2014

Print the name of the people in the gmail chatbox list and number of people in the gmail chat box in lefthand side. Realtime scenario.

import java.util.List;
import java.util.Scanner;
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.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class GmailChatBox {
  
WebDriver driver;

@BeforeTest
public void openurl()throws InterruptedException
{
    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();
}
@Test
public void chatlist() throws InterruptedException
{
    List<WebElement> chatboxsize = driver.findElements(By.xpath("//table[@class='vH']/tbody"));
    System.out.println("number of people in the gmail chat: "+chatboxsize.size());
    String name="";
    for (int i=1; i <=chatboxsize.size(); i++)
    {
        name = driver.findElement(By.xpath("//table[@class='vH']/tbody["+i+"]/tr[1]//span[1]")).getAttribute("textContent");
        System.out.println(name);
    }
}
@AfterTest
public void closeBrowser(){
    driver.close();
}
}

Saturday, June 14, 2014

Use of getAttribute() method.

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

public class Google {
    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();
        driver.get("https://www.google.co.in/");
        String title = driver.findElement(By.xpath("//div[@id='hplogo']/a/img")).getAttribute("title");
        System.out.println(title);
        driver.close();
    }
}

Use of Actions class

public class ClickCnChildMenu{
    public static void main(String[] args) {
        WebDriver driver=new FirefoxDriver();
        driver.get("http://new.mypomanager.com/Account/Login");
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.manage().window().maximize();
        driver.findElement(By.id("UserName")).sendKeys("retaileradmin");
        driver.findElement(By.id("Password")).sendKeys("password");
        driver.findElement(By.cssSelector("input[type='submit']")).click();
        WebElement parentmenu=driver.findElement(By.linkText("RETAILER"));
        Actions act=new Actions(driver);
        act.moveToElement(parentmenu).perform();
        WebElement createPO = driver.findElement(By.xpath("//a[contains(text(),'Create PO')]"));
        act.moveToElement(createPO).click().perform();
       
        }
}

Tuesday, June 10, 2014

How to check whether the selectbox is singleListbox or multipleListBox.

Monday, June 9, 2014

How to get the text which is not visible in web page but it is there in HTML code ? And how to check whether check box is enabled or not ?

Note: use  getAttribute("textContent") method to get the hidden text.

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Facebook {
    public static void main(String[] args) {
    WebDriver driver = new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
    driver.get("https://www.facebook.com/");
    String value= driver.findElement(By.xpath("//a[@title='Go to Facebook Home']")).getAttribute("textContent");
    System.out.println("Value is : " + value);
    boolean box= driver.findElement(By.xpath("//input[@type='checkbox']")).isEnabled();
    System.out.println(box);
    driver.close();
    }
}

Wednesday, June 4, 2014

Use of @BeforeClass and @AfterClass

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.support.ui.Select;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class Jcrew {
WebDriver driver ;
@Test
public void f() throws Exception {
driver.get("https://www.jcrew.com/index.jsp" );
driver.findElement(By.id("welcomeMatUsLink")).click();
driver.findElement(By.id("globalHeaderSignIn")).click();
Thread.sleep(3000);
driver.findElement(By.xpath("//a[@class='button-general button-nosubmit']")).click();
driver.findElement(By.id("lastName")).sendKeys("mahesh");
driver.findElement(By.id("emailAdd")).sendKeys("cnu.don1@gmail.com");
driver.findElement(By.id("emailAddConf")).sendKeys("cnu.don1@gmail.com");
driver.findElement(By.id("passWord")).sendKeys("mahesh123");
driver.findElement(By.id("passWordConf")).sendKeys("mahesh123");
driver.findElement(By.id("zipCode")).sendKeys("502109");
driver.findElement(By.id("zipCode")).sendKeys("502109");
driver.findElement(By.id("homePhone")).sendKeys("9052404371");
WebElement dd = driver.findElement(By.id("country")) ;
Select d = new Select (dd) ;
d.selectByIndex(3);
String txt = driver.findElement(By.id("erorMsg")).getText() ;
String pnt = "Please enter first name." ;
Thread.sleep(15000);

driver.findElement(By.xpath("//input[@name='createAccountAndStartShopping']")).click();
txt.equals(pnt) ;
}
@BeforeClass
public void beforeClass() {
driver = new FirefoxDriver() ;
//driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

}
@AfterClass
public void afterClass() {
// driver.quit();
}
}

Friday, May 23, 2014

How to login in linkedIn

Note- After login, 1st move cursor over Connections then click on Add Connections.

import java.util.Scanner;
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) {
        Scanner in = new Scanner(System.in);
        System.out.println("enter ur user id: ");
        String userId = in.nextLine();
        System.out.println("enter ur pass: ");
        String pass = in.nextLine();
     
        WebDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("http://www.linkedin.com/?trk=nav_logo");
        //login
        driver.findElement(By.id("session_key-login")).sendKeys(userId);
        driver.findElement(By.id("session_password-login")).sendKeys(pass);
        Actions act = new Actions(driver);
        WebElement sign = driver.findElement(By.id("signin"));
        act.moveToElement(sign).doubleClick().perform();
     
        //1st move cursor over Connections then click on Add Connections
      
        WebElement network = driver.findElement(By.xpath("//li[@class='nav-item']/a[contains(text(),'Connections')]"));
        act.moveToElement(network).perform();
        driver.findElement(By.xpath("//ul[@class='sub-nav']//a[contains(text(),'Add Connections')]")).click();
      
        //signout
        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();
      
        driver.quit();
    }
}

Saturday, May 17, 2014

What is the alternate way to send text in textbox of webpage with out using sendKeys() method ?

Note- Use Javascript to send text.

syntax-

((JavascriptExecutor)driver).executeScript("document.getElementById('attribute value of id').value='text which you want to pass'");

ex-
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class WithoutSendKeys {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://accounts.google.com/ServiceLogin?sacu=1&scc=1...");
((JavascriptExecutor)driver).executeScript("document.getElementById('Email').value='sanjay'");
}
}

Thursday, May 15, 2014

How to change the position of the open browser window.

Note - use command -> driver.manage().window().setPosition(new Point(int value1,int value2));

import java.awt.AWTException;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class MoveWindow
{
public static void main(String[] args) throws InterruptedException, AWTException
{
    WebDriver driver = new FirefoxDriver();
    driver.manage().window().setPosition(new Point(0,0)); //move the window to top right corner
    Thread.sleep(2000);
    driver.manage().window().setPosition(new Point(500,400)); //move the window to 500unit horizontally and 400unit vertical
}
}

How to change the size of the open browser window.

Use the command- driver.manage().window().setSize(new Dimension(int value1, int value2));
where driver is WebDriver type.

ex-

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

public class ResizeWindow {
    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();
        driver.manage().window().setSize(new Dimension(400,600));
    }
}

Tuesday, May 13, 2014

How to download pdf file in desired location without using AutoIt tool.

Note- This code will download the pdf file inside 'C' drive. You can change the directory by changing the path here. profile.setPreference( "browser.download.dir", "download location path" )

import java.util.Iterator;
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.firefox.FirefoxProfile;
import org.openqa.selenium.interactions.Actions;

public class DownloadPdf {
    public static void main(String[] args) {
        FirefoxProfile profile = new FirefoxProfile();
        profile.setPreference( "browser.download.folderList", 2 );
        profile.setPreference( "browser.download.dir", "C:\\" ); //this will download pdf inside 'C' driver. You can give your path where u want to save the file.
        profile.setPreference( "plugin.disable_full_page_plugin_for_types", "application/pdf" );
        profile.setPreference("browser.helperApps.neverAsk.saveToDisk",  
        "application/csv,text/csv,application/pdfss, application/excel" );
        profile.setPreference( "browser.download.manager.showWhenStarting", false );
        profile.setPreference( "pdfjs.disabled", true );
       
        WebDriver driver = new FirefoxDriver(profile);
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
       
        //login
        driver.get("http://myaccount.rinfra.com/myaccnt/login.do");
        driver.findElement(By.id("username")).sendKeys("150811386");
        driver.findElement(By.name("password")).sendKeys("150811386");
        driver.findElement(By.xpath("//input[@alt='Enter']")).click();
       
       
        driver.findElement(By.xpath("//div[text()='close']")).click(); //close pop-up window
       
        Actions act = new Actions(driver);
        WebElement ele = driver.findElement(By.xpath("//span[contains(text(),'bill & payments')]"));
        act.moveToElement(ele).perform(); //move cursor over bill & payment
        driver.findElement(By.xpath("//a[text()='download / print bill']")).click(); //clicking on 3rd element in the list
        driver.findElement(By.xpath("//a[@class='jqTransformSelectOpen']")).click(); // clicking on select drop down     
        driver.findElement(By.xpath("//a[@index='1']")).click();     //selecting 1st element        
        driver.findElement(By.xpath("//img[@src='images/view.gif']")).click(); // clicking on view
       
        //handle the windows
        Iterator<String> it = driver.getWindowHandles().iterator();
        String mainPage = it.next();
        String child = it.next();
        driver.switchTo().window(child); //switch to child window
        driver.findElement(By.xpath("//button[@id='download']")).click(); //click on download button
        driver.quit();
    }
}

Monday, May 12, 2014

How to pass the Data to test script through xml or How to use @Parameters in TestNG ?

Ans- use @Parameters to pass the input through xml.

Ex-

import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class PassInputThruXML {
@Test
@Parameters({"para1","para2"})
public void passInput(String para1, double para2){
    System.out.println(para1);
    System.out.println(para2);
}
}


testng.xml->


<suite name="Suite" parallel="none">
  <test name="Test">
    <parameter name="para1" value="selenium-makeiteasy" />
    <parameter name="para2" value="5.0" />
      <classes>
          <class name="TestNg.PassInputThruXML"/>
      </classes>
  </test>
</suite>

How to get the dropdown value in notepad ?

Note: FileWriter("path of the directory with filename.txt");

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

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 DropdownNotepad extends Thread {
    public static WebDriver driver;
    public static void main(String[] args) throws IOException {
        driver = new FirefoxDriver();
        driver.get("http://www.etouch.net/home/index.html");
        WebElement service = driver.findElement(By.xpath("//a[text()='Services']"));
        Actions act = new Actions(driver);
        act.moveToElement(service).perform();
        List<WebElement> dropdown = driver.findElements(By.xpath("//li[@id='services']//ul//ul/li"));
        System.out.println(dropdown.size());
       
        FileWriter fileWriter = new FileWriter("C:\\selenium\\out1.txt");
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
       
       
       
        for(WebElement ele: dropdown){
            System.out.println(ele.getText());
            bufferedWriter.write(ele.getText()+"\n");
        }
        bufferedWriter.close();
       
        driver.close();
    }
}