Selenium自动化测试

安装配置:
安装java JDK1.6以上,配置环境变量.
安装maven,配置环境变量.
安装Tomcat服务器.
安装eclipse,安装testNg、Properties Editor插件
插件在help->Eclipse Marketplace->search->find里找

安装火狐浏览器,安装相应的Selenium IDE,Firebug和xpather

项目导入:测试项目,和自动化测试项目

自动化测试项目编写:
/trackerSelenium/src/main/java/com/sim/trackerSelenium/WebDriverInterface.java 提供webDriver 接口类
/trackerSelenium/src/main/java/com/sim/trackerSelenium/inf/BrowserDriver.java 创建浏览器驱动,WebDriverInterface的实现类
/trackerSelenium/src/main/java/com/sim/trackerSelenium/page/EntryTimeElment.java 寻找页面元素
/trackerSelenium/src/main/java/com/sim/trackerSelenium/utils/BrowserUtils.java 读取配置文件找驱动的
/trackerSelenium/src/main/java/com/sim/trackerSelenium/utils/ReadExcelUtils.java 读取excel的帮助类
/trackerSelenium/src/main/java/com/sim/trackerSelenium/utils/ScreenshotUtils.java 截图工具类
/trackerSelenium/src/main/java/com/sim/trackerSelenium/utils/WebDriverUtils.java 加载浏览器驱动
/trackerSelenium/src/main/resources/config/config.properties 修改浏览器驱

/trackerSelenium/src/main/resources/data/loginPage.xls 待读取的excel文件
/trackerSelenium/src/main/resources/driver/chromedriver.exe 浏览器驱动
/trackerSelenium/src/main/resources/log4j.properties  log的配置文件
/trackerSelenium/src/test/java/com/sim/trackerSelenium/test/EntryTimeTest.java 自动化测试类
/trackerSelenium/src/test/java/com/sim/trackerSelenium/test/testNg_Time.xml 自动化测试配置文件


主要的三个文件
1. 程序入口文件testNg_***.xml, 自动化测试配置文件。位于com.sim.trackerSelenium.test下。复制后,改名为testNg_***.xml,修改参数配置、运行脚本。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="false" >

	<listeners>
		<listener class-name="org.uncommons.reportng.HTMLReporter" />
		<listener class-name="org.uncommons.reportng.JUnitXMLReporter" />
	</listeners>
	<!-- 参数配置 -->
<!-- url 对应java文件的@Parameters(value = { "url" }) --> 
	<parameter name="url" value="http://localhost:8080/deft/login" />
<!-- url 对应java文件的@Parameters(value = { "userName", "password" })-->
	<parameter name="userName" value="admin" />
	<parameter name="password" value="123456" />

	<!-- 运行脚本 -->
	<test name="Test">
		<classes>
<对应要执行的java代码 ->
			<class name="com.sim.trackerSelenium.test.EntryTimeTest"/>
		</classes>
	</test>

</suite>


2. 自动化测试类,位于com.sim.trackerSelenium.test下。复制后,改类名,改方法名,改写test***方法。
/*
 * Copyright 2002-2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.sim.trackerSelenium.test;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import com.sim.trackerSelenium.page.EntryTimeElment;
import com.sim.trackerSelenium.utils.BrowserUtils;
import com.sim.trackerSelenium.utils.ScreenshotUtils;
import com.thoughtworks.selenium.Selenium;

/**
 * 填写java文件相关信息
 * @packageName com.sim.trackerSelenium.test
 * 
 * @createClass class EntryTimeTest.java
 * 
 * @description <p>
 *              工时模块自动化
 *            </p>
 * @author naiwei ge
 * 
 * @mail [email protected]
 * 
 * @createTime 2014年8月8日 上午11:50:57
 * 
 * @version 1.0
 */
public class EntryTimeTest {

	private WebDriver webDriver;
	private Selenium selenium;
	private EntryTimeElment entryTimeElment;


