From 8049fd99877e114a28ae8e060b1e9d3a7ad1be38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20M=C3=BCller?= Date: Sat, 26 Jun 2021 22:31:08 +0200 Subject: [PATCH] Adding some more cucumber tests --- .../test/java/stepdefs/FavoriteShopList.java | 15 ++-- .../src/test/java/stepdefs/Preconditions.java | 1 + .../src/test/java/stepdefs/PriceAlarm.java | 53 ++++++++------ .../src/test/java/stepdefs/SearchProduct.java | 72 +++++++++++++++---- .../test/resource/favoriteShopList.feature | 8 +-- .../src/test/resource/priceAlarms.feature | 15 ++-- .../src/test/resource/searchProduct.feature | 2 +- 7 files changed, 113 insertions(+), 53 deletions(-) diff --git a/CucumberTests/src/test/java/stepdefs/FavoriteShopList.java b/CucumberTests/src/test/java/stepdefs/FavoriteShopList.java index fa94b68..6ec2cd7 100644 --- a/CucumberTests/src/test/java/stepdefs/FavoriteShopList.java +++ b/CucumberTests/src/test/java/stepdefs/FavoriteShopList.java @@ -3,22 +3,29 @@ package stepdefs; import io.cucumber.java.en.Given; import io.cucumber.java.en.Then; import io.cucumber.java.en.When; +import org.openqa.selenium.By; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.ui.ExpectedConditions; +import org.openqa.selenium.support.ui.WebDriverWait; public class FavoriteShopList { @Given("^the user has at least (\\d+) favorite shop$") public void the_user_has_at_least_favorite_shop(int arg1) throws Exception { } - @When("^the user clicks on favorite shops$") - public void the_user_clicks_on_favorite_shops() throws Exception { + @Then("^the profile page should open$") + public void the_profile_page_should_open() throws Exception { + WebElement profile_info_text = (new WebDriverWait(Preconditions.driver, Preconditions.delaySeconds)) + .until(ExpectedConditions.elementToBeClickable(By.cssSelector("table.table.table-hover"))); + assert(profile_info_text.isDisplayed()); } @Then("^he should see his favorite shops list$") - public void he_Should_see_his_favorite_shops_list() throws Exception { + public void he_should_see_his_favorite_shops_list() throws Exception { } @When("^he clicks on delete a favorite shop entry$") - public void he_clicks_on_delete_a_favorite_shop_enty() throws Exception { + public void he_clicks_on_delete_a_favorite_shop_entry() throws Exception { } @Then("^the favorite shop entry should be deleted$") diff --git a/CucumberTests/src/test/java/stepdefs/Preconditions.java b/CucumberTests/src/test/java/stepdefs/Preconditions.java index f9c22b1..37acde2 100644 --- a/CucumberTests/src/test/java/stepdefs/Preconditions.java +++ b/CucumberTests/src/test/java/stepdefs/Preconditions.java @@ -4,4 +4,5 @@ import org.openqa.selenium.WebDriver; public class Preconditions { public static WebDriver driver; + public static final int delaySeconds = 7; } diff --git a/CucumberTests/src/test/java/stepdefs/PriceAlarm.java b/CucumberTests/src/test/java/stepdefs/PriceAlarm.java index 83c332e..41696ca 100644 --- a/CucumberTests/src/test/java/stepdefs/PriceAlarm.java +++ b/CucumberTests/src/test/java/stepdefs/PriceAlarm.java @@ -4,6 +4,12 @@ import io.cucumber.java.PendingException; import io.cucumber.java.en.Given; import io.cucumber.java.en.Then; import io.cucumber.java.en.When; +import org.openqa.selenium.By; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.ui.ExpectedConditions; +import org.openqa.selenium.support.ui.WebDriverWait; + +import java.util.List; public class PriceAlarm { @Given("^the user has at least (\\d+) price alarm set$") @@ -12,42 +18,43 @@ public class PriceAlarm { @When("^the user clicks on the profile icon$") public void the_user_clicks_on_the_profile_icon() throws Exception { - } - - @Then("^the profile details popup should open$") - public void the_profile_details_popup_should_open() throws Exception { - } - - @When("^the user clicks on price alarms$") - public void the_user_clicks_on_price_alarms() throws Exception { - } - - @Then("^the price alarm list should open$") - public void the_price_alarm_list_should_open() throws Exception { + WebElement profileButton = (new WebDriverWait(Preconditions.driver, Preconditions.delaySeconds)) + .until(ExpectedConditions.elementToBeClickable(By.xpath("//*[contains(text(),'profile')]"))); + profileButton.click(); } @Then("^the price alarm list should contain at least (\\d+) entry$") public void the_price_alarm_list_should_contain_at_least_entry(int arg1) throws Exception { + WebElement alarmEntry = (new WebDriverWait(Preconditions.driver, Preconditions.delaySeconds)) + .until(ExpectedConditions.elementToBeClickable(By.cssSelector("table.table.table-hover tr:nth-child(2)"))); + + assert (alarmEntry != null); } - @Then("^the price alarm list should contain a maximum of (\\d+) entries per page$") - public void the_price_alarm_list_should_contain_a_maximum_of_entries_per_page(int arg1) throws Exception { - } + @Given("^the user is on the profile page$") + public void the_user_is_on_the_profile_page() throws Exception { + Preconditions.driver.get("https://www.betterzon.xyz/profile"); - @Given("^the user is on the price alarm list page$") - public void the_user_is_on_the_price_alarm_list_page() throws Exception { + WebElement profile_info_text = (new WebDriverWait(Preconditions.driver, Preconditions.delaySeconds)) + .until(ExpectedConditions.elementToBeClickable(By.cssSelector("table.table.table-user-information"))); + assert (profile_info_text.isDisplayed()); } @When("^the user clicks on the \"([^\"]*)\" button next to a price alarm$") public void the_user_clicks_on_the_button_next_to_a_price_alarm(String arg1) throws Exception { - } + if (arg1.equals("remove")) { + WebElement entry = (new WebDriverWait(Preconditions.driver, Preconditions.delaySeconds)) + .until(ExpectedConditions.elementToBeClickable(By.cssSelector("table.table.table-hover tr:nth-child(2)"))); - @Then("^a popup should open asking the user to confirm the removal$") - public void a_popup_should_open_asking_the_user_to_confirm_the_removal() throws Exception { - } + if (entry == null) { + throw new Exception("Too few price alarm entries found!"); + } - @When("^the user confirms the removal of the price alarm$") - public void the_user_confirms_the_removal_of_the_price_alarm() throws Exception { + WebElement btn = entry.findElement(By.cssSelector("img.delete[src='../assets/images/Delete_icon-icons.com_55931.png']")); + + btn.click(); + } else if (arg1.equals("edit")) { + } } @Then("^the price alarm should be removed from the database$") diff --git a/CucumberTests/src/test/java/stepdefs/SearchProduct.java b/CucumberTests/src/test/java/stepdefs/SearchProduct.java index 73ab146..07a404b 100644 --- a/CucumberTests/src/test/java/stepdefs/SearchProduct.java +++ b/CucumberTests/src/test/java/stepdefs/SearchProduct.java @@ -4,9 +4,7 @@ import io.cucumber.java.PendingException; import io.cucumber.java.en.Given; import io.cucumber.java.en.Then; import io.cucumber.java.en.When; -import org.openqa.selenium.By; -import org.openqa.selenium.Keys; -import org.openqa.selenium.WebElement; +import org.openqa.selenium.*; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; @@ -15,8 +13,8 @@ public class SearchProduct { public void the_user_is_on_the_landing_page() throws Exception { //throw new PendingException(); Preconditions.driver.get("https://betterzon.xyz"); - WebElement logo = (new WebDriverWait(Preconditions.driver, 10)) - .until(ExpectedConditions.elementToBeClickable(By.cssSelector(".navbar-brand"))); + WebElement logo = (new WebDriverWait(Preconditions.driver, Preconditions.delaySeconds)) + .until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.navbar-brand"))); } @When("^the user enters the search term \"([^\"]*)\" and clicks search$") @@ -24,49 +22,99 @@ public class SearchProduct { WebElement searchField = Preconditions.driver.findElement(By.cssSelector(".ng-untouched.ng-pristine.ng-valid")); searchField.sendKeys(searchTerm); searchField.sendKeys(Keys.ENTER); - WebElement logo = (new WebDriverWait(Preconditions.driver, 10)) + WebElement logo = (new WebDriverWait(Preconditions.driver, Preconditions.delaySeconds)) .until(ExpectedConditions.elementToBeClickable(By.cssSelector(".navbar-brand"))); } @Then("^the user should see the error page \"([^\"]*)\"$") public void the_user_should_see_the_error_page(String arg0) throws Exception { - WebElement noProdsFoundMsg = (new WebDriverWait(Preconditions.driver, 10)) - .until(ExpectedConditions.elementToBeClickable(By.cssSelector(".ng-star-inserted"))); - assert(noProdsFoundMsg.getText().contains("No Products found!")); + WebElement noProdsFoundMsg = (new WebDriverWait(Preconditions.driver, Preconditions.delaySeconds)) + .until(ExpectedConditions.elementToBeClickable(By.xpath("//*[contains(text(),'No Products found!')]"))); + assert (noProdsFoundMsg.isDisplayed()); } @Given("^the user is not logged in$") public void the_user_is_not_logged_in() throws Exception { + try { + WebElement logoutButton = (new WebDriverWait(Preconditions.driver, Preconditions.delaySeconds)) + .until(ExpectedConditions.elementToBeClickable(By.xpath("//*[contains(text(),'log out')]"))); + + logoutButton.click(); + } catch (TimeoutException e) { + + } } @Given("^the user is logged in$") public void the_user_is_logged_in() throws Exception { + try { + WebElement loginButton = (new WebDriverWait(Preconditions.driver, Preconditions.delaySeconds)) + .until(ExpectedConditions.elementToBeClickable(By.xpath("//*[contains(text(),'sign in')]"))); + loginButton.click(); + + WebElement usernameField = (new WebDriverWait(Preconditions.driver, Preconditions.delaySeconds)) + .until(ExpectedConditions.elementToBeClickable(By.id("username"))); + usernameField.sendKeys("Selenium"); + + WebElement passwordField = (new WebDriverWait(Preconditions.driver, Preconditions.delaySeconds)) + .until(ExpectedConditions.elementToBeClickable(By.id("password"))); + passwordField.sendKeys("Selenium"); + + WebElement loginBtn = (new WebDriverWait(Preconditions.driver, Preconditions.delaySeconds)) + .until(ExpectedConditions.elementToBeClickable(By.className("btn_signin"))); + loginBtn.click(); + + } catch (TimeoutException e) { + + } } @Then("^the user should see a list of products$") public void the_user_should_see_a_list_of_products() throws Exception { - WebElement product = (new WebDriverWait(Preconditions.driver, 10)) - .until(ExpectedConditions.elementToBeClickable(By.cssSelector(".productItem.ng-star-inserted"))); - assert(product.isDisplayed()); + WebElement product = (new WebDriverWait(Preconditions.driver, Preconditions.delaySeconds)) + .until(ExpectedConditions.elementToBeClickable(By.cssSelector(".row.p-2.bg-white.border.rounded"))); + assert (product.isDisplayed()); } @When("^the user clicks on the first product$") public void the_user_clicks_on_the_first_product() throws Exception { + WebElement productDetailsBtn = (new WebDriverWait(Preconditions.driver, Preconditions.delaySeconds)) + .until(ExpectedConditions.elementToBeClickable(By.cssSelector(".row.p-2.bg-white.border.rounded button.btn.btn-primary.btn-sm"))); + productDetailsBtn.click(); } @Then("^the user should see the product detail page$") public void the_user_should_see_the_product_detail_page() throws Exception { + WebElement productTitle = (new WebDriverWait(Preconditions.driver, Preconditions.delaySeconds)) + .until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.productTitle"))); + assert (productTitle.isDisplayed()); } @Then("^the set price alarm box should show \"([^\"]*)\"$") public void the_set_price_alarm_box_should_show(String arg0) throws Exception { + WebElement alarmBox = (new WebDriverWait(Preconditions.driver, Preconditions.delaySeconds)) + .until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.priceAlarm"))); + if (arg0.equals("Login to set a price alarm")) { + assert (alarmBox.getText().equals("Login to set a price alarm")); + } else { + assert (alarmBox.isDisplayed()); + } } @When("^the user sets a price alarm$") public void the_user_sets_a_price_alarm() throws Exception { + WebElement alarmBoxField = (new WebDriverWait(Preconditions.driver, Preconditions.delaySeconds)) + .until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.priceAlarm input"))); + alarmBoxField.sendKeys("12345"); + WebElement alarmBox = (new WebDriverWait(Preconditions.driver, Preconditions.delaySeconds)) + .until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.priceAlarm"))); + alarmBox.click(); + + assert (alarmBox.isDisplayed() && alarmBoxField.isDisplayed()); } @Then("^the user should receive an email confirming the price alarm$") public void the_user_should_receive_an_email_confirming_the_price_alarm() throws Exception { + assert (true); } } diff --git a/CucumberTests/src/test/resource/favoriteShopList.feature b/CucumberTests/src/test/resource/favoriteShopList.feature index 079e781..ca31dd9 100644 --- a/CucumberTests/src/test/resource/favoriteShopList.feature +++ b/CucumberTests/src/test/resource/favoriteShopList.feature @@ -5,8 +5,7 @@ Feature: Favorite Shop List And the user is logged in And the user has at least 1 favorite shop When the user clicks on the profile icon - Then the profile details popup should open - When the user clicks on favorite shops + Then the profile page should open Then he should see his favorite shops list Scenario: Remove Favorite Shop Entry @@ -14,7 +13,6 @@ Feature: Favorite Shop List And the user is logged in And the user has at least 1 favorite shop When the user clicks on the profile icon - Then the profile details popup should open - When the user clicks on favorite shops - And he clicks on delete a favorite shop entry + Then the profile page should open + When he clicks on delete a favorite shop entry Then the favorite shop entry should be deleted diff --git a/CucumberTests/src/test/resource/priceAlarms.feature b/CucumberTests/src/test/resource/priceAlarms.feature index 0550eef..11a742c 100644 --- a/CucumberTests/src/test/resource/priceAlarms.feature +++ b/CucumberTests/src/test/resource/priceAlarms.feature @@ -5,23 +5,22 @@ Feature: Price Alarms And the user is logged in And the user has at least 1 price alarm set When the user clicks on the profile icon - Then the profile details popup should open - When the user clicks on price alarms - Then the price alarm list should open + Then the profile page should open And the price alarm list should contain at least 1 entry - And the price alarm list should contain a maximum of 20 entries per page Scenario: Remove a price alarm - Given the user is on the price alarm list page + Given the user is on the landing page And the user is logged in + When the user clicks on the profile icon + Then the profile page should open When the user clicks on the "remove" button next to a price alarm - Then a popup should open asking the user to confirm the removal - When the user confirms the removal of the price alarm Then the price alarm should be removed from the database Scenario: Edit a price alarm - Given the user is on the price alarm list page + Given the user is on the landing page And the user is logged in + When the user clicks on the profile icon + Then the profile page should open When the user clicks on the "edit" button next to a price alarm Then a popup should open where the user can edit the alarm When the user clicks on the "save changes" button diff --git a/CucumberTests/src/test/resource/searchProduct.feature b/CucumberTests/src/test/resource/searchProduct.feature index e73bdc5..cd05362 100644 --- a/CucumberTests/src/test/resource/searchProduct.feature +++ b/CucumberTests/src/test/resource/searchProduct.feature @@ -12,7 +12,7 @@ Feature: Search a Product Then the user should see a list of products When the user clicks on the first product Then the user should see the product detail page - And the set price alarm box should show "Log in to continue" + And the set price alarm box should show "Login to set a price alarm" Scenario: User is logged in, searches for known product Given the user is on the landing page