-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement an API for setting and validating state
- Loading branch information
1 parent
31ee1a6
commit 07ed400
Showing
10 changed files
with
143 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/test/java/com/saucelabs/advancedselenium/saucedemo/apis/AuthenticateAPI.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.saucelabs.advancedselenium.saucedemo.apis; | ||
|
||
import com.saucelabs.advancedselenium.saucedemo.Browser; | ||
import com.saucelabs.advancedselenium.saucedemo.data.User; | ||
|
||
public class AuthenticateAPI extends BaseAPI { | ||
public AuthenticateAPI(Browser browser) { | ||
super(browser); | ||
} | ||
|
||
public void authenticate() { | ||
addCookie("session-username", User.valid().getUsername()); | ||
} | ||
|
||
public Boolean isAuthenticated() { | ||
return getCookie("session-username") != null; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/test/java/com/saucelabs/advancedselenium/saucedemo/apis/BaseAPI.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.saucelabs.advancedselenium.saucedemo.apis; | ||
|
||
import org.openqa.selenium.Cookie; | ||
import org.openqa.selenium.html5.LocalStorage; | ||
import org.openqa.selenium.remote.RemoteExecuteMethod; | ||
import org.openqa.selenium.remote.html5.RemoteWebStorage; | ||
import com.saucelabs.advancedselenium.saucedemo.Browser; | ||
|
||
public class BaseAPI { | ||
protected final Browser browser; | ||
|
||
public BaseAPI(Browser browser) { | ||
this.browser = browser; | ||
} | ||
|
||
protected void addCookie(String name, String value) { | ||
browser.addCookie(name, value); | ||
} | ||
|
||
protected Cookie getCookie(String name) { | ||
return browser.getCookie(name); | ||
} | ||
|
||
protected LocalStorage getLocalStorage() { | ||
RemoteExecuteMethod executeMethod = new RemoteExecuteMethod(browser.getDriver()); | ||
RemoteWebStorage webStorage = new RemoteWebStorage(executeMethod); | ||
return webStorage.getLocalStorage(); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/test/java/com/saucelabs/advancedselenium/saucedemo/apis/CartAPI.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.saucelabs.advancedselenium.saucedemo.apis; | ||
|
||
import com.saucelabs.advancedselenium.saucedemo.Browser; | ||
|
||
import java.util.Arrays; | ||
|
||
public class CartAPI extends BaseAPI { | ||
public CartAPI(Browser browser) { | ||
super(browser); | ||
} | ||
|
||
public Integer getItemCount() { | ||
String item = getLocalStorage().getItem("cart-contents"); | ||
if (item == null || "[]".equals(item)) { | ||
return 0; | ||
} | ||
return Arrays.asList(item.substring(1, item.length() - 1).split(",")).size(); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/test/java/com/saucelabs/advancedselenium/saucedemo/apis/InventoryAPI.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.saucelabs.advancedselenium.saucedemo.apis; | ||
|
||
import com.saucelabs.advancedselenium.saucedemo.Browser; | ||
|
||
import java.util.Random; | ||
|
||
public class InventoryAPI extends BaseAPI{ | ||
public InventoryAPI(Browser browser) { | ||
super(browser); | ||
} | ||
|
||
public void addItemSuccessfully() { | ||
int productID = new Random().nextInt(5); | ||
getLocalStorage().setItem("cart-contents", "[" + String.valueOf(productID) + "]"); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/test/java/com/saucelabs/advancedselenium/saucedemo/apis/SauceDemoAPIFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.saucelabs.advancedselenium.saucedemo.apis; | ||
|
||
import com.saucelabs.advancedselenium.saucedemo.Browser; | ||
|
||
public class SauceDemoAPIFactory { | ||
private final Browser browser; | ||
private AuthenticateAPI authenticateAPI; | ||
private InventoryAPI inventoryAPI; | ||
private CartAPI cartAPI; | ||
|
||
public SauceDemoAPIFactory(Browser browser) { | ||
this.browser = browser; | ||
} | ||
|
||
public AuthenticateAPI getAuthenticateAPI() { | ||
if (this.authenticateAPI == null) { | ||
this.authenticateAPI = new AuthenticateAPI(browser); | ||
} | ||
return this.authenticateAPI; | ||
} | ||
|
||
public InventoryAPI getInventoryAPI() { | ||
if (this.inventoryAPI == null) { | ||
this.inventoryAPI = new InventoryAPI(browser); | ||
} | ||
return this.inventoryAPI; | ||
} | ||
|
||
public CartAPI getCartAPI() { | ||
if (this.cartAPI == null) { | ||
this.cartAPI = new CartAPI(browser); | ||
} | ||
return this.cartAPI; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters