Automated testing is easy! How I tested Pechkin

Not so long ago I began to look towards Selenium WebDriver, which in conjunction with PageObject becomes an excellent tool for automated testing. Those who are not familiar with Selenuim can read this article , but here you can read specifically about Selenium WebDriver.

When writing this article were used:
  1. Maven
  2. Testng
  3. Selenium WebDriver
  4. PageObject
  5. Guinea pig: pechkin-mail.ru


Environment setting


1. You must install the JDK. It can be taken from the official site - http://www.oracle.com
2. Install the build tool, I prefer Maven.
Maven Installation Instructions
Download the latest version of Maven from of.
Unpack the site into any directory, for example, in C: \ Program Files \ maven \
Add the environment variable: M2_HOME = C: \ Program Files \ maven \ and add% M2_HOME% \ bin to the PATH
Check that everything is done correctly:
mvn -version

Must show Maven version
Apache Maven 3.2.3 (33f8c3e1027c3ddde99d3cdebad2656a31e8fdf4; 2014-08-12T00:58:10+04:00)
Maven home: C:\Program Files\maven
Java version: 1.8.0_20, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk1.8.0_20\jre
Default locale: ru_RU, platform encoding: Cp1251
OS name: "windows 7", version: "6.1", arch: "amd64", family: "dos"


3. As an architectural solution, we will use the archetype of Alexei Barantsev ( barancev ). Take the archetype from github
git clone git://github.com/barancev/webdriver-java-quickstart-archetype.git

Let's go to the folder with the archetype and install it
cd webdriver-java-quickstart-archetype
mvn install

The next step is to create a project, we will do this as a team:
mvn archetype:generate -DarchetypeGroupId=ru.stqa.selenium -DarchetypeArtifactId=webdriver-java-quickstart-archetype -DarchetypeVersion=0.7 -DgroupId=ru.mail.pechkin -DartifactId=PechkinFuncTest

where ru.mail.pechkin - id of the project being created, PechkinFuncTest - artifact id of the project being created

Go to practice!


For clarity, we will test the registration page of Pechkin, consisting of 4 text fields, 2 checkboxes and a registration button. We’ll start with them, but first we need to set up our project a little.

We edit the pom.xml :
We use the latest version of Selenium WebDriver
org.seleniumhq.seleniumselenium-java2.42.2

It remains in application.properties to specify the URL of the site and the browser in which the testing will take place.
site.url=http://pechkin-mail.ru/
browser.name=firefox

Lyrical digression

Before you start writing code, you need to get familiar with PageObject. Roughly speaking, PageObject is a design pattern that separates high-level logic from low-level logic of searching and using interface elements, while increasing readability and code support.

The result is a separate class that describes, in our case, a specific page (WebElements), as well as how to interact with them.

Let's get started

In ru.mail.pechkin.pages, create RegistrationPage.java. This will be our registration page template. We begin to describe the fields. As mentioned above, we have:
1. Four text fields:
private WebElement emailField;
private WebElement usernameField;
private WebElement passwordField;
private WebElement inviteField;

2. Two checkboxes:
private WebElement dogovorCheckbox;
private WebElement persCheckbox;

3. Registration button:
private WebElement submitButton;

Armed with a code inspector, we determine the locators of these elements. Our object will look like this:
@FindBy(id = "email")
private WebElement emailField;
@FindBy(id = "username")
private WebElement usernameField;
@FindBy(id = "password")
private WebElement passwordField;
@FindBy(id = "invite")
private WebElement inviteField;
@FindBy(id = "dogovor")
private WebElement dogovorCheckbox;
@FindBy(id = "pers")
private WebElement persCheckbox;
@FindBy(id = "reg_submit")
private WebElement submitButton;

The next step is to create methods through which we will work with the page
public RegistrationPage setEmail(String email){
    emailField.sendKeys(email);
    return this;
}
public RegistrationPage setUsername(String username){
    usernameField.sendKeys(username);
    return this;
}
public RegistrationPage setPassword(String password){
    passwordField.sendKeys(password);
    return this;
}
public RegistrationPage setInvite(String invite){
    inviteField.sendKeys(invite);
    return this;
}
public RegistrationPage setDogovor() {
    if(!dogovorCheckbox.isSelected()) {
        dogovorCheckbox.click();
    }
    return this;
}
public RegistrationPage setPersonal() {
    if(!persCheckbox.isSelected()) {
        persCheckbox.click();
    }
    return this;
}
public void clickSubmitButton() {
    submitButton.click();
}

We also need to track the error message when entering an invalid email.
@FindBy(id = "error_block")
private WebElement errorText;
public boolean isError() {
    if(errorText.isDisplayed()) {
        return true;
    } else {
        return false;
    }
}

