We write an autotest using Selenium Webdriver, Java 8 and the Page Object pattern

This article discusses the creation of a fairly simple self-test. This article will be useful for beginner automation.


The material is presented as accessible as possible, however, it will be much easier to understand what we are talking about if you have at least minimal ideas about the Java language: classes, methods, etc.


We will need:


  • Installed development environment Intellij IDEA (is the most popular IDE, for most cases a free version of Community Edition is enough);
  • installed Java (jdk / openjdk) and Maven, registered in the system environment of the OS;
  • Chrome browser and chromedriver - a program for transmitting commands to the browser.

Project creation


Run Intellij IDEA, go through the first few points regarding sending statistics, importing projects, choosing a color scheme, etc. - just select the default options.


In the window that appears at the end, select “Create New Project”, and in it the project type is Maven. The window will look like:


image


  • Maven is a build tool for Java projects;
  • Project SDK - the version of Java that is installed on the computer;
  • Create from archetype is the ability to create a project with a specific archetype (at this stage, this checkbox does not need to be marked).

Click “Next." The following window will open:


image


Groupid and Artifactid are project identifiers in Maven. There are certain rules for filling these items:


  • Groupid — . Java: . , -, com.email.email;
  • Artifactid — ;
  • Version — .

«Finish»: IDE pom.xml:


image


, : Groupid, Artefiactid, Version. Pom.xml — . Pom- (), .


: Selenium Java Junit. Maven mvnrepository.com, Selenium Java :


image


( 3.14.0). :


image


«Maven» pom.xml


<dependencies> </dependencies>

. Junit ( 4.12).


pom-:


image



. src : «main» «test». , , «test». «test», «java» «New», «Package». . , Groupid — «org.example».


— Java, . «New», «Java Class».


Java , , LoginTest ( Java ). IDE :


image


IDE


, IDE. «Open Module Settings». «Sources» «Language level» 5. 8 ( , Java) :


image


Java: «File», Settings.


«Build, Execution, Deployment» -> «Compiler» -> «Java Compiler». 1.5. 8 :


image


Test Suite


:


  1. ;
  2. ;
  3. — ;
  4. «…».

, .


( ).



LoginTest . «setup()», . , :


WebDriver driver = new ChromeDriver();

WebDriver , chomedriver ( Windows .exe):


System.setProperty("webdriver.chrome.driver", "/usr/bin/chromedriver");

, :


driver.manage().window().maximaze();

, , . . : . Implicitly Wait, :


driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

, , (10 ) 500 . , , , .


:


driver.get("https://passport.yandex.ru/auth")


( ).


«test» «resources», «conf.properties», :


loginpage = https://passport.yandex.ru/auth


chromedriver = /usr/bin/chromedriver

image


«org.example» «ConfProperties», «conf.properties» :


image


package org.example;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class ConfProperties {
    protected static FileInputStream fileInputStream;
    protected static Properties PROPERTIES;
    static {
        try {
            //     
            fileInputStream = new FileInputStream("src/test/resources/conf.properties");
            PROPERTIES = new Properties();
            PROPERTIES.load(fileInputStream);
        } catch (IOException e) {
            e.printStackTrace();
            //   (   ..)
        } finally {
            if (fileInputStream != null)
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace(); } } }
    /**
     *          
     */
    public static String getProperty(String key) {
        return PROPERTIES.getProperty(key); } }


image


«setup()» Junit «@BeforeClass», , . Junit Test.


package org.example;
import org.junit.BeforeClass;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class LoginTest {
    /**
     *   
     */
    @BeforeClass
    public static void setup() {
        //      
        System.setProperty("webdriver.chrome.driver", ConfProperties.getProperty("chromedriver"));
        //  
        WebDriver driver = new ChromeDriver();
        //    
        driver.manage().window().maximize();
        //    = 10 .
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        //       
        driver.get(ConfProperties.getProperty("loginpage")); } }

Page Object


Page Object , , .


«org.example» LoginPage, .


