This repository has been archived by the owner on Feb 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
James Tacker
committed
Jan 15, 2019
0 parents
commit a97424d
Showing
41 changed files
with
1,177 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Compiled files | ||
/out | ||
|
||
# Library files | ||
/lib | ||
|
||
# IDEA files | ||
/.idea | ||
|
||
# Log file | ||
*.log | ||
|
||
*.iml | ||
|
||
*.DS_Store | ||
|
||
# User-specific stuff | ||
.idea/workspace.xml | ||
.idea/tasks.xml | ||
.idea/usage.statistics.xml | ||
.idea/dictionaries | ||
.idea/shelf | ||
.idea/encodings.xml | ||
|
||
# BlueJ files | ||
*.ctxt | ||
|
||
# Mobile Tools for Java (J2ME) | ||
.mtj.tmp/ | ||
|
||
# Package Files # | ||
*.jar | ||
*.war | ||
*.nar | ||
*.ear | ||
*.zip | ||
*.tar.gz | ||
*.rar | ||
|
||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml | ||
hs_err_pid* | ||
|
||
**.idea |
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,165 @@ | ||
# Best Practices for Automated Testing | ||
|
||
This directory contains example scripts and dependencies for the SauceCon workshop: [Best Practices for Automated Testing](https://saucecon.com/). Use these scripts to test your Sauce Labs authentication credentials, setup your automated testing environment, try out Sauce Labs features, and complete the in-class Selenium examples. Download the zip file or clone the entire directory to your local environment. | ||
|
||
#### For Demonstration Purposes Only | ||
|
||
The code in these scripts is provided on an "AS-IS” basis without warranty of any kind, either express or implied, including without limitation any implied warranties of condition, uninterrupted use, merchantability, fitness for a particular purpose, or non-infringement. These scripts are provided for educational and demonstration purposes only, and should not be used in production. Issues regarding these scripts should be submitted through GitHub. These scripts are maintained by the Technical Services team at Sauce Labs. | ||
|
||
<br /> | ||
|
||
## Prerequisites | ||
|
||
These procedures will show you to set up a Selenium environment for Java. The scripts in this repository allow you run a simple automated test to validate your Selenium environment and your [saucelabs.com](https://app.saucelabs.com/login) account credentials. | ||
In order to complete these exercises you must complete the following prerequisite installation and configuration steps: | ||
|
||
* Install Git | ||
* Install IntelliJ | ||
* Install JDK | ||
* Setup Project | ||
|
||
Once your test environment is up and running, refer to the [exercise guides](exercise-guides/getting-started.md) to begin the exercises. | ||
|
||
### Install Git | ||
|
||
[Git](https://git-scm.com/doc) is a version control system that lets you check out code from a repository, | ||
work with that code on your own branch, and then merge that code with any changes that have been made by other developers. | ||
Git is an essential tool for distributed development teams, and is a critical component of the continuous | ||
integration/continuous development toolchain. | ||
|
||
#### MacOSX: | ||
|
||
1. Go to [https://git-scm.com/downloads](https://git-scm.com/downloads). | ||
2. Under **Downloads**, click **Mac OS X**. | ||
3. When the download completes, double-click the `.dmg` file open the installer package. | ||
4. Double-click the installer package to begin the installation. | ||
> *Security Warning* | ||
> | ||
> You may see a warning message that the package can't be opened because it's not from a recognized developer. | ||
If this happens, go to System Preferences > Security and Privacy Settings, and click Open Anyway. | ||
5. Click **Continue** for the installation, and enter your local password to authorize the installation. | ||
|
||
#### Windows: | ||
|
||
1. Go to [https://git-scm.com/downloads](https://git-scm.com/downloads) | ||
2. Under **Downloads**, click on **Windows**. | ||
3. When the dialog opens asking if you want to allow the app to make changes to your device, click Yes. | ||
4. Follow the steps in the setup wizard to complete the installation. You should accept all the default settings. | ||
<br /> | ||
|
||
### Install IntelliJ | ||
|
||
[IntelliJ](https://www.jetbrains.com/idea/) is an integrated development environment that incorporates several tools for developing and running Java code. You will be using IntelliJ to write and edit the sample Selenium scripts used in the exercises. For these exercises you only need to download the free Community edition. | ||
|
||
#### MacOSX: | ||
|
||
1. Go to [https://www.jetbrains.com/idea/](https://www.jetbrains.com/idea/) | ||
2. Click **Download**. | ||
3. On the **Downloads** page, select **macOS**. | ||
4. Under **Community**, click **Download**. | ||
5. When the download completes, double-click the .dmg file open the installer package. | ||
6. Double-click the installer package to begin the installation. | ||
7. Drag and drop the IntelliJ icon into the **Applications** folder. | ||
|
||
#### Windows: | ||
|
||
1. Go to [https://www.jetbrains.com/idea/](https://www.jetbrains.com/idea/) | ||
2. Click **Download**. | ||
3. On the **Downloads** page, select **Windows**. | ||
4. Under **Community**, click **Download**. | ||
5. When the download completes, double-click the `.exe` file to launch the installation wizard. | ||
You should accept all the default settings. | ||
<br /> | ||
|
||
### Install the JDK | ||
|
||
The [Java SE Developer Kit](http://www.oracle.com/technetwork/java/javase/overview/index.html) lets you develop and | ||
deploy Java applications on desktops and servers. It is needed to compile our test code. | ||
|
||
#### MacOSX: | ||
|
||
1. Go to [the JDK downloads](https://www.oracle.com/technetwork/java/javase/downloads/index.html) page, locate the latest release, select the download button. | ||
2. Under **Java SE Development Kit {version}**, select the **Accept License Agreement** radio button. | ||
3. Click the download link for **Mac OS**. | ||
4. When the download completes, double-click the `.dmg` file open the installer package. | ||
Double-click the installer package to begin the installation. | ||
|
||
#### Windows: | ||
|
||
1. Go to [the JDK downloads](https://www.oracle.com/technetwork/java/javase/downloads/index.html) page, locate the latest release, select the download button. | ||
2. Under **Java SE Development Kit {version}**, select the **Accept License Agreement** radio button. | ||
3. Click the download link for **Windows x64**. | ||
4. When the download completes, double-click the `.exe` file open the installer package. | ||
5. Double-click the installer package to begin the installation. You should accept all the default settings. | ||
<br /> | ||
|
||
### Setup the Project | ||
|
||
#### Ensure IntelliJ is Correctly Configured: | ||
|
||
1. **Run Hello World** | ||
* In IntelliJ, select **File > New > Project.** | ||
* In the **New Project** dialog window, select **Java** (default). | ||
* IntelliJ should automatically detect the JDK and appear in the **Project SDK** field. If you don't see the JDK, select **New** and navigate to where you installed the JDK. | ||
* Select **Next**, then select **Create project from template > Java Hello World.** | ||
* Name the project **SauceDemo** and select **Finish.** | ||
> you can also decide whether or not to display IntelliJ tool tips. | ||
* From the main toolbar, select **Run > Run 'Main'**. | ||
> In the main console at the bottom the words ` Hello World!` should appear, along with ` Process finished with exit code 0`. | ||
2. **Configure the Selenium Standalone Server** | ||
* Open a browser and navigate to the [SeleniumHQ Downloads page](https://www.seleniumhq.org/download/). | ||
* Under **Selenium Standalone Server** select the version link to download the file (extract/unzip the archive if necessary). | ||
> You can download the package to any directory, just ensure you remember the location when configuring Selenium in IntelliJ. | ||
* In IntelliJ, select **File > Project Structure > Modules.** | ||
* Select the **Dependencies** tab and navigate to the bottom of the window. | ||
* Select the **+** symbol, followed by **JARs or Directories.** | ||
* Navigate to the location of the **Selenium .jar** file and select **Open.** | ||
* Select **Apply** followed by **OK.** | ||
* In the **Project** pane of IntelliJ, select the triangle next to **External Libraries** | ||
> You should see the Selenium Standalone Server `.jar` file | ||
3. **Configure the TestNG Framework** | ||
* In IntelliJ, select **File > Project Structure > Modules.** | ||
* Select the **Dependecies** tab and navigate to the bottom of the window. | ||
* elect the **+** symbol, followed by **JARs or Directories.** | ||
* Navigate to `Applications/IntelliJ IDEA CE/Contents/plugins/lib/` | ||
* Select both `testng-plugin.jar` and `testng.jar`, select **Apply > OK.** | ||
* In the **Project** pane of IntelliJ, select the triangle next to **External Libraries** | ||
> You should see the TestNG `.jar` files | ||
#### Run The Sample Project in IntelliJ: | ||
4. **Add the Test Script** | ||
* In the **Project** pane of IntelliJ, right-click on **src,** select **New > Java Class.** | ||
* Name the class ` InstantJavaTestNGTest `, then select **OK.** | ||
* Download the test script from [here](https://github.com/saucelabs-training/demo-java/blob/master/InstantSauceTest.java). | ||
* In IntelliJ, delete any code from your default class and paste in the test script | ||
>To run tests on Sauce Labs, you must aquire your user name and access key | ||
5. **Configuring your Sauce Labs Credentials** | ||
* Login to [www.saucelabs.com](https://app.saucelabs.com/login). | ||
> If you don't have a Sauce Labs account, create a free trial [here](https://signup.saucelabs.com/signup/trial) | ||
* In the upper-right corner, select the down arrow next to your name. | ||
* In the drop down menu, select **User Settings.** | ||
* Copy and paste both your **`Username`** and **`Access Key`** to your clipboard | ||
* In IntelliJ, replace the below strings with your **`Username`** and **`Access Key`** | ||
``` | ||
String sauceUserName = "YOUR USER NAME"; | ||
String sauceAccessKey = "YOUR ACCESS KEY"; | ||
``` | ||
6. **Running the Test on Sauce Labs** | ||
* In IntelliJ, select the **`InstantJavaTestNGTest`** class, right-click anywhere, select **`Run shouldOpenSafari()`**, and check the console at the bottom of the window. | ||
* You should see the following in the IntelliJ console: | ||
``` | ||
[TestNG] Running: | ||
/Users/{user}/Library/Caches/IntelliJIdea2018.3/temp-testng-customsuite.xml | ||
``` | ||
* In the Sauce Labs Dashboard, select the **Automated Tests** tab. | ||
> The `shouldOpenSafari()` test should appear running. | ||
* Once the test completes, select the link for **`shouldOpenSafari()`** check the **Commands** tab to see all `HTTP` requests in screenshot form. | ||
* In the **Watch** tab, you can click on the play button to see a full video of the test. | ||
* Finally, in IntelliJ you'll see a green check mark next to a message that read something like so: | ||
``` | ||
Test passed: 1 of 1 test - 17s 750ms | ||
``` |
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,44 @@ | ||
# Exercise 1: Configure Automated Testing on SauceLabs | ||
|
||
##Part One: Set Sauce Labs Account Credentials | ||
1. Checkout branch `01_set_sauce_credentials `. Open `test > exercises > Exercise1.java` | ||
2. Login to [www.saucelabs.com](https://www.sauceslabs.com), and navigate to the User Settings section of your account profile. | ||
|
||
![User Settings](images/user-settings.png) | ||
|
||
3. Copy and paste your Username and SauceLabs Access Key to your local clipboard | ||
4. Navigate back to IntelliJ and in the class `Exercise1.java`, update the following variables: | ||
``` | ||
String sauceUserName = "SAUCE_USERNAME"; | ||
String sauceAccessKey = "SAUCE_ACCESS_KEY"; | ||
``` | ||
5. Save and run your test, you should see the following appear in your Sauce Labs Test Dashboard: | ||
![Exercise1 Result](images/exercise1-result.png) | ||
##Part Two: Set Environment Variables in a Run Configuration | ||
6. Next, modify the `sauceUserName` and `sauceAccessKey` variables to use Environment Variables: | ||
```$xslt | ||
String sauceUserName = System.getenv("SAUCE_USERNAME"); | ||
String sauceAccessKey = System.getenv("SAUCE_ACCESS_KEY"); | ||
``` | ||
7. Create a new Run Configuration in IntelliJ | ||
* Select **Run > Edit Configurations** | ||
* Select the `+` Symbol to add a new configuration | ||
* Name the configuration **Exercise1** | ||
* Ensure the following: `test.exercises.Test1`, exists in the **Class** field | ||
* Select the folder icon next to the **Environment variables** field | ||
![Environment Variables IntelliJ](images/env-var-field.png) | ||
* Add `SAUCE_USERNAME` and `SAUCE_ACCESS_KEY` with approrpirate values | ||
![Sauce Credentials](images/sauce-credentials.png) | ||
* Save and Exit | ||
8. Run your test script again using the IntelliJ toolbar | ||
![Run Toolbar](images/run-toolbar.png) |
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,57 @@ | ||
# Exercise 2: Manage Test Automation Speeds | ||
##Part One: Configure `sauce:options` | ||
1. Checkout branch `02_set_sauce_options `, and open `test > exercises > Exercise2.java` | ||
2. In the **`MutableCapabilities sauceOpts`** object declaration, set the following options: | ||
|
||
```$xslt | ||
sauceOpts.setCapability("name", testInfo.getDisplayName()); | ||
sauceOpts.setCapability("seleniumVersion", "3.141.59"); | ||
sauceOpts.setCapability("user", username); | ||
sauceOpts.setCapability("accessKey", accessKey); | ||
sauceOpts.setCapability("tags", testInfo.getTags()); | ||
sauceOpts.setCapability("commandTimeout", 600); | ||
sauceOpts.setCapability("idleTimeout", 1000); | ||
sauceOpts.setCapability("maxDuration", 3600); | ||
``` | ||
3. In the **`DesiredCapabilities caps`** object declaration, set the following capabilities: | ||
```$xslt | ||
caps.setCapability("sauce:options", sauceOpts); | ||
caps.setCapability("browserName", "firefox"); | ||
caps.setCapability("browserVersion", "53.0"); | ||
caps.setCapability("platformName", "windows 10"); | ||
``` | ||
<br /> | ||
##Part Two: Configure Explicit Waits | ||
1. Create an instance of a Selenium explicit wait so that we can dynamically wait for an element | ||
```$xslt | ||
WebDriverWait wait = new WebDriverWait(driver, 5); | ||
``` | ||
|
||
2. Wait for the username field to be visible and store that element in a variable | ||
|
||
```$xslt | ||
By userNameFieldLocator = By.cssSelector("[type='text']"); | ||
wait.until(ExpectedConditions.visibilityOfElementLocated(userNameFieldLocator)); | ||
``` | ||
3. Enter the username and password strings into their respective fields. Then submit the results by "clicking" the submit button user name field | ||
``` | ||
driver.findElement(userNameFieldLocator).sendKeys("standard_user"); | ||
driver.findElement(By.cssSelector("[type='password']")).sendKeys("secret_sauce"); | ||
driver.findElement(By.cssSelector("[type='submit']")).click(); | ||
``` | ||
4. Ensure the next page loads by creating another locator and storing it an a variable | ||
```$xslt | ||
By inventoryPageLocator = By.id("inventory_container"); | ||
wait.until(ExpectedConditions.visibilityOfElementLocated(inventoryPageLocator)); | ||
``` | ||
5. Create a test assertion to determine if the test passed or failed | ||
```$xslt | ||
Boolean result = driver.findElements(inventoryPageLocator).size() > 0; | ||
Assertions.assertTrue(result); | ||
driver.quit(); | ||
``` |
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,51 @@ | ||
# Exercise 3: Abstract and Configure Declarative Test Code | ||
##Part One: Abstract Test Code | ||
|
||
1. Checkout branch `03_abstract_test_code`, and open `test > exercises > Exercise3.java` | ||
2. Create a `@BeforeEach` method called `setup()`. | ||
3. Abstract the prerequisite tasks (i.e. `DesiredCapabilities`, `MutableCapabilities`, and `RemoteWebDriver`) into the `setup()`method. | ||
4. Refactor the test code from imperative to declarative, for example: | ||
|
||
####**Before** | ||
``` | ||
public void signInSuccessfully(TestInfo testInfo) throws MalformedURLException { | ||
driver.get("https://www.saucedemo.com"); | ||
String username = "standard_user"; | ||
String password = "secret_sauce"; | ||
WebDriverWait explicitWait = new WebDriverWait(driver, 10); | ||
WebElement usernameElement = explicitWait.until( | ||
ExpectedConditions.presenceOfElementLocated(By.cssSelector("[data-test='username']"))); | ||
usernameElement.sendKeys(username); | ||
driver.findElement(By.cssSelector("[data-test='password']")).sendKeys(password); | ||
driver.findElement(By.className("login-button")).submit(); | ||
Boolean result = explicitWait.until(ExpectedConditions.urlMatches("https://www.saucedemo.com/inventory.html")); | ||
Assertions.assertTrue(result); | ||
} | ||
``` | ||
####**After** | ||
``` | ||
public void signInSuccessfully() throws AssertionError { | ||
SignInPage signInPage = SignInPage.visit(driver); | ||
signInPage.signIn(User.validUser()); | ||
HomePage homePage = new HomePage(driver); | ||
Assertions.assertTrue(homePage.isSignedIn()); | ||
} | ||
``` | ||
##Part Two: Send Results to Sauce Labs | ||
1. Create an `@AfterEach` method called `teardown()`. | ||
2. Add the following `JavaScriptExecutor` code in order to send the test results to saucelabs.com. | ||
``` | ||
@AfterEach | ||
public void teardown() { | ||
((JavascriptExecutor) driver).executeScript("sauce:job-result=" + (true ? "passed" : "failed")); | ||
driver.quit(); | ||
} | ||
``` | ||
> Note: Your test is currently incomplete, proceed to the next exercise to create a `SignInPage` object and a `HomePage` object in order for your test to pass. |
Oops, something went wrong.