	@Parameters(value = { "url" })
	@BeforeClass
	public void samplerWebDriver(String url) {
		webDriver = BrowserUtils.getWebDriver();
		// 填充系统地址
		webDriver.get(url);
		// 浏览器最大化
		webDriver.manage().window().maximize();
		selenium = new WebDriverBackedSelenium(webDriver, url);
		
	}
	 @Parameters(value = { "userName", "password" })
	 @Test
	  public void testEntryTime(String userName, String password) throws Exception {
		//此处设置的等待时间 是针对全局设置的,找不到元素会默认等待三十秒。
		webDriver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		entryTimeElment=new EntryTimeElment(webDriver);
		//登录
        //清空元素内信息,一般用于输入框
		entryTimeElment.username.clear();
		entryTimeElment.password.clear();
        //给输入框赋值
		entryTimeElment.username.sendKeys(userName);
		entryTimeElment.password.sendKeys(password);
        //点击元素事件
		entryTimeElment.subLogin.click();
		
		//添加
        //等待2000毫秒
		selenium.waitForPageToLoad("2000");
		entryTimeElment.d9.click();
		selenium.waitForPageToLoad("2000");
	    entryTimeElment.addHour.click();
	    selenium.waitForPageToLoad("2000");
	    entryTimeElment.workinghours.clear();
	    entryTimeElment.workinghours.sendKeys("1");
	    entryTimeElment.remark.clear();
	    entryTimeElment.remark.sendKeys("轻武器");
	    entryTimeElment.submit.click();
	    selenium.waitForPageToLoad("2000");
	    entryTimeElment.submitModal.click();
	    selenium.waitForPageToLoad("2000");
	    entryTimeElment.alertMsg_btn1.click();
	    selenium.waitForPageToLoad("5000");
	    
	    //修改
	    entryTimeElment.d9.click();
	    selenium.waitForPageToLoad("3000");
	    entryTimeElment.modify0.click();
	    selenium.waitForPageToLoad("3000");
	    entryTimeElment.workinghours.clear();
	    entryTimeElment.workinghours.sendKeys("2");
	    entryTimeElment.remark.clear();
	    entryTimeElment.remark.sendKeys("hello!");
	    entryTimeElment.submit.click();
	    selenium.waitForPageToLoad("2000");
	    entryTimeElment.submitModal.click();
	    selenium.waitForPageToLoad("2000");
	    entryTimeElment.alertMsg_btn1.click();
	    selenium.waitForPageToLoad("5000");
	    
	    //删除
	    entryTimeElment.d9.click();
		selenium.waitForPageToLoad("2000");
	    entryTimeElment.del0.click();
	    selenium.waitForPageToLoad("2000");
	    entryTimeElment.Undodel.click();
	    selenium.waitForPageToLoad("10000");
	    entryTimeElment.del1.click();
	    selenium.waitForPageToLoad("2000");
	    entryTimeElment.submitModal.click();
	    selenium.waitForPageToLoad("2000");
	    entryTimeElment.alertMsg_btn1.click();
	    selenium.waitForPageToLoad("2000");
	    entryTimeElment.d9.click();
		selenium.waitForPageToLoad("2000");
        //截图工具,汉字部分是保存截图的文件名
		ScreenshotUtils.screenshot(webDriver,"删除状态");
		entryTimeElment.closeModal.click();
		selenium.waitForPageToLoad("2000");
		
	    //查看
	    entryTimeElment.todaydiv.click();
	    ScreenshotUtils.screenshot(webDriver,"当前月份");
	    selenium.waitForPageToLoad("2000");
	    entryTimeElment.last_month.click();
	    selenium.waitForPageToLoad("2000");
	    ScreenshotUtils.screenshot(webDriver,"上个月");
	    selenium.waitForPageToLoad("2000");
	    entryTimeElment.next_month.click();
	    selenium.waitForPageToLoad("2000");
	    entryTimeElment.next_month.click();
	    selenium.waitForPageToLoad("2000");
	    ScreenshotUtils.screenshot(webDriver,"下个月");
	    selenium.waitForPageToLoad("2000");
	    entryTimeElment.cal.click();
	    selenium.waitForPageToLoad("2000");
	    entryTimeElment.June.click();
	    selenium.waitForPageToLoad("3000");
	    ScreenshotUtils.screenshot(webDriver,"六月");
	  }
	
