Java教程

day04

本文主要是介绍day04,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Testng01.java

package com.bwf.autotest.day04;

import org.junit.Test;

public class Testng01 {
    //这两条用例运行是无序的
    //一条测试用例
    @Test
    public void testCase2(){

    }
    //一条测试用例
    @Test
    public void testCase1(){

    }
}

Test02.java

package com.bwf.autotest.day04;

import org.testng.Assert;
import org.testng.annotations.Test;

public class Testng02 {
    //这两条用例运行是有序的,必须先执行testCase2再执行testCase1
    //强调testCase2成功才能运行testCase1 2失败了,1就不运行了

    //一条测试用例
    @Test
    public void testCase2(){
//        Assert.assertEquals(0,1);
    }

    //一条测试用例
    @Test(dependsOnMethods = {"testCase2"})
    public void testCase1(){

    }
}

AddVip.java

package com.bwf.autotest.day04;

import com.bwf.autotest.day02.Tools;
import com.bwf.autotest.day03.BackLoginPage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.util.concurrent.TimeUnit;

/**
 * 后台添加会员
 * 1.后台登录
 * 2.添加会员,登陆成功后
 */
public class AddVip {
    //在类中声明driver变量,公共变量通用于类中所有方法
    WebDriver driver;

    @Test
    public void login(){
        boolean result = BackLoginPage.login(driver,"admin","password","1234");
        //测试数据是否正确
        Assert.assertTrue(result);
    }
    @Test(dependsOnMethods = {"login"})
    public void addMeber(){
        driver.get("http://127.0.0.1:8088/index.php?m=admin&c=index&a=index&pid=4");
        Tools.sleep(5000);
        driver.switchTo().frame("mainFrame");
        driver.findElement(By.xpath("//span[text()='添加']")).click();
        driver.findElement(By.name("username")).sendKeys("张老五");
        driver.findElement(By.name("mobile_phone")).sendKeys("18655998844");
        driver.findElement(By.xpath("//input[@name='sex' and @value='1']")).click();
        driver.findElement(By.name("birthday")).sendKeys("2000-4-16");
        driver.findElement(By.name("email")).sendKeys("1234@qq.com");
        driver.findElement(By.name("qq")).sendKeys("121734906");
        driver.findElement(By.className("button_search")).click();
        Tools.sleep(8000);
        //获取网页源码判断源码中是否包含"重置密码"
        boolean flag = driver.getPageSource().contains("重置密码");
        Assert.assertTrue(flag);
    }

    //运行当前类中所有用例前
    @BeforeClass
    public void before(){
        //打开浏览器
        driver=new FirefoxDriver();
        //窗口最大化
        driver.manage().window().maximize();
        //隐式等待
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    }

    //运行当前所有用例之后
    @AfterClass
    public void after(){
//        driver.quit(); //quit方法关闭所有的标签页
        driver.close(); //close 方法只关闭当前标签,如chrome ,在脚本运行时,产生了两个标签页,则只关闭当前driver所处的标签页
    }
}

Register.java

package com.bwf.autotest.day04;

import com.bwf.autotest.day02.Tools;
import org.databene.benerator.anno.Source;
import org.databene.feed4testng.FeedTest;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.util.concurrent.TimeUnit;

public class RegisterTest extends FeedTest {
    WebDriver driver;
    //编写一条测试用例运行n次
    @Test(dataProvider = "feeder")
    @Source("data/reg.csv")
    public void reg(boolean flag,String username,String password,String password2,
                    String phone,String email){
        String url = "http://127.0.0.1:8088/index.php?m=user&c=public&a=reg";
        driver.get(url);
        driver.findElement(By.name("username")).sendKeys(username);
        driver.findElement(By.name("password")).sendKeys(password);
        driver.findElement(By.name("userpassword2")).sendKeys(password2);
        driver.findElement(By.name("mobile_phone")).sendKeys(phone);
        driver.findElement(By.name("email")).sendKeys(email);
        driver.findElement(By.className("reg_btn")).click();
        Tools.sleep(5000);
        //获取当前的网址 把返回值赋给url2
        String url2 = driver.getCurrentUrl();
        //断言 根据数据的正确还是错误设定
        if (flag){
            Assert.assertNotEquals(url,url2); //网址不同注册成功
        }else{
            Assert.assertEquals(url,url2); //网址相同注册失败
        }
    }

