📜 ⬆️ ⬇️

Automated testing is easy! How I tested Pechkina

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 , and 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


Setting up the environment


1. You need to install 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 . the site
We unpack in 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
Let's check that everything is done right:
mvn -version 

Must show the version of Maven
 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 architect Alexei Barantsev ( barancev ). We pick up archetyp with github'a
 git clone git://github.com/barancev/webdriver-java-quickstart-archetype.git 

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 with the command:
 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 is the id of the project being created, PechkinFuncTest is the artifact id of the project being created

Forward to practice!


For clarity, we will test the Pechkin registration page , consisting of 4 text fields, 2 checkboxes and a registration button. We will begin with them, but first we need to tweak our project a little.
')
Edit pom.xml :
We will use the latest version of Selenium WebDriver
 <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>2.42.2</version> </dependency> 

It remains to specify in the application.properties 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 we start writing code, you need to become familiar with PageObject. Roughly speaking, PageObject is a design pattern that separates high-level logic from low-level search logic and the use of interface elements, while increasing readability and code support.

The result is a separate class that describes, in our case, a specific page (WebElement'y), as well as ways to interact with them.

Let's get started

In ru.mail.pechkin.pages we create RegistrationPage.java. This will be our registration page template. Let's start to describe the field. 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 define 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 you enter a wrong 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. Let's add in ru.mail.pechkin.logic RegistrationHelper.java. There will be only 3 methods:
Filling all fields with valid values, except email.
 public void setEmailWithValidAllFields(String email) { pages.registrationPage.setEmail(email).setUsername(username).setPassword(password); pages.registrationPage.setDogovor().setPersonal(); } 

Push Registration Button
 public void clickRegistration() { pages.registrationPage.clickSubmitButton(); } 

Check the display of the error message
 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(); } } 


We write tests


In the test directory ru.mail.pechkin create RegistrationPageTest.java. I will show how to test the email field, so open the specification on the email field and compile tests on it. I will give 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 tests to check email. The negative test for checking two consecutive points in the local part of the email suddenly zafeililsya, exactly like the excess of the number of characters in the local part.

Source: https://habr.com/ru/post/236561/


All Articles