1、安装JDK,设置JAVA_HOME的环境变量php
下载地址:http://www.java.comcss
1. 选中 计算机->右键选属性->高级系统设置->高级->环境变量,逐层进入,以下图设置环境变量java
2. 打开cmd窗口,用java -version命令验证是否设置成功web
2、安装Eclipse浏览器
下载地址:http://www.eclipse.org/downloads/app
3、在Eclipse中安装TestNG插件eclipse
1. 点击eclipse中的Help->Install New Softwaremaven
2. 点击Add按钮,输入Name和相应的地址http://beust.com/eclipse,点击OK。勾选加载出来的TestNG选项,点击Install,完成TestNG的安装。ide
4、建立Project测试
一、 建立工程存放文件夹
二、 打开cmd,输入命令:cd G:\project\java,切换到工程文件夹目录
三、 建立一个简单的Java工程
输入命令mvn archetype:generate -DgroupId=com.selenium.test -DartifactId=Autotest -DinteractiveMode=false -DarchetypeCatalog=local
四、 使用Notepad++软件,打开建立好的工程目录下的pom.xml文件
五、 打开pom.xml,添加Junit和selenium包
<dependencies>
<!-- <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>-->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.53.1</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.13.6</version>
</dependency>
</dependencies>
六、 在cmd中打开工程Autotest,输入命令: mvn eclipse:eclipse,生成eclipse项目文件。
等待加载完成,出现如下Success字眼才是成功
七、 打开eclipse软件,打开工程目录,点击OK,进入软件主界面
八、 导入工程:使用General导入使用效率比较好一些,这种方式的缺点修改pom.xml文件不会立刻生效,须要从新mvn eclipse:eclipse,再从新导入工程;使用Maven导入工程时,修改pom.xml能立刻生效,缺点是maven会联网检查包是否更新,致使eclipse比较卡,这种方式的优势是可以保证相关包为最新的。选择哪一种方式,你们根据本身的状况来选择使用。
九、 新建一个简单的class类,以下图所示:
十、 以登陆为例,在文件中,输入如下代码,包括”@BeforeClass、@Test、@AfterClass”三大块。
package com.selenium.test; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; public class Test { WebDriver driver; @BeforeClass public void setUp()throws Exception{ //火狐 System.setProperty("webdriver.firefox.bin", "D:/Program Files (x86)/Mozilla Firefox/firefox.exe"); driver = new FirefoxDriver(); //其余浏览器,请参照Selenium2测试脚本怎样配置不一样的浏览器(https://my.oschina.net/u/2315260/blog/804767) } @Test(description="登陆") public void loginTest(){ driver.get("http://localhost/chadmin/backend/web/index.php"); //登陆 WebElement username =driver.findElement(By.cssSelector("#username")); username.sendKeys("admin"); WebElement password = driver.findElement(By.cssSelector("password")); password.sendKeys("123456"); WebElement login_btn = driver.findElement(By.cssSelector("#login_btn")); login_btn.click(); //等待打开首页页面 (new WebDriverWait(driver,10)).until(new ExpectedCondition<WebElement>(){ @Override public WebElement apply(WebDriver d){ WebElement lastProject = driver.findElement(By.cssSelector(".box-title")); return lastProject; } }); } @AfterClass public void tearDown() throws Exception{ driver.quit(); } }