Skip to content
This repository has been archived by the owner on Feb 10, 2020. It is now read-only.

Commit

Permalink
Merge pull request #4 from saucelabs-training/master_fix_04_05_19
Browse files Browse the repository at this point in the history
Master fix 04 05 19
  • Loading branch information
nadvolod authored Apr 11, 2019
2 parents 5ae77bf + 6435642 commit 17607d3
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 157 deletions.
106 changes: 10 additions & 96 deletions exercise-guides/exercise1.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,119 +78,33 @@
## Part Three: Abstract Test Details:
1. In `src/test/java/exercises/` create a new class called `LoginFeatureTest`.
2. Create a new class method with the following:
2. Create a new `@Test` class method called `ShouldBeAbleToLogin`:
```
public class LoginFeatureTest {
protected WebDriver driver;
@Test
public void ShouldBeAbleToLogin(Method method)
throws MalformedURLException
{
public void ShouldBeAbleToLogin(Method method) {
}
}
```
3. In `FullJourneyTest`, copy everything from:
`Line 21`
```
// Input your SauceLabs Credentials
```
to `Line 87`:
```
driver.findElement(By.cssSelector(loginBtn)).click();
```
and paste it into the `LoginFeatureTest` class method: `ShouldBeAbleToLogin`
4. Delete unecessary element locators such as:
```
String backpack = "div:nth-child(1) > div.pricebar > button";
String jacket = "div:nth-child(4) > div.pricebar > button";
String cart = "#shopping_cart_container";
String rmvBtn = "div:nth-child(4) > div.cart_item_label > div.item_pricebar > button";
String continueShopping = "a.cart_cancel_link";
...
```
5. Add this `Assertion` at the end of the test:
3. In `FullJourneyTest`, copy everything related to the login feature
and paste it into the `LoginFeatureTest` class method: `ShouldBeAbleToLogin`.
4. Add this `Assertion` at the end of the `ShouldBeAbleToLogin`:
```
Assert.assertEquals("https://www.saucedemo.com/inventory.html", driver.getCurrentUrl());
```
6. Run the test:
5. Run the test:
```
mvn test -Dtest=LoginFeatureTest
```
7. Next we need to create an **`@AfterMethod` to send the test results to Sauce Labs and add a `@BeforeMethod` that takes care of the driver instantiation before we run our test:
* ```
@AfterMethod
public void teardown(ITestResult result) {
((JavascriptExecutor)driver).executeScript(
"sauce:job-result=" + (result.isSuccess() ? "passed" : "failed"));
driver.quit();
}
```
* ```
@BeforeMethod
public void setUp(Method method) throws MalformedURLException
{
// Input your SauceLabs Credentials
String sauceUsername = System.getenv("SAUCE_USERNAME");
String sauceAccessKey = System.getenv("SAUCE_ACCESS_KEY");
MutableCapabilities capabilities = new MutableCapabilities();
//sets browser to Safari
capabilities.setCapability("browserName", "Safari");
//sets operating system to macOS version 10.13
capabilities.setCapability("platform", "macOS 10.13");
//sets the browser version to 11.1
capabilities.setCapability("version", "11.1");
//sets your test case name so that it shows up in Sauce Labs
capabilities.setCapability("name", method.getName());
capabilities.setCapability("username", sauceUsername);
capabilities.setCapability("accessKey", sauceAccessKey);
//instantiates a remote WebDriver object with your desired capabilities
driver = new RemoteWebDriver(new URL("https://ondemand.saucelabs.com/wd/hub"), capabilities);
}
```
Your test class should now look like this:
```
@Test
public void ShouldBeAbleToLogin() {
//navigate to the url of the Sauce Labs Sample app
driver.navigate().to("https://www.saucedemo.com");
// Ignore the following selectors
String username = "standard_user";
String password = "secret_sauce";
String userField = "[data-test='username']";
String passField = "[data-test='password']";
String loginBtn = "[value='LOGIN']";
// wait 5 seconds
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
// send username keystrokes
driver.findElement(By.cssSelector(userField)).sendKeys(username);
// send password keystrokes
driver.findElement(By.cssSelector(passField)).sendKeys(password);
// click login button to submit keystrokes
driver.findElement(By.cssSelector(loginBtn)).click();
// assert that the next page opened
Assert.assertEquals("https://www.saucedemo.com/inventory.html", driver.getCurrentUrl());
}
```
8. Run the final test of this exercise:
6. Create a **`@BeforeMethod`** that takes care of `WebDriver` instantiation and setting `capabilities` before we run our test, and add an **`@AfterMethod`** to send the test results to Sauce Labs
7. Run the final test of this exercise:
```
mvn test -Dtest=LoginFeatureTest
```
9. Use `git stash` or `git commit` to discard or save your changes. Checkout the next branch to proceed to the next exercise
8. Use `git stash` or `git commit` to discard or save your changes. Checkout the next branch to proceed to the next exercise
```
git checkout 02_page_objects
```
55 changes: 41 additions & 14 deletions exercise-guides/exercise4.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
# Exercise 4: Configure Atomic Tests

