Test Data
test address
http://120.78.128.25:8765/
Investors
13323234545
lemon123456
Borrower
13323234444
lemon best
background address
http://120.78.128.25:8765/Admin/Index/login.html
lemon7
lemon best
Page Object
Introduction to PO
https://www.selenium.dev/documentation/en/guidelines_and_recommendations/page_object_models/
Page Object is one of the best design patterns for Selenium automated test project development practices. Page Object is mainly reflected in the encapsulation of interface interaction details , which can make test cases more concerned with business rather than interface details, and improve the readability of test cases sex.
The advantages of the Page Object design pattern are as follows:
- Reduce code duplication;
- Improve the readability of test cases;
- Improve the maintainability of test cases, especially for projects with frequent UI changes;
Notes on using Paget Object:
- The public method represents the functionality provided by Page
- Try not to expose the internal details of the Page
- don't assert
- Methods can return other Page Objects
- Page Objects does not need to represent the entire page, it can be any part
- The same operation, different results should be separated (correct login, wrong login)
Using po is the process of abstracting or modeling the page, which needs to treat the page as an object.
In object-oriented programming languages, the following two points need to be considered for object-oriented programming:
- The attribute of the object "global variable" == the element of the web page "location method, location value"
- The behavior of the object "method" == the operation of the element of the web page
Write the page operation base class
package com.zhongxin.pages;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class BasePage {
private WebDriver driver;
public BasePage(WebDriver driver) {
this.driver = driver;
}
/**
* 封装显式等待
*
* @param by 元素定位信息
* @return 元素对象
*/
public WebElement waitElementBisibility(By by) {
WebElement element = null;
try {
//5秒元素可见显式等待
WebDriverWait wait = new WebDriverWait(driver, 5);
element = wait.until(ExpectedConditions.visibilityOfElementLocated(by));
return element;
} catch (Exception e) {
System.out.println("元素定位异常" + e.getMessage());
}
return null;
}
}
Landing page packaging
package com.zhongxin.pages;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class LoginPage extends BasePage {
private WebDriver driver;
// phone元素定位信息
private By phoneBy = By.name("phone");
//password元素定位信息
private By passwordBy = By.name("password");
//登陆按钮元素定位信息
private By loginBtnBy = By.xpath("//button[@class='btn btn-special']");
//页面中央错误提示
private By centerErrorBy = By.className("layui-layer-content");
//页面手机红色错误提示
private By phoneformErrorBy = By.xpath("//input[@name='phone']//following-sibling::div");
//页面密码红色错误提示
private By passwordformErrorBy = By.xpath("//input[@name='password']//following-sibling::div");
public LoginPage(WebDriver driver) {
super(driver);
}
//对手机框进行输入
public void inputPhone(String phone) {
WebElement element = waitElementBisibility(phoneBy);
if (element != null) {
element.clear();
element.sendKeys(phone);
}
}
//对密码框进行输入
public void inputPassword(String password) {
WebElement element = waitElementBisibility(passwordBy);
if (element != null) {
element.clear();
element.sendKeys(password);
}
}
//点击登陆按钮
public void clickLoginBtn() {
WebElement element = waitElementBisibility(loginBtnBy);
if (element != null) {
element.click();
}
}
//获取页面中央错误提示文本
public String getCenterErrorText() {
WebElement element = waitElementBisibility(centerErrorBy);
if (element != null) {
return element.getText();
}
return "";
}
//获取页面手机红色错误提示
public String getPhoneFormErrorText() {
WebElement element = waitElementBisibility(phoneformErrorBy);
if (element != null) {
return element.getText();
}
return "";
}
//获取页面手机红色错误提示
public String getPasswordFormErrorText() {
WebElement element = waitElementBisibility(passwordformErrorBy);
if (element != null) {
return element.getText();
}
return "";
}
}
Home Package
package com.zhongxin.pages;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.WebDriverWait;
public class IndexPage extends BasePage {
private WebDriver driver;
//昵称
private By nicknameBy = By.xpath("//a[contains(text(),'我的帐户[自动化测试帐号1]')]");
public IndexPage(WebDriver driver) {
super(driver);
}
// 昵称是否可见
public boolean nicknameIsVisibility() {
WebElement element = waitElementBisibility(nicknameBy);
if (element != null) {
return element.isDisplayed();
}
return false;
}
}
test code
package com.zhongxin.cases;
import com.zhongxin.pages.IndexPage;
import com.zhongxin.pages.LoginPage;
import okio.Timeout;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class LoginCase {
public WebDriver driver;
@BeforeClass
public void setUp() {
//打开浏览器
driver = open("chrome");
//访问登陆页面
driver.get("http://120.78.128.25:8765/Index/login.html");
}
@Test
public void testFailed01() throws InterruptedException {
LoginPage loginpage = new LoginPage(driver);
loginpage.inputPhone("13212312312");
loginpage.inputPassword("123123123");
loginpage.clickLoginBtn();
String actual = loginpage.getCenterErrorText();
String expected = "此账号没有经过授权,请联系管理员!";
Assert.assertEquals(actual, expected);
}
@Test
public void testFailed02() throws InterruptedException {
LoginPage loginpage = new LoginPage(driver);
loginpage.inputPhone("");
loginpage.inputPassword("123123123");
loginpage.clickLoginBtn();
String actual = loginpage.getPhoneFormErrorText();
String expected = "请输入手机号";
Assert.assertEquals(actual, expected);
}
@Test(dataProvider = "datas")
public void testFailed03(String phone, String password, String expected) throws InterruptedException {
LoginPage loginpage = new LoginPage(driver);
loginpage.inputPhone(phone);
loginpage.inputPassword(password);
loginpage.clickLoginBtn();
String actual = loginpage.getPhoneFormErrorText();
Assert.assertEquals(actual, expected);
}
@Test
public void testFailed04() throws InterruptedException {
LoginPage loginpage = new LoginPage(driver);
loginpage.inputPhone("15859019251");
loginpage.inputPassword("");
loginpage.clickLoginBtn();
String actual = loginpage.getPasswordFormErrorText();
Assert.assertEquals(actual, "请输入密码");
}
@Test
public void testFailed05() throws InterruptedException {
LoginPage loginpage = new LoginPage(driver);
loginpage.inputPhone("13323234545");
loginpage.inputPassword("123123123");
loginpage.clickLoginBtn();
String actual = loginpage.getCenterErrorText();
Assert.assertEquals(actual, "帐号或密码错误!");
}
@Test
public void testSuccess() throws InterruptedException {
LoginPage loginpage = new LoginPage(driver);
loginpage.inputPhone("13323234545");
loginpage.inputPassword("lemon123456");
loginpage.clickLoginBtn();
Thread.sleep(2000);
IndexPage indexPage = new IndexPage(driver);
boolean flag = indexPage.nicknameIsVisibility();
Assert.assertTrue(flag);
}
@DataProvider
public Object[][] datas() {
Object[][] datas = {
{"", "123123123", "请输入手机号"},
{"1585901925", "123123123", "请输入正确的手机号"},
{"158590192534", "123123123", "请输入正确的手机号"},
{"1585901925%", "123123123", "请输入正确的手机号"},
};
return datas;
}
@AfterClass
public void tearDown() throws InterruptedException {
Thread.sleep(2000);
//关闭浏览器
close(driver);
}
public static void close(WebDriver driver) throws InterruptedException {
Thread.sleep(3000);
driver.quit();
}
public static WebDriver open(String type) {
WebDriver driver = null;
if ("chrome".equalsIgnoreCase(type)) {
System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver");
driver = new ChromeDriver();
} else if ("ie".equalsIgnoreCase(type)) {
// 设置ie启动项
DesiredCapabilities capabilities = new DesiredCapabilities();
// 忽略缩放
capabilities.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING, true);
// 忽略保护模式
capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
// 设置初始化浏览器地址
capabilities.setCapability(InternetExplorerDriver.INITIAL_BROWSER_URL, "https://www.baidu.com");
// 配置ie驱动位置
System.setProperty("webdriver.ie.driver", "src/test/resources/IEDriverServer.exe");
// 创建ie驱动对象
driver = new InternetExplorerDriver();
} else if ("firefox".equalsIgnoreCase(type)) {
System.setProperty("webdriver.firefox.bin", "D:\\Mozilla Firefox\\firefox.exe");
System.setProperty("webdriver.firefox.driver", "src/test/resources/geckodriver.exe");
driver = new FirefoxDriver();
}
return driver;
}
}
Finally, a modest effort
Thanks to everyone who read my article carefully, although it is not a very valuable thing, if you can use it, you can take it away:
These materials should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey, and I hope it can help you too!