Java+Selenium自动化测试学习(三)— 断言

1、修改Login类加入断言;

  断言:检查我们操作页面后得到的结果与我们预期的结果是否一致。

2、使用xml文件运行所有的测试类;

Login类写入两个测试用例:

package com.test;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class Login  {
    WebDriver driver = null;
    //调用定位元素的方法
    ElementLocation elementLocation =  new ElementLocation();
    //在一个方法运行之前运行
    @BeforeMethod
    public void before(){
        System.setProperty("webdriver.chrome.driver", "E:\\selenium\\chromedriver.exe");
        driver = new ChromeDriver();
        String url = "http://xadev.alsolife.com/";
        driver.manage().window().maximize();
        driver.get(url);
    }
    /**
     * 定位登录界面元素
     * 1.输入正确手机号码
     * 2.输入正确密码
     * 3.登录成功
     */
//    @Test
//    public void test_login1(){
//        elementLocation.findElementByCssClearSendkeys("input[type='text']","15211111111",driver);
//        elementLocation.findElementByCssClearSendkeys("input[type='password']","123456",driver);
//        elementLocation.findElementByCssClick("button[type='button']",driver);
//        System.out.println("登录成功,跳转到首页");
//    }

    //输入错误用户名
    @Test
    public void test_login2(){
        String phone = "153";
        elementLocation.findElementByCssClearSendkeys("input[type='text']",phone,driver);
        elementLocation.findElementByCssClick("button[type='button']",driver);
     //加入断言
try{ Assert.assertEquals(phone,"15211111111"); }catch(AssertionError e){ System.out.println( "手机号格式有误:"+e.getMessage()); } } //不输入手机号 @Test public void test_login3(){ String phone = ""; //输入手机号 elementLocation.findElementByCssClearSendkeys("input[type='text']",phone,driver); //点击登录 elementLocation.findElementByCssClick("button[type='button']",driver); try{ Assert.assertEquals(phone,"15211111111"); }catch (AssertionError e){ System.out.println("手机号不能为空"+e.getMessage()); } } //在一个方法运行完之后运行 @AfterMethod public void after(){ try{ Thread thread = new Thread(); thread.sleep(5000); }catch (InterruptedException e){ e.printStackTrace(); } driver.quit(); } }

创建一个TestSuit.xml文件(名称随便起):

<suite name="TestSuite Demo">
    <test name="TestSuite Demo Test">
        <classes>
            <!-- 执行测试用例的类-->
       <!-- name:被执行类的包名
          <class>会有多个 --> <class name="com.test.Login"></class> </classes> </test> </suite>

直接运行TestSuit.xml文件,会执行Login类。

运行结果如下:

 内容:

1、TestNG中常用的断言方法:

  assertEquals(String actual, String expected)  //判断真实值与预期值是否相等,如果不相等测试失败会抛出一个异常

       assertEqual(String actual,String expected, Stringmessage) //检查两个字符串是否相等, 如果不相等,测试失败, 且在抛出异常中打印出我们提供的第三个message参数信息

  assertTrue(boolean condition) //如果值为true,则用例通过,否则抛出一个AssertionError异常

  assertFalse(boolean condition)

2、测试用例的执行顺序,Login类中的两个测试用例:test_login2,test_login3

  一般是以字符排序,如果字符相同以数字排序。

说一下之前的问题:

1、之前存日期,一直没有保存成功,原因是按钮元素定位方式不对:

          之前的写法:driver.findElement(By.ByXPath.xpath("(//button[@type='button'])[1]")).click();

    改正之后:    driver.findElement(By.ByXPath.xpath("//button[contains(@class,'submit-infor')]")).click();

一直以为是日期的定位元素不对一直修改,最后发现是按钮定位的不正确,但是存在的疑点是:其他内容都能保存成功就日期不行。

猜你喜欢

转载自www.cnblogs.com/liho/p/12217906.html