├── .gitignore ├── build.gradle └── src └── test ├── java └── guru │ └── qa │ ├── FilesParsingTest.java │ ├── SelenideFilesTest.java │ └── model │ └── Glossary.java └── resources ├── Jacque_Fresco.jpg ├── glossary.json ├── qa_guru.csv ├── sample-xlsx-file.xlsx └── sample.txt.zip /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | build/ 3 | !gradle/wrapper/gradle-wrapper.jar 4 | !**/src/main/**/build/ 5 | !**/src/test/**/build/ 6 | 7 | ### IntelliJ IDEA ### 8 | .idea/modules.xml 9 | .idea/jarRepositories.xml 10 | .idea/compiler.xml 11 | .idea/libraries/ 12 | *.iws 13 | *.iml 14 | *.ipr 15 | out/ 16 | !**/src/main/**/out/ 17 | !**/src/test/**/out/ 18 | 19 | ### Eclipse ### 20 | .apt_generated 21 | .classpath 22 | .factorypath 23 | .project 24 | .settings 25 | .springBeans 26 | .sts4-cache 27 | bin/ 28 | !**/src/main/**/bin/ 29 | !**/src/test/**/bin/ 30 | 31 | ### NetBeans ### 32 | /nbproject/private/ 33 | /nbbuild/ 34 | /dist/ 35 | /nbdist/ 36 | /.nb-gradle/ 37 | 38 | ### VS Code ### 39 | .vscode/ 40 | 41 | ### Mac OS ### 42 | .DS_Store -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'java' 3 | } 4 | 5 | group = 'guru.qa' 6 | version = '1.0-SNAPSHOT' 7 | 8 | repositories { 9 | mavenCentral() 10 | } 11 | 12 | dependencies { 13 | testImplementation ( 14 | 'org.junit.jupiter:junit-jupiter:5.8.1', // Junit5 15 | 'com.codeborne:selenide:6.18.0', // Selenide 16 | 'org.assertj:assertj-core:3.23.1', // Assert 17 | 'com.codeborne:pdf-test:1.5.0', // PDF-файл 18 | 'com.codeborne:xls-test:1.4.3', // XLS-файл 19 | 'com.opencsv:opencsv:5.7.1', // CSV-файл 20 | 'com.google.code.gson:gson:2.10' // JSON-файл 21 | ) 22 | } 23 | 24 | test { 25 | useJUnitPlatform() 26 | } -------------------------------------------------------------------------------- /src/test/java/guru/qa/FilesParsingTest.java: -------------------------------------------------------------------------------- 1 | package guru.qa; 2 | 3 | import com.codeborne.pdftest.PDF; 4 | import com.codeborne.xlstest.XLS; 5 | import com.google.gson.Gson; 6 | import com.google.gson.JsonObject; 7 | import com.opencsv.CSVReader; 8 | import guru.qa.model.Glossary; 9 | import org.junit.jupiter.api.DisplayName; 10 | import org.junit.jupiter.api.Test; 11 | 12 | import java.io.File; 13 | import java.io.InputStream; 14 | import java.io.InputStreamReader; 15 | import java.util.List; 16 | import java.util.zip.ZipEntry; 17 | import java.util.zip.ZipInputStream; 18 | 19 | import static com.codeborne.selenide.Selenide.$; 20 | import static com.codeborne.selenide.Selenide.open; 21 | import static org.assertj.core.api.Assertions.assertThat; 22 | 23 | public class FilesParsingTest { 24 | 25 | ClassLoader cl = FilesParsingTest.class.getClassLoader(); 26 | 27 | @Test 28 | @DisplayName("Скачать PDF-файл и проверить содержимое") 29 | void pdfParseTest() throws Exception { 30 | open("https://junit.org/junit5/docs/current/user-guide/"); 31 | File downloadedPdf = $("a[href='junit-user-guide-5.10.0.pdf']").download(); 32 | PDF content = new PDF(downloadedPdf); 33 | assertThat(content.author).contains("Sam Brannen"); 34 | } 35 | 36 | @Test 37 | @DisplayName("XLS-файл - exel-документ") 38 | void xlsParseTest() throws Exception { 39 | try (InputStream resourceAsStream = cl.getResourceAsStream("sample-xlsx-file.xlsx")) { 40 | XLS content = new XLS(resourceAsStream); 41 | assertThat(content.excel.getSheetAt(0).getRow(1).getCell(1).getStringCellValue()).contains("Dulce"); 42 | } 43 | } 44 | 45 | @Test 46 | @DisplayName("CSV-файл - Comma-separated values (значения, разделённые запятыми)") 47 | void csvParseTest() throws Exception { 48 | try ( 49 | InputStream resource = cl.getResourceAsStream("qa_guru.csv"); 50 | CSVReader reader = new CSVReader(new InputStreamReader(resource)) 51 | ) { 52 | List content = reader.readAll(); 53 | assertThat(content.get(0)[1]).contains("salary"); 54 | } 55 | } 56 | 57 | @Test 58 | @DisplayName("ZIP-файл. Зная имя, можем получить его содержимое.") 59 | void zipParseTest() throws Exception { 60 | try ( 61 | InputStream resource = cl.getResourceAsStream("sample.txt.zip"); 62 | ZipInputStream zis = new ZipInputStream(resource) 63 | ) { 64 | ZipEntry entry; 65 | while((entry = zis.getNextEntry()) != null) { 66 | assertThat(entry.getName()).contains("sample.txt"); 67 | } 68 | } 69 | } 70 | 71 | @Test 72 | @DisplayName("JSON-файл, проверяем ключ-значение") 73 | void jsonParseTest() throws Exception{ 74 | Gson gson = new Gson(); 75 | try ( 76 | InputStream resource = cl.getResourceAsStream("glossary.json"); 77 | InputStreamReader reader = new InputStreamReader(resource) 78 | ) { 79 | JsonObject jsonObject = gson.fromJson(reader, JsonObject.class); 80 | assertThat(jsonObject.get("title").getAsString()).isEqualTo("example glossary"); 81 | assertThat(jsonObject.get("GlossDiv").getAsJsonObject().get("title").getAsString()).isEqualTo("S"); 82 | assertThat(jsonObject.get("GlossDiv").getAsJsonObject().get("flag").getAsBoolean()).isTrue(); 83 | } 84 | } 85 | 86 | @Test 87 | @DisplayName("JSON-файл, расширенный тест (pojo)") 88 | void jsonParseWithModelTest() throws Exception { 89 | Gson gson = new Gson(); 90 | try ( 91 | InputStream resource = cl.getResourceAsStream("glossary.json"); 92 | InputStreamReader reader = new InputStreamReader(resource) 93 | ) { 94 | Glossary jsonObject = gson.fromJson(reader, Glossary.class); 95 | assertThat(jsonObject.title).isEqualTo("example glossary"); 96 | assertThat(jsonObject.glossDiv.title).isEqualTo("S"); 97 | assertThat(jsonObject.glossDiv.flag).isTrue(); 98 | } 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /src/test/java/guru/qa/SelenideFilesTest.java: -------------------------------------------------------------------------------- 1 | package guru.qa; 2 | 3 | import com.codeborne.selenide.Condition; 4 | import org.junit.jupiter.api.DisplayName; 5 | import org.junit.jupiter.api.Test; 6 | 7 | import java.io.File; 8 | import java.io.FileInputStream; 9 | import java.io.InputStream; 10 | import java.nio.charset.StandardCharsets; 11 | 12 | import static com.codeborne.selenide.Selectors.byAttribute; 13 | import static com.codeborne.selenide.Selenide.$; 14 | import static com.codeborne.selenide.Selenide.open; 15 | import static org.assertj.core.api.Assertions.assertThat; 16 | 17 | public class SelenideFilesTest { 18 | 19 | @Test 20 | @DisplayName("Скачать файл") 21 | void selenideDownloadTest() throws Exception { 22 | open("https://github.com/junit-team/junit5/blob/main/README.md"); 23 | File downloadedFile = $(byAttribute("href","/junit-team/junit5/raw/main/README.md")).download(); 24 | try (InputStream is = new FileInputStream(downloadedFile)) { 25 | byte[] bytes = is.readAllBytes(); 26 | String textContent = new String(bytes, StandardCharsets.UTF_8); 27 | assertThat(textContent).contains("This repository is the home of _JUnit 5_."); 28 | } 29 | } 30 | 31 | @Test 32 | @DisplayName("Загрузить файл") 33 | void selenideUploadTest() { 34 | open("https://fineuploader.com/demos.html"); 35 | $("input[type='file']").uploadFromClasspath("Jacque_Fresco.jpg"); 36 | $("div.qq-file-info").shouldHave(Condition.text("Jacque_Fresco.jpg")); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/test/java/guru/qa/model/Glossary.java: -------------------------------------------------------------------------------- 1 | package guru.qa.model; 2 | 3 | import com.google.gson.annotations.SerializedName; 4 | 5 | public class Glossary { 6 | public String title; 7 | @SerializedName("GlossDiv") 8 | public GlossDiv glossDiv; 9 | 10 | public static class GlossDiv { 11 | public String title; 12 | public Boolean flag; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/test/resources/Jacque_Fresco.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sbrownbear/work_with_java_files/1c5ca468f8f463db7764b21890d759ca937f694e/src/test/resources/Jacque_Fresco.jpg -------------------------------------------------------------------------------- /src/test/resources/glossary.json: -------------------------------------------------------------------------------- 1 | { 2 | "title": "example glossary", 3 | "GlossDiv": { 4 | "title": "S", 5 | "flag": true 6 | } 7 | } -------------------------------------------------------------------------------- /src/test/resources/qa_guru.csv: -------------------------------------------------------------------------------- 1 | Workers,salary 2 | Konoplev,1000 3 | Ivanov, REST Assured -------------------------------------------------------------------------------- /src/test/resources/sample-xlsx-file.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sbrownbear/work_with_java_files/1c5ca468f8f463db7764b21890d759ca937f694e/src/test/resources/sample-xlsx-file.xlsx -------------------------------------------------------------------------------- /src/test/resources/sample.txt.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sbrownbear/work_with_java_files/1c5ca468f8f463db7764b21890d759ca937f694e/src/test/resources/sample.txt.zip --------------------------------------------------------------------------------