Commit 56f1aa38 authored by s9_bulank_k's avatar s9_bulank_k
Browse files

homework

parents
Pipeline #1265 failed with stages
# Created by .ignore support plugin (hsz.mobi)
.idea/
web-automation-basic.iml
target/
build/
.project
.factorypath
.classpath
.settings
allure-results/
allure-report/
.DS_STORE
.vscode
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.testdevlab.web.automation</groupId>
<artifactId>web-automation-basic</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>3.0.0-M3</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<dependencies>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>3.0.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-junit -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>3.0.2</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.codeborne/selenide -->
<dependency>
<groupId>com.codeborne</groupId>
<artifactId>selenide</artifactId>
<version>5.2.4</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.12.2</version>
<scope>compile</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/datatable -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>datatable</artifactId>
<version>1.0.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-core -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-core</artifactId>
<version>3.0.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-api -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-api</artifactId>
<version>3.141.59</version>
</dependency>
</dependencies>
</project>
package general;
import com.codeborne.selenide.Condition;
import com.codeborne.selenide.SelenideElement;
public class Loading {
// Methods
public static void waitUntilDisplayed(SelenideElement elem, Integer time){
elem.waitUntil(Condition.visible, time * 1000);
}
public static Boolean isDisplayed(SelenideElement elem, Integer time){
try{
waitUntilDisplayed(elem, time);
return true;
}catch (Error e){
return false;
}
}
}
package general;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
public class Logger {
// Variables
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
private final String INFO = "[INFO]";
// Methods
public void info(String str){
output(infoContent(str));
}
public void compare(List<String> expected, List<String> actual) {
output(compareContent(expected, actual));
}
// Private Methods
private void output(String str){
System.out.println(str);
}
private String getTime(){
String date = sdf.format(new Date());
return "[" + date + "]";
}
private String infoContent(String str){
String INFO_ = INFO + ": ";
return getTime() + INFO_ + str;
}
private String compareContent(List<String> expected, List<String> actual){
String comparingStr = getTime() + INFO + "[COMPARING]";
String expectedStr = getTime() + INFO + "[EXPECTED]: " + expected.toString();
String actualStr = getTime() + INFO + "[ACTUAL] : " + actual.toString();
return comparingStr + "\n" + expectedStr + "\n" + actualStr;
}
}
package general;
import pages.PageManager;
import static com.codeborne.selenide.Selenide.open;
public class Navigation {
// Variables
public static final String HOME_PAGE = "http://localhost:3000/";
public static final String LOGIN_PAGE = HOME_PAGE + "#/login";
// Methods
public static void navigator(String page) {
switch (page.toLowerCase()) {
case "home":
navigateToHomePage();
break;
case "login":
navigateToLoginPage();
break;
default: throw new Error("Given page '" + page + "' is undefined");
}
}
public static void navigateToHomePage(){
open(HOME_PAGE);
PageManager.setCurrentPage(PageManager.getHomePage());
}
public static void navigateToLoginPage(){
open(LOGIN_PAGE);
PageManager.setCurrentPage(PageManager.getLoginPage());
}
}
package pages;
import pages.pageLib.*;
public class PageManager {
// Variables
private static BasePage currentPage = null;
private static HomePage HOME_PAGE = new HomePage();
private static HomePageLoggedIn HOME_PAGE_LOGGED_IN = new HomePageLoggedIn();
private static SearchResultsPage SEARCH_RESULTS_PAGE = new SearchResultsPage();
private static LoginPage LOGIN_PAGE = new LoginPage();
private static RegistrationPage registrationPage = new RegistrationPage();
private static BasketPage basketPage = new BasketPage();
private static SelectAddressPage selectAddressPage = new SelectAddressPage();
private static DeliveryPage deliveryPage = new DeliveryPage();
private static PaymentOptionsPage paymentOptionsPage = new PaymentOptionsPage();
private static OrderSummaryPage orderSummaryPage = new OrderSummaryPage();
private static ChangePasswordPage changePasswordPage = new ChangePasswordPage();
private static CreateAddressPage createAddressPage = new CreateAddressPage();
// Methods
public static void setCurrentPage(BasePage page){
currentPage = page;
PageManager.getCurrentPage().validatePage();
}
public static BasePage getCurrentPage(){
return currentPage;
}
// Getters & setters
public static HomePage getHomePage() {
return HOME_PAGE;
}
public static HomePageLoggedIn getHomePageLoggedIn() {
return HOME_PAGE_LOGGED_IN;
}
public static SearchResultsPage getSearchResultsPage() {
return SEARCH_RESULTS_PAGE;
}
public static LoginPage getLoginPage() {
return LOGIN_PAGE;
}
public static RegistrationPage getRegistrationPage() {
return registrationPage;
}
public static BasketPage getBasketPage() {
return basketPage;
}
public static SelectAddressPage getSelectAddressPage() {
return selectAddressPage;
}
public static DeliveryPage getDeliveryPage() {
return deliveryPage;
}
public static PaymentOptionsPage getPaymentOptionsPage() {
return paymentOptionsPage;
}
public static OrderSummaryPage getOrderSummaryPage() {
return orderSummaryPage;
}
public static ChangePasswordPage getChangePasswordPage(){
return changePasswordPage;
}
public static CreateAddressPage getCreateAddressPage(){
return createAddressPage;
}
}
package pages.pageLib;
import com.codeborne.selenide.SelenideElement;
import general.Loading;
import general.Logger;
import io.cucumber.datatable.DataTable;
import java.util.HashMap;
import java.util.List;
import static com.codeborne.selenide.Selectors.byXpath;
import static com.codeborne.selenide.Selenide.$;
import static org.assertj.core.api.Assertions.assertThat;
public class BasePage {
// Variables
private final Integer defaultTimeout = 10;
private final Logger logger = new Logger();
private HashMap<String, String> elementCollection;
// Constructor
public BasePage() {
elementCollection = new HashMap<String, String>();
}
// General methods
public void click(String element) {
String capElement = capitalizeSecond(element);
getElement(capElement).click();
info("Clicking " + capElement);
navigate(capElement);
}
public void doAction(String action, DataTable dataTable) {
throw new Error(action + " is undefined");
}
public void doAction(String action, String data) {
throw new Error(action + " is undefined");
}
public void validatePage() {
info("Validating page: " + this.getClass());
assertThat(isDisplayed(getMainElement(), getTimeout())).isTrue();
}
public void clickText(String text) {
getTextElement(text).click();
}
public Boolean isTextVisible(String text) {
return getTextElement(text).isDisplayed();
}
public SelenideElement getTextElement(String text) {
return $(byXpath("//*[contains(text(), '" + text + "')]"));
}
public void addElement(String key, String path){
elementCollection.put(key, path);
}
public SelenideElement getElement(String element){
info("Getting: " + element);
String path = elementCollection.get(element);
if(path == null){
throw new Error("Element: " + element + " is undefined");
}else {
int count = 0;
int maxTries = 3;
while(true) {
try {
if(path.contains("//")){
return $(byXpath(path));
}else {
return $(path);
}
} catch (Error e) {
if (++count == maxTries) throw new Error("Failed to get " + element);
}
}
}
}
public void validateFields(DataTable dataTable) {
info("Validating fields: " + dataTable.asList());
for(String element : dataTable.asList()){
assertThat(isDisplayed(getElement(capitalizeSecond(element)), getTimeout()));
}
}
public Integer getTimeout(){
return defaultTimeout;
}
public void setValue(String element, String value){
String capElement = capitalizeSecond(element);
getElement(capElement).sendKeys(value);
info("Setting value of " + capElement + " to " + value);
}
public void validateText(String text){
info("Validating: " + text);
assertThat(isDisplayed(getTextElement(text), 10)).isTrue();
}
// Overridable methods
public void navigate(String element){}
public SelenideElement getMainElement(){
throw new Error("getMainElement is undefined for " + this.getClass());
}
// Helper methods
public void info(String str){
logger.info(str);
}
public void compare(List<String> expected, List<String> actual) {
logger.compare(expected, actual);
}
public String capitalize(String words){
words = words.toLowerCase().
replaceAll("[^a-zA-Z0-9]", " ").
replaceAll("\\s+"," ");
StringBuilder capitalized = new StringBuilder();
for(String word: words.split(" ")){
capitalized.append(Character.toUpperCase(word.charAt(0))).append(word.substring(1)).append(" ");
}
return capitalized.toString().replaceAll("\\s+","");
}
public String capitalizeSecond(String words){
String str = capitalize(words);
return Character.toLowerCase(str.charAt(0)) + str.substring(1);
}
public Boolean isDisplayed(SelenideElement element, Integer time){
if (Loading.isDisplayed(element, time)){
return true;
} else {
return false;
}
}
}
package pages.pageLib;
import com.codeborne.selenide.SelenideElement;
import pages.PageManager;
public class BasketPage extends HomePageLoggedIn {
// Constructor
public BasketPage(){
addElement("checkoutButton", "button[id='checkoutButton']");
}
// Methods
@Override
public SelenideElement getMainElement() {
return getElement("checkoutButton");
}
@Override
public void navigate(String element) {
switch (element){
case "checkoutButton":
PageManager.setCurrentPage(PageManager.getSelectAddressPage()); break;
}
}
}
package pages.pageLib;
import com.codeborne.selenide.SelenideElement;
import pages.PageManager;
public class ChangePasswordPage extends BasePage {
//Constructor
public ChangePasswordPage() {
addElement("title", "//h1[contains(., 'Change Password')]");
addElement("currentPasswordField", "input[aria-label*='Field to enter the current password']");
addElement("newPasswordField", "input[aria-label*='Field for the new password']");
addElement("repeatNewPasswordField", "input[aria-label*='Field to repeat the new password']");
addElement("change", "button[aria-label*='Button to confirm the change']");
addElement("accountButton", "button[aria-label*='Show/hide account menu']");
addElement("logout", "button[aria-label*='Logout']");
}
@Override
public SelenideElement getMainElement() {
return getElement("title");
}
@Override
public void navigate(String element) {
switch (element){
case "logout":
PageManager.setCurrentPage(PageManager.getHomePage()); break;
}
}
}
\ No newline at end of file
package pages.pageLib;
import java.util.Map;
import com.codeborne.selenide.SelenideElement;
import io.cucumber.datatable.DataTable;
import pages.PageManager;
import static org.assertj.core.api.Assertions.assertThat;
public class CreateAddressPage extends BasePage {
public CreateAddressPage(){
addElement("title", "//h1[contains(., 'Add New Address')]");
addElement("country", "input[placeholder='Please provide a country.']");
addElement("name", "input[placeholder='Please provide a name.']");
addElement("mobileNumber", "input[placeholder='Please provide a mobile number.']");
addElement("zipCode", "input[placeholder='Please provide a ZIP code.']");
addElement("address", "textarea[placeholder='Please provide an address.']");
addElement("city", "input[placeholder='Please provide a city.']");
addElement("state", "input[placeholder='Please provide a state.']");
addElement("submit", "button[id='submitButton']");
}
@Override
public SelenideElement getMainElement() {
return getElement("title");
}
public void doAction(String action, DataTable dataTable) {
switch(capitalizeSecond(action)){
case "validateAddress": validateAddress(dataTable); break;
default: super.doAction(action, dataTable);
}
}
public void validateAddress(DataTable dataTable){
Map<String,String> data = dataTable.transpose().asMaps().get(0);
info("Validating Address: " + data);
if(data.get("name") != null){
assertThat(isTextVisible(data.get("name"))).isTrue();
}
if(data.get("address") != null){
assertThat(isTextVisible(data.get("address"))).isTrue();
}
if(data.get("country") != null){
assertThat(isTextVisible(data.get("country"))).isTrue();
}
}
}
\ No newline at end of file
package pages.pageLib;
import com.codeborne.selenide.SelenideElement;
import pages.PageManager;
public class DeliveryPage extends SelectAddressPage {
// Constructor
public DeliveryPage(){
addElement("expectedDelivery", "mat-header-cell[translate='LABEL_EXPECTED_DELIVERY']");
addElement("oneDayDelivery", "//mat-row[contains(., 'One Day Delivery')]//mat-radio-button");
addElement("standardDelivery", "//mat-row[contains(., 'Standard Delivery')]//mat-radio-button");
addElement("continue", "button[aria-label='Proceed to delivery method selection']");
}
// Methods
@Override
public SelenideElement getMainElement() {
return getElement("expectedDelivery");
}
@Override
public void navigate(String element) {
switch (element){
case "continue": PageManager.setCurrentPage(PageManager.getPaymentOptionsPage()); break;
}
}
}
package pages.pageLib;
import com.codeborne.selenide.SelenideElement;
import org.openqa.selenium.Keys;
import pages.PageManager;
public class HomePage extends BasePage {
// Constructor
public HomePage(){
addElement("title", "div[class*='heading'] div[class='ng-star-inserted']");
addElement("loginButton", "button[aria-label*='Go to login page']");
addElement("searchField", "input[class*='mat-input-element']");
addElement("searchButton", "mat-search-bar[aria-label*='Click to search']");
addElement("accountButton", "button[aria-label*='Show/hide account menu']");
addElement("dismissButton", "button[aria-label*='Close Welcome Banner']");
addElement("meWantIt", "a[aria-label='dismiss cookie message']");
addElement("snackbarButton", "simple-snack-bar button");
addElement("chooseLanguageButton", "button[aria-label='Language selection menu']");
}
// Methods
@Override
public void doAction(String action, String data) {
switch(capitalizeSecond(action)){
case "searchFor": searchFor(data); break;
default : super.doAction(action, data);
}
}
public void searchFor(String string) {
info("Searching for '" + string + "'");
getElement("searchButton").click();
getElement("searchField").sendKeys(string);
getElement("searchField").sendKeys(Keys.ENTER);
PageManager.setCurrentPage(PageManager.getSearchResultsPage());
}
public void navigate(String element) {
switch (element){
case "loginButton": PageManager.setCurrentPage(PageManager.getLoginPage());
}
}
public SelenideElement getMainElement(){
return getElement("title");
}
}
package pages.pageLib;
import java.util.Map;
import com.codeborne.selenide.SelenideElement;
import io.cucumber.datatable.DataTable;
import pages.PageManager;
import static org.assertj.core.api.Assertions.assertThat;
public class HomePageLoggedIn extends HomePage {
// Constructor
public HomePageLoggedIn(){
addElement("logoutButton", "button[aria-label='Logout']");
addElement("userProfileButton", "button[aria-label='Go to user profile'] span");
addElement("shoppingCartButton", "button[aria-label='Show the shopping cart']");
addElement("ordersPaymentButton", "button[aria-label='Show Orders and Payment Menu']");
addElement("privacySecurityButton", "button[aria-label='Show Privacy and Security Menu']");
addElement("addToBasket", "button[aria-label='Add to Basket']");
addElement("changePassword", "button[aria-label='Go to change password page']");
}
// Methods
public SelenideElement getMainElement(){