How to use print method of org.openqa.selenium.Interface PrintsPage class

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

Source:Windows.java Github

copy

Full Screen

...22import org.openqa.selenium.WebElement;23import org.openqa.selenium.WindowType;24import org.openqa.selenium.chrome.ChromeDriver;25import org.openqa.selenium.chrome.ChromeOptions;26import org.openqa.selenium.print.PrintOptions;27import org.openqa.selenium.support.ui.ExpectedConditions;28import org.openqa.selenium.support.ui.WebDriverWait;29import br.com.ayrton.first_selenium_app.app.Resources;30import io.github.bonigarcia.wdm.WebDriverManager;31public class Windows {32 private final String RESOURCES_PATH = Paths.get(Resources.PATH, "browser").toString(); 33 private final String SCRIP_EXAMPLE_HTML = Paths.get(this.RESOURCES_PATH, "windows-script-examples.html").toString();34 private final String PRINT_EXAMPLE_PDF = Paths.get(this.RESOURCES_PATH, "printPageContent.pdf").toString();35 private final String SCREENSHOT_EXAMPLE = Paths.get(this.RESOURCES_PATH, "imageBrowser.png").toString();36 private final String ELEMENT_SCREENSHOT_EXAMPLE = Paths.get(this.RESOURCES_PATH, "imageElement.png").toString();37 38 public void getWindowHandler() {39 WebDriverManager.chromedriver().setup();40 ChromeOptions options = new ChromeOptions();41 WebDriver driver = new ChromeDriver(options);42 try {43 System.out.println(driver.getWindowHandle());44 } finally {45 driver.quit();46 }47 }48 49 public void switchWindowsOrTabs() {50 WebDriver driver = new ChromeDriver();51 try {52 driver.get("https://www.selenium.dev/documentation/webdriver/browser/windows/");53 driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));54 55 //Store the ID of the original window56 String originalWindow = driver.getWindowHandle();57 58 //Check we don't have other windows open already59 assert driver.getWindowHandles().size() == 1;60 61 //Click the link which opens in a new window62 //driver.findElement(By.linkText("new window")).click();63 driver.findElement(By.linkText("new window")).click();64 65 //Wait for the new window or tab66 WebDriverWait wait = new WebDriverWait(driver, Duration.ofMillis(1500));67 wait.until(ExpectedConditions.numberOfWindowsToBe(2));68 69 //Loop through until we find a new window handle70 for (String windowHandle : driver.getWindowHandles()) {71 if (!originalWindow.contentEquals(windowHandle)) {72 driver.switchTo().window(windowHandle);73 break;74 }75 }76 77 //Wait for the new tab to finish loading content78 //wait.until(ExpectedConditions.titleIs("Selenium documentation"));79 wait.until(ExpectedConditions.titleIs("Selenium"));80 81 driver.switchTo().window(originalWindow);82 } finally {83 driver.quit();84 }85 }86 87 public void createWindowOrTabAndSwitch() {88 WebDriver driver = new ChromeDriver();89 try {90 driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));91 92 //Opens a new tab and switches to new tab93 driver.switchTo().newWindow(WindowType.TAB);94 driver.get("https://www.selenium.dev/documentation/webdriver/browser/windows/");95 96 //Opens a new window and switches to new window97 driver.switchTo().newWindow(WindowType.WINDOW);98 driver.get("https://www.selenium.dev/documentation/webdriver/browser/windows/");99 } finally {100 driver.quit();101 }102 }103 104 //When you are finished with a window or tab and it is not the last105 //window or tab open in your browser, you should close it and switch106 //back to the window you were using previously.107 //You must switch back to a valid window handle in order to continue108 //execution.109 public void closingAWindowOrTab() {110 WebDriver driver = new ChromeDriver();111 try {112 driver.get("https://www.selenium.dev/documentation/webdriver/browser/windows/");113 driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));114 115 //Store the ID of the original window116 String originalWindow = driver.getWindowHandle();117 118 //Check we don't have other windows open already119 assert driver.getWindowHandles().size() == 1;120 121 //Click the link which opens in a new window122 //driver.findElement(By.linkText("new window")).click();123 driver.findElement(By.linkText("new window")).click();124 125 //Wait for the new window or tab126 WebDriverWait wait = new WebDriverWait(driver, Duration.ofMillis(1500));127 wait.until(ExpectedConditions.numberOfWindowsToBe(2));128 129 //Loop through until we find a new window handle130 for (String windowHandle : driver.getWindowHandles()) {131 if (!originalWindow.contentEquals(windowHandle)) {132 driver.switchTo().window(windowHandle);133 break;134 }135 }136 137 //Wait for the new tab to finish loading content138 //wait.until(ExpectedConditions.titleIs("Selenium documentation"));139 wait.until(ExpectedConditions.titleIs("Selenium"));140 141 //Close the tab or window142 driver.close();143 144 //Switch back to the old tab or window145 driver.switchTo().window(originalWindow);146 147 driver.get("https://www.google.com.br/");148 } finally {149 driver.quit();150 }151 }152 153 //driver.quit();154 //Quit will:155 //1. Close all the windows and tabs associated with that WebDriver session156 //2. Close the browser process157 //3. Close the background driver process158 //4. Notify Selenium Grid that the browser is no longer in use so it can be159 //used by another session (if you are using Selenium Grid)160 //161 //Some test framework offer methods and annotations which you can hook into162 //to tear down at the end of a test.163 //@AfterAll164 //public static void tearDown(){165 // driver.quit();166 //}167 //168 //If not runnig WebDriver in a test context, you may consider using try/finally169 //which is offered by most languages so that an exception will still clean up the170 //WebDriver session.171 //try {172 // //WebDriver code here...173 //} finally {174 // driver.quit();175 //}176 public void quitingTheBrowserAtEndOfSession() {177 WebDriver driver = new ChromeDriver();178 try {179 driver.get("https://www.selenium.dev/documentation/webdriver/browser/windows/");180 driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));181 } finally {182 driver.quit();183 }184 }185 186 /* Window management */187 public void getWindowSize() {188 WebDriver driver = new ChromeDriver();189 try {190 //Access each dimension individually191 int width = driver.manage().window().getSize().getWidth();192 int height = driver.manage().window().getSize().getHeight();193 194 //Or store the dimensions and query them later195 Dimension size = driver.manage().window().getSize();196 int width1 = size.getWidth();197 int height1 = size.getHeight();198 199 System.out.println(width);200 System.out.println(height);201 System.out.println(width1);202 System.out.println(height1);203 } finally {204 driver.quit();205 }206 }207 208 public void setWindowSize() {209 WebDriver driver = new ChromeDriver();210 try {211 driver.get("https://www.selenium.dev/documentation/webdriver/browser/windows/");212 driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));213 driver.manage().window().setSize(new Dimension(1300, 500));214 driver.get("https://www.selenium.dev/documentation/webdriver/browser/windows/");215 } finally {216 driver.quit();217 }218 }219 220 public void getWindowPosition() {221 WebDriver driver = new ChromeDriver();222 try {223 //Access each dimension individually224 int x = driver.manage().window().getPosition().getX();225 int y = driver.manage().window().getPosition().getY();226 227 //Or store the dimensions and query them later228 Point position = driver.manage().window().getPosition();229 int x1 = position.getX();230 int y1 = position.getY();231 232 System.out.println(x);233 System.out.println(y);234 System.out.println(x1);235 System.out.println(y1);236 } finally {237 driver.quit();238 }239 }240 241 public void setWindowPosition() {242 WebDriver driver = new ChromeDriver();243 try {244 driver.get("https://www.selenium.dev/documentation/webdriver/browser/windows/");245 driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));246 driver.manage().window().setPosition(new Point(0,0));247 //driver.get("https://www.selenium.dev/documentation/webdriver/browser/windows/");248 driver.navigate().refresh();249 } finally {250 driver.quit();251 }252 }253 254 public void maximizeWindow() {255 WebDriver driver = new ChromeDriver();256 try {257 driver.get("https://www.selenium.dev/documentation/webdriver/browser/windows/");258 driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));259 driver.manage().window().maximize();260 //driver.get("https://www.selenium.dev/documentation/webdriver/browser/windows/");261 driver.navigate().refresh();262 } finally {263 driver.quit();264 }265 }266 267 //This feature works with Selenium 4 and later versions.268 public void minimizeWindow() {269 WebDriver driver = new ChromeDriver();270 try {271 driver.get("https://www.selenium.dev/documentation/webdriver/browser/windows/");272 driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));273 driver.manage().window().minimize();274 //driver.get("https://www.selenium.dev/documentation/webdriver/browser/windows/");275 driver.navigate().refresh();276 } finally {277 driver.quit();278 }279 }280 281 public void fullscreenWindow() {282 WebDriver driver = new ChromeDriver();283 try {284 driver.get("https://www.selenium.dev/documentation/webdriver/browser/windows/");285 driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));286 driver.manage().window().fullscreen();287 driver.navigate().refresh();288 } finally {289 driver.quit();290 }291 }292 293 public void takeScreenshot() throws IOException {294 WebDriver driver = new ChromeDriver();295 try {296 driver.get("http://example.com");297 driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));298 File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);299 FileUtils.copyFile(scrFile, new File(SCREENSHOT_EXAMPLE));300 } finally {301 driver.quit();302 }303 }304 305 public void takeElementScreenshot() throws IOException {306 WebDriver driver = new ChromeDriver();307 try {308 driver.get("http://example.com");309 driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));310 WebElement element = driver.findElement(By.cssSelector("h1"));311 File scrFile = element.getScreenshotAs(OutputType.FILE);312 FileUtils.copyFile(scrFile, new File(ELEMENT_SCREENSHOT_EXAMPLE));313 } finally {314 driver.quit();315 }316 }317 318 public void executeScript() {319 WebDriver driver = new ChromeDriver();320 try {321 driver.get(SCRIP_EXAMPLE_HTML);322 driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));323 WebElement loginField = driver.findElement(By.name("login"));324 WebElement passwordField = driver.findElement(By.name("pass"));325 loginField.sendKeys("test@gmail.com");326 passwordField.sendKeys("1234");327 328 //Creating the JavascriptExecutor interface object by Type casting329 JavascriptExecutor js = (JavascriptExecutor) driver;330 331 //Button Element332 WebElement button = driver.findElement(By.name("btnLogin"));333 334 //Executing JavaScript to click on element335 js.executeScript("arguments[0].click()", button);336 337 //Get return value from script338 String text = (String) js.executeScript("return arguments[0].innerText", button);339 System.out.println("Text: " + text);340 341 //Executing JavaScript directly342 js.executeScript("console.log('hello world')");343 344 } finally {345 driver.quit();346 }347 }348 349 public void printPage() throws IOException {350 ChromeOptions options = new ChromeOptions();351 options.addArguments("headless");352 WebDriver driver = new ChromeDriver(options);353 try {354 driver.get("https://www.selenium.dev");355 //driver.get("https://www.selenium.dev/documentation/webdriver/browser/windows/");356 driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));357 PrintsPage printer = (PrintsPage) driver;358 359 PrintOptions printOptions = new PrintOptions();360 printOptions.setPageRanges("1-2");361 362 Pdf pdf = printer.print(printOptions);363 String content = pdf.getContent();364 System.out.println(content);365 366 byte[] bytes = Base64.getDecoder().decode(content);367 368 String dir = PRINT_EXAMPLE_PDF;369 Path path = Paths.get(dir);370 File file = path.toFile();371 if (!file.exists()) {372 Files.createFile(path);373 } else {374 Files.delete(path);375 }376 Files.write(path, bytes, StandardOpenOption.CREATE);377 } finally {378 driver.quit();...

Full Screen

Full Screen

Source:PrintsPage.java Github

copy

Full Screen

...14// KIND, either express or implied. See the License for the15// specific language governing permissions and limitations16// under the License.17package org.openqa.selenium;18import org.openqa.selenium.print.PrintOptions;19public interface PrintsPage {20 Pdf print(PrintOptions printOptions) throws WebDriverException;21}...

Full Screen

Full Screen

print

Using AI Code Generation

copy

Full Screen

1package com.selenium4beginners.java.webdriver;2import org.openqa.selenium.WebDriver;3import org.openqa.selenium.chrome.ChromeDriver;4import org.openqa.selenium.devtools.DevTools;5import org.openqa.selenium.devtools.v91.log.Log;6import org.openqa.selenium.devtools.v91.log.model.LogEntry;7import java.util.List;8import static org.openqa.selenium.devtools.v91.log.Log.entryAdded;9public class PrintLogs {10 public static void main(String[] args) {11 System.setProperty("webdriver.chrome.driver", "C:\\Users\\Admin\\Downloads\\chromedriver_win32\\chromedriver.exe");12 WebDriver driver = new ChromeDriver();13 DevTools devTools = ((ChromeDriver) driver).getDevTools();14 devTools.createSession();15 devTools.send(Log.enable());16 driver.quit();17 devTools.send(Log.disable());18 List<LogEntry> logEntries = devTools.send(Log.getEntries());19 logEntries.forEach(logEntry -> System.out.println(logEntry.getText()));20 }21}

Full Screen

Full Screen

print

Using AI Code Generation

copy

Full Screen

1package com.selenium.webdriver;2import java.util.concurrent.TimeUnit;3import org.openqa.selenium.WebDriver;4import org.openqa.selenium.firefox.FirefoxDriver;5import org.openqa.selenium.print.PrintsPage;6public class PrintPage {7 public static void main(String[] args) {8 WebDriver driver = new FirefoxDriver();9 driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);10 PrintsPage printPage = (PrintsPage) driver;11 printPage.print();12 }13}

