How to use Interface PrintsPage class of org.openqa.selenium package

Best Selenium code snippet using org.openqa.selenium.Interface PrintsPage

Run Selenium automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

copy
1package org.openqa.selenium.remote;
2
3import org.openqa.selenium.HasCapabilities;
4import org.openqa.selenium.JavascriptExecutor;
5import org.openqa.selenium.PrintsPage;
6import org.openqa.selenium.TakesScreenshot;
7import org.openqa.selenium.WebDriver;
8import org.openqa.selenium.interactions.HasInputDevices;
9import org.openqa.selenium.interactions.Interactive;
10import org.openqa.selenium.virtualauthenticator.HasVirtualAuthenticator;
11
12public interface IsRemoteWebDriver extends WebDriver,
13  JavascriptExecutor,
14  HasInputDevices,
15  HasCapabilities,
16  Interactive,
17  TakesScreenshot,
18  HasVirtualAuthenticator,
19  PrintsPage {
20
21  public SessionId getSessionId();
22
23}
24
Full Screen
copy
1package br.com.ayrton.first_selenium_app.browser;
2
3import java.io.File;
4import java.io.IOException;
5import java.nio.file.Files;
6import java.nio.file.Path;
7import java.nio.file.Paths;
8import java.nio.file.StandardOpenOption;
9import java.time.Duration;
10import java.util.Base64;
11import java.util.Iterator;
12
13import org.apache.commons.io.FileUtils;
14import org.openqa.selenium.Alert;
15import org.openqa.selenium.By;
16import org.openqa.selenium.Dimension;
17import org.openqa.selenium.JavascriptExecutor;
18import org.openqa.selenium.OutputType;
19import org.openqa.selenium.Pdf;
20import org.openqa.selenium.Point;
21import org.openqa.selenium.PrintsPage;
22import org.openqa.selenium.TakesScreenshot;
23import org.openqa.selenium.WebDriver;
24import org.openqa.selenium.WebElement;
25import org.openqa.selenium.WindowType;
26import org.openqa.selenium.chrome.ChromeDriver;
27import org.openqa.selenium.chrome.ChromeOptions;
28import org.openqa.selenium.print.PrintOptions;
29import org.openqa.selenium.support.ui.ExpectedConditions;
30import org.openqa.selenium.support.ui.WebDriverWait;
31
32import br.com.ayrton.first_selenium_app.app.Resources;
33import io.github.bonigarcia.wdm.WebDriverManager;
34
35public class Windows {
36
37	private final String RESOURCES_PATH = Paths.get(Resources.PATH, "browser").toString(); 
38	private final String SCRIP_EXAMPLE_HTML = Paths.get(this.RESOURCES_PATH, "windows-script-examples.html").toString();
39	private final String PRINT_EXAMPLE_PDF = Paths.get(this.RESOURCES_PATH, "printPageContent.pdf").toString();
40	private final String SCREENSHOT_EXAMPLE = Paths.get(this.RESOURCES_PATH, "imageBrowser.png").toString();
41	private final String ELEMENT_SCREENSHOT_EXAMPLE = Paths.get(this.RESOURCES_PATH, "imageElement.png").toString();
42	
43	public void getWindowHandler() {
44		WebDriverManager.chromedriver().setup();
45		ChromeOptions options = new ChromeOptions();
46		WebDriver driver = new ChromeDriver(options);
47		try {
48			System.out.println(driver.getWindowHandle());
49		} finally {
50			driver.quit();
51		}
52	}
53	
54	public void switchWindowsOrTabs() {
55		WebDriver driver = new ChromeDriver();
56		try {
57			driver.get("https://www.selenium.dev/documentation/webdriver/browser/windows/");
58			driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));
59			
60			//Store the ID of the original window
61			String originalWindow = driver.getWindowHandle();
62			
63			//Check we don't have other windows open already
64			assert driver.getWindowHandles().size() == 1;
65			
66			//Click the link which opens in a new window
67			//driver.findElement(By.linkText("new window")).click();
68			driver.findElement(By.linkText("new window")).click();
69			
70			//Wait for the new window or tab
71			WebDriverWait wait = new WebDriverWait(driver, Duration.ofMillis(1500));
72			wait.until(ExpectedConditions.numberOfWindowsToBe(2));
73			
74			//Loop through until we find a new window handle
75			for (String windowHandle : driver.getWindowHandles()) {
76				if (!originalWindow.contentEquals(windowHandle)) {
77					driver.switchTo().window(windowHandle);
78					break;
79				}
80			}
81			
82			//Wait for the new tab to finish loading content
83			//wait.until(ExpectedConditions.titleIs("Selenium documentation"));
84			wait.until(ExpectedConditions.titleIs("Selenium"));
85			
86			driver.switchTo().window(originalWindow);
87		} finally {
88			driver.quit();
89		}
90	}
91	
92	public void createWindowOrTabAndSwitch() {
93		WebDriver driver = new ChromeDriver();
94		try {
95			driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));
96			
97			//Opens a new tab and switches to new tab
98			driver.switchTo().newWindow(WindowType.TAB);
99			driver.get("https://www.selenium.dev/documentation/webdriver/browser/windows/");
100			
101			//Opens a new window and switches to new window
102			driver.switchTo().newWindow(WindowType.WINDOW);
103			driver.get("https://www.selenium.dev/documentation/webdriver/browser/windows/");
104		} finally {
105			driver.quit();
106		}
107	}
108	
109	//When you are finished with a window or tab and it is not the last
110	//window or tab open in your browser, you should close it and switch
111	//back to the window you were using previously.
112	//You must switch back to a valid window handle in order to continue
113	//execution.
114	public void closingAWindowOrTab() {
115		WebDriver driver = new ChromeDriver();
116		try {
117			driver.get("https://www.selenium.dev/documentation/webdriver/browser/windows/");
118			driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));
119			
120			//Store the ID of the original window
121			String originalWindow = driver.getWindowHandle();
122			
123			//Check we don't have other windows open already
124			assert driver.getWindowHandles().size() == 1;
125			
126			//Click the link which opens in a new window
127			//driver.findElement(By.linkText("new window")).click();
128			driver.findElement(By.linkText("new window")).click();
129			
130			//Wait for the new window or tab
131			WebDriverWait wait = new WebDriverWait(driver, Duration.ofMillis(1500));
132			wait.until(ExpectedConditions.numberOfWindowsToBe(2));
133			
134			//Loop through until we find a new window handle
135			for (String windowHandle : driver.getWindowHandles()) {
136				if (!originalWindow.contentEquals(windowHandle)) {
137					driver.switchTo().window(windowHandle);
138					break;
139				}
140			}
141			
142			//Wait for the new tab to finish loading content
143			//wait.until(ExpectedConditions.titleIs("Selenium documentation"));
144			wait.until(ExpectedConditions.titleIs("Selenium"));
145			
146			//Close the tab or window
147			driver.close();
148			
149			//Switch back to the old tab or window
150			driver.switchTo().window(originalWindow);
151			
152			driver.get("https://www.google.com.br/");
153		} finally {
154			driver.quit();
155		}
156	}
157	
158	//driver.quit();
159	//Quit will:
160	//1. Close all the windows and tabs associated with that WebDriver session
161	//2. Close the browser process
162	//3. Close the background driver process
163	//4. Notify Selenium Grid that the browser is no longer in use so it can be
164	//used by another session (if you are using Selenium Grid)
165	//
166	//Some test framework offer methods and annotations which you can hook into
167	//to tear down at the end of a test.
168	//@AfterAll
169	//public static void tearDown(){
170	//	driver.quit();
171	//}
172	//
173	//If not runnig WebDriver in a test context, you may consider using try/finally
174	//which is offered by most languages so that an exception will still clean up the
175	//WebDriver session.
176	//try {
177	//	//WebDriver code here...
178	//} finally {
179	//	driver.quit();
180	//}
181	public void quitingTheBrowserAtEndOfSession() {
182		WebDriver driver = new ChromeDriver();
183		try {
184			driver.get("https://www.selenium.dev/documentation/webdriver/browser/windows/");
185			driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));
186		} finally {
187			driver.quit();
188		}
189	}
190	
191	/* Window management */
192	public void getWindowSize() {
193		WebDriver driver = new ChromeDriver();
194		try {
195			//Access each dimension individually
196			int width = driver.manage().window().getSize().getWidth();
197			int height = driver.manage().window().getSize().getHeight();
198			
199			//Or store the dimensions and query them later
200			Dimension size = driver.manage().window().getSize();
201			int width1 = size.getWidth();
202			int height1 = size.getHeight();
203			
204			System.out.println(width);
205			System.out.println(height);
206			System.out.println(width1);
207			System.out.println(height1);
208		} finally {
209			driver.quit();
210		}
211	}
212	
213	public void setWindowSize() {
214		WebDriver driver = new ChromeDriver();
215		try {
216			driver.get("https://www.selenium.dev/documentation/webdriver/browser/windows/");
217			driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));
218			driver.manage().window().setSize(new Dimension(1300, 500));
219			driver.get("https://www.selenium.dev/documentation/webdriver/browser/windows/");
220		} finally {
221			driver.quit();
222		}
223	}
224	
225	public void getWindowPosition() {
226		WebDriver driver = new ChromeDriver();
227		try {
228			//Access each dimension individually
229			int x = driver.manage().window().getPosition().getX();
230			int y = driver.manage().window().getPosition().getY();
231			
232			//Or store the dimensions and query them later
233			Point position = driver.manage().window().getPosition();
234			int x1 = position.getX();
235			int y1 = position.getY();
236			
237			System.out.println(x);
238			System.out.println(y);
239			System.out.println(x1);
240			System.out.println(y1);
241		} finally {
242			driver.quit();
243		}
244	}
245	
246	public void setWindowPosition() {
247		WebDriver driver = new ChromeDriver();
248		try {
249			driver.get("https://www.selenium.dev/documentation/webdriver/browser/windows/");
250			driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));
251			driver.manage().window().setPosition(new Point(0,0));
252			//driver.get("https://www.selenium.dev/documentation/webdriver/browser/windows/");
253			driver.navigate().refresh();
254		} finally {
255			driver.quit();
256		}
257	}
258	
259	public void maximizeWindow() {
260		WebDriver driver = new ChromeDriver();
261		try {
262			driver.get("https://www.selenium.dev/documentation/webdriver/browser/windows/");
263			driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));
264			driver.manage().window().maximize();
265			//driver.get("https://www.selenium.dev/documentation/webdriver/browser/windows/");
266			driver.navigate().refresh();
267		} finally {
268			driver.quit();
269		}
270	}
271	
272	//This feature works with Selenium 4 and later versions.
273	public void minimizeWindow() {
274		WebDriver driver = new ChromeDriver();
275		try {
276			driver.get("https://www.selenium.dev/documentation/webdriver/browser/windows/");
277			driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));
278			driver.manage().window().minimize();
279			//driver.get("https://www.selenium.dev/documentation/webdriver/browser/windows/");
280			driver.navigate().refresh();
281		} finally {
282			driver.quit();
283		}
284	}
285	
286	public void fullscreenWindow() {
287		WebDriver driver = new ChromeDriver();
288		try {
289			driver.get("https://www.selenium.dev/documentation/webdriver/browser/windows/");
290			driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));
291			driver.manage().window().fullscreen();
292			driver.navigate().refresh();
293		} finally {
294			driver.quit();
295		}
296	}
297	
298	public void takeScreenshot() throws IOException {
299		WebDriver driver = new ChromeDriver();
300		try {
301			driver.get("http://example.com");
302			driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));
303			File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
304			FileUtils.copyFile(scrFile, new File(SCREENSHOT_EXAMPLE));
305		} finally {
306			driver.quit();
307		}
308	}
309	
310	public void takeElementScreenshot() throws IOException {
311		WebDriver driver = new ChromeDriver();
312		try {
313			driver.get("http://example.com");
314			driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));
315			WebElement element = driver.findElement(By.cssSelector("h1"));
316			File scrFile = element.getScreenshotAs(OutputType.FILE);
317			FileUtils.copyFile(scrFile, new File(ELEMENT_SCREENSHOT_EXAMPLE));
318		} finally {
319			driver.quit();
320		}
321	}
322	
323	public void executeScript() {
324		WebDriver driver = new ChromeDriver();
325		try {
326			driver.get(SCRIP_EXAMPLE_HTML);
327			driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));
328			WebElement loginField = driver.findElement(By.name("login"));
329			WebElement passwordField = driver.findElement(By.name("pass"));
330			loginField.sendKeys("[email protected]");
331			passwordField.sendKeys("1234");
332			
333			//Creating the JavascriptExecutor interface object by Type casting
334			JavascriptExecutor js = (JavascriptExecutor) driver;
335			
336			//Button Element
337			WebElement button = driver.findElement(By.name("btnLogin"));
338			
339			//Executing JavaScript to click on element
340			js.executeScript("arguments[0].click()", button);
341			
342			//Get return value from script
343			String text = (String) js.executeScript("return arguments[0].innerText", button);
344			System.out.println("Text: " + text);
345			
346			//Executing JavaScript directly
347			js.executeScript("console.log('hello world')");
348			
349		} finally {
350			driver.quit();
351		}
352	}
353	
354	public void printPage() throws IOException {
355		ChromeOptions options = new ChromeOptions();
356		options.addArguments("headless");
357		WebDriver driver = new ChromeDriver(options);
358		try {
359			driver.get("https://www.selenium.dev");
360			//driver.get("https://www.selenium.dev/documentation/webdriver/browser/windows/");
361			driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));
362			PrintsPage printer = (PrintsPage) driver;
363			
364			PrintOptions printOptions = new PrintOptions();
365			printOptions.setPageRanges("1-2");
366			
367			Pdf pdf = printer.print(printOptions);
368			String content = pdf.getContent();
369			System.out.println(content);
370			
371			byte[] bytes = Base64.getDecoder().decode(content);
372			
373			String dir = PRINT_EXAMPLE_PDF;
374			Path path = Paths.get(dir);
375			File file = path.toFile();
376			if (!file.exists()) {
377				Files.createFile(path);
378			} else {
379				Files.delete(path);
380			}
381			Files.write(path, bytes, StandardOpenOption.CREATE);
382		} finally {
383			driver.quit();
384		}
385	}
386	
387}
388
Full Screen
copy
1// Licensed to the Software Freedom Conservancy (SFC) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The SFC licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17package org.openqa.selenium;
18
19import org.openqa.selenium.print.PrintOptions;
20
21public interface PrintsPage {
22  Pdf print(PrintOptions printOptions) throws WebDriverException;
23}
24
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

...Most popular Stackoverflow questions on Interface-PrintsPage

    No relevant questions found for this class 😞

Most used methods in Interface-PrintsPage

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)