JApiTest的selenium测试脚本

这是为http://git.oschina.net/penngo/JApiTest工具写的selenium测试脚本,目前只写了主要功能的自动化测试。由于JApiTest前端使用了ligerUI开发,selenium操作页面元素会相对麻烦,而对于部分有延迟处理的,需要使用WebDriverWait来等待节点元素的出现。

package com.penngo.test;

import java.io.File;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

public class TestUnit {
	WebDriver driver;
	
	@Before
	public void setUp() throws Exception {
		driver = new ChromeDriver();
	}
	
	/**
	 * 测试添加项目、添加用例、用例测试、删除用例、删除项目
	 * @throws Exception
	 */
	@Test
	public void nodeTest() throws Exception {
		driver.get("http://localhost:8000");
		this.addProject();
		clickLastProject();
		clickLastCase();
		this.addCase();
		this.runCase();
		this.deleteProjectCase();
	}
	
	
	@After
	public void tearDown() throws Exception {
		driver.quit();
	}
	public void screenshot(String fileName){
		try{
			File screenFile = ((TakesScreenshot) driver)
					.getScreenshotAs(OutputType.FILE);
			FileUtils.copyFile(screenFile, new File("screen/" + fileName));
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * @param b
	 */
	private void clickLastProject(){
		this.screenshot(System.currentTimeMillis() + "_clickLastProject.jpg");
		WebElement allProject = driver.findElement(By.cssSelector("#tree1 li[outlinelevel='1'].l-last"));
		List<WebElement> openNodes = allProject.findElements(By.cssSelector(".l-body .l-box"));
		Assert.assertEquals(openNodes.size(), 2);
		openNodes.get(0).click();
		this.screenshot(System.currentTimeMillis() + "_clickLastProject.jpg");
	}
	
	private void clickLastCase(){
		this.screenshot(System.currentTimeMillis() + "_clickLastCase.jpg");
		WebDriverWait wait = new WebDriverWait(driver,10);  
		WebElement openNode = wait.until(new ExpectedCondition<WebElement>(){  
            @Override  
            public WebElement apply(WebDriver d) {
            	WebElement lastProject = driver.findElement(By.cssSelector("#tree1 li[outlinelevel='2'].l-last"));
        		List<WebElement> openNodes = lastProject.findElements(By.cssSelector(".l-body .l-box"));
                return openNodes.get(1);  
            }});
		openNode.click();
		this.screenshot(System.currentTimeMillis() + "_clickLastCase.jpg");
	}

	/**
	 * 根据文本内容点击菜单
	 * @param btnText
	 */
	private void clickMenuByText(String btnText){
		this.screenshot(System.currentTimeMillis() + "_clickMenuByText.jpg");
		List<WebElement> menuItems = driver.findElements(By.cssSelector(".l-menu .l-menu-item"));
		for(WebElement menu:menuItems){
			if(menu.getText().equals(btnText)){
				menu.click();
			}
		}
		this.screenshot(System.currentTimeMillis() + "_clickMenuByText.jpg");
	}
	
	//添加项目
	private void addProject() throws Exception{
		
		// 弹出右键添加项目
		WebElement body1_box1 = driver.findElement(By.cssSelector(".l-body .l-box"));
		Actions action = new Actions(driver); 
		action.contextClick(body1_box1).perform();
		screenshot(System.currentTimeMillis() + "_addProject.jpg");
		WebElement projectMenu = driver.findElement(By.cssSelector("div[ligeruimenutemid='1']"));  //.l-menu-item-text
		projectMenu.click();
		
		screenshot(System.currentTimeMillis() + "_addProject.jpg");
		// 添加项目
		List<WebElement> dialogs = driver.findElements(By.cssSelector("div.l-dialog.l-dialog-win"));
		for(WebElement dialog:dialogs ){
			List<WebElement> directory_names = dialog.findElements(By.cssSelector("#directory_name"));
			if(directory_names.size() > 0){
				WebElement directory_name = directory_names.get(0);
				directory_name.sendKeys("测试项目一");
				screenshot(System.currentTimeMillis() + "_addProject.jpg");
				List<WebElement> btns = dialog.findElements(By.cssSelector(".l-dialog-btn-inner"));
				Assert.assertNotNull(btns);
				btns.get(1).click();
				
				WebDriverWait wait = new WebDriverWait(driver,10);  
				WebElement successDialog = wait.until(new ExpectedCondition<WebElement>(){  
		            @Override  
		            public WebElement apply(WebDriver d) {
		                return d.findElement(By.cssSelector("[class='l-dialog']"));  
		            }});
				this.screenshot(System.currentTimeMillis() + "_addProject.jpg");
				action.sendKeys(Keys.ENTER).perform();
			}
		}
	}
	
	// 添加用例
	private void addCase() throws Exception{
		WebElement lastProject = driver.findElement(By.cssSelector("#tree1 li[outlinelevel='2'].l-last"));
		Actions action = new Actions(driver); 
		action.contextClick(lastProject).perform();
		this.screenshot(System.currentTimeMillis() + "_addCase.jpg");
		
		clickMenuByText("添加用例");
		this.screenshot(System.currentTimeMillis() + "_addCase.jpg");
		WebDriverWait wait = new WebDriverWait(driver,10);  
		WebElement case_dir_name = wait.until(new ExpectedCondition<WebElement>(){  
            @Override  
            public WebElement apply(WebDriver d) {
                return d.findElement(By.cssSelector("#case_dir_name"));
            }});
		case_dir_name.sendKeys("接口测试2");
		this.clickDialogBtn("确定");
		this.screenshot(System.currentTimeMillis() + "_addCase.jpg");
		this.clickTipDialogBtn();
		this.screenshot(System.currentTimeMillis() + "_addCase.jpg");
	}
	
	private void clickDialogBtn(String text){
		this.screenshot(System.currentTimeMillis() + "_clickDialogBtn.jpg");
		List<WebElement> dialogs = driver.findElements(By.cssSelector(".l-dialog.l-dialog-win"));
		for(WebElement dialog:dialogs){
			if(dialog.isDisplayed() == true){
				List<WebElement> btns = dialog.findElements(By.cssSelector(".l-dialog-btn-inner"));
				for(WebElement b:btns){
					if(b.getText().equals(text)){
						b.click();
					}
				}
			}
		}
		this.screenshot(System.currentTimeMillis() + "_clickDialogBtn.jpg");
	}
	private void clickTipDialogBtn(){
		this.screenshot(System.currentTimeMillis() + "_clickTipDialogBtn.jpg");
		WebDriverWait wait = new WebDriverWait(driver,10);  
		WebElement successDialog = wait.until(new ExpectedCondition<WebElement>(){  
            @Override  
            public WebElement apply(WebDriver d) {
                return d.findElement(By.cssSelector("[class='l-dialog']"));  
            }});
		Actions action = new Actions(driver); 
		action.sendKeys(Keys.ENTER).perform();
		this.screenshot(System.currentTimeMillis() + "_clickTipDialogBtn.jpg");
	}
	/**
	 * 删除用例
	 */
	private void deleteProjectCase(){
		driver.switchTo().parentFrame();
		this.screenshot(System.currentTimeMillis() + "_deleteProjectCase.jpg");
		//删除case
		WebElement lastCase = driver.findElement(By.cssSelector("#tree1 li[outlinelevel='3'].l-last"));
		Actions action = new Actions(driver); 
		action.contextClick(lastCase).perform();
		this.screenshot(System.currentTimeMillis() + "_deleteProjectCase.jpg");
		this.clickMenuByText("删除用例");
		clickConfirmDialog("是");
		this.screenshot(System.currentTimeMillis() + "_deleteProjectCase.jpg");
		//删除project
		WebElement lastProject = driver.findElement(By.cssSelector("#tree1 li[outlinelevel='2'].l-last span"));
		action.contextClick(lastProject).perform();
		this.clickMenuByText("删除项目");
		clickConfirmDialog("是");
		this.screenshot(System.currentTimeMillis() + "_deleteProjectCase.jpg");
	}
	
	private void runCase(){
		this.screenshot(System.currentTimeMillis() + "_runCase.jpg");
		WebElement lastCase = driver.findElement(By.cssSelector("#tree1 .l-children li[outlinelevel='3'].l-last"));
		lastCase.click();
		this.screenshot(System.currentTimeMillis() + "_runCase.jpg");
		final WebDriver frame = driver.switchTo().frame(0);
		WebElement urlElement = frame.findElement(By.cssSelector("#url"));
		urlElement.clear();
		urlElement.sendKeys("http://localhost:8000/api/testLogin");
		this.screenshot(System.currentTimeMillis() + "_runCase.jpg");
		Select methodSelect = new Select(frame.findElement(By.id("method")));
		methodSelect.selectByIndex(0);
		this.screenshot(System.currentTimeMillis() + "_runCase.jpg");
		Select typeSelect = new Select(frame.findElement(By.id("type")));
		typeSelect.selectByIndex(0);
		this.screenshot(System.currentTimeMillis() + "_runCase.jpg");
		Select codeSelect = new Select(frame.findElement(By.id("code")));
		codeSelect.selectByIndex(0);
		this.screenshot(System.currentTimeMillis() + "_runCase.jpg");
		Select timeOutSelect = new Select(frame.findElement(By.id("timeOut")));
		timeOutSelect.selectByIndex(0);
		this.screenshot(System.currentTimeMillis() + "_runCase.jpg");
		WebElement requestElement = frame.findElement(By.id("request"));
		requestElement.clear();
		requestElement.sendKeys("{\"name\":\"penngo\",\"psw\":\"test123456\"}");
		this.screenshot(System.currentTimeMillis() + "_runCase.jpg");
		WebElement btnExcElement = frame.findElement(By.id("btnExc"));
		btnExcElement.click();
		this.screenshot(System.currentTimeMillis() + "_runCase.jpg");
		
		WebDriverWait wait = new WebDriverWait(driver,10);  
		wait.until(new ExpectedCondition<WebElement>(){
            @Override  
            public WebElement apply(WebDriver d) {
            	WebElement e = frame.findElement(By.cssSelector("#stateResult"));
            	String css = e.getAttribute("class");
                if(css.equals("stateResult_error") || css.equals("stateResult_success")){
                	return e;
                }
                else{
                	return null;
                }
            }}
		);
		this.screenshot(System.currentTimeMillis() + "_runCase.jpg");
		WebElement navtab2 = frame.findElement(By.id("navtab2"));
		WebElement resTabItem1 =navtab2.findElement(By.linkText("响应内容解析"));
		resTabItem1.click();
		this.screenshot(System.currentTimeMillis() + "_runCase.jpg");
		WebDriverWait wait2 = new WebDriverWait(driver,10);  
		WebElement tr0 = wait2.until(new ExpectedCondition<WebElement>(){
            @Override  
            public WebElement apply(WebDriver d) {
            	List<WebElement> response_maingrid_trs = frame.findElements(By.cssSelector("#response_maingrid table.l-grid-body-table tbody tr"));
            	if(response_maingrid_trs.get(0).isDisplayed()){
                	return response_maingrid_trs.get(0);
                }
                else{
                	return null;
                }
            }}
		);
		tr0.click();
		this.screenshot(System.currentTimeMillis() + "_runCase.jpg");
		WebElement addAssert = frame.findElement(By.cssSelector("#response_maingrid .l-toolbar-item"));
		addAssert.click();
		this.screenshot(System.currentTimeMillis() + "_runCase.jpg");
		WebElement resTabItem2 =navtab2.findElement(By.linkText("响应内容判断"));
		resTabItem2.click();
		this.screenshot(System.currentTimeMillis() + "_runCase.jpg");
		btnExcElement.click();
		WebDriverWait wait3 = new WebDriverWait(frame,10);  
		wait3.until(new ExpectedCondition<WebElement>(){
            @Override  
            public WebElement apply(WebDriver d) {
            	WebElement e = frame.findElement(By.cssSelector("#stateResult"));
            	String css = e.getAttribute("class");
                if(css.equals("stateResult_error") || css.equals("stateResult_success")){
                	return e;
                }
                else{
                	return null;
                }
            }}
		);
		this.screenshot(System.currentTimeMillis() + "_runCase.jpg");
	}
	
	private void clickConfirmDialog(String text){
		this.screenshot(System.currentTimeMillis() + "_clickConfirmDialog.jpg");
		WebDriverWait wait = new WebDriverWait(driver,10);  
		WebElement dialog = wait.until(new ExpectedCondition<WebElement>(){
            @Override  
            public WebElement apply(WebDriver d) {
            	return driver.findElement(By.cssSelector("div[class='l-dialog']"));
            }}
		);
		List<WebElement> btns = dialog.findElements(By.cssSelector(".l-dialog-btn-inner"));
		for(WebElement b:btns){
			if(b.getText().equals(text)){
				b.click();
			}
		}
		this.screenshot(System.currentTimeMillis() + "_clickConfirmDialog.jpg");
	}
}

完整工程源码


你可能感兴趣的:(JApiTest的selenium测试脚本)