PageRenderTime 29ms CodeModel.GetById 17ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/sitebricks-acceptance-tests/src/test/java/com/google/sitebricks/acceptance/page/ConversionPage.java

http://github.com/dhanji/sitebricks
Java | 95 lines | 76 code | 19 blank | 0 comment | 12 complexity | c6ba22eadd491da48f0701cc0487a633 MD5 | raw file
 1package com.google.sitebricks.acceptance.page;
 2
 3
 4import java.net.URLEncoder;
 5import java.text.SimpleDateFormat;
 6import java.util.Calendar;
 7import java.util.Date;
 8
 9import org.openqa.selenium.By;
10import org.openqa.selenium.WebDriver;
11import org.openqa.selenium.support.PageFactory;
12
13import com.google.sitebricks.acceptance.util.AcceptanceTest;
14import com.google.sitebricks.example.SitebricksConfig;
15
16public class ConversionPage {
17	private WebDriver driver;
18
19	public ConversionPage(WebDriver driver) {
20		this.driver = driver;
21	}
22	
23	public boolean hasDate(Date date)	{
24		SimpleDateFormat sdf = new SimpleDateFormat(SitebricksConfig.DEFAULT_DATE_TIME_FORMAT);
25		String target = sdf.format(date);
26
27	    return driver.findElement(By.id("boundDate"))
28	        .getText()
29	        .contains(target);
30	}
31
32	public boolean hasCalendar(Calendar calendar)	{
33		SimpleDateFormat sdf = new SimpleDateFormat(SitebricksConfig.DEFAULT_DATE_TIME_FORMAT);
34		String target = sdf.format(calendar.getTime());
35
36		String node = driver.getPageSource();
37	    return  driver.findElement(By.id("boundCalendar"))
38	    	.getText()
39	        .contains(target);
40	}
41
42	public boolean hasMessage(String message)	{
43	    return driver.findElement(By.id("boundText"))
44	        .getText()
45	        .contains(message);
46	}
47
48	public boolean hasDouble(Double dbl)	{
49	    return driver.findElement(By.id("boundDouble"))
50	        .getText()
51	        .contains(dbl.toString());
52	}
53	
54	public static ConversionPage open(WebDriver driver, Date date, Calendar calendar, String dateFormat, String msg, Double dbl) {
55		SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
56		StringBuilder sb = new StringBuilder ();
57		
58		if (date != null)	{
59			if (sb.length() > 0)
60				sb.append("&");
61			sb.append ("date=").append(encode(sdf.format(date)));
62		}
63		
64		if (calendar != null)	{
65			if (sb.length() > 0)
66				sb.append("&");
67			sb.append ("calendar=").append(encode(sdf.format(calendar.getTime())));
68		}
69		
70		if (msg != null)	{
71			if (sb.length() > 0)
72				sb.append("&");
73			sb.append ("message=").append(encode(msg));
74		}
75
76		if (msg != null)	{
77			if (sb.length() > 0)
78				sb.append("&");
79			sb.append ("dbl=").append(encode(dbl.toString()));
80		}
81			
82		sb.insert(0,"/conversion?").insert(0, AcceptanceTest.baseUrl());
83		driver.get(sb.toString());
84		return PageFactory.initElements(driver, ConversionPage.class);
85	}
86
87	private static String encode(String s){
88		try	{
89			return URLEncoder.encode(s,"UTF-8");
90		}
91		catch(Exception e){
92			return s;
93		}
94	}
95}