Full Screen

Full Screen

Selenium 4 Tutorial:

LambdaTest’s Selenium 4 tutorial is covering every aspects of Selenium 4 testing with examples and best practices. Here you will learn basics, such as how to upgrade from Selenium 3 to Selenium 4, to some advanced concepts, such as Relative locators and Selenium Grid 4 for Distributed testing. Also will learn new features of Selenium 4, such as capturing screenshots of specific elements, opening a new tab or window on the browser, and new protocol adoptions.

Chapters:

  1. Upgrading From Selenium 3 To Selenium 4?: In this chapter, learn in detail how to update Selenium 3 to Selenium 4 for Java binding. Also, learn how to upgrade while using different build tools such as Maven or Gradle and get comprehensive guidance for upgrading Selenium.

  2. What’s New In Selenium 4 & What’s Being Deprecated? : Get all information about new implementations in Selenium 4, such as W3S protocol adaption, Optimized Selenium Grid, and Enhanced Selenium IDE. Also, learn what is deprecated for Selenium 4, such as DesiredCapabilites and FindsBy methods, etc.

  3. Selenium 4 With Python: Selenium supports all major languages, such as Python, C#, Ruby, and JavaScript. In this chapter, learn how to install Selenium 4 for Python and the features of Python in Selenium 4, such as Relative locators, Browser manipulation, and Chrom DevTool protocol.

  4. Selenium 4 Is Now W3C Compliant: JSON Wireframe protocol is retiring from Selenium 4, and they are adopting W3C protocol to learn in detail about the advantages and impact of these changes.

  5. How To Use Selenium 4 Relative Locator? : Selenium 4 came with new features such as Relative Locators that allow constructing locators with reference and easily located constructors nearby. Get to know its different use cases with examples.

  6. Selenium Grid 4 Tutorial For Distributed Testing: Selenium Grid 4 allows you to perform tests over different browsers, OS, and device combinations. It also enables parallel execution browser testing, reads up on various features of Selenium Grid 4 and how to download it, and runs a test on Selenium Grid 4 with best practices.

  7. Selenium Video Tutorials: Binge on video tutorials on Selenium by industry experts to get step-by-step direction from automating basic to complex test scenarios with Selenium.

Selenium 101 certifications:

LambdaTest also provides certification for Selenium testing to accelerate your career in Selenium automation testing.

Run Selenium automation tests on LambdaTest cloud grid

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

Most used method in Interface-PrintsPage

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful