selenium+java+testng分层设计(一)

来源:互联网 发布:linux循环语句实例 编辑:程序博客网 时间:2024/06/11 11:04

之前使用selenium+java+testng写的自动化脚本都是放在一个java类中,后面越写越长,不仅有一些重复的脚本,而且维护起来很麻烦,后面开始使用分层设计,这里主要记录下分层设计的整个流程。

1.新建一个java项目Demo,新建一个包testng和一个类test.jave,这里主要是还原最初的所有脚本在一个类的场景,所有就不详细介绍引入selenium包之类的,主要是介绍后面的分层设计流程,下面是原始的代码,全部在一个test.java类中的,如下

这个脚本里面包括了一个登陆操作和一个查询界面的按键测试,虽然看起来还是比较有层次,但是脚本多了后,劣势自然会体现出来,总之,分层设计势在必行啦。

package testng;import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.firefox.FirefoxDriver;import org.testng.annotations.Test;import org.testng.annotations.BeforeTest;import org.testng.annotations.AfterTest;public class test {WebDriver driver;@BeforeTestpublic void beforeTest() {driver = new FirefoxDriver();driver.get("http://123.57.56.45:7778/test/initLogin");driver.manage().window().maximize();// 窗口最大化}@Test(priority=1)public void login()//登录throws InterruptedException {driver.findElement(By.id("username")).sendKeys("999999");// 输入登录用户名driver.findElement(By.id("password")).sendKeys("111111");// 输入登录密码Thread.sleep(2000);driver.findElement(By.id("loginButton")).click(); // 点击登录}@Test(priority=2)public void query_keys() throws InterruptedException{   //查询界面按键测试driver.findElement(By.linkText("下一页")).click();//点击下一页Thread.sleep(2000);driver.findElement(By.linkText("最后一页")).click();//点击最后一页Thread.sleep(2000);driver.findElement(By.id("pageInput")).sendKeys("20");//输入跳转页面为20driver.findElement(By.linkText("go")).click();//点击go,跳转到20页Thread.sleep(2000);driver.findElement(By.linkText("上一页")).click();//点击上一页Thread.sleep(2000);driver.findElement(By.linkText("第一页")).click();//点击第一页} @AfterTest  public void afterTest() {  driver.close();  }}

3 0
原创粉丝点击