RegistrationPage.java
package ru.mail.pechkin.pages;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
public class RegistrationPage extends AnyPage{
    @FindBy(id = "reg_submit")
    private WebElement submitButton;
    @FindBy(id = "email")
    private WebElement emailField;
    @FindBy(id = "username")
    private WebElement usernameField;
    @FindBy(id = "password")
    private WebElement passwordField;
    @FindBy(id = "invite")
    private WebElement inviteField;
    @FindBy(id = "dogovor")
    private WebElement dogovorCheckbox;
    @FindBy(id = "pers")
    private WebElement persCheckbox;
    @FindBy(id = "error_block")
    private WebElement errorText;
    public RegistrationPage(PageManager pages) {
        super(pages);
    }
    public RegistrationPage setEmail(String email){
        emailField.sendKeys(email);
        return this;
    }
    public RegistrationPage setUsername(String username){
        usernameField.sendKeys(username);
        return this;
    }
    public RegistrationPage setPassword(String password){
        passwordField.sendKeys(password);
        return this;
    }
    public RegistrationPage setInvite(String invite){
        inviteField.sendKeys(invite);
        return this;
    }
    public RegistrationPage setDogovor() {
        if(!dogovorCheckbox.isSelected()) {
            dogovorCheckbox.click();
        }
        return this;
    }
    public RegistrationPage setPersonal() {
        if(!persCheckbox.isSelected()) {
            persCheckbox.click();
        }
        return this;
    }
    public void clickSubmitButton() {
        submitButton.click();
    }
    public boolean isError() {
        if(errorText.isDisplayed()) {
            return true;
        } else {
            return false;
        }
    }
}


Now we need the one who will manage our page. Add to ru.mail.pechkin.logic RegistrationHelper.java. It will have only 3 methods:
Filling all the fields with the correct values, except email.
public void setEmailWithValidAllFields(String email) {
    pages.registrationPage.setEmail(email).setUsername(username).setPassword(password);
    pages.registrationPage.setDogovor().setPersonal();
}

Register button click
public void clickRegistration() {
    pages.registrationPage.clickSubmitButton();
}

Check error message display
public boolean isError() {
    return pages.registrationPage.isError();
}

RegistrationHelper.java
package ru.mail.pechkin.logic;
public class RegistrationHelper extends DriverBasedHelper implements IRegistrationHelper {
    private final String username = "username";
    private final String password = "passw";
    private ApplicationManager manager;
    public RegistrationHelper(ApplicationManager applicationManager) {
        super(applicationManager.getWebDriver());
        this.manager = applicationManager;
    }
    @Override
    public String getTitle() {
        return driver.getTitle();
    }
    public void setEmailWithValidAllFields(String email) {
        pages.registrationPage.setEmail(email).setUsername(username).setPassword(password);
        pages.registrationPage.setDogovor().setPersonal();
    }
    public void clickRegistration() {
        pages.registrationPage.clickSubmitButton();
    }
    public boolean isError() {
        return pages.registrationPage.isError();
    }
}


Writing tests


In the test ru.mail.pechkin directory, create RegistrationPageTest.java. I will show how to test an email field, so we open the specification for email fields and compile tests on it. Let me give you a couple of examples.
Positive tests

@Test
public void validEmailWithLowerAndUpperLetters() {
    app.getNavigationHelper().openRegistrationPage();
    app.getRegistrationHelper().setEmailWithValidAllFields("testEmail@mailinator.com");
    app.getRegistrationHelper().clickRegistration();
    Assert.assertTrue(!app.getRegistrationHelper().isError());
}
@Test
public void validEmailWithNumberInLocalPart() {
    app.getNavigationHelper().openRegistrationPage();
    app.getRegistrationHelper().setEmailWithValidAllFields("1testEmail1@mailinator.com");
    app.getRegistrationHelper().clickRegistration();
    Assert.assertTrue(!app.getRegistrationHelper().isError());
}

Negative tests

@Test
public void notValidEmailEmptyField() {
    app.getNavigationHelper().openRegistrationPage();
    app.getRegistrationHelper().setEmailWithValidAllFields("");
    app.getRegistrationHelper().clickRegistration();
    Assert.assertTrue(app.getRegistrationHelper().isError());
}
@Test
public void notValidEmailWithLackOfLocalPart(){
    app.getNavigationHelper().openRegistrationPage();
    app.getRegistrationHelper().setEmailWithValidAllFields("@mailinator.com");
    app.getRegistrationHelper().clickRegistration();
    Assert.assertTrue(app.getRegistrationHelper().isError());
}

Conclusion


On this and finish. I got 37 email verification tests. A negative test for checking two consecutive points in the local part of the email unexpectedly flipped, just like the excess of the number of characters in the local part.

Also popular now: