Advertisement
smaction

Test Suite So Far

Dec 14th, 2011
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 17.38 KB | None | 0 0
  1. package com.skgcom.unitTests;
  2.  
  3. import static org.testng.Assert.*;
  4.  
  5. import java.io.BufferedReader;
  6. import java.io.IOException;
  7. import java.io.InputStreamReader;
  8. import java.io.OutputStreamWriter;
  9. import java.net.URL;
  10. import java.net.URLConnection;
  11. import java.util.concurrent.TimeUnit;
  12.  
  13. import org.openqa.selenium.By;
  14. import org.openqa.selenium.Dimension;
  15. import org.openqa.selenium.JavascriptExecutor;
  16. import org.openqa.selenium.NoSuchElementException;
  17. import org.openqa.selenium.WebDriver;
  18. import org.openqa.selenium.WebElement;
  19.  
  20. import org.openqa.selenium.remote.DesiredCapabilities;
  21. import org.openqa.selenium.remote.RemoteWebDriver;
  22. import org.openqa.selenium.support.ui.ExpectedCondition;
  23. import org.openqa.selenium.support.ui.Select;
  24. import org.openqa.selenium.support.ui.WebDriverWait;
  25.  
  26. import org.testng.annotations.Test;
  27.  
  28. public class roomTest {
  29.     static String[][] post_vars;
  30.     private static int post_var_count;
  31.     private static WebDriver driver;
  32.     //String agent = "FF";
  33.     String agent = System.getProperty("agent").trim();
  34.     private static StringBuffer verificationErrors = new StringBuffer();
  35.  
  36.     @Test(groups = "unit")
  37.     public void resTest() throws IOException, InterruptedException {
  38.         post_vars = new String[255][255];
  39.  
  40.         String baseUrl = System.getProperty("url");
  41.         String testSite = System.getProperty("site");
  42.         //String agent = System.getProperty("agent");
  43.        
  44.         if (agent.equals("IE")) {
  45.             //System.out.println(agent);
  46.            
  47.             driver = new RemoteWebDriver(
  48.                     new URL("http://localhost:4444/wd/hub"),
  49.                     DesiredCapabilities.internetExplorer());
  50.  
  51.         }
  52.         if (agent.equals("FF")) {
  53.  
  54.             driver = new RemoteWebDriver(
  55.                     new URL("http://localhost:4444/wd/hub"),
  56.                     DesiredCapabilities.firefox());
  57.         }
  58.        
  59.         driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
  60.        
  61.         addMessage("Site", testSite);
  62.         addMessage("URL", baseUrl);
  63.         addMessage("Room", "started...");
  64.         http_post();
  65.         post_vars[2][0] = null;
  66.         post_vars[2][1] = null;
  67.         driver.get(baseUrl);
  68.         waitForElementVisible(driver, By.id("close_welcome_box")).click();
  69.         driver.manage().window().setSize(new Dimension(1024, 762));
  70.  
  71.         // waitForElementVisible(driver, By.id("dashTabOverlay")).click();
  72.         while ((Long) ((JavascriptExecutor) driver)
  73.                 .executeScript("return animationQueue;") == 0) {
  74.             ((JavascriptExecutor) driver)
  75.                     .executeScript("window.dashboardApp.behaviors.toggle();");
  76.         }
  77.  
  78.         waitForElementVisible(driver, By.id("obRoom")).click();
  79.         if (isElementPresent(By.xpath("//form[@id='roomsStep1']/div[7]"))) {
  80.             driver.findElement(By.xpath("//form[@id='roomsStep1']/div[7]"))
  81.                     .click();
  82.         }
  83.  
  84.         if (isElementPresent(By
  85.                 .cssSelector("button.button_large_white.dashboard_button_padding"))) {
  86.             driver.findElement(
  87.                     By.cssSelector("button.button_large_white.dashboard_button_padding"))
  88.                     .click();
  89.         }
  90.  
  91.         waitForElementVisible(driver, By.name("name0")).click();
  92.         if (isElementPresent(By
  93.                 .linkText("No thank you, continue with original selection"))) {
  94.             driver.findElement(
  95.                     By.linkText("No thank you, continue with original selection"))
  96.                     .click();
  97.         }
  98.  
  99.         Thread.sleep(10000);
  100.         if (isElementPresent(By.id("firstname"))) {
  101.         } else {
  102.             ((JavascriptExecutor) driver)
  103.                     .executeScript("(function(self) { echo('click');"
  104.                             + "var data = 'cart=' + $(self).data('cart') + '&checkin=' + $(self).data('checkin')"
  105.                             + "+ '&checkout=' + $(self).data('checkout') + '&adults=' + $(self).data('adults');"
  106.                             + "var $form = $('form#roomsStep2'); dashboardApp.openPanel('/dashboard/cart?type=room&template=cart&' + data,'',1,'single', function(){"
  107.                             + "dashboardApp.openPanel('/dashboard/rooms/billing',$form.serialize(),2,'triple',window.loadTotals);"
  108.                             + "});isBooking = true;$(self).unmodal(); })($('.skipupgrade'));");
  109.             Thread.sleep(15000);
  110.         }
  111.         // ((JavascriptExecutor)driver).executeScript(" $('.selectRoomOption').click();");
  112.         // waitForElementVisible(driver, By.id("acceptUpsell")).click();
  113.  
  114.         driver.findElement(By.id("firstname")).clear();
  115.         driver.findElement(By.id("firstname")).sendKeys("Scott");
  116.         driver.findElement(By.id("lastname")).clear();
  117.         driver.findElement(By.id("lastname")).sendKeys("Miller");
  118.         driver.findElement(By.id("email1")).clear();
  119.         driver.findElement(By.id("email1")).sendKeys("scott.miller@skgadv.com");
  120.         driver.findElement(By.id("email2")).clear();
  121.         driver.findElement(By.id("email2")).sendKeys("scott.miller@skgadv.com");
  122.         driver.findElement(By.id("phone")).clear();
  123.         driver.findElement(By.id("phone")).sendKeys("7024600705");
  124.         driver.findElement(By.id("address")).clear();
  125.         driver.findElement(By.id("address")).sendKeys("3491 Sego Glen");
  126.         driver.findElement(By.id("city")).clear();
  127.         driver.findElement(By.id("city")).sendKeys("Las Vegas");
  128.         driver.findElement(By.id("state")).clear();
  129.         driver.findElement(By.id("state")).sendKeys("NV");
  130.         driver.findElement(By.id("payZip")).clear();
  131.         driver.findElement(By.id("payZip")).sendKeys("89121");
  132.         driver.findElement(By.id("creditcardfield")).clear();
  133.         driver.findElement(By.id("creditcardfield")).sendKeys(
  134.                 "4111111111111111");
  135.         //driver.findElement(By.name("LRESYC")).click();
  136.         driver.findElement(By.name("LRESYC")).sendKeys("22");
  137.         // ERROR: Caught exception [ERROR: Unsupported command [select]]
  138.         /*if (isElementPresent(By.name("LRESYC"))) {
  139.             Select select = new Select(driver.findElement(By.name("LRESYC")));
  140.             select.selectByVisibleText("2015");
  141.         }*/
  142.         driver.findElement(By.id("tccheck")).click();
  143.         if (isElementPresent(By
  144.                 .cssSelector("div.submitReservation.simpleSprite"))) {
  145.             driver.findElement(
  146.                     By.cssSelector("div.submitReservation.simpleSprite"))
  147.                     .click();
  148.         }
  149.         if (isElementPresent(By.id("roomSubmitBtn"))) {
  150.             // System.out.format("I am executing the right code.");
  151.             driver.findElement(By.id("roomSubmitBtn")).click();
  152.         }
  153.         Thread.sleep(30000);
  154.         if (isElementPresent(By.xpath("//*[contains(.,'" + "Congratulations!"
  155.                 + "')]"))) {
  156.             addMessage("Room", "success");
  157.         } else {
  158.             if (isElementPresent(By.xpath("//*[contains(.,'" + "successful"
  159.                     + "')]"))) {
  160.                 addMessage("Room", "success");
  161.             } else {
  162.                 addMessage("Room", "FAIL");
  163.             }
  164.         }
  165.  
  166.         driver.quit();
  167.         http_post();
  168.         String verificationErrorString = verificationErrors.toString();
  169.         if (!"".equals(verificationErrorString)) {
  170.  
  171.             fail(verificationErrorString);
  172.         }
  173.     }
  174.  
  175.     @Test(groups = "unit")
  176.     public void golfTest() throws IOException, InterruptedException {
  177.         post_vars = new String[255][255];
  178.         String baseUrl = System.getProperty("url");
  179.         String testSite = System.getProperty("site");
  180.     //  String agent = System.getProperty("agent");
  181.         if (agent.equals("FF")) {
  182.  
  183.             driver = new RemoteWebDriver(
  184.                     new URL("http://localhost:4444/wd/hub"),
  185.                     DesiredCapabilities.firefox());
  186.         }
  187.         if (agent.equals("IE")) {
  188.             driver = new RemoteWebDriver(
  189.                     new URL("http://localhost:4444/wd/hub"),
  190.                     DesiredCapabilities.internetExplorer());
  191.         }
  192.  
  193.         driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
  194.  
  195.         // ERROR: Caught exception [ERROR: Unsupported command [setSpeed]]
  196.         addMessage("Site", testSite);
  197.         addMessage("URL", baseUrl);
  198.         addMessage("Golf", "started...");
  199.         http_post();
  200.         post_vars[2][0] = null;
  201.         post_vars[2][1] = null;
  202.         driver.get(baseUrl);
  203.         driver.manage().window().setSize(new Dimension(1024, 762));
  204.         // Thread.sleep(30000);
  205.         // System.out.println(isElementPresent(By.id("close_welcome_box")));
  206.         if (isElementPresent(By.id("close_welcome_box")))
  207.             driver.findElement(By.id("close_welcome_box")).click();
  208.         Thread.sleep(15000);
  209.  
  210.         driver.findElement(By.id("dashTabOverlay")).click();
  211.         // Thread.sleep(30000);
  212.         if (isElementPresent(By.id("obGolf"))) {
  213.             driver.findElement(By.id("obGolf")).click();
  214.         }
  215.  
  216.         // Thread.sleep(30000);
  217.  
  218.         if (isElementPresent(By.id("time"))) {
  219.             Select select = new Select(driver.findElement(By.id("time")));
  220.             select.selectByVisibleText("12:00PM");
  221.         }
  222.         if (isElementPresent(By.id("checkAvailability"))) {
  223.             driver.findElement(By.id("checkAvailability")).click();
  224.         }
  225.  
  226.         // Thread.sleep(30000);
  227.         // if
  228.         // (isElementPresent(By.id("booking_0"))){addMessage("bltgolf","success");}else{addMessage("bltgolf",
  229.         // "FAIL");}
  230.         if (isElementPresent(By.id("booking_0"))) {
  231.             driver.findElement(By.id("booking_0")).click();
  232.         }
  233.         // Thread.sleep(30000);
  234.         driver.findElement(By.id("firstname")).click();
  235.         driver.findElement(By.id("firstname")).clear();
  236.         driver.findElement(By.id("firstname")).sendKeys("Scott");
  237.         driver.findElement(By.id("lastname")).clear();
  238.         driver.findElement(By.id("lastname")).sendKeys("Miller");
  239.         driver.findElement(By.id("email")).clear();
  240.         driver.findElement(By.id("email")).sendKeys("scott.miller@skgadv.com");
  241.         driver.findElement(By.id("confirm_email")).clear();
  242.         driver.findElement(By.id("confirm_email")).sendKeys(
  243.                 "scott.miller@skgadv.com");
  244.         driver.findElement(By.id("phone")).clear();
  245.         driver.findElement(By.id("phone")).sendKeys("7024600705");
  246.         driver.findElement(By.id("address")).clear();
  247.         driver.findElement(By.id("address")).sendKeys("3491 Sego Glen Circ");
  248.         driver.findElement(By.id("city")).clear();
  249.         driver.findElement(By.id("city")).sendKeys("Las Vegas");
  250.         driver.findElement(By.id("state")).clear();
  251.         driver.findElement(By.id("state")).sendKeys("NV");
  252.         driver.findElement(By.id("zip")).clear();
  253.         driver.findElement(By.id("zip")).sendKeys("89121");
  254.         driver.findElement(By.id("antibot_answer")).clear();
  255.         driver.findElement(By.id("antibot_answer")).sendKeys("c@ptch@oVeRride");
  256.         driver.findElement(By.id("submit_reservation")).click();
  257.         // Thread.sleep(30000);
  258.         if (isElementPresent(By.xpath("//*[contains(.,'" + "Congratulations!"
  259.                 + "')]"))) {
  260.             addMessage("Golf", "success");
  261.         } else {
  262.             if (isElementPresent(By.xpath("//*[contains(.,'" + "successful"
  263.                     + "')]"))) {
  264.                 addMessage("Golf", "success");
  265.             } else {
  266.                 addMessage("Golf", "FAIL");
  267.             }
  268.         }
  269.         driver.quit();
  270.         http_post();
  271.         String verificationErrorString = verificationErrors.toString();
  272.         if (!"".equals(verificationErrorString)) {
  273.  
  274.             fail(verificationErrorString);
  275.         }
  276.     }
  277.  
  278.     @Test(groups = "unit")
  279.     public void gaTest() throws IOException, InterruptedException {
  280.         post_vars = new String[255][255];
  281.         String baseUrl = System.getProperty("url");
  282.         String testSite = System.getProperty("site");
  283.     //  String agent = System.getProperty("agent");
  284.         if (agent.equals("FF")) {
  285.  
  286.             driver = new RemoteWebDriver(
  287.                     new URL("http://localhost:4444/wd/hub"),
  288.                     DesiredCapabilities.firefox());
  289.         }
  290.         if (agent.equals("IE")) {
  291.             driver = new RemoteWebDriver(
  292.                     new URL("http://localhost:4444/wd/hub"),
  293.                     DesiredCapabilities.internetExplorer());
  294.         }
  295.  
  296.         driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
  297.  
  298.         // ERROR: Caught exception [ERROR: Unsupported command [setSpeed]]
  299.         addMessage("Site", testSite);
  300.         addMessage("URL", baseUrl);
  301.         addMessage("GA", "started...");
  302.         http_post();
  303.         post_vars[2][0] = null;
  304.         post_vars[2][1] = null;
  305.         driver.get(baseUrl);
  306.         driver.manage().window().setSize(new Dimension(1024, 762));
  307.         // Thread.sleep(30000);
  308.         // System.out.println(isElementPresent(By.id("close_welcome_box")));
  309.  
  310.         if (isElementPresent(By.xpath("//*[contains(.,'"
  311.                 + "google-analytics.com/ga.js" + "')]"))) {
  312.             addMessage("GA", "success");
  313.         } else {
  314.             addMessage("GA", "FAIL");
  315.         }
  316.  
  317.         driver.quit();
  318.         http_post();
  319.         String verificationErrorString = verificationErrors.toString();
  320.         if (!"".equals(verificationErrorString)) {
  321.  
  322.             fail(verificationErrorString);
  323.         }
  324.     }
  325.  
  326.     @Test(groups = "unit")
  327.     public void spaTest() throws IOException, InterruptedException {
  328.         post_vars = new String[255][255];
  329.         String baseUrl = System.getProperty("url");
  330.         String testSite = System.getProperty("site");
  331. //      String agent = System.getProperty("agent");
  332.         if (agent.equals("FF")) {
  333.  
  334.             driver = new RemoteWebDriver(
  335.                     new URL("http://localhost:4444/wd/hub"),
  336.                     DesiredCapabilities.firefox());
  337.         }
  338.         if (agent.equals("IE")) {
  339.             driver = new RemoteWebDriver(
  340.                     new URL("http://localhost:4444/wd/hub"),
  341.                     DesiredCapabilities.internetExplorer());
  342.         }
  343.         driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
  344.  
  345.         // ERROR: Caught exception [ERROR: Unsupported command [setSpeed]]
  346.         addMessage("Site", testSite);
  347.         addMessage("URL", baseUrl);
  348.         addMessage("Spa", "started...");
  349.         http_post();
  350.         post_vars[2][0] = null;
  351.         post_vars[2][1] = null;
  352.         driver.get(baseUrl);
  353.         driver.manage().window().setSize(new Dimension(1024, 762));
  354.         // Thread.sleep(30000);
  355.         // System.out.println(isElementPresent(By.id("close_welcome_box")));
  356.         if (isElementPresent(By.id("close_welcome_box")))
  357.             driver.findElement(By.id("close_welcome_box")).click();
  358.         Thread.sleep(15000);
  359.  
  360.         driver.findElement(By.id("dashTabOverlay")).click();
  361.         // Thread.sleep(30000);
  362.         if (isElementPresent(By.id("obSpa"))) {
  363.             driver.findElement(By.id("obSpa")).click();
  364.         }
  365.  
  366.         // Thread.sleep(30000);
  367.         // if
  368.         // (isElementPresent(By.xpath(".//*[@id='category']/option[6]"))){addMessage("bltspa","success");}else{addMessage("bltspa",
  369.         // "FAIL");}
  370.         // ERROR: Caught exception [ERROR: Unsupported command [select]]
  371.         if (isElementPresent(By.id("category")))
  372.             driver.findElement(By.id("category")).click();
  373.         driver.findElement(By.id("category")).sendKeys("h");
  374.         driver.findElement(By.id("num_of_people")).click();
  375.         // driver.findElement(By.id("time")).click();
  376.         Thread.sleep(15000);
  377.         // ERROR: Caught exception [ERROR: Unsupported command [select]]
  378.         if (isElementPresent(By.id("subcategory")))
  379.             driver.findElement(By.id("subcategory")).click();
  380.         driver.findElement(By.id("subcategory")).sendKeys("m");
  381.         driver.findElement(By.id("num_of_people")).click();
  382.         // driver.findElement(By.id("time")).click();
  383.         Thread.sleep(15000);
  384.         // ERROR: Caught exception [ERROR: Unsupported command [select]]
  385.         if (isElementPresent(By.id("time"))) {
  386.             Select select = new Select(driver.findElement(By.id("time")));
  387.             select.selectByVisibleText("9am - 12pm");
  388.         }
  389.         driver.findElement(By.id("createSpaReservation")).click();
  390.         // Thread.sleep(30000);
  391.         driver.findElement(By.id("spa_firstname")).click();
  392.         driver.findElement(By.id("spa_firstname")).clear();
  393.         driver.findElement(By.id("spa_firstname")).sendKeys("Scott");
  394.         driver.findElement(By.id("spa_lastname")).clear();
  395.         driver.findElement(By.id("spa_lastname")).sendKeys("Miller");
  396.         driver.findElement(By.id("spa_email")).clear();
  397.         driver.findElement(By.id("spa_email")).sendKeys(
  398.                 "scott.miller@skgadv.com");
  399.         driver.findElement(By.id("spa_confirm_email")).clear();
  400.         driver.findElement(By.id("spa_confirm_email")).sendKeys(
  401.                 "scott.miller@skgadv.com");
  402.         driver.findElement(By.id("spa_phone")).clear();
  403.         driver.findElement(By.id("spa_phone")).sendKeys("7024600705");
  404.         driver.findElement(By.id("spa_address")).clear();
  405.         driver.findElement(By.id("spa_address")).sendKeys("3491 Sego Glen");
  406.         driver.findElement(By.id("spa_city")).clear();
  407.         driver.findElement(By.id("spa_city")).sendKeys("Las Vegas");
  408.         driver.findElement(By.id("spa_state")).clear();
  409.         driver.findElement(By.id("spa_state")).sendKeys("NV");
  410.         driver.findElement(By.id("spa_zip")).clear();
  411.         driver.findElement(By.id("spa_zip")).sendKeys("89121");
  412.         driver.findElement(By.id("spa_comments")).clear();
  413.         driver.findElement(By.id("spa_comments")).sendKeys("test");
  414.         driver.findElement(By.id("antibot_answer")).clear();
  415.         driver.findElement(By.id("antibot_answer")).sendKeys("c@ptch@oVeRride");
  416.         driver.findElement(By.id("submit_spa_request")).click();
  417.  
  418.         // Thread.sleep(30000);
  419.         if (isElementPresent(By.xpath("//*[contains(.,'"
  420.                 + "Thank you for requesting a spa treatment." + "')]"))) {
  421.             addMessage("Spa", "success");
  422.         } else {
  423.             addMessage("Spa", "FAIL");
  424.         }
  425.  
  426.         driver.quit();
  427.         http_post();
  428.         String verificationErrorString = verificationErrors.toString();
  429.         if (!"".equals(verificationErrorString)) {
  430.  
  431.             fail(verificationErrorString);
  432.         }
  433.     }
  434.  
  435.     private static boolean isElementPresent(By by) {
  436.         try {
  437.             driver.findElement(by);
  438.             return true;
  439.         } catch (NoSuchElementException e) {
  440.             return false;
  441.         }
  442.     }
  443.  
  444.     public static void http_post() throws IOException {
  445.  
  446.         // Construct data
  447.         String data = "";
  448.         for (int n = 0; n <= post_vars.length - 1; n++) {
  449.             if (post_vars[n][0] != null) {
  450.                 data += post_vars[n][0] + "=" + post_vars[n][1] + "&";
  451.             }
  452.         }
  453.         // Send data
  454.         URL url = new URL("http://logs.skgcom.com/UnitTest.php");
  455.         ;
  456.         URLConnection conn = url.openConnection();
  457.         conn.setDoOutput(true);
  458.         OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
  459.         wr.write(data);
  460.         wr.flush();
  461.  
  462.         // Get the response
  463.         BufferedReader rd = new BufferedReader(new InputStreamReader(
  464.                 conn.getInputStream()));
  465.         String line;
  466.         while ((line = rd.readLine()) != null) {
  467.             System.out.println(line);
  468.         }
  469.         wr.close();
  470.         rd.close();
  471.  
  472.     }
  473.  
  474.     public static void addMessage(String key, String value) {
  475.  
  476.         post_vars[post_var_count][0] = key;
  477.         post_vars[post_var_count][1] = value;
  478.         post_var_count++;
  479.     }
  480.  
  481.     private static WebElement waitForElementVisible(WebDriver driver,
  482.             final By by) {
  483.         return (new WebDriverWait(driver, 60))
  484.                 .until(new ExpectedCondition<WebElement>() {
  485.                     @Override
  486.                     public WebElement apply(WebDriver d) {
  487.                         WebElement el = d.findElement(by);
  488.                         if (el != null && el.isDisplayed()) {
  489.                             return el;
  490.                         }
  491.                         return null;
  492.                     }
  493.                 });
  494.     }
  495. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement