美文网首页
登录模块-反向case自动化测试实现(3)

登录模块-反向case自动化测试实现(3)

作者: testerPM | 来源:发表于2020-01-26 16:11 被阅读0次

登录模块其他反向用例编写


package com.lemon.testcases;

import static org.testng.Assert.assertTrue;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class Logincases {

    public static WebDriver driver;

    @BeforeMethod
    public void setUp() {
        // 前置
        // 打开浏览器
        openBrowser("chrome");
        driver.get("http://120.78.128.25:8765/Index/login.html");
    }

    /**
     * 反向case数据提供源01
     * 
     * @return
     */
    @DataProvider
    public Object[][] loginDatas01() {

        Object[][] datas = { { "13333333331", "lemon123456", "此账号没有经过授权,请联系管理员!" },
                { "13323234545", "lemon12345", "帐号或密码错误!" }, { "13323234545", "lemon1234567", "帐号或密码错误!" },
                { "13323234545", "LEMON123456", "帐号或密码错误!" }, { "13323234545", "lemon  123456", "帐号或密码错误!" }

        };

        return datas;
    }

    /**
     * 反向case数据提供源02
     * 
     * @return
     */
    @DataProvider
    public Object[][] loginDatas02() {

        Object[][] datas = { { "", "lemon123456", "请输入手机号" }, { "13323234545", "", "请输入密码" },
                { "1332323454", "lemon123456", "请输入正确的手机号" }, { "133232345456", "lemon123456", "请输入正确的手机号" },
                { "133232$454%", "lemon123456", "请输入正确的手机号" }

        };

        return datas;
    }

    /**
     * 正向用例 成功登录
     */
    @Test(priority = 1, enabled = false)
    public void Login_success() throws InterruptedException {
        // 找到手机号输入框 输入手机号
        WebElement phone = driver.findElement(By.name("phone"));
        phone.sendKeys("13323234545");
        // 找到到密码输入框 输入密码
        WebElement password = driver.findElement(By.name("password"));
        password.sendKeys("lemon123456");
        // 找到登录按钮 点击登录
        WebElement login = driver.findElement(By.xpath("//button[text()='登录']"));
        login.click();
        // 断言
        // 显示等待----我的账户元素可见且存在
        WebDriverWait wait = new WebDriverWait(driver, 6);
        WebElement until = wait
                .until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[contains(text(),'我的帐户')]")));
        // 断言--我的账户 元素可见 until.isDisplayed()==true 说明断言成功
        Assert.assertTrue(until.isDisplayed());

    }

    /**
     * 反向用例1 未注册 密码错误(区分大小写,密码长度与正确的密码长度少一位和多一位)
     */
    @Test(priority = 2, dataProvider = "loginDatas01", enabled = false)
    public void login_failure01(String mobilephone, String pwd, String msg) {
        // 找到手机号输入框 输入 未注册的手机号
        WebElement phone = driver.findElement(By.name("phone"));
        phone.sendKeys(mobilephone);
        // 找到到密码输入框 输入密码
        WebElement password = driver.findElement(By.name("password"));
        password.sendKeys(pwd);
        // 找到登录按钮 点击登录
        WebElement login = driver.findElement(By.xpath("//button[text()='登录']"));
        login.click();
        // 断言
        WebDriverWait wait = new WebDriverWait(driver, 5);
        WebElement until = wait
                .until(ExpectedConditions.visibilityOfElementLocated(By.className("layui-layer-content")));
        String actualValue = msg;
        String expectedValue = until.getText();// 获取元素的文本的值
        Assert.assertEquals(actualValue, expectedValue);

    }

    /**
     * 反向用例02 密码为空 账号为空 账号错误(10位,12位,数字和特殊字符组合)
     * 
     * @param mobilephone
     * @param pwd
     * @param msg
     */
    @Test(priority = 3, dataProvider = "loginDatas02", enabled = false)
    public void login_failure02(String mobilephone, String pwd, String msg) {
        // 找到手机号输入框 输入 未注册的手机号
        WebElement phone = driver.findElement(By.name("phone"));
        phone.sendKeys(mobilephone);
        // 找到到密码输入框 输入密码
        WebElement password = driver.findElement(By.name("password"));
        password.sendKeys(pwd);
        // 找到登录按钮 点击登录
        WebElement login = driver.findElement(By.xpath("//button[text()='登录']"));
        login.click();
        // 断言
        WebDriverWait wait = new WebDriverWait(driver, 5);
        WebElement until = wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("form-error-info")));
        String actualValue = msg;
        String expectedValue = until.getText();// 获取元素的文本的值
        Assert.assertEquals(actualValue, expectedValue);

    }

    /**
     * 记住手机号
     * 记住手机号,这里默认是勾选的,所有脚本里面不用再找到
     * 记住手机号的元素进行.click()
     * @param mobilephone
     * @param pwd
     * @param msg
     */
    @Test
    public void login_remeber() {
        // 找到手机号输入框 输入 未注册的手机号
        WebElement phone = driver.findElement(By.name("phone"));
        phone.sendKeys("13323234545");
        // 找到到密码输入框 输入密码
        WebElement password = driver.findElement(By.name("password"));
        password.sendKeys("lemon123456");
        // 找到登录按钮 点击登录
        WebElement login = driver.findElement(By.xpath("//button[text()='登录']"));
        login.click();
        // 退出
        WebDriverWait wait = new WebDriverWait(driver, 5);
        wait.until(ExpectedConditions.elementToBeClickable(By.partialLinkText("退出"))).click();
    
        // 登录
        wait.until(ExpectedConditions.elementToBeClickable(By.partialLinkText("登录"))).click();
        // 获取手机号---注意这里的手机号是<input value="手机号">
        String actualValue = wait
                .until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@name=\"phone\"]")))
                .getAttribute("value");
        String expectedValue = "13323234545";// 获取元素的文本的值
        Assert.assertEquals(actualValue, expectedValue);

    }

    @AfterMethod
    public void teardown() {
        // 退出浏览器
        driver.quit();
    }

    public static void openBrowser(String browser) {
        if (browser.equals("chrome")) {
            // 1.设置chromedriver驱动文件的路径
            System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver.exe");
            // 2.打开浏览器
            driver = new ChromeDriver();
            // 4.退出浏览器即关闭浏览器
            // quit是退出浏览器,close是只关闭当前打开的窗口,不等于关闭整个浏览器
            // driver.quit();
        }
    }

}





(1)@Test(priority = 3, dataProvider = "loginDatas02", enabled = false)
priority = 3,执行的优先级,值越高,越最后执行
dataProvider = "loginDatas02",数据驱动
enabled = false 该条脚本不执行

相关文章

网友评论

      本文标题:登录模块-反向case自动化测试实现(3)

      本文链接:https://www.haomeiwen.com/subject/vtbjthtx.html