diff --git a/README.md b/README.md index 39af52c..f106bd6 100644 --- a/README.md +++ b/README.md @@ -1,29 +1,24 @@ -# README # - -This README would normally document whatever steps are necessary to get your application up and running. - -### What is this repository for? ### - -* Quick summary -* Version -* [Learn Markdown](https://bitbucket.org/tutorials/markdowndemo) - -### How do I get set up? ### - -* Summary of set up -* Configuration -* Dependencies -* Database configuration -* How to run tests -* Deployment instructions - -### Contribution guidelines ### - -* Writing tests -* Code review -* Other guidelines - -### Who do I talk to? ### - -* Repo owner or admin -* Other community or team contact \ No newline at end of file +# Manual-Tester-Auto-Test +## Used: +1. Java 11+ +2. Maven +3. Selenium WebDriver +4. jUnit 5.x +5. Page object pattern +6. Page factory + +### for page: +[Automationpractice](http://automationpractice.com ) + +## Added: + + +| to pageobjects: | to tests: | to utils: | +| --------------------- |:---------------------:|-----------:| +| BasePage | BaseTest | RandomUser | +| HomePage | CartTest | | +| ExecutionOfOrdersPage | ExecutionOfOrdersTest | | +| LoginPage | LoginTest | | +| ProductsPage | RegisterTest | | +| RegisterPage | SearchTest | | +| SearchResultPage | | | diff --git a/src/test/java/pageobjects/BasePage.java b/src/test/java/pageobjects/BasePage.java index c7b51f8..cb44ae3 100644 --- a/src/test/java/pageobjects/BasePage.java +++ b/src/test/java/pageobjects/BasePage.java @@ -1,17 +1,63 @@ package pageobjects; -import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.FindBy; +import org.openqa.selenium.support.PageFactory; +import org.openqa.selenium.support.ui.WebDriverWait; + +import java.util.List; + +// here we put selectors and methods common to all subpages of our test application +// we do that we can see in automationpractice.com that footer and headers are the same on all subpages public class BasePage { + // @FindBy to Page Factory and expands Page Object + @FindBy(id="search_query_top") // it's the same as driver.findElement(By.id("search_query_top")) + WebElement searchBox; // it's the same as driver.findElement(By.id("search_query_top")) + + @FindBy(css = ".menu-content>li>a") + List productCategories; // List productCategories = driver.findElements (By.cssSelector (". Menu-content> li> a")); + + @FindBy(css = ".shopping_cart .ajax_cart_quantity") + WebElement cartQuantity; + + @FindBy(className = "login") + WebElement signInButton; + + WebDriver driver; // because our diver is here so every other class inherits this driver + WebDriverWait wait; - WebDriver driver; static final String BASE_URL = "http://automationpractice.com/"; + public BasePage(WebDriver driverIn, WebDriverWait waitIn) { // we need create constructor to give 'static' WebDriver diver;' from tests / java / tests / BaseTests + this.driver = driverIn; // driverIn is driver with comes from our method executed + this.wait = waitIn; + // we must say in constructor that InteliJ IDEA should initialize and find elements @FindBy + PageFactory.initElements(driver, this); + } + public void searchForProduct(String productName) { - driver.findElement(By.id("search_query_top")).sendKeys("dress"); - driver.findElement(By.id("search_query_top")).sendKeys(Keys.ENTER); + // write searching keyword in search box and enter + searchBox.sendKeys("dress"); // in search pool we type dress + searchBox.sendKeys(Keys.ENTER); // confirm searching phrase with enter + } + + // Menu: WOMEN | DRESSES | T-SHIRTS is available in all subpages we can put method + // opening selected category in our BasePage class: + public void goToProductCategoryByIndex(int productCategoryIndex) { // depending with we choose (WOMEN, DRESSES, T-SHIRTS) method will click on this what we choose + // we created locator to three elements on menu bar in @FindBy + productCategories.get(productCategoryIndex).click(); // we click on forwarded "productCategoryIndex" in menu + } + + public int getCartSize() { + String cartQuantityText = cartQuantity.getText(); + return Integer.parseInt(cartQuantityText); + } + + public void clickSignIn() { + signInButton.click(); } } diff --git a/src/test/java/pageobjects/ExecutionOfOrdersPage.java b/src/test/java/pageobjects/ExecutionOfOrdersPage.java new file mode 100644 index 0000000..71c5d2a --- /dev/null +++ b/src/test/java/pageobjects/ExecutionOfOrdersPage.java @@ -0,0 +1,40 @@ +package pageobjects; + +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.FindBy; +import org.openqa.selenium.support.How; +import org.openqa.selenium.support.ui.WebDriverWait; + +public class ExecutionOfOrdersPage extends BasePage { + @FindBy(how = How.NAME, using = "processAddress") + WebElement proceedToCheckout; + + @FindBy(id = "uniform-cgv") + WebElement termsOfServiceCheckBox; + + @FindBy(how = How.NAME, using = "processCarrier") + WebElement proceedToCheckoutAgain; + + @FindBy(className = "bankwire") + WebElement payByBankWireButton; + + @FindBy(className = "cheque") + WebElement payByCheckButton; + + @FindBy(xpath = "//button[@type='submit' and contains(.,'confirm ' )]") + WebElement confirmMyOrderButton; + + public ExecutionOfOrdersPage(WebDriver driverIn, WebDriverWait waitIn) { + super(driverIn, waitIn); + } + + public void completionOfTheOrder() { + proceedToCheckout.click(); + termsOfServiceCheckBox.click(); + proceedToCheckoutAgain.click(); + payByBankWireButton.click(); + confirmMyOrderButton.click(); + } + +} \ No newline at end of file diff --git a/src/test/java/pageobjects/HomePage.java b/src/test/java/pageobjects/HomePage.java index 95ac93f..d3f4a07 100644 --- a/src/test/java/pageobjects/HomePage.java +++ b/src/test/java/pageobjects/HomePage.java @@ -1,15 +1,16 @@ package pageobjects; import org.openqa.selenium.WebDriver; +import org.openqa.selenium.support.ui.WebDriverWait; public class HomePage extends BasePage { + public HomePage(WebDriver driverIn, WebDriverWait waitIn) { + super(driverIn, waitIn); + } - public HomePage(WebDriver driverIn) { - this.driver = driverIn; - } + // open main page + public void openPage() { + driver.get(BASE_URL + "index.php"); + } - public void openPage() { - driver.get(BASE_URL + "index.php"); - } - -} +} \ No newline at end of file diff --git a/src/test/java/pageobjects/LoginPage.java b/src/test/java/pageobjects/LoginPage.java index ad09bf1..93ee43c 100644 --- a/src/test/java/pageobjects/LoginPage.java +++ b/src/test/java/pageobjects/LoginPage.java @@ -1,4 +1,61 @@ package pageobjects; -public class LoginPage { -} +import org.junit.jupiter.api.Assertions; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.FindBy; +import org.openqa.selenium.support.ui.WebDriverWait; +import utils.RandomUser; + +public class LoginPage extends BasePage { + @FindBy(id = "email") + WebElement emailAddressField; + + @FindBy(id = "passwd") + WebElement passwdField; + + @FindBy(id = "SubmitLogin") + WebElement signInButton; + + @FindBy(className = "logout") + WebElement signOutButton; + + @FindBy(id = "email_create") + WebElement createNewAccountField; + + @FindBy(id = "SubmitCreate") + WebElement createNewAccountButton; + + public LoginPage(WebDriver driverIn, WebDriverWait waitIn) { + super(driverIn, waitIn); + } + + public void goToRegisterForm() throws InterruptedException { + RandomUser user = new RandomUser(); + clickSignIn(); + createNewAccountField.sendKeys(user.email); + createNewAccountButton.click(); + } + + public void signInToRegisteredAccount() { + emailAddressField.sendKeys("test@autotest.pl"); // registered + passwdField.sendKeys("autoTest_1234!"); // registered + signInButton.click(); + Assertions.assertEquals(signOutButton.getText(), "Sign out"); + } + + public void signInToNotRegisteredAccount() { + RandomUser user = new RandomUser(); + emailAddressField.sendKeys(user.email); // not registered + passwdField.sendKeys(user.password); // not registered + signInButton.click(); + // Assertions.assertEquals + } + + public void continueOrderShouldAddProductToTheBasketAndBuyThroughRegisterForm() { + RandomUser user = new RandomUser(); + createNewAccountField.sendKeys(user.email); + createNewAccountButton.click(); + } + +} \ No newline at end of file diff --git a/src/test/java/pageobjects/MyAccountPage.java b/src/test/java/pageobjects/MyAccountPage.java deleted file mode 100644 index 93949fc..0000000 --- a/src/test/java/pageobjects/MyAccountPage.java +++ /dev/null @@ -1,4 +0,0 @@ -package pageobjects; - -public class MyAccountPage { -} diff --git a/src/test/java/pageobjects/ProductsPage.java b/src/test/java/pageobjects/ProductsPage.java new file mode 100644 index 0000000..1d7e25b --- /dev/null +++ b/src/test/java/pageobjects/ProductsPage.java @@ -0,0 +1,74 @@ +package pageobjects; + +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.interactions.Actions; +import org.openqa.selenium.support.FindBy; +import org.openqa.selenium.support.ui.ExpectedConditions; +import org.openqa.selenium.support.ui.WebDriverWait; + +import java.util.List; +import java.util.Random; + +public class ProductsPage extends BasePage { + @FindBy(css = ".product_list .product-container") + List productsContainers; + + @FindBy(css = ".ajax_add_to_cart_button") + List addToCartButtons; + + @FindBy(className = "continue") + WebElement continueShoppingButton; + + @FindBy(linkText = "Proceed to checkout") + WebElement proceedCheckoutButton; + + // we need create constructor to give 'static WebDriver driver;' from src/test/java/tests/BaseTest + public ProductsPage(WebDriver driverIn, WebDriverWait waitIn) { + super(driverIn, waitIn); + } + + public void moveMouseToProductContainer(int productIndex) { // List productsContainers = diver.findElements(By.cssSelector(".product_list .product-container")); + Actions builder = new Actions(driver); + builder.moveToElement(productsContainers.get(productIndex)).build().perform(); // we must add ".build().perform();" every invoke moveToElement + } + + public void addProductToTheBasket(int productIndex) { + // wait until element: "addToCartButtons.get(productIndex)" will be clickable + wait.until(ExpectedConditions.elementToBeClickable(addToCartButtons.get(productIndex))); + // if is clickable > click + addToCartButtons.get(productIndex).click(); + // wait until element: "continueShoppingButton" will be clickable + wait.until(ExpectedConditions.elementToBeClickable(continueShoppingButton)); + // if is clickable > click + continueShoppingButton.click(); + } + + public void addProductToTheBasketAndProceedCheckout(int productIndex) { + // wait until element: "addToCartButtons.get(productIndex)" will be clickable + wait.until(ExpectedConditions.elementToBeClickable(addToCartButtons.get(productIndex))); + // if is clickable > click + addToCartButtons.get(productIndex).click(); + // wait until element: "proceedCheckoutButton" will be clickable + wait.until(ExpectedConditions.elementToBeClickable(proceedCheckoutButton)); + // if is clickable > click + proceedCheckoutButton.click(); + // now we see summary and we must click proceedCheckoutButton again to continue shopping + proceedCheckoutButton.click(); + // now we are on LoginPage and we are ask to CREATE AN ACCOUNT or login by ALREADY REGISTERED? form + } + + public void addRandomProductToCart() { + Random random = new Random(); + int productIndex = random.nextInt(productsContainers.size()); // set random number of product from showed product list + moveMouseToProductContainer(productIndex); // we except that sixth element from the list will be clicked // 5 + addProductToTheBasket(productIndex); // we click on sixth element to add to the basket // 5 + } + + public void addRandomProductToCartAndProceedCheckout() { + Random random = new Random(); + int productIndex = random.nextInt(productsContainers.size()); // set random number of product from showed product list + moveMouseToProductContainer(productIndex); // we except that sixth element from the list will be clicked // 5 + addProductToTheBasketAndProceedCheckout(productIndex); // we click on sixth element to add to the basket // 5 + } +} \ No newline at end of file diff --git a/src/test/java/pageobjects/RegisterPage.java b/src/test/java/pageobjects/RegisterPage.java new file mode 100644 index 0000000..9defe1d --- /dev/null +++ b/src/test/java/pageobjects/RegisterPage.java @@ -0,0 +1,98 @@ +package pageobjects; + +import org.junit.jupiter.api.Assertions; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.FindBy; +import org.openqa.selenium.support.ui.Select; +import org.openqa.selenium.support.ui.WebDriverWait; +import utils.RandomUser; + +import java.util.List; +import java.util.Random; + +public class RegisterPage extends BasePage { + @FindBy(id = "customer_firstname") + WebElement customerFirstName; + + @FindBy(id = "customer_lastname") + WebElement customerLastName; + + @FindBy(id = "id_gender1") + WebElement customerGender1; + + @FindBy(id = "id_gender2") + WebElement customerGender2; + + @FindBy(id = "passwd") + WebElement password; + + @FindBy(id = "days") + WebElement daysSelect; + Select selectDays = new Select(daysSelect); + + @FindBy(id = "month") + WebElement monthsSelect; + Select selectMonths = new Select(monthsSelect); + + @FindBy(id = "years") + WebElement yearsSelect; + Select selectYears = new Select(yearsSelect); + + @FindBy(id = "address1") + WebElement customerAddress; + + @FindBy(id = "city") + WebElement customerCity; + + @FindBy(id = "id_state") + WebElement customerStateSelect; + Select stateSelect = new Select(customerStateSelect); + + @FindBy(id ="postcode") + WebElement postCode; + + @FindBy(id = "phone_mobile") + WebElement customerPhoneMobile; + + @FindBy(id = "alias") + WebElement customerAlias; + + @FindBy(id = "submitAccount") + WebElement submitAccountButton; + + @FindBy(id = "logout") + WebElement signOutButton; + + public RegisterPage(WebDriver driverIn, WebDriverWait waitIn) { + super(driverIn, waitIn); + } + + public void registerUser(RandomUser user) throws InterruptedException { + RandomGender(); + customerFirstName.sendKeys(user.firstName); + customerLastName.sendKeys(user.lastName); + password.sendKeys(user.password); + selectDays.selectByIndex(1); + selectMonths.selectByIndex(1); + selectYears.selectByIndex(1); + customerAddress.sendKeys(user.address1); + customerCity.sendKeys(user.city); // we don't have city generator in faker so we type anything + stateSelect.selectByIndex(1); + postCode.sendKeys(user.zipCode); + customerPhoneMobile.sendKeys(user.phoneNumber); + customerAlias.sendKeys(user.firstName); + submitAccountButton.click(); + Assertions.assertEquals(signOutButton.getText(), "Sign out"); + } + + private void RandomGender() { + Random random = new Random(); + int gender = random.nextInt(2); + if (gender == 0) { + customerGender1.click(); + } else { + customerGender2.click(); + } + } +} \ No newline at end of file diff --git a/src/test/java/pageobjects/SearchResultPage.java b/src/test/java/pageobjects/SearchResultPage.java index e7a128d..32621a2 100644 --- a/src/test/java/pageobjects/SearchResultPage.java +++ b/src/test/java/pageobjects/SearchResultPage.java @@ -1,31 +1,42 @@ package pageobjects; -import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.FindBy; +import org.openqa.selenium.support.ui.WebDriverWait; import java.util.List; public class SearchResultPage extends BasePage { + @FindBy(id = ".product_list .product-name") + List productsNames; - public SearchResultPage(WebDriver driverIn) { - this.driver = driverIn; + @FindBy(css = ".heading-counter") + WebElement searchSummary; + + public SearchResultPage(WebDriver driverIn, WebDriverWait waitIn) { + super(driverIn, waitIn); } public boolean isProductWithNameVisible(String expectedProductName) { - List productsNames = driver.findElements(By.cssSelector(".product_list .product-name")); - int counter = 0; + // findElements return List of elements so we must create variable to use found elements after in our code. cssSelector(String) + // List productsNames = driver.findElements(By.cssSelector(".product_list .product-name")); + + // Now we must create method to check every find element if it have 'expectedProductName'. We use foreach loop: + // First we tell what is kind of element (WebElement), next give him a name (productName), and in with collection are these productsName ? (productsNames) + for (WebElement productName : productsNames) { - System.out.println(productName.getText()); + System.out.println(productName.getText()); // in console view we type what is find if (productName.getText().toLowerCase().contains(expectedProductName.toLowerCase())) { - return true; + return true; // if is return true } } - return false; + return false; // when loop end find nothing } public String getSearchSummary() { - WebElement searchSummary = driver.findElement(By.cssSelector(".heading-counter")); - return searchSummary.getText(); + // WebElement searchSummary = driver.findElement(By.cssSelector(".heading-counter")); + + return searchSummary.getText(); // we must return text we found } -} +} \ No newline at end of file diff --git a/src/test/java/tests/BaseTest.java b/src/test/java/tests/BaseTest.java deleted file mode 100644 index ccc4e57..0000000 --- a/src/test/java/tests/BaseTest.java +++ /dev/null @@ -1,32 +0,0 @@ -package tests; - -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.BeforeEach; -import org.openqa.selenium.Dimension; -import org.openqa.selenium.WebDriver; -import org.openqa.selenium.chrome.ChromeDriver; - -import java.util.concurrent.TimeUnit; - -public class BaseTest { - static WebDriver driver; - - @BeforeAll - static void setUp() { - driver = new ChromeDriver(); - driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); - driver.manage().window().setSize(new Dimension(1920, 1080)); - } - - @BeforeEach - void clearCookies() { - driver.manage().deleteAllCookies(); - } - - @AfterAll - static void tearDown() { - driver.quit(); // close all browser tabs/windows and webdriver - } - -} diff --git a/src/test/java/tests/BaseTests.java b/src/test/java/tests/BaseTests.java new file mode 100644 index 0000000..d4b9318 --- /dev/null +++ b/src/test/java/tests/BaseTests.java @@ -0,0 +1,43 @@ +/* This class contains methods: + - @BeforeAll - initialization WebDriver + - @BeforeEach - cleaning of cookies files + - @AfterAll - closing WebDriver */ + +package tests; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.chrome.ChromeDriver; +import org.openqa.selenium.support.ui.WebDriverWait; + +import java.util.concurrent.TimeUnit; + +public class BaseTests { + static WebDriver driver; + static WebDriverWait wait; + + @BeforeAll + static void setUp() { + // WebDriver drives a browser natively, like a user would be do it + driver = new ChromeDriver(); + wait = new WebDriverWait(driver, 5); + // we must set timeouts because without this our tests will fail + // Implicit wait: before every calling 'find element by..' we will wait 5 seconds to load all elements on the page + driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); + // we need to open the page in full screen because if the page is responsive it looks different with different screen sizes + driver.manage().window().maximize(); // setSize.(new Dimension(1920, 1080)); // we open in full HD resolution + } + + @BeforeEach + void clearCookies() { + driver.manage().deleteAllCookies(); + } + + @AfterAll + static void tearDown() { + driver.quit(); // close all browser tabs/windows and webdriver + } + +} \ No newline at end of file diff --git a/src/test/java/tests/CartTests.java b/src/test/java/tests/CartTests.java new file mode 100644 index 0000000..7131aae --- /dev/null +++ b/src/test/java/tests/CartTests.java @@ -0,0 +1,35 @@ +package tests; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import pageobjects.HomePage; +import pageobjects.ProductsPage; + +public class CartTests extends BaseTests { + @Test + void shouldBeAbleAddProductToTheCart() { + // create a Product page using the "goToProductCategoryPage()" method through which we go to any page + ProductsPage productsPage = goToProductCategoryPage(0); // it's a result of goToProductCategoryPage(); method + productsPage.addRandomProductToCart(); // order random product + // Assertions.assertTrue(product in the basket); + Assertions.assertEquals(1, productsPage.getCartSize()); // check if we have one product in the basket. Get how many products is in the basket and check if there is as many as we excepted (1) + } + + @Test + void shouldBeAbleAddMultipleProductsToTheCart() { + ProductsPage productsPage = goToProductCategoryPage(1); + productsPage.addRandomProductToCart(); + productsPage.addRandomProductToCart(); + Assertions.assertEquals(2, productsPage.getCartSize()); + } + + private ProductsPage goToProductCategoryPage(int i) { + HomePage homePage = new HomePage(driver, wait); // we sent to this Page Object class driver and wait + homePage.openPage(); // Open main Page + // on homepage we click on first element '0' from the menu list "productCategories" (WOMEN, DRESSES, T-SHIRTS) + homePage.goToProductCategoryByIndex(0); // go to MENU > WOMEN + // Then we want to simulate mouse move on products from the showed list on the page + ProductsPage productsPage = new ProductsPage(driver, wait); + return productsPage; + } +} \ No newline at end of file diff --git a/src/test/java/tests/ExecutionOfOrdersTests.java b/src/test/java/tests/ExecutionOfOrdersTests.java new file mode 100644 index 0000000..6c4bf00 --- /dev/null +++ b/src/test/java/tests/ExecutionOfOrdersTests.java @@ -0,0 +1,26 @@ +package tests; + +import org.junit.jupiter.api.Test; +import pageobjects.*; +import utils.RandomUser; + +public class ExecutionOfOrdersTests extends BaseTests { + @Test + void shouldAddProductToTheBasketAndBuyThroughRegisterForm() throws InterruptedException { + RandomUser user = new RandomUser(); + HomePage homePage = new HomePage(driver, wait); + homePage.openPage(); + ProductsPage productsPage = new ProductsPage(driver, wait); + productsPage.addRandomProductToCartAndProceedCheckout(); + + LoginPage loginPage = new LoginPage(driver, wait); + + loginPage.continueOrderShouldAddProductToTheBasketAndBuyThroughRegisterForm(); + + RegisterPage registerPage = new RegisterPage(driver, wait); + registerPage.registerUser(user); + + ExecutionOfOrdersPage executionOfOrdersPage = new ExecutionOfOrdersPage(driver, wait); + executionOfOrdersPage.completionOfTheOrder(); + } +} \ No newline at end of file diff --git a/src/test/java/tests/LoginTest.java b/src/test/java/tests/LoginTest.java deleted file mode 100644 index 754d97d..0000000 --- a/src/test/java/tests/LoginTest.java +++ /dev/null @@ -1,13 +0,0 @@ -package tests; - -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.openqa.selenium.WebDriver; -import org.openqa.selenium.chrome.ChromeDriver; - -public class LoginTest { - @Test - void shouldRedirectToMyAccountPageWhenCorrectCredentialsAreUsed() { - // implementacja testu (korzystamy z LoginPage oraz MyAccountPage) - } -} diff --git a/src/test/java/tests/LoginTests.java b/src/test/java/tests/LoginTests.java new file mode 100644 index 0000000..47bfcf5 --- /dev/null +++ b/src/test/java/tests/LoginTests.java @@ -0,0 +1,36 @@ +package tests; + +import org.junit.jupiter.api.Test; +import pageobjects.BasePage; +import pageobjects.HomePage; +import pageobjects.LoginPage; + +public class LoginTests extends BaseTests { + @Test + void clickSignInButtonAndGoLoginForm() { + HomePage homePage = new HomePage(driver, wait); + homePage.openPage(); // Open main Page + + BasePage basePage = new BasePage(driver, wait); + basePage.clickSignIn(); + + LoginPage loginPage = new LoginPage(driver, wait); + loginPage.signInToRegisteredAccount(); + + loginPage.signInToNotRegisteredAccount(); + } + + // negative test: signInToNotRegisteredAccount + @Test + void clickSignInButtonAndGoToLoginFormNegative() { + HomePage homePage = new HomePage(driver, wait); + homePage.openPage(); // Open main Page + + BasePage basePage = new BasePage(driver, wait); + basePage.clickSignIn(); + + LoginPage loginPage = new LoginPage(driver, wait); + loginPage.signInToNotRegisteredAccount(); + // assertion + } +} \ No newline at end of file diff --git a/src/test/java/tests/RegisterTests.java b/src/test/java/tests/RegisterTests.java new file mode 100644 index 0000000..19bd7ab --- /dev/null +++ b/src/test/java/tests/RegisterTests.java @@ -0,0 +1,24 @@ +package tests; + +import org.junit.jupiter.api.Test; +import pageobjects.HomePage; +import pageobjects.LoginPage; +import pageobjects.RegisterPage; +import utils.RandomUser; + +public class RegisterTests extends BaseTests { + @Test + void shouldRegisterNewUserWhenAllMandatoryDataIsProvided() throws InterruptedException { // positive registration + RandomUser user = new RandomUser(); + System.out.println(user); // we print used user to test. It's needed as log for tests + + HomePage homePage = new HomePage(driver, wait); + homePage.openPage(); // Open main Page + + LoginPage loginPage = new LoginPage(driver, wait); + loginPage.goToRegisterForm(); + + RegisterPage registerPage = new RegisterPage(driver, wait); + registerPage.registerUser(user); + } +} \ No newline at end of file diff --git a/src/test/java/tests/SearchResultTests.java b/src/test/java/tests/SearchResultTests.java new file mode 100644 index 0000000..c0ac2b7 --- /dev/null +++ b/src/test/java/tests/SearchResultTests.java @@ -0,0 +1,24 @@ +package tests; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import pageobjects.HomePage; +import pageobjects.SearchResultPage; + +// extends mean that we load every elements from class BaseTests to class SearchTests +public class SearchResultTests extends BaseTests { + @Test + void shouldReturnCorrectProductListWhenPositiveSearchPhraseIsUsed() { + // because our method from class HomePage are not static so we must create object + HomePage homePage = new HomePage(driver, wait); + homePage.openPage(); // Open main Page + homePage.searchForProduct("dress"); // give searching keyword in search box + // every open new page we need to create new object of this page SearchResultPage searchResultPage = new SearchResultPage(driver, wait); + SearchResultPage searchResultPage = new SearchResultPage(driver, wait); + // here we have object from class SearchResultPage so now we can execute on this object some methods + // adding assertion to check if search result is correct (shown find product names) + + Assertions.assertTrue(searchResultPage.isProductWithNameVisible("dress")); + Assertions.assertEquals("7 results have been found.", searchResultPage.getSearchSummary()); + } +} \ No newline at end of file diff --git a/src/test/java/tests/SearchTest.java b/src/test/java/tests/SearchTest.java deleted file mode 100644 index 3d47a81..0000000 --- a/src/test/java/tests/SearchTest.java +++ /dev/null @@ -1,22 +0,0 @@ -package tests; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; -import org.openqa.selenium.By; -import org.openqa.selenium.Keys; -import pageobjects.HomePage; -import pageobjects.SearchResultPage; - -public class SearchTest extends BaseTest { - - @Test - void shouldReturnCorrectProductListWhenPositiveSearchPhraseIsUsed() { - HomePage homePage = new HomePage(driver); - homePage.openPage(); - homePage.searchForProduct("dress"); - - SearchResultPage searchResultPage = new SearchResultPage(driver); - Assertions.assertTrue(searchResultPage.isProductWithNameVisible("dress")); - Assertions.assertEquals("7 results have been found.", searchResultPage.getSearchSummary()); - } -} diff --git a/src/test/java/utils/RandomUser.java b/src/test/java/utils/RandomUser.java new file mode 100644 index 0000000..990bffd --- /dev/null +++ b/src/test/java/utils/RandomUser.java @@ -0,0 +1,60 @@ +package utils; + +import com.github.javafaker.Faker; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.FindBy; + +import java.util.Random; + +public class RandomUser { + @FindBy(id = "id_gender1") + WebElement CustomerGender1; + + @FindBy(id = "id_gender2") + WebElement CustomerGender2; + + // we create pools we need to registration + public String firstName; + public String lastName; + public String email; + public String password; + public String address1; + public String phoneNumber; + public String city; + public String zipCode; + public int dayOfBirth; + public int monthOfBirth; + public int yearOfBirth; + + public RandomUser() { + Faker faker = new Faker(); + firstName = faker.name().firstName(); + lastName = faker.name().lastName(); + yearOfBirth = faker.random().nextInt(1910, 2020); + email = firstName + lastName + yearOfBirth + "@gmail.com"; + zipCode = faker.random().nextInt(10000, 99999).toString(); + password = faker.pokemon().name() + yearOfBirth; + phoneNumber = faker.numerify("#########"); + city = faker.address().city(); + } + + @Override + public String toString() { + return "RandomUser{" + + "firstName='" + firstName + '\'' + + ", lastName='" + lastName + '\'' + + ", email='" + email + '\'' + + ", password='" + password + '\'' + + ", address1='" + address1 + '\'' + + ", zipCode=" + zipCode + + ", dayOfBirth=" + dayOfBirth + + ", monthOfBirth=" + monthOfBirth + + ", yearOfBirth=" + yearOfBirth + + '}'; + } + + public static void main(String[] args) { + RandomUser user = new RandomUser(); + System.out.println(user); + } +} \ No newline at end of file