Skip to content

Commit f4f5c38

Browse files
Abhi Singhfrancisf
authored andcommitted
added BstackDemo test
1 parent 1bb065c commit f4f5c38

File tree

5 files changed

+71
-43
lines changed

5 files changed

+71
-43
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.browserstack.pageobjects;
2+
3+
import org.openqa.selenium.By;
4+
import org.openqa.selenium.WebDriver;
5+
import org.openqa.selenium.support.ui.ExpectedConditions;
6+
import org.openqa.selenium.support.ui.WebDriverWait;
7+
8+
public class HomePage {
9+
private WebDriver webDriver;
10+
11+
private String selectedProductName;
12+
13+
public HomePage(WebDriver webDriver) {
14+
this.webDriver = webDriver;
15+
this.selectedProductName = "";
16+
}
17+
18+
private By firstProductName = By.xpath("//*[@id=\"1\"]/p");
19+
20+
private By firstProductAddToCartButton = By.xpath("//*[@id=\"1\"]/div[4]");
21+
22+
private By cartPane = By.className("float-cart__content");
23+
24+
private By productCartText = By.xpath("//*[@id=\"__next\"]/div/div/div[2]/div[2]/div[2]/div/div[3]/p[1]");
25+
26+
public void selectFirstProductName() {
27+
String firstProduct = webDriver.findElement(firstProductName).getText();
28+
setSelectedProductName(firstProduct);
29+
}
30+
31+
public void clickAddToCartButton() {
32+
webDriver.findElement(firstProductAddToCartButton).click();
33+
}
34+
35+
public void waitForCartToOpen() {
36+
new WebDriverWait(webDriver,30).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(cartPane));
37+
}
38+
39+
public String getProductCartText() {
40+
return webDriver.findElement(productCartText).getText();
41+
}
42+
43+
public void setSelectedProductName(String selectedProductName) {
44+
this.selectedProductName = selectedProductName;
45+
}
46+
47+
public String getSelectedProductName() {
48+
return selectedProductName;
49+
}
50+
}

src/test/java/com/browserstack/pageobjects/SearchPage.java

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/test/java/com/browserstack/stepdefs/SearchSteps.java renamed to src/test/java/com/browserstack/stepdefs/StackDemoSteps.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import com.browserstack.TestRunner;
44
import com.browserstack.local.Local;
5-
import com.browserstack.pageobjects.SearchPage;
5+
import com.browserstack.pageobjects.HomePage;
66
import com.browserstack.util.Utility;
77
import io.cucumber.java.After;
88
import io.cucumber.java.Before;
@@ -22,9 +22,9 @@
2222
import static org.junit.Assert.assertEquals;
2323
import static org.junit.Assert.assertTrue;
2424

25-
public class SearchSteps {
25+
public class StackDemoSteps {
2626
private WebDriver driver;
27-
private SearchPage searchPage;
27+
private HomePage homePage;
2828
private Local l;
2929

3030
@Before
@@ -36,13 +36,13 @@ public void setUp(Scenario scenario) throws Exception {
3636
caps.setCapability("name", scenario.getName());
3737
if (caps.getCapability("browserstack.local")!=null && caps.getCapability("browserstack.local").toString().equals("true")) {
3838
l = new Local();
39-
Map<String, String> options = new HashMap<String, String>();
39+
Map<String, String> options = new HashMap<>();
4040
options.put("key", caps.getCapability("browserstack.key").toString());
4141
l.start(options);
4242
}
4343

4444
driver = new RemoteWebDriver(new URL(URL), caps);
45-
searchPage = new SearchPage(driver);
45+
homePage = new HomePage(driver);
4646
}
4747

4848
@Given("^I am on the website '(.+)'$")
@@ -51,20 +51,21 @@ public void I_am_on_the_website(String url) throws Throwable {
5151
Thread.sleep(2000);
5252
}
5353

54-
@When("^I submit the search term '(.+)'$")
55-
public void I_submit_the_search_term(String searchTerm) throws Throwable {
56-
searchPage.enterSearchTerm(searchTerm);
57-
searchPage.submitSearch();
54+
@When("^I select a product and click on 'Add to cart' button")
55+
public void I_select_a_product_and_add_to_cart() throws Throwable {
56+
homePage.selectFirstProductName();
57+
homePage.clickAddToCartButton();
5858
Thread.sleep(2000);
5959
}
6060

61-
@Then("the page title should be '(.+)'$")
62-
public void I_should_see_pagetitle(String expectedTitle) throws Throwable {
63-
assertEquals(expectedTitle, driver.getTitle());
61+
@Then("the product should be added to cart")
62+
public void product_should_be_added_to_cart() {
63+
homePage.waitForCartToOpen();
64+
assertEquals(homePage.getSelectedProductName(), homePage.getProductCartText());
6465
}
6566

6667
@Then("the page should contain '(.+)'$")
67-
public void page_should_contain(String expectedTitle) throws Throwable {
68+
public void page_should_contain(String expectedTitle) {
6869
assertTrue(driver.getPageSource().contains(expectedTitle));
6970
}
7071

@@ -79,4 +80,4 @@ public void teardown(Scenario scenario) throws Exception {
7980
driver.quit();
8081
if (l != null) l.stop();
8182
}
82-
}
83+
}

src/test/resources/features/test/Search.feature

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Feature: BrowserStack Demo
2+
3+
Scenario: Add product to cart
4+
Given I am on the website 'https://www.bstackdemo.com'
5+
When I select a product and click on 'Add to cart' button
6+
Then the product should be added to cart

0 commit comments

Comments
 (0)