Este artículo analiza la creación de una autoevaluación bastante simple. Este artículo será útil para la automatización de principiantes.
El material se presenta lo más accesible posible, sin embargo, será mucho más fácil entender de qué estamos hablando si tiene al menos ideas mínimas sobre el lenguaje Java: clases, métodos, etc.
Necesitaremos:
- Entorno de desarrollo instalado Intellij IDEA (es el IDE más popular, para la mayoría de los casos es suficiente una versión gratuita de Community Edition);
- Java instalado (jdk / openjdk) y Maven, registrado en el entorno del sistema operativo;
- Chrome browser y chromedriver: un programa para transmitir comandos al navegador.
Creación de proyectos
Ejecute Intellij IDEA, repase los primeros puntos relacionados con el envío de estadísticas, la importación de proyectos, la elección de un esquema de color, etc. - solo seleccione las opciones predeterminadas.
En la ventana que aparece al final, seleccione "Crear nuevo proyecto", y en ella el tipo de proyecto es Maven. La ventana se verá así:

- Maven es una herramienta de construcción para proyectos Java;
- Project SDK: la versión de Java que está instalada en la computadora;
- Crear desde arquetipo es la capacidad de crear un proyecto con un arquetipo específico (en esta etapa, esta casilla de verificación no necesita ser marcada).
Haga clic en Siguiente." Se abrirá la siguiente ventana:

Groupid y Artifactid son identificadores de proyecto en Maven. Existen ciertas reglas para llenar estos artículos:
- Groupid — . Java: . , -, com.email.email;
- Artifactid — ;
- Version — .
«Finish»: IDE pom.xml:

, : Groupid, Artefiactid, Version. Pom.xml — . Pom- (), .
: Selenium Java Junit. Maven mvnrepository.com, Selenium Java :

( 3.14.0). :

«Maven» pom.xml
<dependencies> </dependencies>
. Junit ( 4.12).
pom-:

. src : «main» «test». , , «test». «test», «java» «New», «Package». . , Groupid — «org.example».
— Java, . «New», «Java Class».
Java , , LoginTest ( Java ). IDE :

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

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

Test Suite
:
- ;
- ;
- — ;
- «…».
, .
( ).
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:
chromedriver = /usr/bin/chromedriver

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

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); } }

«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();
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».
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:

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; }

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, ( ), . , , .
, :

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() {
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();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get(ConfProperties.getProperty("loginpage")); }
@Test
public void loginTest() {
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 :
, Idea , loginTest() :
