Thursday, May 8, 2014

How to print the cart content for multiple rows. (Real scenario)

Step1- Open http://shopping.rediff.com/shopping/index.html
Step2- Go to Mobiles.
Step3- click on Mobile Handsets.
Step4- click on 1st mobile.
Step5- click on Buy now.
Step6- again go back on Mobile Handsets page and click on 2nd mobile and repeat the
           step5.
Step7. likewise repeat the step6 for rest of the mobile on Mobile Handsets page.
Step8. print all the rows from the cart.

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 Rediff {
    public static void main(String[] args) throws AWTException {
        WebDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, 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();
                }
            }
        }
       
}

1 comment:

  1. hi sanjay ,
    Thanks for the valuable knowlwdge
    I have an small issue
    My Integration Testing scanerio is like

    in hybridframwork>> @BM(launcheBrowser)>> driver.get(gmail.com)>>login to gmail and send messgage to yahoo user>> logout>>
    Now i want to check yahoo as i have received the message or not in same class
    again driver.get(yahoo.mail)>> after this i am not able to click on web element on yahoo mail>> it is giving null pointer exception >>

    plz help how can we manage driver.get() method in this scanerio...twice

    ReplyDelete