(https://passport.yandex.ru/auth) Chrome. , , . « ». ( ) — .


. . , «Copy» -> «Copy XPath».


//*[@id="root"]/div/div/div[2]/div/div/div[3]/div[2]/div/div/div[1]/form/div[1]/div[1]/label

Page Object @FindBy.


:


@FindBy(xpath = "//*[@id="root"]/div/div/div[2]/div/div/div[3]/div[2]/div/div/div[1]/form/div[1]/div[1]/label")
private WebElement loginField;

loginField ( LoginPage, .. ).


, xpath ( « xpath ». , , id:


image


xpath:


@FindBy(xpath = "//*[contains(@id, 'passp-field-login')]")

, , .


.


«»:


@FindBy(xpath = "//*[contains(text(), '')]")
private WebElement loginBtn;

:


@FindBy(xpath = "//*[contains(@id, 'passp-field-passwd')]")
private WebElement passwdField;

.


:


public void inputLogin(String login) {
        loginField.sendKeys(login); }

:


public void inputPasswd(String passwd) {
        passwdField.sendKeys(passwd); }

:


public void clickLoginBtn() {
        loginBtn.click(); }

, @FindBy , PageFactory. Webdriver:


public WebDriver driver;
public LoginPage(WebDriver driver) {
        PageFactory.initElements(driver, this);
        this.driver = driver; }

image


package org.example;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class LoginPage {
    /**
     *  ,    
     */
    public WebDriver driver;
    public LoginPage(WebDriver driver) {
        PageFactory.initElements(driver, this);
        this.driver = driver; }
    /**
     *     
     */
    @FindBy(xpath = "//*[contains(@id, 'passp-field-login')]")
    private WebElement loginField;
    /**
     *      
     */
    @FindBy(xpath = "//*[contains(text(), '')]/..")
    private WebElement loginBtn;
    /**
     *     
     */
    @FindBy(xpath = "//*[contains(@id, 'passp-field-passwd')]")
    private WebElement passwdField;
    /**
     *    
     */
    public void inputLogin(String login) {
        loginField.sendKeys(login); }
    /**
     *    
     */
    public void inputPasswd(String passwd) {
        passwdField.sendKeys(passwd); }
    /**
     *        
     */
    public void clickLoginBtn() {
        loginBtn.click(); } }

. .. , Page Object . ProfilePage, ( ), . , , .


, :


image


package org.example;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class ProfilePage {
    /**
     *  ,    
     */
    public WebDriver driver;
    public ProfilePage(WebDriver driver) {
        PageFactory.initElements(driver, this);
        this.driver = driver; }
    /**
     *    
     */
    @FindBy(xpath = "//*[contains(@class, 'account__name_hasAccentLetter')]")
    private WebElement userMenu;
    /**
     *      
     */
    @FindBy(xpath = "//*[contains(@class, 'menu-item_action_exit menu__item menu__item_type_link')]")
    private WebElement logoutBtn;
    /**
     *        
     */
    public String getUserName() {
        String userName = userMenu.getText();
        return userName; }
    /**
     *      
     */
    public void entryMenu() {
        userMenu.click(); }
    /**
     *       
     */
    public void userLogout() {
        logoutBtn.click(); } }

: getUserName() , .. «» . , . getUserName() :


public String getUserName() {
        WebDriverWait wait = new WebDriverWait(driver, 10);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[contains(@class, 'account__name_hasAccentLetter')]")));
        String userName = userMenu.getText();
        return userName; }

LoginTest - :


public static LoginPage loginPage;
public static ProfilePage profilePage;


public static WebDriver driver;

@BeforeClass . new. driver, , :


loginPage = new LoginPage(driver);
profilePage = new ProfilePage(driver);

(.. ):


driver = new ChromeDriver();


. loginTest() :


@Test
    public void loginTest() {
        // login/password        chromedriver
// loginpage
// 
        loginPage.inputLogin(ConfProperties.getProperty("login"));
    //  
        loginPage.clickLoginBtn();
    // 
        loginPage.inputPasswd(ConfProperties.getProperty("password"));
    //  
        loginPage.clickLoginBtn();
        //  
        String user = profilePage.getUserName();
    //       
        Assert.assertEquals(ConfProperties.getProperty("login"), user); }

. @AfterClass ( , ).


«», .


@AfterClass
    public static void tearDown() {
        profilePage.entryMenu();
        profilePage.userLogout();
        driver.quit(); }

.



, :


package org.example;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class LoginTest {
    public static LoginPage loginPage;
    public static ProfilePage profilePage;
    public static WebDriver driver;

    /**
     *   
     */
    @BeforeClass
    public static void setup() {
        //      
        System.setProperty("webdriver.chrome.driver", ConfProperties.getProperty("chromedriver"));
        //  
        driver = new ChromeDriver();
        loginPage = new LoginPage(driver);
        profilePage = new ProfilePage(driver);
        //    
        driver.manage().window().maximize();
        //    = 10 .
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        //       
        driver.get(ConfProperties.getProperty("loginpage")); }
    /**
     *     
     */
    @Test
    public void loginTest() {
        //     LoginPage     
        // login/password        chromedriver
        // loginpage
        // 
        loginPage.inputLogin(ConfProperties.getProperty("login"));
        //  
        loginPage.clickLoginBtn();
        // 
        loginPage.inputPasswd(ConfProperties.getProperty("password"));
        //  
        loginPage.clickLoginBtn();
        //  
        String user = profilePage.getUserName();
        //       
        Assert.assertEquals(ConfProperties.getProperty("login"), user); }
    /**
     *         
     */
    @AfterClass
    public static void tearDown() {
        profilePage.entryMenu();
        profilePage.userLogout();
        driver.quit(); } }


Intellij Idea :


  • Alt+Shift+F10;
  • , Run;

, Idea , loginTest() :


image


All Articles