forked from sergueik/selenium_java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.java
More file actions
201 lines (164 loc) · 7.76 KB
/
Copy pathApp.java
File metadata and controls
201 lines (164 loc) · 7.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package com.mycompany.app;
import java.io.File;
import java.io.IOException;
import java.io.File;
import java.io.InputStream;
import java.io.StringWriter;
import java.lang.StringBuilder;
import java.util.concurrent.TimeUnit;
import java.util.NoSuchElementException;
import java.util.Set;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.OutputType;
import org.apache.commons.io.FilenameUtils;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.openqa.selenium.interactions.Actions;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Platform;
import org.apache.http.message.BasicHttpEntityEnclosingRequest;
import org.openqa.selenium.interactions.Actions;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.HttpCommandExecutor;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
// import org.openqa.selenium.firefox.ProfileManager;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.BindException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.Charset;
import org.junit.Assert;
import static org.junit.Assert.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.firefox.internal.ProfilesIni;
// https://eveningsamurai.wordpress.com/2013/06/29/capturing-network-traffic-using-selenium-webdriverfirebug/
public class App {
static WebDriver driver;
public static void main(String[] args) throws InterruptedException,java.io.IOException {
System.out.println(System.getProperty("user.dir"));
// http://getfirebug.com/releases/firebug/2.0/firebug-2.0.8.xpi
// http://getfirebug.com/releases/firebug/1.10/firebug-1.10.6.xpi
File firebug = new File(System.getProperty("user.dir") + "\\resources\\firebug-3.0.0-beta.3.xpi");
// http://getfirebug.com/releases/netexport/netExport-0.9b7.xpi
File netExport = new File(System.getProperty("user.dir") + "\\resources\\netExport-0.9b7.xpi");
FirefoxProfile profile = new FirefoxProfile();
try {
profile.addExtension(firebug);
profile.addExtension(netExport);
} catch (IOException e) {
e.printStackTrace();
}
profile.setPreference("app.update.enabled", false);
//Setting Firebug preferences
profile.setPreference("extensions.firebug.currentVersion", "2.0");
profile.setPreference("extensions.firebug.addonBarOpened", true);
profile.setPreference("extensions.firebug.console.enableSites", true);
profile.setPreference("extensions.firebug.script.enableSites", true);
profile.setPreference("extensions.firebug.net.enableSites", true);
profile.setPreference("extensions.firebug.previousPlacement", 1);
profile.setPreference("extensions.firebug.allPagesActivation", "on");
profile.setPreference("extensions.firebug.onByDefault", true);
profile.setPreference("extensions.firebug.defaultPanelName", "net");
// Setting netExport preferences
profile.setPreference("extensions.firebug.netexport.alwaysEnableAutoExport", true);
profile.setPreference("extensions.firebug.netexport.autoExportToFile", true);
profile.setPreference("extensions.firebug.netexport.Automation", true);
profile.setPreference("extensions.firebug.netexport.showPreview", false);
profile.setPreference("extensions.firebug.netexport.defaultLogDir", "C:\\developer\\sergueik\\powershell_ui_samples\\external\\java\\capture");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setBrowserName("firefox");
capabilities.setPlatform(org.openqa.selenium.Platform.ANY);
capabilities.setCapability(FirefoxDriver.PROFILE, profile);
driver = new RemoteWebDriver(capabilities);
navigate();
driver.quit();
}
public static void navigate () throws InterruptedException,java.io.IOException {
// Wait For Page To Load
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Go to URL
driver.get("http://www.hollandamerica.com/");
// Maximize Window
driver.manage().window().maximize();
// Wait For Page To Load
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
String value0 = "pnav-destinations";
String title0 = "Destinations & Excursions - opens submenu";
String css_selector0 = String.format("a#%s", value0);
// Hover over menu
WebElement element0 = driver.findElement(By.cssSelector(css_selector0));
Actions a1 = new Actions(driver);
a1.moveToElement(element0).build().perform();
// http://junit.sourceforge.net/javadoc/org/junit/Assert.html
// assertEquals(200, response.getStatusLine().getStatusCode());
assertTrue(String.format("Unexpected title '%s'", element0.getAttribute("title")), element0.getAttribute("title").matches(title0) );
/*
String value1 = "/cruise-destinations/alaska?WT.ac=pnav_DestMap_Alaska";
String text1 = "Alaska & Yukon";
String title1 = "Alaska Cruise Vacations";
String css_selector1 = String.format("a[href='%s']", value1);
WebDriverWait wait = new WebDriverWait(driver, 1);
wait.withTimeout(1, TimeUnit.SECONDS).pollingEvery(150, TimeUnit.MICROSECONDS );
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(css_selector1)));
WebElement element1 = driver.findElement(By.cssSelector(css_selector1));
assertTrue(String.format("Unexpected text '%s'" , element1.getText()), element1.getText().matches(text1) );
System.out.println(String.format("Click on '%s'", element1.getText()));
new Actions(driver).moveToElement(element1).click().build().perform();
Thread.sleep(60000L);
assertTrue(driver.getTitle(),driver.getTitle().contains(title1));
driver.navigate().back();
*/
//take a screenshot
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
String currentDir = System.getProperty("user.dir");
//save the screenshot in png format on the disk.
FileUtils.copyFile(scrFile, new File(FilenameUtils.concat(currentDir, "screenshot.png")));
//closing current driver window
driver.close();
}
// http://selenium-interview-questions.blogspot.in/2014/02/how-to-take-screenshot-of-webpage-using.html
}