一、修改Login类加入断言;web
断言:检查咱们操做页面后获得的结果与咱们预期的结果是否一致。chrome
二、使用xml文件运行全部的测试类;测试
Login类写入两个测试用例:ui
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文件(名称随便起):url
<suite name="TestSuite Demo"> <test name="TestSuite Demo Test"> <classes> <!-- 执行测试用例的类-->
<!-- name:被执行类的包名
<class>会有多个 --> <class name="com.test.Login"></class> </classes> </test> </suite>
直接运行TestSuit.xml文件,会执行Login类。spa
运行结果以下:code
内容:xml
一、TestNG中经常使用的断言方法:blog
assertEquals(String actual, String expected) //判断真实值与预期值是否相等,若是不相等测试失败会抛出一个异常排序
assertEqual(String actual,String expected, Stringmessage) //检查两个字符串是否相等, 若是不相等,测试失败, 且在抛出异常中打印出咱们提供的第三个message参数信息
assertTrue(boolean condition) //若是值为true,则用例经过,不然抛出一个AssertionError异常
assertFalse(boolean condition)
二、测试用例的执行顺序,Login类中的两个测试用例:test_login2,test_login3
通常是以字符排序,若是字符相同以数字排序。
说一下以前的问题:
一、以前存日期,一直没有保存成功,缘由是按钮元素定位方式不对:
以前的写法:driver.findElement(By.ByXPath.xpath("(//button[@type='button'])[1]")).click();
改正以后: driver.findElement(By.ByXPath.xpath("//button[contains(@class,'submit-infor')]")).click();
一直觉得是日期的定位元素不对一直修改,最后发现是按钮定位的不正确,可是存在的疑点是:其余内容都能保存成功就日期不行。