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

Commit

Permalink
Updated Exercise guide, code sample
Browse files Browse the repository at this point in the history
  • Loading branch information
James Tacker committed Mar 13, 2019
1 parent 163143a commit 17fda38
Show file tree
Hide file tree
Showing 13 changed files with 516 additions and 340 deletions.
144 changes: 137 additions & 7 deletions exercise-guides/exercise1.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Exercise 1: Configure Automated Testing on SauceLabs

##Part One: Set Sauce Labs Account Credentials
## Part One: Set Sauce Labs Account Credentials
1. Checkout branch `01_set_sauce_credentials `. Open `src > test > java > exercises > FullJourneyTest.java`
2. Login to [www.saucelabs.com](https://www.sauceslabs.com), and navigate to the User Settings section of your account profile.

Expand All @@ -18,7 +18,7 @@
```
you should see the results appear in your Sauce Labs Test Dashboard
##Part Two: Set Environment Variables
## Part Two: Set Environment Variables
6. Next, modify the `sauceUserName` and `sauceAccessKey` variables to use Environment Variables:
Expand All @@ -40,6 +40,7 @@
```
> To set an environment variables permanently in Windows, you must append it to the `PATH` variable.
> Go to "Control Panel > System > Windows version > Advanced System Settings > Environment Variables > System Variables > Edit > New. Then enter the "Name" and "Value"
9. Test the environment variables
###### Mac OSX:
```
Expand All @@ -51,16 +52,145 @@
echo %SAUCE_USERNAME%
echo %SAUCE_ACCESS_KEY%
```
10. Run your test using Maven
```
mvn test
```
You should see the following build info appear (after sometime) in your console:
> To refresh a bash shell if you don't see the values run any of the following commands:
![Successful Test Build Info](images/ex1-test-build.png)
> ** Warning:**
> If you have problems propogating your envirnoment variables into IntelliJ, try refreshing your by running any of the following commands:
> * `$ source ~/.bashrc`
> * `$ source ~/.bash_profile`
> * `$ source /etc/profile`
8. Run your test using Maven
> Or append the details to your `.bash_profile` to set them globally
> ```
> vim ~/.bash_profile
> export SAUCE_USERNAME="xxx"
> export SAUCE_ACCESS_KEY="XXXXXXX-XXXX-XXXX-XXXXXXXXXXX"
> launchctl setenv SAUCE_USERNAME $SAUCE_USERNAME
> launchctl setenv SAUCE_ACCESS_KEY $SAUCE_ACCESS_KEY
> ```
</br>
## 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:
```
mvn test
public class LoginFeatureTest {
protected WebDriver driver;
@Test
public void ShouldBeAbleToLogin(Method method)
throws MalformedURLException
{
}
}
```
You should see the following build info appear (after sometime) in your console:
3. In `FullJourneyTest`, copy everything from:
![Successful Test Build Info](images/ex1-test-build.png)
`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:
```
Assert.assertEquals("https://www.saucedemo.com/inventory.html", driver.getCurrentUrl());
```
6. 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:
```
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
```
git checkout 02_page_objects
```
160 changes: 65 additions & 95 deletions exercise-guides/exercise2.md
Original file line number Diff line number Diff line change
@@ -1,111 +1,81 @@
# Exercise 2: Create Page Objects

## Part One: Create Test Methods
1. Checkout branch `02_create_page_objects`. Open `src > test > java > exercises > FullJourneyTest.java`
2. Create a new package in **`src > test > java`** called **`pages`**.
3. In the **`pages`** package, create a new class called **`LogInPage`**
4. Instantiate the `LogInPage` object:
```
public static LogInPage visit(WebDriver driver) {
LogInPage page = new LogInPage(driver);
## Part One: Create a `LoginPage`
1. Checkout branch `02_create_page_objects`.
3. In the **`pages`** package, navigate to the class called **`LoginPage`**
4. Create a `visit` method in the `LoginPage` object:
```
public LoginPage visit() {
driver.get("https://www.saucedemo.com");
return page;
}
public LogInPage(WebDriver driver) {
this.driver = driver;
return this;
}
```
5. Identify any repeatable test actions and create a method for each action. For example, a method for entering user information into a contact form could look like this:
5. Create the constructor for the page object:
```
public void signInSuccessfully() {
logIn(driver);
public LoginPage(WebDriver driver) {
this.driver = driver;
}
private void logIn(WebDriver driver) {
String username = "standard_user";
String password = "secret_sauce";
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.findElement(userField).sendKeys(username);
driver.findElement(passwordField).sendKeys(password);
driver.findElement(loginButton).click();
}
```
6. Add the locators as private variables, for example:
```
private By userField = By.cssSelector(
"[placeholder = 'Username']");
private By passwordField = By.cssSelector(
"[placeholder = 'Password']");
private By loginButton = By.className(
"login-button");
```
6. Open **`LoginFeatureTest`** and import the **`LoginPage`** changes to replace **`driver.navigate().to("https://www.saucedemo.com")`** For Example:
* Before
```
driver.navigate().to("https://www.saucedemo.com");
```
* After
```
LoginPage LoginPage = new LoginPage(driver);
LoginPage.visit();
```
7. Run a `mvn test` command to see if the test executes successfully:
```
mvn test -Dtest=LoginFeatureTest
```
<br />
## Part Two: Create a `LogInTest` Class
1. Open **`LogInTest`** and add the following `@BeforeMethod` and `@AfterMethod` classes:
In **`LogInTest`** add the following methods:
```
public class LogInTest {
protected WebDriver driver;
@BeforeMethod
public void setup(Method method) throws MalformedURLException {
String username = System.getenv("SAUCE_USERNAME");
String accessKey = System.getenv("SAUCE_ACCESS_KEY");
String methodName = method.getName();
ChromeOptions chromeOpts = new ChromeOptions();
chromeOpts.setExperimentalOption("w3c", true);
MutableCapabilities sauceOpts = new MutableCapabilities();
sauceOpts.setCapability("username", username);
sauceOpts.setCapability("accessKey", accessKey);
sauceOpts.setCapability("name", methodName);
sauceOpts.setCapability("seleniumVersion", "3.141.59");
sauceOpts.setCapability("name", methodName);
sauceOpts.setCapability("build", "saucecon19-best-practices");
sauceOpts.setCapability("tags", "['best-practices', 'saucecon19']");
MutableCapabilities caps = new MutableCapabilities();
caps.setCapability(ChromeOptions.CAPABILITY, chromeOpts);
caps.setCapability("sauce:options", sauceOpts);
caps.setCapability("browserName", "googlechrome");
caps.setCapability("browserVersion", "71.0");
caps.setCapability("platformName", "windows 10");
String sauceUrl = "https://ondemand.saucelabs.com:443/wd/hub";
URL url = new URL(sauceUrl);
driver = new RemoteWebDriver(url, caps);
}
...
## Part Two: Create `login()` Class Method
1. Open **`LoginPage`** and create a new class method called `login()`. This method will return a new page object that represents the next page in the journey (i.e. `InventoryPage`)
2. Add the **`LoginPage.visit()`** action in place of **`driver.navigate().to("https://www.saucedemo.com")`** The method will also expect some String data for the credentials (`username` and `password`) input. For Example:
```
public InventoryPage login(String username, String password)
{
@AfterMethod
public void teardown(ITestResult result) {
((JavascriptExecutor)driver).executeScript("sauce:job-result=" + (result.isSuccess() ? "passed" : "failed"));
driver.quit();
}
}
```
2. Import the **`LogInPage`** and reference the `@Test` class method. For example:
```
import pages.LogInPage;
...
public class LogInTest {
@Tag(name = "logInSuccessfully()")
@Test
/** Tests for a successful login **/
public void logInSuccessfully(Method method) {
LogInPage logInPage = LogInPage.visit(driver);
logInPage.signInSuccessfully();
Assert.assertEquals(driver.getCurrentUrl(), "https://www.saucedemo.com/inventory.html");
}
...
3. In `LoginFeatureTest` copy line 55-73 and paste it into `LoginPage`. The `login()` method should now look like the following:
```
<br />
public InventoryPage login(String username, String password)
{
String userField = "[data-test='username']";
String passField = "[data-test='password']";
String loginBtn = "[value='LOGIN']";
## Part Three: Test the Results
1. Save all and run **`mvn test`** in your terminal. We now have duplication in not only our tests, but potentially our future page objects. Next we will create a `BasePage` object class and a `BaseTest` object class.
// 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();
return new InventoryPage(driver);
}
```
4. Add the following to the `LoginFeatureTest`, add the following to the `ShouldBeAbleToLogin` method:
```
String username = "standard_user";
String password = "secret_sauce";
loginPage.login(username, password);
```
5. After the change, save and run `mvn test` to ensure the test still runs.
```
mvn test -Dtest=LoginFeatureTest
```
6. Use `git stash` or `git commit` to discard or save your changes. Checkout the next branch to proceed to the next exercise
```
git checkout 03_remove_duplication
```
Loading

0 comments on commit 17fda38

Please sign in to comment.