1- using System ;
2- using Microsoft . VisualStudio . TestTools . UnitTesting ;
1+ using NUnit . Framework ;
32using OpenQA . Selenium ;
3+ using OpenQA . Selenium . PrintOptions ;
44using OpenQA . Selenium . Chrome ;
5+ using OpenQA . Selenium . Print
6+ using System;
57
6- namespace SeleniumDocumentation . SeleniumInteractions
8+ [ TestFixture ]
9+ public class PrintPageTest
710{
8- [ TestClass ]
9- public class PrintOptionsTest
10- {
11- [ TestMethod ]
12- public void TestOrientation ( )
13- {
14- IWebDriver driver = new ChromeDriver ( ) ;
15- driver . Navigate ( ) . GoToUrl ( "https://selenium.dev" ) ;
16- PrintOptions printOptions = new PrintOptions ( ) ;
17- printOptions . Orientation = PrintOrientation . Landscape ;
18- PrintOrientation currentOrientation = printOptions . Orientation ;
19- }
11+ private const string MAGIC_STRING = "JVBER" ;
12+ private IPrintsPage printer ;
13+ private IWebDriver driver ;
2014
21- [ TestMethod ]
22- public void TestRange ( )
23- {
24- IWebDriver driver = new ChromeDriver ( ) ;
25- driver . Navigate ( ) . GoToUrl ( "https://selenium.dev" ) ;
26- PrintOptions printOptions = new PrintOptions ( ) ;
27- printOptions . AddPageRangeToPrint ( "1-3" ) ; // add range of pages
28- printOptions . AddPageToPrint ( 5 ) ; // add individual page
29- }
15+ [ SetUp ]
16+ public void SetUp ( )
17+ {
18+ driver = new ChromeDriver ( ) ;
19+ Assert . IsInstanceOf < IPrintsPage > ( driver , "Driver does not implement PrintsPage." ) ;
20+ printer = ( IPrintsPage ) driver ;
21+ driver . Navigate ( ) . GoToUrl ( "http://selenium.dev" ) ;
22+ }
3023
31- [ TestMethod ]
32- public void TestSize ( )
33- {
34- IWebDriver driver = new ChromeDriver ( ) ;
35- driver . Navigate ( ) . GoToUrl ( "https://www.selenium.dev/" ) ;
36- PrintOptions printOptions = new PrintOptions ( ) ;
37- PrintOptions . PageSize currentDimensions = printOptions . PageDimensions ;
38- }
24+ [ Test ]
25+ [ Ignore ( "Skipped for Chrome because it needs to run headless, a workaround is needed." ) ]
26+ public void CanPrintPage ( )
27+ {
28+ PrintOptions printOptions = new PrintOptions ( ) ;
29+ var pdf = printer . Print ( printOptions ) ;
30+ Assert . That ( pdf . Content . Contains ( MAGIC_STRING ) , Is . True , "Printed PDF does not contain the expected magic string." ) ;
31+ }
3932
40- [ TestMethod ]
41- public void TestBackgrounds ( )
33+ [ Test ]
34+ [ Ignore ( "Skipped for Chrome because it needs to run headless, a workaround is needed." ) ]
35+ public void CanPrintTwoPages ( )
36+ {
37+ PrintOptions printOptions = new PrintOptions
4238 {
43- IWebDriver driver = new ChromeDriver ( ) ;
44- driver . Navigate ( ) . GoToUrl ( "https://www.selenium.dev/" ) ;
45- PrintOptions printOptions = new PrintOptions ( ) ;
46- printOptions . OutputBackgroundImages = true ;
47- bool currentBackgrounds = printOptions . OutputBackgroundImages ;
48- }
39+ PageRanges = "1-2"
40+ } ;
4941
50- [ TestMethod ]
51- public void TestMargins ( )
52- {
53- IWebDriver driver = new ChromeDriver ( ) ;
54- driver . Navigate ( ) . GoToUrl ( "https://www.selenium.dev/" ) ;
55- PrintOptions printOptions = new PrintOptions ( ) ;
56- PrintOptions . Margins currentMargins = printOptions . PageMargins ;
57- }
42+ var pdf = printer . Print ( printOptions ) ;
43+ Assert . That ( pdf . Content . Contains ( MAGIC_STRING ) , Is . True , "Printed PDF does not contain the expected magic string." ) ;
44+ }
5845
46+ [ Test ]
47+ [ Ignore ( "Skipped for Chrome because it needs to run headless, a workaround is needed." ) ]
48+ public void CanPrintWithValidParams ( )
49+ {
50+ PrintOptions printOptions = new PrintOptions ( ) ;
5951
60- [ TestMethod ]
61- public void TestScale ( )
62- {
63- IWebDriver driver = new ChromeDriver ( ) ;
64- driver . Navigate ( ) . GoToUrl ( "https://www.selenium.dev/" ) ;
65- PrintOptions printOptions = new PrintOptions ( ) ;
66- printOptions . ScaleFactor = 0.5 ;
67- double currentScale = printOptions . ScaleFactor ;
68- }
52+ //set all options
53+ printOptions . PageRanges = "1" ;
54+ printOptions . Orientation = PrintOrientation . Landscape ;
55+ printOptions . PageSize = new PageSize ( ) ;
56+ printOptions . Scale = 0.5 ;
57+ printOptions . DisplayHeaderFooter = true ;
58+ printOptions . HeaderTemplate = "Header" ;
59+ printOptions . FooterTemplate = "Footer" ;
60+ printOptions . PrintBackground = true ;
61+ printOptions . shrinkToFit = true ;
6962
70- [ TestMethod ]
71- public void TestShrinkToFit ( )
72- {
73- IWebDriver driver = new ChromeDriver ( ) ;
74- driver . Navigate ( ) . GoToUrl ( "https://www.selenium.dev/" ) ;
75- PrintOptions printOptions = new PrintOptions ( ) ;
76- printOptions . ShrinkToFit = true ;
77- bool currentShrinkToFit = printOptions . ShrinkToFit ;
78- }
63+ var pdf = printer . Print ( printOptions ) ;
64+ Assert . That ( pdf . Content . Contains ( MAGIC_STRING ) , Is . True , "Printed PDF does not contain the expected magic string." ) ;
7965 }
80-
81- }
66+ }
0 commit comments