思路
注册成功后,会出现积分奖励的提示框,但是几秒钟后该提示框会消失。自动化脚本需要验证该提示框里面的信息。
1)浏览器里面抓取显示威望的ajax请求
2)进入到积分奖励(creditReward)的JS函数
3)在发送ajax请求的代码行设置断点
4)重新注册(退出,进入注册里面,填写数据,提交注册)
5)使用F10进行调试(逐步执行),直到威望的提示框div出现为止
6)找到该提示框div对应的html代码
7)将该提示框div里面span子节点的xpath取到
抓取显示威望的ajax请求
进入到积分奖励(creditReward)的JS函数
在发送ajax请求的代码行设置断点
重新注册
退出,进入注册里面,填写数据,提交注册
使用F10进行调试
逐步执行,直到威望的提示框div出现为止
找到该提示框div对应的html代码
将该提示框div里面span子节点的xpath取到
span节点的xpath如下:
//*[@id='J_credittxt_pop']/span
验证的Java代码
Assert.assertThat(driver.findElement(By.xpath("//*[@id='J_credittxt_pop']/span")).getText(),Matchers.containsString(expected));
完整的Java代码
package day05;
import java.util.concurrent.TimeUnit;
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class RegisterTest {
private WebDriver driver;//驱动工具
@DataProvider(name="ds01")
public static Object[][] dataFail(){
Object[][] arr= {
{"wsq001","123456","123456","wsq0006@126.com","用户名已经存在,请选择其他用户名"},
{"ws","123456","123456","wsq0006@126.com","用户名长度错误,长度控制在3-15位,不能有空格"},
{"wsq1234567890123","123456","123456","wsq0006@126.com","用户名长度错误,长度控制在3-15位,不能有空格"},
{"$$$","123456","123456","wsq0006@126.com","用户名含有非法字符,请选择其他用户名"},
{"","123456","123456","wsq0006@126.com","用户名不能为空"},
{"wsq0006","12345","12345","wsq0006@126.com","密码长度错误,最小长度6个字,最大长度15个字"},
{"wsq0006","1234567890123456","1234567890123456","wsq0006@126.com","密码长度错误,最小长度6个字,最大长度15个字"},
{"wsq0006","","123456","wsq0006@126.com","密码不能为空"},
{"wsq0006","123456","123","wsq0006@126.com","两次输入的密码不一致。请重新输入"},
{"wsq0006","123456","","wsq0006@126.com","确认密码不能为空"},
{"wsq0006","123456","123456","wsq004126.com","请输入正确的电子邮箱地址"},
{"wsq0006","123456","123456","wsq001@126.com","该邮箱地址已经被注册"},
{"wsq0006","123456","123456","","邮箱不能为空"},
};
return arr;
}
@DataProvider(name="ds02")
public static Object[][] dataPass(){
Object[][] arr= {
{"wsq0007","123456","123456","wsq0007@126.com","威望+2"},
};
return arr;
}
@BeforeMethod
public void begin() {
//设置火狐浏览器驱动文件的路径
System.setProperty("webdriver.gecko.driver", "D:\\geckodriver.exe");
//设置获取浏览器执行文件的路径
System.setProperty("webdriver.firefox.bin", "C:\\Program Files\\Mozilla Firefox\\firefox.exe");
driver = new FirefoxDriver();
//设置响应时间
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
//设置窗口大小
driver.manage().window().maximize();
}
@Test(dataProvider="ds01",enabled=false)
public void registerFail(String username,String password,String password2,String email,String expected) {
//打开注册页面
driver.get("http://192.168.0.127:8080/phpwind/index.php?m=u&c=register");
//输入注册名
driver.findElement(By.xpath("//*[@id=\'J_reg_username\']")).sendKeys(username);
//输入密码
driver.findElement(By.xpath("//*[@id=\'J_reg_password\']")).sendKeys(password);
//重复密码
driver.findElement(By.xpath("//*[@id=\'J_reg_repassword\']")).sendKeys(password2);
//输入邮箱
driver.findElement(By.xpath("//*[@id=\'J_reg_email\']")).sendKeys(email);
//输入回车
driver.findElement(By.xpath("//*[@id=\'J_reg_email\']")).sendKeys(Keys.ENTER);
//查找报错
driver.findElement(By.className("tips_icon_error"));
//检查报错
Assert.assertThat(driver.findElement(By.className("tips_icon_error")).getText(),Matchers.containsString(expected));
}
@Test(dataProvider="ds02")
public void registerPass(String username,String password,String password2,String email,String expected) {
//打开注册页面
driver.get("http://192.168.0.131:8090/phpwind/index.php?m=u&c=register");
//输入注册名
driver.findElement(By.xpath("//*[@id=\'J_reg_username\']")).sendKeys(username);
//输入密码
driver.findElement(By.xpath("//*[@id=\'J_reg_password\']")).sendKeys(password);
//重复密码
driver.findElement(By.xpath("//*[@id=\'J_reg_repassword\']")).sendKeys(password2);
//输入邮箱
driver.findElement(By.xpath("//*[@id=\'J_reg_email\']")).sendKeys(email);
//输入回车
driver.findElement(By.xpath("//*[@id=\'J_reg_email\']")).sendKeys(Keys.ENTER);
driver.findElement(By.className("reg_activation"));
//检查注册成功
Assert.assertThat(driver.findElement(By.xpath("//*[@id='J_credittxt_pop']/span")).getText(),Matchers.containsString(expected));
}
@AfterMethod
public void end() throws Exception{
driver.quit();
}
}
微信扫一扫关注该公众号【测试开发者部落】
点击链接加入群聊【软件测试学习交流群】
https://jq.qq.com/?_wv=1027&k=5eVEhfN
软件测试学习交流QQ群号:511619105