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
Mar 13, 2019
1 parent
163143a
commit 17fda38
Showing
13 changed files
with
516 additions
and
340 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
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 | ||
``` |
Oops, something went wrong.