Subversion Repositories SmartDukaan

Rev

Blame | Last modification | View Log | RSS feed

package com;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;

import com.thoughtworks.selenium.*;
import com.unitedinternet.portal.selenium.utils.logging.HtmlResultFormatter;
import com.unitedinternet.portal.selenium.utils.logging.LoggingCommandProcessor;
import com.unitedinternet.portal.selenium.utils.logging.LoggingDefaultSelenium;
import com.unitedinternet.portal.selenium.utils.logging.LoggingResultsFormatter;
import com.unitedinternet.portal.selenium.utils.logging.LoggingSelenium;
import com.unitedinternet.portal.selenium.utils.logging.LoggingUtils;
//import static com.unitedinternet.portal.selenium.utils.logging.LoggingAssert.assertEquals;
//import static com.unitedinternet.portal.selenium.utils.logging.LoggingAssert.assertTrue;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
//import org.openqa.selenium.server.SeleniumServer;

//import java.util.regex.Pattern;

public class Buyproductonprod extends SeleneseTestBase {
        

        BufferedWriter loggingWriter;
        protected LoggingSelenium selenium;
        final String resultbasePath = "/opt/seleniumTests/results/prod";
        //final String resultbasePath = "/opt/seleniumTests/results";
    final String resultHtmlFileName = resultbasePath + "/" + "result0.html";
    final String resultEncoding = "UTF-8";          
    final String screenshotpath="screenshot";
    final String screenshotresultPath=new File("/var/www/results"+"/"+screenshotpath).getAbsolutePath();
        @Before
        public void setUp() throws Exception {
                if (!new File(screenshotresultPath).exists()) {
                         new File(screenshotresultPath).mkdirs();
                }
            loggingWriter = LoggingUtils.createWriter(resultHtmlFileName, resultEncoding , true);
            LoggingResultsFormatter htmlFormatter = 
                new HtmlResultFormatter(loggingWriter, resultEncoding);
           htmlFormatter.setScreenShotBaseUri("/results"+"/"+screenshotpath+"/"); // this is for linking to the screenshots
           htmlFormatter.setAutomaticScreenshotPath(screenshotresultPath);
            // wrap HttpCommandProcessor from remote-control
            LoggingCommandProcessor myProcessor = 
            //new LoggingCommandProcessor(new HttpCommandProcessor("localhost", 4444, "*firefox", "http://saholic:shop2020@shop2020.in:8080/"), htmlFormatter);
            new LoggingCommandProcessor(new HttpCommandProcessor("localhost", 4444, "*firefox", "http://saholic.com/"), htmlFormatter);
            selenium = new LoggingDefaultSelenium(myProcessor);
            myProcessor.setExcludedCommands(new String[] {});
            selenium.start();   
        }