## Part One: Modify `ConfirmationPage`
## Part One: Create `CheckoutCompletePage`
1. Checkout the branch `04_configure_atomic_tests`.
2. Open `ConfirmationPage` in `src > test > java > pages`.
3. Add the following class methods:
2. In `src > test > java > pages`, create a new class called `CheckoutCompletePage`
3. Add the following:
```
public class CheckoutCompletePage {
private final WebDriver driver;
public CheckoutCompletePage(WebDriver driver) {
this.driver = driver;
}
}
```
4. Add a new class method called `IsLoaded()` to confirm the correct checkout page is loaded:
```
public boolean IsLoaded()
{
return driver.getCurrentUrl().contains("https://www.saucedemo.com/checkout-complete.html");
}
```
<br />
## Part Two: Modify `ConfirmationPage`
1. Open `ConfirmationPage` in `src > test > java > pages`.
2. Add the following class methods:
```
public void visit() {
driver.get("https://www.saucedemo.com/checkout-step-two.html");
Expand All @@ -16,14 +38,15 @@
}
```
```
public CheckoutCompletePage finishCheckout() {
String checkoutLink = "cart_checkout_link";
driver.findElement(By.className(checkoutLink)).click();
return new CheckoutCompletePage;
public CheckoutCompletePage FinishCheckout()
{
String finished =".btn_action.cart_button";
driver.findElement(By.cssSelector(finished)).click();
return new CheckoutCompletePage(driver);
}
```
4. Open **`CheckoutFeatureTest`** located in `src > test > java > exercises`.
5. You'll notice that the **`ShouldBeAbleToCheckout()`** class method steps through many pages to get to the checkout function. The existing test flow works like this:
3. Open **`CheckoutFeatureTest`** located in `src > test > java > exercises`.
4. You'll notice that the **`ShouldBeAbleToCheckout()`** class method steps through many pages to get to the checkout function. The existing test flow works like this:
* User logs in
* Adds some items to the cart
* Clicks the cart icon to proceed to checkout
Expand All @@ -32,14 +55,14 @@ This approach is under-optimized because our tests shouldn't rely on the asserti
<br />
## Part Two: Implement the `JavascriptExecutor` to Bypass Pages
## Part Three: Implement the `JavascriptExecutor` to Bypass Pages
1. Go back to **`ConfirmationPage`** and add the following class method:
```
public void setCartState() {
public void setPageState() {
driver.navigate().refresh();
}
```
2. In **`setCartState()`** add the following **`JavaScriptExecutor`** command to bypass logging in through the **`LoginPage`** object:
2. In **`setPageState()`** add the following **`JavaScriptExecutor`** command to bypass logging in through the **`LoginPage`** object:
```
((JavascriptExecutor)driver).executeScript("window.sessionStorage.setItem('standard-username', 'standard-user')");
```
Expand All @@ -53,11 +76,15 @@ This approach is under-optimized because our tests shouldn't rely on the asserti
public void ShouldBeAbleToCheckoutWithItems() {
// wait 5 seconds
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS) ;
ConfirmationPage confirmationPage = new ConfirmationPage(driver);
confirmationPage.visit();
confirmationPage.setCartState();
confirmationPage.checkout();
confirmationPage.setPageState();
Assert.assertTrue(confirmationPage.hasItems());
CheckoutCompletePage completePage = confirmationPage.FinishCheckout();
// assert that the test is finished by checking the last page's URL
Assert.assertTrue(completePage.IsLoaded());
}
```
5. Save all and run the following command to ensure the build passes:
Expand Down
6 changes: 0 additions & 6 deletions exercise-guides/exercise6.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
2. Open up the `pom.xml` and modify the following `plugin` setting:
* Before:
```
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<parallel>classes</parallel>
<threadCount>1</threadCount>
Expand All @@ -16,9 +13,6 @@
```
* After:
```
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<parallel>methods</parallel>
<threadCount>10</threadCount>
Expand Down
2 changes: 1 addition & 1 deletion exercise-guides/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Before you begin, consult the parent [README](../README.md#getting-started-with-
* [Setup Selenium Project](../README.md#setup-the-project)


Download the latest [release](https://github.com/saucelabs-training/Getting-Started-with-Selenium/releases) archive and import the project into your local environment and begin experimenting with creating your own automated Selenium tests!
Download the latest [release](https://github.com/saucelabs-training/saucecon19-best-practices-workshop/releases) (`saucecon-bestpractices-workshop-<version>.zip`) archive and import the project into your local environment and begin experimenting with creating your own automated Selenium tests!

## Exercise List
1. [Automate Tests on SauceLabs](exercise1.md)
Expand Down
Loading

0 comments on commit 17607d3

Please sign in to comment.