Advertisement
smaction

Basic Test

Nov 29th, 2011
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.14 KB | None | 0 0
  1. package com.skgcom.tutorial;
  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.NoSuchElementException;
  15. import org.openqa.selenium.WebDriver;
  16. import org.openqa.selenium.firefox.FirefoxDriver;
  17. //import org.openqa.selenium.ie.InternetExplorerDriver;
  18. import org.testng.annotations.Test;
  19.  
  20. public class galdl1Test {
  21.  
  22.     static String[][] post_vars;
  23.     private static int post_var_count;
  24.     private static WebDriver driver;
  25.    
  26.  
  27.     private static StringBuffer verificationErrors = new StringBuffer();
  28.  
  29.     @Test(groups = "unit")
  30.     public void betaTest() throws IOException {
  31.         post_vars = new String[255][255];
  32.         driver = new FirefoxDriver();
  33.         // driver = new InternetExplorerDriver();
  34.         String baseUrl = System.getProperty("url");
  35.     //  String baseUrl = System.getenv("url");
  36.         String testSite = System.getProperty("site");
  37.     //  String testSite = System.getenv("site");
  38.     //  System.out.format("%s=%s%n", "urlvar", System.getenv("url"));
  39.     //  System.out.format("%s=%s%n", "url", baseUrl);
  40.         driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
  41.  
  42.         // ERROR: Caught exception [ERROR: Unsupported command [setSpeed]]
  43.         addMessage("Site", testSite);
  44.         addMessage("URL", baseUrl);
  45.         addMessage("ldlGA", "started...");
  46.         http_post();
  47.         post_vars[2][0] = null;
  48.         post_vars[2][1] = null;
  49.        
  50.         driver.get(baseUrl + "/hotel/");
  51.  
  52.         // Thread.sleep(30000);
  53.         // System.out.println(isElementPresent(By.id("close_welcome_box")));
  54.  
  55.         if (isElementPresent(By.xpath("//*[contains(.,'"
  56.                 + "google-analytics.com/ga.js" + "')]"))) {
  57.             addMessage("ldlGA", "success");
  58.         } else {
  59.             addMessage("ldlGA", "FAIL");
  60.         }
  61.  
  62.         driver.quit();
  63.         http_post();
  64.         String verificationErrorString = verificationErrors.toString();
  65.         if (!"".equals(verificationErrorString)) {
  66.  
  67.             fail(verificationErrorString);
  68.         }
  69.     }
  70.  
  71.     private static boolean isElementPresent(By by) {
  72.         try {
  73.             driver.findElement(by);
  74.             return true;
  75.         } catch (NoSuchElementException e) {
  76.             return false;
  77.         }
  78.     }
  79.  
  80.     public static void http_post() throws IOException {
  81.  
  82.         // Construct data
  83.         String data = "";
  84.         for (int n = 0; n <= post_vars.length - 1; n++) {
  85.             if (post_vars[n][0] != null) {
  86.                 data += post_vars[n][0] + "=" + post_vars[n][1] + "&";
  87.             }
  88.         }
  89.         // Send data
  90.         URL url = new URL("http://logs.skgcom.com/UnitTest.php");
  91.         URLConnection conn = url.openConnection();
  92.         conn.setDoOutput(true);
  93.         OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
  94.         wr.write(data);
  95.         wr.flush();
  96.  
  97.         // Get the response
  98.         BufferedReader rd = new BufferedReader(new InputStreamReader(
  99.                 conn.getInputStream()));
  100.         String line;
  101.         while ((line = rd.readLine()) != null) {
  102.             System.out.println(line);
  103.         }
  104.         wr.close();
  105.         rd.close();
  106.  
  107.     }
  108.  
  109.     public static void addMessage(String key, String value) {
  110.  
  111.         post_vars[post_var_count][0] = key;
  112.         post_vars[post_var_count][1] = value;
  113.         post_var_count++;
  114.     }
  115. }
  116.  
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement