/
RotativaTests.cs
162 lines (145 loc) · 5.52 KB
/
RotativaTests.cs
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
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Text;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
using SharpTestsEx;
namespace Rotativa.Tests
{
[TestFixture]
public class RotativaTests
{
private IWebDriver selenium;
private StringBuilder verificationErrors;
[TestFixtureSetUp]
public void SetupTest()
{
selenium = new FirefoxDriver();
selenium.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 10));
verificationErrors = new StringBuilder();
}
[SetUp]
public void TestSetUp()
{
var rotativaDemoUrl = ConfigurationManager.AppSettings["RotativaDemoUrl"];
selenium.Navigate().GoToUrl(rotativaDemoUrl);
}
[TestFixtureTearDown]
public void FixtureTearDown()
{
if (selenium != null) selenium.Close();
}
[Test]
public void Is_the_site_reachable()
{
Assert.AreEqual("Home Page", selenium.Title);
}
[Test]
public void Can_print_the_test_pdf()
{
var testLink = selenium.FindElement(By.LinkText("Test"));
var pdfHref = testLink.GetAttribute("href");
using (var wc = new WebClient())
{
var pdfResult = wc.DownloadData(new Uri(pdfHref));
var pdfTester = new PdfTester();
pdfTester.LoadPdf(pdfResult);
pdfTester.PdfIsValid.Should().Be.True();
pdfTester.PdfContains("My MVC Application").Should().Be.True();
}
}
[Test]
public void Can_print_the_authorized_pdf()
{
var testLink = selenium.FindElement(By.LinkText("Logged In Test"));
var pdfHref = testLink.GetAttribute("href");
testLink.Click();
var username = selenium.FindElement(By.Id("UserName"));
username.SendKeys("admin");
var password = selenium.FindElement(By.Id("Password"));
password.SendKeys("admin");
password.Submit();
var manage = selenium.Manage();
var cookies = manage.Cookies.AllCookies;
using (var wc = new WebClient())
{
foreach (var cookie in cookies)
{
var cookieText = cookie.Name + "=" + cookie.Value;
wc.Headers.Add(HttpRequestHeader.Cookie, cookieText);
}
var pdfResult = wc.DownloadData(new Uri(pdfHref));
var pdfTester = new PdfTester();
pdfTester.LoadPdf(pdfResult);
pdfTester.PdfIsValid.Should().Be.True();
pdfTester.PdfContains("My MVC Application").Should().Be.True();
pdfTester.PdfContains("admin").Should().Be.True();
}
}
[Test]
public void Can_print_the_pdf_from_a_view()
{
var testLink = selenium.FindElement(By.LinkText("Test View"));
var pdfHref = testLink.GetAttribute("href");
using (var wc = new WebClient())
{
var pdfResult = wc.DownloadData(new Uri(pdfHref));
var pdfTester = new PdfTester();
pdfTester.LoadPdf(pdfResult);
pdfTester.PdfIsValid.Should().Be.True();
pdfTester.PdfContains("My MVC Application").Should().Be.True();
}
}
[Test]
public void Can_print_the_pdf_from_a_vie_with_non_ascii_chars()
{
var testLink = selenium.FindElement(By.LinkText("Test View"));
var pdfHref = testLink.GetAttribute("href");
using (var wc = new WebClient())
{
var pdfResult = wc.DownloadData(new Uri(pdfHref));
var pdfTester = new PdfTester();
pdfTester.LoadPdf(pdfResult);
pdfTester.PdfIsValid.Should().Be.True();
pdfTester.PdfContains("àéù").Should().Be.True();
}
}
[Test]
public void Can_print_the_pdf_from_a_view_with_a_model()
{
var testLink = selenium.FindElement(By.LinkText("Test ViewAsPdf with a model"));
var pdfHref = testLink.GetAttribute("href");
var title = "This is a test";
using (var wc = new WebClient())
{
var pdfResult = wc.DownloadData(new Uri(pdfHref));
var pdfTester = new PdfTester();
pdfTester.LoadPdf(pdfResult);
pdfTester.PdfIsValid.Should().Be.True();
pdfTester.PdfContains(title).Should().Be.True();
}
}
[Test]
public void Can_print_the_pdf_from_a_partial_view_with_a_model()
{
var testLink = selenium.FindElement(By.LinkText("Test PartialViewAsPdf with a model"));
var pdfHref = testLink.GetAttribute("href");
var content = "This is a test with a partial view";
using (var wc = new WebClient())
{
var pdfResult = wc.DownloadData(new Uri(pdfHref));
var pdfTester = new PdfTester();
pdfTester.LoadPdf(pdfResult);
pdfTester.PdfIsValid.Should().Be.True();
pdfTester.PdfContains(content).Should().Be.True();
}
}
}
}