当前位置:   article > 正文

Java自动化测试(web自动化测试框架 )_java 自动化测试

java 自动化测试

 

测试数据

测试地址

http://120.78.128.25:8765/

投资人

13323234545

lemon123456

借款人

13323234444

lemonbest

后台地址

http://120.78.128.25:8765/Admin/Index/login.html

lemon7

lemonbest

Page Object

PO简介

https://www.selenium.dev/documentation/en/guidelines_and_recommendations/page_object_models/

Page Object 是 Selenium 自动化测试项目开发实践的最佳设计模式之一,Page Object 的主要体现于对界面交互细节的封装,这样可以使测试案例更关注与业务而非界面细节,提高测试案例的可读性。

Page Object 设计模式的优点如下:

  • 减少代码的重复;
  • 提高测试用例的可读性;
  • 提高测试用例的可维护性,特别是针对 UI 频繁变化的项目;

Paget Object 使用注意:

  • public方法代表Page提供的功能
  • 尽量不要暴露Page的内部细节
  • 不要assertion
  • 方法可以返回其他Page Objects
  • Page Objects不用代表整个页面,可以是任意一个部分
  • 一样的操作,不同的结果应该分开(正确登录,错误登录)

使用po是对页面进行抽象或者说建模的过程,需要把页面当作一个对象。

面向对象编程语言中,进行面向对象编程需要考虑以下两点:

  1. 对象的属性「全局变量」 == web页面的元素「定位方法,定位值」
  2. 对象的行为「方法」 == web页面的元素的操作

编写页面操作基类

  1. package com.zhongxin.pages;
  2. import org.openqa.selenium.By;
  3. import org.openqa.selenium.WebDriver;
  4. import org.openqa.selenium.WebElement;
  5. import org.openqa.selenium.support.ui.ExpectedConditions;
  6. import org.openqa.selenium.support.ui.WebDriverWait;
  7. public class BasePage {
  8. private WebDriver driver;
  9. public BasePage(WebDriver driver) {
  10. this.driver = driver;
  11. }
  12. /**
  13. * 封装显式等待
  14. *
  15. * @param by 元素定位信息
  16. * @return 元素对象
  17. */
  18. public WebElement waitElementBisibility(By by) {
  19. WebElement element = null;
  20. try {
  21. //5秒元素可见显式等待
  22. WebDriverWait wait = new WebDriverWait(driver, 5);
  23. element = wait.until(ExpectedConditions.visibilityOfElementLocated(by));
  24. return element;
  25. } catch (Exception e) {
  26. System.out.println("元素定位异常" + e.getMessage());
  27. }
  28. return null;
  29. }
  30. }

登陆页面封装

  1. package com.zhongxin.pages;
  2. import org.openqa.selenium.By;
  3. import org.openqa.selenium.WebDriver;
  4. import org.openqa.selenium.WebElement;
  5. import org.openqa.selenium.support.ui.ExpectedConditions;
  6. import org.openqa.selenium.support.ui.WebDriverWait;
  7. public class LoginPage extends BasePage {
  8. private WebDriver driver;
  9. // phone元素定位信息
  10. private By phoneBy = By.name("phone");
  11. //password元素定位信息
  12. private By passwordBy = By.name("password");
  13. //登陆按钮元素定位信息
  14. private By loginBtnBy = By.xpath("//button[@class='btn btn-special']");
  15. //页面中央错误提示
  16. private By centerErrorBy = By.className("layui-layer-content");
  17. //页面手机红色错误提示
  18. private By phoneformErrorBy = By.xpath("//input[@name='phone']//following-sibling::div");
  19. //页面密码红色错误提示
  20. private By passwordformErrorBy = By.xpath("//input[@name='password']//following-sibling::div");
  21. public LoginPage(WebDriver driver) {
  22. super(driver);
  23. }
  24. //对手机框进行输入
  25. public void inputPhone(String phone) {
  26. WebElement element = waitElementBisibility(phoneBy);
  27. if (element != null) {
  28. element.clear();
  29. element.sendKeys(phone);
  30. }
  31. }
  32. //对密码框进行输入
  33. public void inputPassword(String password) {
  34. WebElement element = waitElementBisibility(passwordBy);
  35. if (element != null) {
  36. element.clear();
  37. element.sendKeys(password);
  38. }
  39. }
  40. //点击登陆按钮
  41. public void clickLoginBtn() {
  42. WebElement element = waitElementBisibility(loginBtnBy);
  43. if (element != null) {
  44. element.click();
  45. }
  46. }
  47. //获取页面中央错误提示文本
  48. public String getCenterErrorText() {
  49. WebElement element = waitElementBisibility(centerErrorBy);
  50. if (element != null) {
  51. return element.getText();
  52. }
  53. return "";
  54. }
  55. //获取页面手机红色错误提示
  56. public String getPhoneFormErrorText() {
  57. WebElement element = waitElementBisibility(phoneformErrorBy);
  58. if (element != null) {
  59. return element.getText();
  60. }
  61. return "";
  62. }
  63. //获取页面手机红色错误提示
  64. public String getPasswordFormErrorText() {
  65. WebElement element = waitElementBisibility(passwordformErrorBy);
  66. if (element != null) {
  67. return element.getText();
  68. }
  69. return "";
  70. }
  71. }

