本文讨论了一个相当简单的自测的创建。本文将对初学者自动化很有用。
所提供的资料尽可能地易于访问,但是,如果您至少对Java语言有最少的想法(类,方法等),那么将更容易理解我们在说什么。
我们会需要:
- 已安装的开发环境Intellij IDEA(是最受欢迎的IDE,在大多数情况下,社区版本的免费版本就足够了);
- 在操作系统的系统环境中注册的已安装Java(jdk / openjdk)和Maven;
- Chrome浏览器和chromedriver-用于将命令传输到浏览器的程序。
项目创建
运行Intellij IDEA,检查有关发送统计信息,导入项目,选择配色方案等的前几点。-只需选择默认选项即可。
在最后出现的窗口中,选择“创建新项目”,项目类型为Maven。该窗口将如下所示:

- Maven是Java项目的构建工具。
- Project SDK-计算机上安装的Java版本;
- 从原型创建是创建具有特定原型的项目的能力(在此阶段,无需选中此复选框)。
点击下一步。” 将打开以下窗口:

Groupid和Artifactid是Maven中的项目标识符。填写这些项目有一些规则:
- 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() :
