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

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

   在视频中的方法就不赘述了,下面我们看一个典型的传统TestNG case,初学者可能比较多的使用IDE录制工具,然后再补充进一些个人方法和API的应用等,但这样一来,显然不利于长期维护,也会降低可读性。
实例1:
  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了,关于架构方面,有很多高手,我仅仅抛砖引玉了:
实例2:
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了

猜你喜欢

转载自frances657132.iteye.com/blog/2026274
今日推荐