Java自動テスト(Web自動テストフレームワーク)

 

テストデータ

テストアドレス

http://120.78.128.25:8765/

投資家

13323234545

レモン123456

借り手

13323234444

レモンベスト

バックグラウンドアドレス

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

レモン7

レモンベスト

ページオブジェクト

POの紹介

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

ページ オブジェクトは、 Selenium 自動テスト プロジェクトの開発プラクティスに最適な設計パターンの 1 つです。ページ オブジェクトは主にインターフェイス対話の詳細のカプセル化に反映され、テスト ケースがインターフェイスの詳細ではなくビジネスに関心を持つようになり、テストの読みやすさが向上します。場合のセックス。

ページ オブジェクト デザイン パターンの利点は次のとおりです。

  • コードの重複を減らします。
  • テストケースの読みやすさを向上させます。
  • 特に UI が頻繁に変更されるプロジェクトの場合、テスト ケースの保守性が向上します。

Paget オブジェクトの使用に関する注意:

  • public メソッドは、Page によって提供される機能を表します。
  • ページの内部詳細を公開しないようにしてください
  • 主張しないでください
  • メソッドは他のページ オブジェクトを返すことができます
  • ページ オブジェクトはページ全体を表す必要はなく、どの部分であってもかまいません
  • 同じ操作でも異なる結果は分離する必要があります (正しいログイン、間違ったログイン)

po の使用は、ページを抽象化またはモデル化するプロセスであり、ページをオブジェクトとして扱う必要があります。

オブジェクト指向プログラミング言語では、次の 2 点を考慮する必要があります。

  1. オブジェクトの属性「グローバル変数」 == Webページの要素「ロケーションメソッド、ロケーション値」
  2. オブジェクト「メソッド」の動作 == Webページの要素の動作

ページ操作の基本クラスを作成する

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

ランディングページのパッケージ化

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


}

ホームパッケージ

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

}

テストコード

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

最後に、ささやかな努力

私の記事を注意深く読んでくださった皆さんに感謝します。あまり価値のあるものではありませんが、使用できる場合は持ち帰っても構いません。

 これらの資料は、[ソフトウェア テスト] の友人にとって、最も包括的かつ完全な準備倉庫となるはずです。この倉庫は、最も困難な旅を乗り越える何万人ものテスト エンジニアにも同行してきました。そして、あなたにも役立つことを願っています。

情報取得方法:

おすすめ

転載: blog.csdn.net/qq_56271699/article/details/131263717