1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package edu.samplu.common;
18
19 import org.junit.After;
20 import org.junit.Before;
21 import org.openqa.selenium.By;
22 import org.openqa.selenium.WebDriver;
23 import org.openqa.selenium.firefox.FirefoxDriver;
24 import org.openqa.selenium.firefox.FirefoxProfile;
25
26 import java.util.concurrent.TimeUnit;
27
28 import static org.junit.Assert.assertEquals;
29
30
31
32
33
34
35 public abstract class WebDriverITBase {
36
37 int DEFAULT_IMPLICIT_WAIT_TIME = 30;
38 int SHORT_IMPLICIT_WAIT_TIME = 1;
39
40 public WebDriver driver;
41
42
43
44
45
46
47 public abstract String getTestUrl();
48
49
50
51
52
53
54 @Before
55 public void setUp() throws Exception {
56 FirefoxProfile profile = new FirefoxProfile();
57 profile.setEnableNativeEvents(false);
58 driver = new FirefoxDriver(profile);
59 driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
60
61
62 driver.get(getTestUrl());
63 driver.findElement(By.name("__login_user")).clear();
64 driver.findElement(By.name("__login_user")).sendKeys("admin");
65 driver.findElement(By.cssSelector("input[type=\"submit\"]")).click();
66 }
67
68
69
70
71
72
73 @After
74 public void tearDown() throws Exception {
75 driver.quit();
76 }
77
78
79
80
81
82
83
84
85
86
87
88 public boolean isElementPresent(By by) {
89 if (driver.findElements(by).isEmpty()) {
90 return false;
91 } else {
92 return true;
93 }
94 }
95
96
97
98
99
100
101
102
103
104
105
106
107 public boolean isElementPresentQuick(By by) {
108 driver.manage().timeouts().implicitlyWait(SHORT_IMPLICIT_WAIT_TIME, TimeUnit.SECONDS);
109 boolean result = isElementPresent(by);
110 driver.manage().timeouts().implicitlyWait(DEFAULT_IMPLICIT_WAIT_TIME, TimeUnit.SECONDS);
111 return result;
112 }
113
114
115
116
117
118
119
120
121 public void assertPopUpWindowUrl(By by, String windowName, String url) {
122 driver.findElement(by).click();
123 String parentWindowHandle = driver.getWindowHandle();
124
125 driver.switchTo().window(windowName).findElements(By.tagName("head"));
126 assertEquals(url, driver.getCurrentUrl());
127 driver.switchTo().window(parentWindowHandle);
128 }
129
130 }