How to use PageMargin class of org.openqa.selenium.print package

Best Selenium code snippet using org.openqa.selenium.print.PageMargin

Run Selenium automation tests on LambdaTest cloud grid

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

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.
17
18package org.openqa.selenium.print;
19
20import org.junit.Test;
21import org.junit.experimental.categories.Category;
22import static org.assertj.core.api.Assertions.assertThat;
23
24import org.openqa.selenium.testing.UnitTests;
25
26
27@Category(UnitTests.class)
28public class PageMarginTest {
29
30  // Defaults assertion
31  private static final double TOP = 1.0;
32  private static final double BOTTOM = 1.0;
33  private static final double LEFT = 1.0;
34  private static final double RIGHT = 1.0;
35
36  @Test
37  public void setsDefaultMarginValues() {
38    PageMargin pageMargin = new PageMargin();
39
40    assertThat(pageMargin.getTop()).isEqualTo(TOP);
41    assertThat(pageMargin.getBottom()).isEqualTo(BOTTOM);
42    assertThat(pageMargin.getLeft()).isEqualTo(LEFT);
43    assertThat(pageMargin.getRight()).isEqualTo(RIGHT);
44  }
45}
46
Full Screen
copy
1import org.apache.logging.log4j.LogManager;
2import org.apache.logging.log4j.Logger;
3import org.junit.jupiter.api.AfterEach;
4import org.junit.jupiter.api.BeforeEach;
5import org.junit.jupiter.api.Test;
6import org.openqa.selenium.Pdf;
7import org.openqa.selenium.PrintsPage;
8import org.openqa.selenium.WebDriver;
9import org.openqa.selenium.print.PageMargin;
10import org.openqa.selenium.print.PageSize;
11import org.openqa.selenium.print.PrintOptions;
12
13import java.io.FileOutputStream;
14import java.io.IOException;
15
16public class PrintPageTest {
17  protected static WebDriver driver;
18  private Logger logger = LogManager.getLogger(PrintPageTest.class);
19
20  @BeforeEach
21  public void setUp() {
22    driver = WebDriverFactory.getDriver("firefox");
23    logger.info("Драйвер стартовал!");
24  }
25
26  @Test
27  public void savePageToBase64TextTest() {
28    // Открыть страницу webdriveruniversity.com
29    driver.get("https://webdriveruniversity.com/Click-Buttons/index.html");
30    logger.info("Открыта страница webdriveruniversity.com - " + "https://webdriveruniversity.com/Click-Buttons/index.html");
31    // Сохранение страницы в PDF
32    PrintsPage printer = (PrintsPage) driver;
33    PrintOptions printOptions = new PrintOptions();
34    printOptions.setPageRanges("1-2");
35    Pdf pdf = printer.print(printOptions);
36    String content = pdf.getContent();
37    logger.info(content);
38  }
39
40  @Test
41  public void savePageToFileTest() {
42    // Открыть страницу webdriveruniversity.com
43    driver.get("https://webdriveruniversity.com/Click-Buttons/index.html");
44    logger.info("Открыта страница webdriveruniversity.com - " + "https://webdriveruniversity.com/Click-Buttons/index.html");
45    // Сохранение страницы в PDF
46    PrintsPage printer = (PrintsPage) driver;
47    PrintOptions printOptions = new PrintOptions();
48    printOptions.setPageRanges("1-2");
49    Pdf pdf = printer.print(printOptions);
50    String content = pdf.getContent();
51    byte[] decoded = java.util.Base64.getDecoder().decode(content);
52    try {
53      FileOutputStream fos = new FileOutputStream("pdf.pdf");
54      fos.write(decoded);
55      fos.flush();
56      fos.close();
57      logger.info("Страница сохранена в файл pdf.pdf");
58    } catch (IOException e) {
59      e.printStackTrace();
60    }
61  }
62
63  @AfterEach
64  public void setDown() {
65    if(driver != null) {
66      driver.quit();
67      logger.info("Драйвер остановлен!");
68    }
69  }
70}
Full Screen
copy
1public void changeTabsFont( PagerSlidingTabStrip tabs)
2{
3    try
4    {
5        Typeface tab_face;
6        tab_face = Typeface.create(Typeface.createFromAsset(activity.getAssets(), "font/NanumBarunGothic.ttf"), Typeface.NORMAL);
7        ViewGroup vg = (ViewGroup) tabs.getChildAt(0);
8        int tabsCount = vg.getChildCount();
9        for (int j = 0; j < tabsCount; j++)
10        {
11            ViewGroup vgTab = (ViewGroup) vg.getChildAt(j);
12            int tabChildsCount = vgTab.getChildCount();
13            for (int i = 0; i < tabChildsCount; i++)
14            {
15                View tabViewChild = vgTab.getChildAt(i);
16                if (tabViewChild instanceof TextView)
17                {
18                    ((TextView) tabViewChild).setTypeface(tab_face, Typeface.NORMAL);
19                }
20            }
21        }
22    }
23    catch (Exception e)
24    {
25        e.printStackTrace();
26    }
27}
28
Full Screen
copy
1mTabTextTypeface = Typeface.create(tabTextTypefaceName, mTabTextTypefaceStyle);
2
Full Screen
copy
1mTabTextTypeface = Typeface.create(Typeface.createFromAsset(context.getAssets(),"DroidNaskh.ttf"), mTabTextTypefaceStyle);
2
Full Screen
copy
1com.viewpagerindicator.TabPageIndicator indicator = (com.viewpagerindicator.TabPageIndicator) findViewById(R.id.tabs);
2    Typeface type = Typeface.createFromAsset(getAssets(),"fonts/BYekan+ Bold.ttf"); 
3    LinearLayout view = (LinearLayout) indicator.getChildAt(0);
4    int tabCount=3;
5    for(int i=0;i<tabCount;i++){
6        TextView textView = (TextView) view.getChildAt(i);
7        textView.setTypeface(type);
8    }
9
Full Screen
copy
1CTSectPr sectPr = document.getDocument().getBody().getSectPr();
2CTPageSz pageSize = sectPr.getPgSz();
3double pageWidth = pageSize.getW().doubleValue();
4
Full Screen
copy
1CTPageMar pageMargin = sectPr.getPgMar();
2double pageMarginLeft = pageMargin.getLeft().doubleValue();
3double pageMarginRight = pageMargin.getRight().doubleValue();
4double effectivePageWidth = pageWidth - pageMarginLeft - pageMarginRight;
5
Full Screen
copy
1double widthInPercent = 0.6;
2int size = (int) (effectivePageWidth * widthInPercent);
3
Full Screen
copy
1CTTblWidth width = table.getCTTbl().addNewTblPr().addNewTblW();
2width.setType(STTblWidth.DXA);
3width.setW(new BigInteger(size + ""));
4
Full Screen
copy
1  XWPFTable table = doc.createTable(nRows, nCols);
2  table.getCTTbl().addNewTblPr().addNewTblW().setW(BigInteger.valueOf(10000));
3
Full Screen
copy
1cell.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(2000));
2
Full Screen
copy
1package io.github.baijifeilong.excel;
2
3import lombok.SneakyThrows;
4import org.apache.poi.xwpf.usermodel.XWPFDocument;
5import org.apache.poi.xwpf.usermodel.XWPFTable;
6
7import java.io.FileOutputStream;
8
9/**
10 * Created by BaiJiFeiLong@gmail.com at 2019-08-21 13:49
11 */
12public class WordDemo {
13
14    @SneakyThrows
15    public static void main(String[] args) {
16        XWPFDocument document = new XWPFDocument();
17        XWPFTable table = document.createTable(3, 4);
18        table.setWidth("100%");
19        for (int i = 0; i < 3; ++i) {
20            for (int j = 0; j < 4; ++j) {
21                table.getRow(i).getCell(j).setText(String.format("%d:%d", i + i, j + 1));
22            }
23        }
24        document.write(new FileOutputStream("hello.docx"));
25    }
26}
27
Full Screen
copy
1CTTbl table        = poiTable.getCTTbl();
2CTTblPr pr         = table.getTblPr();
3CTTblWidth  tblW = pr.getTblW();
4tblW.setW(BigInteger.valueOf(5000));
5tblW.setType(STTblWidth.PCT);
6pr.setTblW(tblW);
7table.setTblPr(pr);
8
Full Screen
copy
1CTJc jc = pr.addNewJc();        
2jc.setVal(STJc.RIGHT);
3pr.setJc(jc);
4
Full Screen
copy
1String[] days = new String[getAmountOfDaysUsed()];
2
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 used methods in PageMargin

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)