首页封装

  1. package com.zhongxin.pages;
  2. import org.openqa.selenium.By;
  3. import org.openqa.selenium.WebDriver;
  4. import org.openqa.selenium.WebElement;
  5. import org.openqa.selenium.support.ui.WebDriverWait;
  6. public class IndexPage extends BasePage {
  7. private WebDriver driver;
  8. //昵称
  9. private By nicknameBy = By.xpath("//a[contains(text(),'我的帐户[自动化测试帐号1]')]");
  10. public IndexPage(WebDriver driver) {
  11. super(driver);
  12. }
  13. // 昵称是否可见
  14. public boolean nicknameIsVisibility() {
  15. WebElement element = waitElementBisibility(nicknameBy);
  16. if (element != null) {
  17. return element.isDisplayed();
  18. }
  19. return false;
  20. }
  21. }

测试代码

  1. package com.zhongxin.cases;
  2. import com.zhongxin.pages.IndexPage;
  3. import com.zhongxin.pages.LoginPage;
  4. import okio.Timeout;
  5. import org.openqa.selenium.*;
  6. import org.openqa.selenium.chrome.ChromeDriver;
  7. import org.openqa.selenium.firefox.FirefoxDriver;
  8. import org.openqa.selenium.ie.InternetExplorerDriver;
  9. import org.openqa.selenium.remote.DesiredCapabilities;
  10. import org.openqa.selenium.support.ui.ExpectedConditions;
  11. import org.openqa.selenium.support.ui.WebDriverWait;
  12. import org.testng.Assert;
  13. import org.testng.annotations.AfterClass;
  14. import org.testng.annotations.BeforeClass;
  15. import org.testng.annotations.DataProvider;
  16. import org.testng.annotations.Test;
  17. public class LoginCase {
  18. public WebDriver driver;
  19. @BeforeClass
  20. public void setUp() {
  21. //打开浏览器
  22. driver = open("chrome");
  23. //访问登陆页面
  24. driver.get("http://120.78.128.25:8765/Index/login.html");
  25. }
  26. @Test
  27. public void testFailed01() throws InterruptedException {
  28. LoginPage loginpage = new LoginPage(driver);
  29. loginpage.inputPhone("13212312312");
  30. loginpage.inputPassword("123123123");
  31. loginpage.clickLoginBtn();
  32. String actual = loginpage.getCenterErrorText();
  33. String expected = "此账号没有经过授权,请联系管理员!";
  34. Assert.assertEquals(actual, expected);
  35. }
  36. @Test
  37. public void testFailed02() throws InterruptedException {
  38. LoginPage loginpage = new LoginPage(driver);
  39. loginpage.inputPhone("");
  40. loginpage.inputPassword("123123123");
  41. loginpage.clickLoginBtn();
  42. String actual = loginpage.getPhoneFormErrorText();
  43. String expected = "请输入手机号";
  44. Assert.assertEquals(actual, expected);
  45. }
  46. @Test(dataProvider = "datas")
  47. public void testFailed03(String phone, String password, String expected) throws InterruptedException {
  48. LoginPage loginpage = new LoginPage(driver);
  49. loginpage.inputPhone(phone);
  50. loginpage.inputPassword(password);
  51. loginpage.clickLoginBtn();
  52. String actual = loginpage.getPhoneFormErrorText();
  53. Assert.assertEquals(actual, expected);
  54. }
  55. @Test
  56. public void testFailed04() throws InterruptedException {
  57. LoginPage loginpage = new LoginPage(driver);
  58. loginpage.inputPhone("15859019251");
  59. loginpage.inputPassword("");
  60. loginpage.clickLoginBtn();
  61. String actual = loginpage.getPasswordFormErrorText();
  62. Assert.assertEquals(actual, "请输入密码");
  63. }
  64. @Test
  65. public void testFailed05() throws InterruptedException {
  66. LoginPage loginpage = new LoginPage(driver);
  67. loginpage.inputPhone("13323234545");
  68. loginpage.inputPassword("123123123");
  69. loginpage.clickLoginBtn();
  70. String actual = loginpage.getCenterErrorText();
  71. Assert.assertEquals(actual, "帐号或密码错误!");
  72. }
  73. @Test
  74. public void testSuccess() throws InterruptedException {
  75. LoginPage loginpage = new LoginPage(driver);
  76. loginpage.inputPhone("13323234545");
  77. loginpage.inputPassword("lemon123456");
  78. loginpage.clickLoginBtn();
  79. Thread.sleep(2000);
  80. IndexPage indexPage = new IndexPage(driver);
  81. boolean flag = indexPage.nicknameIsVisibility();
  82. Assert.assertTrue(flag);
  83. }
  84. @DataProvider
  85. public Object[][] datas() {
  86. Object[][] datas = {
  87. {"", "123123123", "请输入手机号"},
  88. {"1585901925", "123123123", "请输入正确的手机号"},
  89. {"158590192534", "123123123", "请输入正确的手机号"},
  90. {"1585901925%", "123123123", "请输入正确的手机号"},
  91. };
  92. return datas;
  93. }
  94. @AfterClass
  95. public void tearDown() throws InterruptedException {
  96. Thread.sleep(2000);
  97. //关闭浏览器
  98. close(driver);
  99. }
  100. public static void close(WebDriver driver) throws InterruptedException {
  101. Thread.sleep(3000);
  102. driver.quit();
  103. }
  104. public static WebDriver open(String type) {
  105. WebDriver driver = null;
  106. if ("chrome".equalsIgnoreCase(type)) {
  107. System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver");
  108. driver = new ChromeDriver();
  109. } else if ("ie".equalsIgnoreCase(type)) {
  110. // 设置ie启动项
  111. DesiredCapabilities capabilities = new DesiredCapabilities();
  112. // 忽略缩放
  113. capabilities.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING, true);
  114. // 忽略保护模式
  115. capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
  116. // 设置初始化浏览器地址
  117. capabilities.setCapability(InternetExplorerDriver.INITIAL_BROWSER_URL, "https://www.baidu.com");
  118. // 配置ie驱动位置
  119. System.setProperty("webdriver.ie.driver", "src/test/resources/IEDriverServer.exe");
  120. // 创建ie驱动对象
  121. driver = new InternetExplorerDriver();
  122. } else if ("firefox".equalsIgnoreCase(type)) {
  123. System.setProperty("webdriver.firefox.bin", "D:\\Mozilla Firefox\\firefox.exe");
  124. System.setProperty("webdriver.firefox.driver", "src/test/resources/geckodriver.exe");
  125. driver = new FirefoxDriver();
  126. }
  127. return driver;
  128. }
  129. }

最后,绵薄之力

感谢每一个认真阅读我文章的人,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

 这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!

资料获取方式:

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号