	@AfterClass
	public void webDriverQuit() {		
		webDriver.quit();
	}
}


3. 寻找页面元素java类,com.sim.trackerSelenium.page目录下。复制后,重命名,定义自己需要的元素。
获取xpath
谷歌:在需要的元素上点击鼠标右键->审查元素->Elements->蓝色部分->鼠标右键->Copy XPath //*[@id="main_div"]/div[1]/div[2]/a/span/img,修改双引号为单引号粘贴到@FindBy(xpath = "/html/body/div[4]/div[4]/table/tbody/tr/td/span[6]")红色字体中。有id在谷歌copy出的路径前面要加“.”
火狐:在需要的元素上点击鼠标右键->View XPath->
将XPath复制粘贴到@FindBy(xpath = "/html/body/div[4]/div[4]/table/tbody/tr/td/span[6]")红色字体中,去掉火狐XPath中的“x:”
/*
 * Copyright 2002-2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.sim.trackerSelenium.page;


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;


/**
 * 
 * @packageName com.sim.trackerSelenium.loginpage
 * 
 * @createClass class LoginPageElment.java
 * 
 * @description <p>
 *              page页封装
 * 
 *              </p>
 * @author naiwei ge
 * 
 * @mail [email protected]
 * 
 * @createTime 2014年8月5日 下午3:35:36
 * 
 * @version 1.0
 */
public class EntryTimeElment {

	
	//private Logger logger=LoggerFactory.getLogger(getClass());
	/**
	 *  
	 */
	@FindBy(xpath = ".//*[@id='d9']")
	public WebElement d9;

	@FindBy(xpath = ".//*[@id='addHour']")
	public WebElement addHour;

	@FindBy(xpath = ".//*[@id='workinghours']")
	public WebElement workinghours;

	@FindBy(xpath = ".//*[@id='remark']")
	public WebElement remark;
	
	@FindBy(xpath = ".//*[@id='submit']")
	public WebElement submit;
	
	@FindBy(xpath = ".//*[@id='username']")
	public WebElement username;
	
	@FindBy(xpath = ".//*[@id='password']")
	public WebElement password;
	
	@FindBy(xpath = ".//*[@id='subLogin']")
	public WebElement subLogin;
	
	@FindBy(xpath = ".//*[@id='submitModal']")
	public WebElement submitModal;
	
	@FindBy(xpath = ".//*[@id='alertMsg_btn1']")
	public WebElement alertMsg_btn1;
	
	@FindBy(xpath = ".//*[@id='del0']")
	public WebElement del0;
	
	@FindBy(xpath = ".//*[@id='del1']")
	public WebElement del1;
	
	@FindBy(xpath = ".//*[@id='del0_deldiv']/font")
	public WebElement Undodel;
	
	@FindBy(xpath = ".//*[@id='modify0']")
	public WebElement modify0;
	
	@FindBy(xpath = ".//*[@id='todaydiv']")
	public WebElement todaydiv;
	
	@FindBy(xpath = ".//*[@id='last_month']")
	public WebElement last_month;
	
	@FindBy(xpath = ".//*[@id='next_month']")
	public WebElement next_month;
	
	@FindBy(xpath = ".//*[@id='selectMonth']/span/span")
	public WebElement cal;

	@FindBy(xpath = "/html/body/div[4]/div[4]/table/tbody/tr/td/span[6]")
	public WebElement June;
	
	@FindBy(xpath = ".//*[@id='godiv']")
	public WebElement godiv;
	
	@FindBy(xpath = ".//*[@id='closeModal']")
	public WebElement closeModal;
	
	
	/**
	 * 
	 * 
	 * constructed function
	 * 
	 * @Author naiwei ge
	 * 
	 * @createDate 2014年8月5日 下午3:50:37
	 * 
	 * @param driver
	 */
	public EntryTimeElment(WebDriver driver) {
		PageFactory.initElements(driver, this);
	}

	
}

猜你喜欢

转载自genaiwei.iteye.com/blog/2177924