Selenium Webdriver+TestNG的封装及后期测试用例维护

众所周知,在基于项目考虑的话,selenium在Testng中的不同API,方法和annotations在测试class中会被频繁调用,而如果用页面封装,例如PageFactory的话,可以大大简化测试用例中的方法调用,并且命名规则也可以得到统一规范,之前看到一个selenium教程,还很不错,在此推荐一下:
http://www.seleniumcn.cn/read.php?tid=8003

在视频中的方法就不赘述了,下面我们看一个典型的传统TestNG case,初学者可能比较多的使用IDE录制工具,然后再补充进一些个人方法和API的应用等,但这样一来,显然不利于长期维护,也会降低可读性。
[b]实例1:[/b]
public class BaiduMap {
WebDriver driver;
String baseUrl= "http://map.baidu.com/";


@Test
public void Map_case1() {
driver.get(baseUrl);
driver.findElement(By.xpath("//*[@id='PoiSearch']")).sendKeys("abc");
driver.findElement(By.id("poiSearchBtn")).click();
sleep(3000);
getScreenshot();


}
@BeforeMethod
public void beforeMethod() {
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS );
driver.manage().window().maximize();
}

@AfterMethod
public void afterMethod() {
}



public void sleep(long timer)
{
try {
Thread.sleep(timer);
}
catch(InterruptedException e) {
Thread.currentThread().interrupt();
}
}



//Get screenshot method,to save the file to date format
public void getScreenshot()
//throws IOException

{

Date date1=new Date();
String h=String.format("%tH", date1);
String m=String.format("%tM", date1);
String s=String.format("%tS", date1);
String d=String.format("%tF", date1);
String PathOfSnapshot = "E:/VS testing/Eclipse_Test/";
String SnapshotName = d+"_"+h+m+s + ".png";

PathOfSnapshot = PathOfSnapshot + SnapshotName;
//Selenium.CaptureScreenshot(PathOfError);
File screenShotFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

try
{
FileUtils.copyFile(screenShotFile, new File(PathOfSnapshot));
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}


private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
}
catch (NoSuchElementException e) {
return false;
}
}

}
而如果使用封装将一些常用的方法封装到一个UnitTestBase文件里,当我们需要调用,修改逻辑,页面结构时,只需要变动这个Base文件就ok了,关于架构方面,有很多高手,我仅仅抛砖引玉了:
[b]实例2:[/b]
public class BaseUnit {

WebDriver driver;
String baseUrl= "http://map.baidu.com";


//@Test

public void getScreenshot(WebDriver driver)
{

Date date1=new Date();
String h=String.format("%tH", date1);
String m=String.format("%tM", date1);
String s=String.format("%tS", date1);
String d=String.format("%tF", date1);
String PathOfSnapshot = "E:/VS testing/Eclipse_Test/";
String SnapshotName = d+"_"+h+m+s + ".png";

PathOfSnapshot = PathOfSnapshot + SnapshotName;
//Selenium.CaptureScreenshot(PathOfError);
File screenShotFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

try
{
FileUtils.copyFile(screenShotFile, new File(PathOfSnapshot));
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

}



private boolean isElementPresent(By xpath, WebDriver driver) {
// TODO Auto-generated method stub
try {
driver.findElement(xpath);
return true;
}
catch (NoSuchElementException e) {
return false;
}

}


@BeforeMethod
public void beforeMethod() {

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS );
driver.manage().window().maximize();
}

@AfterMethod
public void afterMethod() {
}

}
而我们的测试类是UnitTestBase的子类,我们可以直接写到@test里面,简单的一个例子来说:
@Test
public void Map_case1() {
driver.get(baseUrl);
driver.findElement(By.xpath("//*[@id='PoiSearch']")).sendKeys("abc");
driver.findElement(By.id("poiSearchBtn")).click();
getScreenshot();
}
同时也不用再每次都写@BeforeMethod, @AfterMethod这种标注了,只需要在Base文件里设定好就ok了
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值