    @BeforeMethod
    public void before(){
        driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    }

    @AfterMethod
    public void after(){
        driver.quit();
    }
}

Register2.java

package com.bwf.autotest.day04;

import com.bwf.autotest.day02.Tools;
import org.databene.benerator.anno.Source;
import org.databene.feed4testng.FeedTest;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.*;

public class RegisterTest2 extends FeedTest { // 为了使用FeedTest中的feeder方法
    WebDriver driver;

    // 编写 1条测试用例,运行 n次
    @Test(dataProvider ="feeder")// 使用feeder读取数据文件,方法必须是本类中,所以继承FeedTest
    @Source("data/reg.csv")// 指定数据文件的位置
    public void reg(boolean flag, String username, String password, String password2,
                    String phone, String email) {
        String url = "http://127.0.0.1:8088/index.php?m=user&c=public&a=reg";
        driver.get(url);
        driver.findElement(By.name("username")).sendKeys(username);
        driver.findElement(By.name("password")).sendKeys(password);
        driver.findElement(By.name("userpassword2")).sendKeys(password2);
        driver.findElement(By.name("mobile_phone")).sendKeys(phone);
        driver.findElement(By.name("email")).sendKeys(email);
        driver.findElement(By.className("reg_btn")).click();
        Tools.sleep(5000);
        // 获取当前的网址  把返回值赋值给url2
        String url2 = driver.getCurrentUrl();

        if(!url.equals(url2)){// 判断登录成功  点击退出登录
            driver.findElement(By.linkText("[退出]")).click();
        }
        // 断言 根据数据的正确还是错误设定
        if (flag) {
            Assert.assertNotEquals(url, url2);// 网址不同注册成功
        } else {
            Assert.assertEquals(url, url2);// 网址相同注册失败
        }


    }

    @AfterClass
    public void a() {
        driver.quit();
    }

    @BeforeClass
    public void b() {
        driver = new FirefoxDriver();
        driver.manage().window().maximize();
    }
}

DeleteVip.java

package com.bwf.autotest.day04;

import com.bwf.autotest.day02.Tools;
import com.bwf.autotest.day03.BackLoginPage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.*;

import java.util.concurrent.TimeUnit;

public class DeleteVip {
    WebDriver driver;

    @Test
    public void login(){
        boolean result = BackLoginPage.login(driver, "admin","password","1234");
        Assert.assertTrue(result);
    }

    @Test(dependsOnMethods = {"login"})
    public void deleteVip(){
        driver.get("http://127.0.0.1:8088/index.php?m=admin&c=index&a=index&pid=4");
        Tools.sleep(5000);
        driver.switchTo().frame("mainFrame");
        //删除一个会员
//        driver.findElements(By.linkText("删除")).get(1).click();
//        driver.findElement(By.linkText("确定")).click();
        //删除多个会员
//        driver.findElement(By.xpath("//input[@name='id' and @value='34']")).click();
        driver.findElement(By.xpath("//input[@name='id' and @value='33']")).click();
        driver.findElement(By.xpath("//input[@name='id' and @value='32']")).click();
        driver.findElement(By.xpath("//input[@name='id' and @value='31']")).click();
        driver.findElement(By.xpath("//input[@name='id' and @value='30']")).click();
        driver.findElement(By.xpath("//input[@name='id' and @value='29']")).click();
        driver.findElements(By.linkText("删除")).get(0).click();
        driver.findElement(By.linkText("确定")).click();
    }

    @BeforeClass
    public void before(){
        driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    }
    @AfterClass
    public void after(){
        driver.quit();
    }
}

 

这篇关于day04的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!