        @Test(expected=AssertionError.class)
        public void testBuyproductonprod() throws Exception {
                selenium.setContext("testBuyprodonprod()");
                selenium.setTimeout("180000");
                selenium.open("/");
                selenium.windowMaximize();
                com.unitedinternet.portal.selenium.utils.logging.LoggingAssert.assertEquals("Page Title Mobile Phone Reviews and Best Deals in India | saholic.com not Found","Mobile Phone Reviews and Best Deals in India | saholic.com", selenium.getTitle(),selenium);
                verifyTrue(selenium.isElementPresent("css=img[alt=\"Saholic\"]"));
                verifyTrue(selenium.isTextPresent("Search"));
                verifyTrue(selenium.isElementPresent("id=searchButton"));
                verifyEquals("BEST DEALS", selenium.getText("css=div.home-listing-common-header-text > span.left"));
                verifyTrue(selenium.isElementPresent("//div[@id='products']/div[2]/div/div/span"));
                verifyTrue(selenium.isElementPresent("css=span.common-widget-top-bar-text-right"));
                verifyTrue(selenium.isElementPresent("css=#browsehistory > div.common-widget-top-bar > div.common-widget-top-bar-text > span.common-widget-top-bar-text-right"));
                verifyEquals("BEST SELLERS", selenium.getText("//div[@id='products']/div[3]/div/div/span"));
                verifyTrue(selenium.isElementPresent("css=div.home-listing-common-content.best-sellers"));
                verifyTrue(selenium.isElementPresent("css=div.home-listing-common-content.latest-arrivals"));
                verifyTrue(selenium.isElementPresent("css=div.home-listing-common-content.best-deals"));
                for(int i=8;i<20;i++)
                {
                        System.out.println("abc"+i+"");
                        selenium.isElementPresent("dom=document.images["+i+"]");
                        System.out.println("dom=document.images["+i+"]");
                        assertTrue("Not able to buy Product",selenium.isElementPresent("dom=document.images["+i+"]"));
                selenium.click("dom=document.images["+i+"]");
                Thread.sleep(20000);
                selenium.waitForPageToLoad("30000");
                Thread.sleep(8000);
                com.unitedinternet.portal.selenium.utils.logging.LoggingAssert.assertTrue("Product detail element not found",selenium.isElementPresent("id=productDetail"),selenium);
                //selenium.captureScreenshot(screenshotresultPath+"/"+"ErrorPage"+".png");
                //com.unitedinternet.portal.selenium.utils.logging.LoggingAssert.assertTrue("Failed assert",selenium.isElementPresent("id=productDetail"),selenium);
                //com.unitedinternet.portal.selenium.utils.logging.LoggingAssert.assertTrue("Cart Summary not displayed",selenium.isElementPresent("css=div.cart"),selenium);
                //com.unitedinternet.portal.selenium.utils.logging.LoggingAssert.assertTrue("Cart Summary not displayed",selenium.isElementPresent("css=div.cart-top"),selenium);
                //verifyTrue(selenium.isElementPresent("css=#accessories > div.common-widget-top-bar > div.common-widget-top-bar-text"));
                com.unitedinternet.portal.selenium.utils.logging.LoggingAssert.assertTrue("Add to card button not present",selenium.isElementPresent("id=addToCart"),selenium);
                verifyTrue(selenium.isElementPresent("css=div.common-widget-top-bar-text"));
                verifyTrue(selenium.isElementPresent("css=#myresearch > div.common-widget-top-bar > div.common-widget-top-bar-text"));
                verifyTrue(selenium.isElementPresent("css=#browsehistory > div.common-widget-top-bar > div.common-widget-top-bar-text > span.common-widget-top-bar-text-right"));
        //verifyTrue(selenium.isElementPresent("id=Image1"));
                verifyEquals("Look n Feel", selenium.getText("//ul[@id='sideNav']/li[2]/a/span"));
                verifyEquals("Introduction", selenium.getText("css=a.vt > span"));
                verifyTrue(selenium.isElementPresent("css=span.common-widget-top-bar-text-right"));
                selenium.setTimeout("60000");
                selenium.click("id=addToCart");
                Thread.sleep(10000);
                if(selenium.isVisible("id=cboxLoadedContent"))
                {
                        selenium.goBack();
                        Thread.sleep(15000);
                        //selenium.click("link=HOME");
                        continue;
                }
                break;
                }
                Thread.sleep(60);
                verifyEquals("Cart Details", selenium.getText("css=span.label"));
                verifyEquals("Shipping Info.", selenium.getText("//div[@id='cartFlowHeader']/div[2]/span[2]"));
                verifyEquals("Make Payment", selenium.getText("//div[@id='cartFlowHeader']/div[3]/span[2]"));
                verifyEquals("Order Status", selenium.getText("//div[@id='cartFlowHeader']/div[4]/span[2]"));
                verifyTrue(selenium.isElementPresent("id=zipcode"));
                verifyTrue(selenium.isElementPresent("id=computeEstimate"));
                verifyEquals("Total Amount:", selenium.getText("css=tr.total-amount.payable > td.label"));
                Thread.sleep(6000);
                verifyTrue(selenium.isElementPresent("xpath=//div[@class='cartflow-main-action-button right checkout-button-below']"));
                selenium.click("xpath=//div[@class='cartflow-main-action-button right checkout-button-below']");
                Thread.sleep(6000);
                selenium.type("id=loginEmail", "kshitijsood@ymail.com");
                selenium.type("id=loginPassword", "123abc");
                selenium.click("id=submitAddress");
                selenium.waitForPageToLoad("30000");
                verifyEquals("Shipping Info.", selenium.getText("//div[@id='cartFlowHeader']/div/span[2]"));
                verifyEquals("Item & Color", selenium.getText("css=th.first"));
                verifyEquals("Delivery Time", selenium.getText("//table[@id='cartDetail']/thead/tr/th[2]"));
                verifyEquals("Total Price", selenium.getText("//table[@id='cartDetail']/thead/tr/th[3]"));
                verifyTrue(selenium.isTextPresent("Select Shipping Address"));
                Thread.sleep(5000);
                com.unitedinternet.portal.selenium.utils.logging.LoggingAssert.assertEquals("Proceed to payment button not present","PROCEED TO PAYMENT >", selenium.getText("id=proceedToPay"),selenium);
                selenium.click("id=proceedToPay");
                selenium.waitForPageToLoad("30000");
                Thread.sleep(5000);
                assertEquals("Make Payment", selenium.getText("//div[@id='cartFlowHeader']/div/span[2]"));
                assertEquals("Order Amount:", selenium.getText("css=h4 > span"));
                String htmlstr = selenium.getBodyText();
        //System.out.println("Index is " + pos);
        int pos = htmlstr.indexOf("Order Amount:");
        //System.out.println("Index is " + pos);
        int pos1=pos+16;
        int pos2=pos+28;
        String amount;
        amount = htmlstr.substring(pos1, pos2);
        //System.out.println("pos1 pos2" + pos1 + ":" + pos2);
        //int price=Integer.parseInt(amount);
        //System.out.println("Amount : :" + amount + ":" + price );
        amount=amount.replaceAll(",", "");
        System.out.println("Amount : :" + amount);
        ///double price = Integer.parseInt(amount);
        double price = Double.parseDouble(amount);
        System.out.println("Price : :" + price);
        verifyTrue(selenium.isTextPresent("CREDIT CARDS"));
                verifyTrue(selenium.isTextPresent("EMI"));
                verifyTrue(selenium.isTextPresent("DEBIT/ATM CARDS"));
                verifyTrue(selenium.isTextPresent("NET BANKING"));
                verifyTrue(selenium.isTextPresent("CASH CARDS"));
                assertTrue(selenium.isElementPresent("css=input.make-payment-button"));
                if((price > 300) && (price < 20000))
                {
                verifyTrue(selenium.isTextPresent("CASH ON DELIVERY")); 
                selenium.click("//div[@id='vtab']/ul/li[6]");
                verifyTrue(selenium.isElementPresent("id=captcha_response_box"));
                assertTrue(selenium.isElementPresent("id=captchaImage"));
                assertTrue(selenium.isElementPresent("css=div.text-center > input.make-payment-button"));
                }
                selenium.deleteAllVisibleCookies();
        }
        @After
        public void tearDown() throws Exception {
                selenium.stop();
        //stopSeleniumServer();
                try {
                if (null != loggingWriter) {
                    loggingWriter.close();
                }
            } catch (IOException e) {
              System.out.println("IO exception");
            }
        }
}