You are here: Ultra Guide>Automation Web>EndToEndJavaAutomationExample (20 Oct 2020, MichaelComstock)Edit AttachTags

End To End Java Automation Example

Introduction

The purpose of this document is to provide you with enough information to create your very first end to end automation test using a number of tools

Prerequisites

Java - here
Maven - For Windows/For Mac, you probably already have it.
Eclipse - here
Chrome Browser - here

Step 1: Create Maven Project in Eclipse

Once everything is installed, it's time to get to work. Open Eclipse. Click File - New - Project
On the Select a Wizard screen, select Maven Project
Click Next - Next
On the New Maven project screen, in the filter, type 'maven-archetype-simple'
Select 'org.apache.maven.archetypes' and click next
Provide a Group Id and Artifact Id
Click Finish

Step 2: Add Dependencies to POM.XML File

In your new Eclipse project, look for POM.XML - double click it to edit it

In your POM.XML file, look for the Dependencies section, it will look something like this
<dependencies>
<dependency>
<groupId>something</groupId>
<artifactId>something</artifactId>
<version>####</version>
</dependency>
</dependencies>

To make the testing automation experience much easier, we're going to add some dependencies

As a sidenote, the best place to find Maven Dependencies is https://mvnrepository.com
You simple search for the dependency you need and if it exists, you'll find it on this site

There are two dependencies that need to be added before we start writing our end to end test
- Selenium
- WebDriverManager
To add selenium, go to here
Or paste the following under the <dependencies> tag
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>4.2.2</version>
</dependency>

Please note that the versions in this example is the current versions at the time of writing

Once you add the dependencies to your POM.XML file, Eclipse will update automatically

Step 3: Creating a Page Object Model test with Page Factory

Here is a great reference for more information on Page Object Model with Page Factory

For this expample, we're testing against https://www.saucedemo.com/index.html

In Eclipse, in your project, expand src > main > java

Right click on java and select New > Class

Give the class a name and click Finish

In your new class, add the following import statements
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
import static org.junit.Assert.assertEquals;

To use Page Factory add the following to your class
WebDriver driver;
public youClassName(WebDriver driver){ PageFactory.initElements(driver, this);
}

The first thing we need to do is find all of the necessary elements.
The elements we need are
- SwagLabs title
- Username text box
- Password text box
- Login button

To find the elements we are going to use the browsers Inspect tool

Simple right click on the words SwagLabs and select Inspect. That will open browsers dev tools and show you the sites elements. The SwagLabs element should be highlighted, but if it's not, just inspect it again.
You should see something like this

<div class="login_logo"></div>

Right click on your target element and select Copy > Copy selector and paste that into Eclipse

It will look something like this

body > div.login_logo

Do this for the remaining needed elements. If an element has an id, use that. For example, the email field provides the id 'user[email]'. With the id, you don't have to copy the css.

Now that we have idenified our web elements we can use Page Factory, with some Selenium and Java mixed in, to use our elements

//SwagLabs Title
@FindBy(css = "body > div.login_logo")
public WebElement exampleSwagLabsTitle;

//Email UserName Login field
@FindBy(id = "username")
public WebElement exampleLoginUsername;

//Password Login field
@FindBy(id = "password")
public WebElement exampleLoginPassword;

//Login Button
@FindBy(id = "login-button")
public WebElement exampleLoginBtn;

//Products Title
@FindBy(css = "#inventory_filter_container > div")
public WebElement exampleProductsTitle;

The webElement name can be whatever you want, but it should indicate what it is. It needs to make sense for other people who come along after you.

//This is your opportunity to provide a description of the test. Provide as much information as possible. Keep in mind that someone else may be executing your tests and will need to know what is the expected outcome.

@Test //The Test annotation tells Eclipse it's a Test
public void testSwagLabsPage() { //This is your opportunity to name your tests. Don't be afraid to be descriptive. Remember, someone else may need to run the test in the future.
WebDriverManager.chromedriver().setup(); //This line uses WebdriverManager. This method allows you to use an supported browser without the need of pointing to a local installed version of the browser. This is great when different people are running the tests

driver = new ChromeDriver();
driver.get("https://courses.ultimateqa.com/users/sign_in"); //This will take you to the provided URL
String swagMessage = exampleSwagLabsTitle.getText(); //In Java, you're getting the title and assigning it the name welcomeMessage to be used later
assertEquals(swagMessage, "SwagLabs"); //This is making sure swagMessage really is "SWAGLABS" This assert will fail, do you know why?
exampleLoginUsername.sendKeys("standard_user");
exampleLoginPassword.sendKeys("secret_sauce"); //The next two lines will simple input the provided username and password into each field
exampleLoginBtn.click(); //Click 'Login'
String productsPageTitle = exampleProductsTitle.getText();
assertEquals(productsPageTitle, "Products"); //This assert will verify that we've made it passed the Login page

}

And there you have it.

Should you run into an isses, I would recommend looking here. It's a great rundown on how to use stack overflow and finding answers to errors

-- MichaelComstock - 09 Oct 2020
Topic revision: r7 - 20 Oct 2020, MichaelComstock
© 2020 Ultranauts - 75 Broad Street, 2nd Floor, Suite 206, New York, NY 10004 - info@ultranauts.co