Changed all the scenarios with JUnit and did some tweaks on the code so that all tests are independent

This commit is contained in:
Marko 2024-05-16 18:58:38 +02:00
parent c8386f6311
commit 8e27420b0a
9 changed files with 86 additions and 153 deletions

View file

@ -146,14 +146,6 @@
<version>4.20.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.9.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.uncommons/reportng -->
<dependency>
<groupId>org.uncommons</groupId>

View file

@ -1,5 +1,7 @@
package selenium;
import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
@ -9,9 +11,6 @@ import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import java.io.FileReader;
import java.io.IOException;
@ -37,7 +36,7 @@ public class TestBase {
public static FileReader frInput;
@BeforeMethod
@Before
public void setUp() throws IOException {
String userDirectory = System.getProperty("user.dir");
String basePath = "\\src\\test\\java\\selenium\\config\\";
@ -86,12 +85,11 @@ public class TestBase {
WebElement welcomePhoto = driver.findElement(By.className(locators.getProperty("welcomePhoto")));
String ActualWelcomePhotoSrc = welcomePhoto.getAttribute("src");
String expectedWelcomePhoto = tap.getProperty("welcomePhoto");
Assert.assertEquals(ActualWelcomePhotoSrc, expectedWelcomePhoto);
org.junit.Assert.assertEquals(ActualWelcomePhotoSrc, expectedWelcomePhoto);
}
@AfterMethod
@After
public void tearDown() {
driver.quit();
}
}

View file

@ -10,9 +10,10 @@ import java.time.Duration;
import java.util.List;
import java.util.Properties;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOf;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
public class ListOwnersPage extends TestBase {
@ -35,15 +36,15 @@ public class ListOwnersPage extends TestBase {
assertTrue(tableElement.isDisplayed(), "Table is displayed");
assertEquals(rows.size(), 6, "Expected 6 rows in the table");
String owner1 = tap.getProperty("Owner1");
String owner2 = tap.getProperty("Owner2");
String owner1 = driver.findElement(By.xpath("(//tr)[2]")).getText();
String owner2 = driver.findElement(By.xpath("(//tr)[3]")).getText();
assertEquals(rows.get(1).getText(), owner1, "Incorrect data in row 1");
assertEquals(rows.get(2).getText(), owner2, "Incorrect data in row 2");
}
public void clickOnNameFromTable() {
WebElement firstRow = rows.get(1);
public void clickOnNameFromTable(int row) {
WebElement firstRow = rows.get(row);
List<WebElement> name = firstRow.findElements(By.tagName("td"));
WebElement firstName = name.get(0);
String firstNameText = firstName.getText();

View file

@ -1,26 +1,22 @@
package selenium.scenarios;
import jdk.jfr.Description;
import org.jetbrains.annotations.NotNull;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.junit.Before;
import org.junit.Test;
import selenium.TestBase;
import selenium.pages.AddOwnerPage;
import selenium.pages.FindOwnersPage;
import selenium.pages.ListOwnersPage;
import selenium.pages.OwnerPage;
import static org.testng.AssertJUnit.*;
import static org.junit.Assert.*;
public class TS02AddOwnerTest extends TestBase {
public class AddOwnerTest extends TestBase {
private AddOwnerPage addOwnerPage;
private OwnerPage ownerPage;
private FindOwnersPage findOwnersPage;
@BeforeMethod
@Before
public void setObjects() {
addOwnerPage = new AddOwnerPage(driver, locators);
ownerPage = new OwnerPage(driver, locators);
@ -32,8 +28,8 @@ public class TS02AddOwnerTest extends TestBase {
findOwnersPage.clickOnAddOwnerButton();
}
public void addOrEditAnOwner(@NotNull String action, String firstName, String lastName, String address, String city,
String telephone) {
public void addOrEditAnOwner(String action, String firstName, String lastName, String address, String city,
String telephone) {
String firstNameText = input.getProperty(firstName);
String lastNameText = input.getProperty(lastName);
String addressText = input.getProperty(address);
@ -43,14 +39,12 @@ public class TS02AddOwnerTest extends TestBase {
addOwnerPage.setTextInFields(firstNameText, lastNameText, addressText, cityText, telephoneText);
if (action.equalsIgnoreCase("add")) {
addOwnerPage.clickingOnAddOwnerButton();
}
else if (action.equalsIgnoreCase("update")) {
} else if (action.equalsIgnoreCase("update")) {
addOwnerPage.clickOnUpdateOwnerButton();
}
}
@Test
@Description("Validate successfully adding an owner")
public void testAddingAnOwner() {
navigateToAddOwner();
addOrEditAnOwner("add", "firstName", "lastName", "address", "city", "telephone");
@ -59,8 +53,7 @@ public class TS02AddOwnerTest extends TestBase {
assertTrue(ownerPage.isLastNameDisplayed(input.getProperty("lastName")));
}
@Test(priority = 1)
@Description("Validate adding an owner without filling any of the fields")
@Test
public void testEmptyFields() {
navigateToAddOwner();
addOwnerPage.clickingOnAddOwnerButton();
@ -69,9 +62,8 @@ public class TS02AddOwnerTest extends TestBase {
assertTrue(addOwnerPage.isErrorMessageDisplayedForEmptyFields(expectedErrorMessage));
}
// User is still created - REPORT DEFECT!!!
@Test(priority = 2)
@Description("Validate if an owner is added after putting numbers in the name fields")
// User is still created after putting numbers in the name fields - REPORT DEFECT!!!
@Test
public void testNumbersInNameFields() {
navigateToAddOwner();
@ -81,8 +73,7 @@ public class TS02AddOwnerTest extends TestBase {
}
// You can add the same owner twice - REPORT DEFECT!!!
@Test(priority = 3)
@Description("Validate if you can add the same owner twice")
@Test
public void testCreateSameOwnerTwice() {
navigateToAddOwner();
@ -93,8 +84,7 @@ public class TS02AddOwnerTest extends TestBase {
assertFalse("Message should not be visible", ownerPage.isSuccessMessageDisplayed());
}
@Test(priority = 4)
@Description("Validate if an owner is added after putting text in the 'Telephone' field")
@Test
public void testTextInTelephoneField() {
navigateToAddOwner();
@ -102,37 +92,35 @@ public class TS02AddOwnerTest extends TestBase {
String expectedErrorMessage = tap.getProperty("errorMessageTelephoneField");
assertTrue("Error message should be displayed for invalid telephone number",
addOwnerPage.isErrorMessageDisplayedForTextInTelephoneField(expectedErrorMessage));
addOwnerPage.isErrorMessageDisplayedForTextInTelephoneField(expectedErrorMessage));
}
@Test(priority = 5)
@Description("Validate updating an owner")
@Test
public void testUpdateOwner() {
findOwnersPage.navigateToFindOwnersPage();
findOwnersPage.clickOnFindOwnerButton();
ListOwnersPage listOwnersPage = new ListOwnersPage(driver, locators);
listOwnersPage.clickOnNameFromTable();
listOwnersPage.clickOnNameFromTable(2);
ownerPage.clickOnEditOwnerButton();
addOwnerPage.clearFields();
addOrEditAnOwner("update", "updateFirstName", "updateLastName", "updateAddress", "updateCity",
"updateTelephone");
"updateTelephone");
assertTrue(ownerPage.isUpdateMessageDisplayed());
assertTrue(ownerPage.isLastNameDisplayed(input.getProperty("updateLastName")));
}
// User can still be updated - REPORT DEFECT!!!
@Test(priority = 6)
@Description("Validate updating an owner with the details of an already existing owner")
public void testUpdateOwnerWithExistingDetails() {
@Test
public void testUpdateOwnerWithSameDetailsFromOtherOwner() {
findOwnersPage.navigateToFindOwnersPage();
findOwnersPage.clickOnFindOwnerButton();
ListOwnersPage listOwnersPage = new ListOwnersPage(driver, locators);
listOwnersPage.clickOnNameFromTable();
listOwnersPage.clickOnNameFromTable(3);
ownerPage.clickOnEditOwnerButton();
addOwnerPage.clearFields();
@ -142,8 +130,7 @@ public class TS02AddOwnerTest extends TestBase {
assertFalse("Error message is not displayed", ownerPage.isUpdateMessageDisplayed());
}
@Test(priority = 7)
@Description("Validate if a newly added owner can be updated")
@Test
public void testUpdateNewlyAddedOwner() {
navigateToAddOwner();
addOrEditAnOwner("add", "firstName3", "lastName3", "address3", "city3", "telephone3");
@ -152,10 +139,9 @@ public class TS02AddOwnerTest extends TestBase {
addOwnerPage.clearFields();
addOrEditAnOwner("update", "updateFirstName2", "updateLastName2", "updateAddress2", "updateCity2",
"updateTelephone2");
"updateTelephone2");
assertTrue(ownerPage.isUpdateMessageDisplayed());
assertTrue(ownerPage.isLastNameDisplayed(input.getProperty("updateLastName2")));
}
}

View file

@ -1,25 +1,20 @@
package selenium.scenarios;
import jdk.jfr.Description;
import org.jetbrains.annotations.NotNull;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.junit.Before;
import org.junit.Test;
import selenium.TestBase;
import selenium.pages.*;
import static org.testng.AssertJUnit.*;
import static org.junit.Assert.*;
public class TS03AddPetTest extends TestBase {
public class AddPetTest extends TestBase {
private AddOwnerPage addOwnerPage;
private OwnerPage ownerPage;
private FindOwnersPage findOwnersPage;
private AddPetPage addPetPage;
@BeforeMethod
@Before
public void setObjects() {
addOwnerPage = new AddOwnerPage(driver, locators);
ownerPage = new OwnerPage(driver, locators);
@ -27,7 +22,7 @@ public class TS03AddPetTest extends TestBase {
addPetPage = new AddPetPage(driver, locators);
}
public void addOrEditPet(@NotNull String action, String petName, String birthDate, String petType) {
public void addOrEditPet(String action, String petName, String birthDate, String petType) {
String petNameText = input.getProperty(petName);
String petBirthDateText = input.getProperty(birthDate);
String petTypeOption = input.getProperty(petType);
@ -35,23 +30,21 @@ public class TS03AddPetTest extends TestBase {
addPetPage.fillTheFields(petNameText, petBirthDateText, petTypeOption);
if (action.equalsIgnoreCase("add")) {
addPetPage.clickOnAddPetButton();
}
else if (action.equalsIgnoreCase("update")) {
} else if (action.equalsIgnoreCase("update")) {
addPetPage.clickOnUpdatePetButton();
}
}
public void navigateToAddPetForExistingOwner() {
public void navigateToAddPetForExistingOwner(int row) {
findOwnersPage.navigateToFindOwnersPage();
findOwnersPage.clickOnFindOwnerButton();
ListOwnersPage listOwnersPage = new ListOwnersPage(driver, locators);
listOwnersPage.clickOnNameFromTable();
listOwnersPage.clickOnNameFromTable(row);
ownerPage.clickOnAddNewPetButton();
}
@Test
@Description("Validate adding a pet to a newly added owner")
public void testAddPetToNewlyAddedOwner() {
findOwnersPage.navigateToFindOwnersPage();
findOwnersPage.clickOnAddOwnerButton();
@ -71,20 +64,18 @@ public class TS03AddPetTest extends TestBase {
assertTrue(ownerPage.isPetNameDisplayed("petName"));
}
@Test(priority = 1)
@Description("Validate adding a new pet to an already existing owner")
@Test
public void testAddPetToExistingOwner() {
navigateToAddPetForExistingOwner();
navigateToAddPetForExistingOwner(4);
addOrEditPet("add", "petName2", "birthDate2", "petType2");
assertTrue(ownerPage.isPetAddedSuccessMessageDisplayed());
assertTrue(ownerPage.isPetNameDisplayed("petName2"));
}
@Test(priority = 2)
@Description("Validate adding the same pet twice")
@Test
public void testAddingSamePetTwice() {
navigateToAddPetForExistingOwner();
navigateToAddPetForExistingOwner(1);
addOrEditPet("add", "petName3", "birthDate3", "petType3");
driver.navigate().back();
@ -93,43 +84,39 @@ public class TS03AddPetTest extends TestBase {
assertTrue(addPetPage.isErrorMessageDisplayedForSamePetName());
}
@Test(priority = 3)
@Description("Validate adding a pet without filling any of the fields")
@Test
public void testAddPetWithEmptyFields() {
navigateToAddPetForExistingOwner();
navigateToAddPetForExistingOwner(1);
addPetPage.clickOnAddPetButton();
String expectedErrorMessage = tap.getProperty("emptyPetFieldsErrorMessage");
assertTrue(addPetPage.isErrorMessageDisplayedForEmptyFields(expectedErrorMessage));
}
@Test(priority = 4)
@Description("Validate adding a pet with a future birth date")
@Test
public void testAddPetWithFutureBirthDate() {
navigateToAddPetForExistingOwner();
navigateToAddPetForExistingOwner(1);
addOrEditPet("add", "petName4", "futureBirthDate", "petType4");
assertTrue(addPetPage.isInvalidDateErrorMessageDisplayed());
}
// Pet is still added after putting numbers in 'Name' field - REPORT DEFECT!!!
@Test(priority = 5)
@Description("Validate adding numbers in the 'Name' field")
@Test
public void testAddNumbersInNameField() {
navigateToAddPetForExistingOwner();
navigateToAddPetForExistingOwner(1);
addOrEditPet("add", "numberPetName", "birthDate5", "petType5");
assertFalse(ownerPage.isPetAddedSuccessMessageDisplayed());
}
@Test(priority = 6)
@Description("Validate updating a pet")
@Test
public void testUpdatePet() {
findOwnersPage.navigateToFindOwnersPage();
findOwnersPage.clickOnFindOwnerButton();
ListOwnersPage listOwnersPage = new ListOwnersPage(driver, locators);
listOwnersPage.clickOnNameFromTable();
listOwnersPage.clickOnNameFromTable(1);
ownerPage.clickOnEditPetButton();
addPetPage.clearFields();

View file

@ -1,22 +1,19 @@
package selenium.scenarios;
import jdk.jfr.Description;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.junit.Before;
import org.junit.Test;
import selenium.TestBase;
import selenium.pages.*;
import static org.testng.AssertJUnit.assertTrue;
import static org.junit.Assert.assertTrue;
public class TS04AddVisitTest extends TestBase {
public class AddVisitTest extends TestBase {
private OwnerPage ownerPage;
private FindOwnersPage findOwnersPage;
private AddVisitPage addVisitPage;
@BeforeMethod
@Before
public void setObjects() {
ownerPage = new OwnerPage(driver, locators);
findOwnersPage = new FindOwnersPage(driver, locators);
@ -38,13 +35,11 @@ public class TS04AddVisitTest extends TestBase {
findOwnersPage.clickOnFindOwnerButton();
ListOwnersPage listOwnersPage = new ListOwnersPage(driver, locators);
listOwnersPage.clickOnNameFromTable();
listOwnersPage.clickOnNameFromTable(1);
ownerPage.clickOnAddVisitButton();
}
// Spelling mistake in success message - Report
@Test
@Description("Validate adding a visit for a pet")
public void testAddVisitForPet() {
navigateToVisitPage();
@ -53,8 +48,7 @@ public class TS04AddVisitTest extends TestBase {
assertTrue(ownerPage.isVisitAdded("description"));
}
@Test(priority = 1)
@Description("Validate adding a visit without filling any of the fields")
@Test
public void testVisitEmptyFields() {
navigateToVisitPage();
@ -63,8 +57,7 @@ public class TS04AddVisitTest extends TestBase {
assertTrue(addVisitPage.isErrorMessageDisplayedForEmptyField(expectedErrorMessage));
}
@Test(priority = 2)
@Description("Validate adding a visit with an invalid date")
@Test
public void testInvalidDate() {
navigateToVisitPage();

View file

@ -1,10 +1,9 @@
package selenium.scenarios;
import jdk.jfr.Description;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import selenium.TestBase;
import selenium.pages.AddOwnerPage;
import selenium.pages.FindOwnersPage;
@ -13,16 +12,16 @@ import selenium.pages.OwnerPage;
import java.util.List;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class TS01FindOwnersTest extends TestBase {
public class FindOwnersTest extends TestBase {
private OwnerPage ownerPage;
private FindOwnersPage findOwnersPage;
@BeforeMethod
@Before
public void setObjects() {
ownerPage = new OwnerPage(driver, locators);
findOwnersPage = new FindOwnersPage(driver, locators);
@ -35,46 +34,38 @@ public class TS01FindOwnersTest extends TestBase {
}
@Test
@Description("Validate if the correct owner is displayed after searching by an existing last name")
public void testFindOwnerByExistingLastName() {
setupFindOwnersPage(input.getProperty("existingLastName"));
assertTrue(ownerPage.isLastNameDisplayed(input.getProperty("existingLastName")),
"The last name should be displayed on the Owner page");
assertTrue(ownerPage.isLastNameDisplayed(input.getProperty("existingLastName")));
}
@Test(priority = 1)
@Description("Validate if an owner is displayed after searching by an existing first name")
@Test
public void testFindOwnerByExistingFirstName() {
setupFindOwnersPage(input.getProperty("existingFirstName"));
String ownerNotFoundText = findOwnersPage.getOwnerNotFoundText();
String expectedText = tap.getProperty("ownerNotFoundText");
assertEquals(ownerNotFoundText, expectedText, "Expected text has been displayed");
assertEquals(expectedText, ownerNotFoundText);
}
@Test(priority = 2)
@Description("Validate if an owner is displayed after searching for a non-existing last name")
@Test
public void testFindOwnerByNonExistingLastName() {
setupFindOwnersPage(input.getProperty("nonExistingLastName"));
String ownerNotFoundText = findOwnersPage.getOwnerNotFoundText();
String expectedText = tap.getProperty("ownerNotFoundText");
assertEquals(ownerNotFoundText, expectedText, "Expected text has been displayed");
assertEquals(expectedText, ownerNotFoundText);
}
@Test(priority = 3)
@Description("Validate case sensitivity after searching by an existing last name")
@Test
public void testCaseSensitiveLastName() {
setupFindOwnersPage(input.getProperty("caseSensitiveLastName"));
assertTrue(ownerPage.isLastNameDisplayed(input.getProperty("existingLastName")),
"The last name should be displayed on the Owner page");
assertTrue(ownerPage.isLastNameDisplayed(input.getProperty("existingLastName")));
}
@Test(priority = 4)
@Description("Validate if all owners are displayed after clicking the 'Find Owner' button "
+ "without filling the 'Last name' field")
@Test
public void testEmptyLastNameField() {
setupFindOwnersPage("");
@ -82,17 +73,15 @@ public class TS01FindOwnersTest extends TestBase {
listOwnersPage.tableAppearance();
}
@Test(priority = 5)
@Description("Validate if you can navigate to the Owner page after clicking on a name from the table")
@Test
public void testNavigateToOwnerPageFromTable() {
setupFindOwnersPage("");
ListOwnersPage listOwnersPage = new ListOwnersPage(driver, locators);
listOwnersPage.clickOnNameFromTable();
listOwnersPage.clickOnNameFromTable(1);
}
@Test(priority = 6)
@Description("Validate finding a newly added owner")
@Test
public void testFindNewlyAddedOwner() {
findOwnersPage.navigateToFindOwnersPage();
findOwnersPage.clickOnAddOwnerButton();

View file

@ -1,21 +1,19 @@
package selenium.scenarios;
import org.springframework.context.annotation.Description;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.junit.Before;
import org.junit.Test;
import selenium.TestBase;
import selenium.pages.FindOwnersPage;
import selenium.pages.HomePage;
import static org.testng.AssertJUnit.assertTrue;
import static org.junit.Assert.assertTrue;
public class TS05HomePageTest extends TestBase {
public class HomePageTest extends TestBase {
private HomePage homePage;
private FindOwnersPage findOwnersPage;
@BeforeMethod
@Before
public void setObjects() {
homePage = new HomePage(driver, locators);
findOwnersPage = new FindOwnersPage(driver, locators);
@ -27,7 +25,6 @@ public class TS05HomePageTest extends TestBase {
}
@Test
@Description("Validate if you can navigate to home page by clicking on the 'Home' header")
public void testNavigateHomeFromHeader() {
findOwnersPage.navigateToFindOwnersPage();
@ -35,8 +32,7 @@ public class TS05HomePageTest extends TestBase {
checkForCorrectWelcomePhoto();
}
@Test(priority = 1)
@Description("Validate if you can navigate to home by page by clicking on the logo of the application")
@Test
public void navigateHomeFromLogo() {
findOwnersPage.navigateToFindOwnersPage();

View file

@ -1,9 +0,0 @@
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="SpringPetClinic">
<test name="SpringPetClinicTest">
<packages>
<package name="selenium.scenarios"/>
</packages>
</test>
</suite>