Advertisement
smaction

First Testng

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