Skip to content

Commit b283b3b

Browse files
committed
pdf scenario
1 parent 73d97b1 commit b283b3b

File tree

10 files changed

+359
-0
lines changed

10 files changed

+359
-0
lines changed

resources/ImageTester.jar

20.1 MB
Binary file not shown.

resources/Invoice_PDFs/INV12345.pdf

109 KB
Binary file not shown.

resources/logo.png

5.2 KB
Loading

resources/test.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ site.largedom.url=https://the-internet.herokuapp.com/large
66
site.dynamic.url=https://the-internet.herokuapp.com/dynamic_content
77
site.frames.url=https://the-internet.herokuapp.com/nested_frames
88
sites.tables.url=https://the-internet.herokuapp.com/tables
9+
sites.invoices.url=https://www.invoicesimple.com/invoice-generator
10+
911

1012
################
1113
# SELENIUM #
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
package pages;
2+
3+
import org.openqa.selenium.By;
4+
import org.openqa.selenium.WebDriver;
5+
import org.openqa.selenium.WebElement;
6+
import org.openqa.selenium.support.ui.ExpectedConditions;
7+
import org.openqa.selenium.support.ui.WebDriverWait;
8+
9+
import java.io.File;
10+
11+
public class InvoiceGeneratorPage {
12+
13+
private WebDriver driver;
14+
WebDriverWait wait;
15+
16+
//HEADER
17+
private By invoiceTitleField = By.id("invoice-title");
18+
private By logoInput = By.xpath("//div[contains(@class, 'invoice-logo')]/descendant::input");
19+
20+
//FROM FIELDS
21+
private By fromNameField = By.id("invoice-company-name");
22+
private By fromEmailField = By.id("invoice-company-email");
23+
private By fromAddressField = By.id("invoice-company-address1");
24+
private By fromCityStateField = By.id("invoice-company-address2");
25+
private By fromZipCodeField = By.id("invoice-company-address3");
26+
private By fromPhoneField = By.id("invoice-company-phone");
27+
private By fromBusinessPhoneField = By.id("invoice-company-business-number");
28+
29+
//TO FIELDS
30+
private By toNameField = By.id("invoice-client-name");
31+
private By toEmailField = By.id("invoice-client-email");
32+
private By toAddressField = By.id("invoice-client-address1");
33+
private By toPhoneField = By.id("invoice-client-phone");
34+
35+
36+
private By invoiceNumberField = By.id("invoice-number");
37+
38+
//ITEMS
39+
private String itemRow_Format = "//tr[contains(@class, 'item-row-%d')]";
40+
private By itemDescription_Child = By.id("invoice-item-code");
41+
private By itemPrice_Child = By.xpath("td[contains(@class, 'item-row-rate')]/descendant::input");
42+
private By itemQuantity_Child = By.xpath("td[contains(@class, 'item-row-quantity')]/descendant::input");
43+
private By itemDetails_Child = By.tagName("textarea");
44+
private By addItemButton = By.id("invoice-item-add");
45+
46+
private By notesArea = By.id("invoice-notes");
47+
private By getLinkButton = By.xpath("//span[text()='Get Link']/parent::button");
48+
49+
private By colorSelectList = By.className("color-select-option");
50+
51+
52+
53+
public InvoiceGeneratorPage(WebDriver driver){
54+
this.driver = driver;
55+
wait = new WebDriverWait(driver, 5);
56+
}
57+
58+
public void setInvoiceTitle(String title){
59+
WebElement field = driver.findElement(invoiceTitleField);
60+
wait.until(ExpectedConditions.elementToBeClickable(field));
61+
field.clear();
62+
field.sendKeys(title);
63+
}
64+
65+
public void setLogo(File imgFile){
66+
driver.findElement(logoInput).sendKeys(imgFile.getAbsolutePath());
67+
}
68+
69+
public void setFromName(String name){
70+
driver.findElement(fromNameField).sendKeys(name);
71+
}
72+
73+
public void setFromEmail(String email){
74+
driver.findElement(fromEmailField).sendKeys(email);
75+
}
76+
77+
public void setFromAddress(String address){
78+
driver.findElement(fromAddressField).sendKeys(address);
79+
}
80+
81+
public void setFromCityState(String cityAndState){
82+
driver.findElement(fromCityStateField).sendKeys(cityAndState);
83+
}
84+
85+
public void setFromZipCode(String zipCode){
86+
driver.findElement(fromZipCodeField).sendKeys(zipCode);
87+
}
88+
89+
public void setFromPhone(String phoneNumber){
90+
driver.findElement(fromPhoneField).sendKeys(phoneNumber);
91+
}
92+
93+
public void setFromBusinessPhone(String phoneNumber){
94+
driver.findElement(fromBusinessPhoneField).sendKeys(phoneNumber);
95+
}
96+
97+
public void setToName(String name){
98+
driver.findElement(toNameField).sendKeys(name);
99+
}
100+
101+
public void setToEmail(String email){
102+
driver.findElement(toEmailField).sendKeys(email);
103+
}
104+
105+
public void setToAddress(String address){
106+
driver.findElement(toAddressField).sendKeys(address);
107+
}
108+
109+
public void setToPhone(String phoneNumber){
110+
driver.findElement(toPhoneField).sendKeys(phoneNumber);
111+
}
112+
113+
public void setInvoiceNumber(String invoiceNumber){
114+
WebElement field = driver.findElement(invoiceNumberField);
115+
field.clear();
116+
field.sendKeys(invoiceNumber);
117+
}
118+
119+
public void setItemDescription(int itemIndex, String description){
120+
getItemRow(itemIndex).findElement(itemDescription_Child).sendKeys(description);
121+
}
122+
123+
public void setItemPrice(int itemIndex, String price){
124+
getItemRow(itemIndex).findElement(itemPrice_Child).sendKeys(price);
125+
}
126+
127+
public void setItemQuantity(int itemIndex, String quantity){
128+
getItemRow(itemIndex).findElement(itemQuantity_Child).sendKeys(quantity);
129+
}
130+
131+
public void setItemAdditionalDetails(int itemIndex, String details){
132+
getItemRow(itemIndex).findElement(itemDetails_Child).sendKeys(details);
133+
}
134+
135+
public void clickToAddNewItem(){
136+
WebElement button = driver.findElement(addItemButton);
137+
wait.until(ExpectedConditions.elementToBeClickable(button));
138+
button.click();
139+
}
140+
141+
public void selectColor(int index){
142+
driver.findElements(colorSelectList).get(index).click();
143+
}
144+
145+
public InvoicePreviewPage clickGetLink(){
146+
String invoiceWindowTitle = String.format("Invoice %s - %s",
147+
driver.findElement(invoiceNumberField).getAttribute("value"),
148+
driver.findElement(fromNameField).getAttribute("value"));
149+
150+
driver.findElement(getLinkButton).click();
151+
return new InvoicePreviewPage(driver, invoiceWindowTitle);
152+
}
153+
154+
155+
156+
private WebElement getItemRow(int index){
157+
String xpath = String.format(itemRow_Format, index);
158+
return driver.findElement(By.xpath(xpath));
159+
}
160+
161+
162+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package pages;
2+
3+
import org.openqa.selenium.By;
4+
import org.openqa.selenium.WebDriver;
5+
import org.openqa.selenium.support.ui.ExpectedCondition;
6+
import org.openqa.selenium.support.ui.ExpectedConditions;
7+
import org.openqa.selenium.support.ui.WebDriverWait;
8+
9+
import java.io.File;
10+
11+
12+
public class InvoicePreviewPage {
13+
14+
private WebDriver driver;
15+
WebDriverWait wait;
16+
17+
private By pdfButton = By.xpath("//span[text()='PDF']/parent::button");
18+
19+
public InvoicePreviewPage(WebDriver driver, String windowTitle) {
20+
this.driver = driver;
21+
22+
wait = new WebDriverWait(driver, 5);
23+
wait.until(ExpectedConditions.numberOfWindowsToBe(2));
24+
25+
for(String window: driver.getWindowHandles()){
26+
driver.switchTo().window(window);
27+
}
28+
29+
wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(pdfButton));
30+
}
31+
32+
public void clickPDFButton(){
33+
driver.findElement(pdfButton).click();
34+
35+
//Wait for download to finish before exiting this method
36+
File file = new File("/Users/angie/Downloads/INV12345.pdf");
37+
while (!file.exists()) {
38+
try{
39+
Thread.sleep(1000);
40+
}catch(Exception e){
41+
e.printStackTrace();
42+
}
43+
}
44+
}
45+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package pages;
2+
3+
import org.openqa.selenium.By;
4+
import org.openqa.selenium.WebDriver;
5+
6+
public class SortableDataTablesPage {
7+
8+
private WebDriver driver;
9+
10+
private By TABLE = By.xpath("//table[@id='table1']");
11+
12+
private String HEADER_FORMAT = "//table[@id='table1']/descendant::span[text()='%s']/parent::th[contains(@class, 'header')]";
13+
private By LAST_NAME_HEADER = By.xpath(String.format(HEADER_FORMAT, "Last Name"));
14+
private By FIRST_NAME_HEADER = By.xpath(String.format(HEADER_FORMAT, "First Name"));
15+
private By EMAIL_HEADER = By.xpath(String.format(HEADER_FORMAT, "Email"));
16+
private By DUE_HEADER = By.xpath(String.format(HEADER_FORMAT, "Due"));
17+
private By WEBSITE_HEADER = By.xpath(String.format(HEADER_FORMAT, "Web Site"));
18+
19+
public SortableDataTablesPage(WebDriver driver){
20+
this.driver = driver;
21+
}
22+
23+
public By getTableElementLocator(){
24+
return TABLE;
25+
}
26+
27+
public void sortLastNameColumn(){
28+
driver.findElement(LAST_NAME_HEADER).click();
29+
}
30+
31+
public void sortFirstNameColumn(){
32+
driver.findElement(FIRST_NAME_HEADER).click();
33+
}
34+
public void sortEmailColumn(){
35+
driver.findElement(EMAIL_HEADER).click();
36+
}
37+
public void sortDueColumn(){
38+
driver.findElement(DUE_HEADER).click();
39+
}
40+
public void sortWebsiteColumn(){
41+
driver.findElement(WEBSITE_HEADER).click();
42+
}
43+
}

src/main/java/utils/FileUtils.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package utils;
2+
3+
import java.io.File;
4+
5+
public class FileUtils {
6+
7+
/**
8+
* Moves file from one location to another.
9+
* Deletes file from destination if it already exists, before moving
10+
* @param file file to move
11+
* @param destination pathname to move file to
12+
* @return was move successful
13+
*/
14+
public static boolean moveFile(File file, String destination){
15+
16+
File existingFile = new File(destination);
17+
if(existingFile.exists()){
18+
existingFile.delete();
19+
}
20+
21+
return file.renameTo(new File(destination));
22+
}
23+
}

src/test/java/PDFTests.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import base.BaseTests;
2+
import base.EyesManager;
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
import pages.InvoiceGeneratorPage;
6+
import pages.InvoicePreviewPage;
7+
import utils.FileUtils;
8+
9+
import java.io.File;
10+
import java.io.IOException;
11+
12+
public class PDFTests extends BaseTests {
13+
14+
@Test
15+
public void testInvoices() throws IOException, InterruptedException {
16+
driver.get(System.getProperty("sites.invoices.url"));
17+
18+
String invoiceNumber = "INV12345";
19+
20+
InvoiceGeneratorPage generatorPage = new InvoiceGeneratorPage(driver);
21+
generatorPage.setInvoiceTitle("Invoice from your Personal Stylist");
22+
generatorPage.setLogo(new File("resources/logo.png"));
23+
generatorPage.selectColor(4);
24+
25+
generatorPage.setFromName("Marie Combs");
26+
generatorPage.setFromEmail("[email protected]");
27+
generatorPage.setFromAddress("123 Main Street");
28+
generatorPage.setFromCityState("New York, NY");
29+
generatorPage.setFromZipCode("12345");
30+
generatorPage.setFromPhone("(555) 555 5555");
31+
32+
generatorPage.setToName("John Brown");
33+
generatorPage.setToEmail("[email protected]");
34+
generatorPage.setToAddress("456 Main Street");
35+
36+
generatorPage.setInvoiceNumber(invoiceNumber);
37+
38+
generatorPage.setItemDescription(1, "Dress");
39+
generatorPage.setItemPrice(1, "54.99");
40+
generatorPage.setItemAdditionalDetails(1, "Floral print maxi dress");
41+
generatorPage.clickToAddNewItem();
42+
43+
generatorPage.setItemDescription(2, "Leggings");
44+
generatorPage.setItemPrice(2, "14.99");
45+
generatorPage.setItemAdditionalDetails(2, "pink sheer leggings");
46+
generatorPage.clickToAddNewItem();
47+
48+
generatorPage.setItemDescription(3, "Shoes");
49+
generatorPage.setItemPrice(3, "29.99");
50+
generatorPage.setItemAdditionalDetails(3, "yellow mary jane heels");
51+
52+
InvoicePreviewPage previewPage = generatorPage.clickGetLink();
53+
eyesManager.validateWindow();
54+
previewPage.clickPDFButton();
55+
56+
File downloadedPDF = new File("/Users/angie/Downloads/" + invoiceNumber + ".pdf");
57+
String destination = "resources/Invoice_PDFs/" + invoiceNumber + ".pdf";
58+
Assert.assertTrue(invoiceNumber + ".pdf file was not moved to test location",
59+
FileUtils.moveFile(downloadedPDF, destination));
60+
61+
Assert.assertTrue("Error validating PDF", EyesManager.validatePDF(destination));
62+
}
63+
}

src/test/java/base/EyesManager.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
import com.applitools.eyes.BatchInfo;
44
import com.applitools.eyes.selenium.Eyes;
5+
import org.apache.commons.io.IOUtils;
56
import org.openqa.selenium.By;
67
import org.openqa.selenium.WebDriver;
78

9+
import java.io.IOException;
10+
811
public class EyesManager {
912

1013
private Eyes eyes;
@@ -52,4 +55,22 @@ public void abort(){
5255
public Eyes getEyes(){
5356
return eyes;
5457
}
58+
59+
public static boolean validatePDF(String filepath) throws IOException, InterruptedException {
60+
String command = String.format(
61+
"java -jar resources/ImageTester.jar -k %s -f %s",
62+
System.getProperty("applitools.api.key"),
63+
filepath);
64+
65+
Process process = Runtime.getRuntime().exec(command);
66+
process.waitFor();
67+
String stream = IOUtils.toString(process.getInputStream(), "UTF-8");
68+
System.out.println(stream);
69+
70+
if(stream != null && stream.contains("Mismatch")){
71+
return false;
72+
}
73+
74+
return true;
75+
}
5576
}

0 commit comments

Comments
 (0)