接上一篇Maven环境搭建,环境就绪,开始练习1.
登录测试:
对实际的项目进行登录测试脚本编写:
1)使用创建好的maven项目:maventest
2)创建ExcelWorkBook类:用于读取excel数据,进行参数化代码如下:
package com.test.practice.maventest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import jxl.Sheet;
import jxl.Workbook;
/*
* 获取Excel文件的内容,使用Workbook方式来读取excel
*/
public class ExcelWorkBook {
// 利用list集合来存放数据,其类型为String
private List<String> list = new ArrayList<String>();
// 通过Workbook方式来读取excel
Workbook book;
String username;
String sourceFile = "C:\\testfiles\\bosstest1.xls";
/*
* 获取excel文件第一列的值,这里取得值为username
*/
public List<String> readUsername(String sourceString) throws IOException, Exception {
List<String> userList = new ArrayList<String>();
try {
Workbook book = Workbook.getWorkbook(new File(sourceFile));
Sheet sheet = book.getSheet(0);
// 获取文件的行数
int rows = sheet.getRows();
// 获取文件的列数
int cols = sheet.getColumns();
// 获取第一行的数据,一般第一行为属性值,所以这里可以忽略
String col1 = sheet.getCell(0, 0).getContents().trim();
String col2 = sheet.getCell(1, 0).getContents().trim();
System.out.println(col1 + "," + col2);
// 把第一列的值放在userlist中
for (int z = 1; z < rows; z++) {
String username = sheet.getCell(0, z).getContents();
userList.add(username);
System.out.println("username:"+username);
}
} catch (Exception e) {
e.printStackTrace();
}
// 把获取的值放回出去,方便调用
return userList;
}
/*
* 获取excel文件第二列的值,这里取得值为password
*/
public List<String> readPassword(String sourceString) throws IOException, Exception {
List<String> passList = new ArrayList<String>();
try {
Workbook book = Workbook.getWorkbook(new File(sourceFile));
Sheet sheet = book.getSheet(0);
int rows = sheet.getRows();
for (int z = 1; z < rows; z++) {
String password = sheet.getCell(1, z).getContents();
passList.add(password);
System.out.println("password:"+password);
}
} catch (Exception e) {
e.printStackTrace();
}
return passList;
}
public List<String> getList() {
return list;
}
}
- 创建LoginPar类:进行测试,使用testng的注解方法(@test 和 @BeforeClass)
package com.test.practice.maventest;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
/**
* @author lucychen
* @date 2018/08/16 登录参数化
* @description selenium+testng+maven测试框架搭建
*/
public class LoginPar {
private WebDriver driver;
String sourceFile="C:\\testfiles\\bosstest1.xls";
@BeforeClass
public void beforeClass() {
// 设置firefox浏览器
System.setProperty("webdriver.firefox.marionette", "D:\\selenium\\geckodriver-v0.21.0-win64\\geckodriver.exe");
driver = new FirefoxDriver();
driver.manage().window().maximize();
}
@Test
public void loginBoss() throws Exception {
//初始化ExcelWorkBook Class
ExcelWorkBook excelbook = new ExcelWorkBook();
// 进入到你的测试界面
driver.get("需要打开的网址");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
try {
// 把取出的username放在userlist集合里面
List<String> userList = excelbook.readUsername(sourceFile);
// 把取出的password放在passlist集合里面
List<String> passList = excelbook.readPassword(sourceFile);
// 把取出来的值,输入到界面的输入框中
for (int i = 0; i < userList.size(); i++) {
// 通过css定位到username输入框
WebElement username = driver.findElement(By.xpath(".//*[@id='inuptUser']"));
// 通过css定位到password输入框
WebElement password = driver.findElement(By.id("inuptPwd"));
// 通过xpath定位登录按钮
WebElement submit = driver.findElement(By.xpath(".//*[@id='logbtn']"));
// 清除username输入框的内容
username.clear();
// 把list中数据一个一个的取出来
String name = userList.get(i);
// 然后填写到username输入框
username.sendKeys(name);
for (int j = 0; j < passList.size(); j++) {
password.clear();
String pass = passList.get(j);
password.sendKeys(pass);
}
// 点击登录按钮
submit.click();
Thread.sleep(1000);
//点击boss按钮
WebElement sboss = driver.findElement(By.xpath("html/body/div[1]/div/div/div[2]/div[2]/div[5]/div[3]/div"));
sboss.click();
Thread.sleep(3000);
// 通过xpath定位登出按钮
WebElement logoutButton = driver.findElement(By.xpath(".//*[@id='log_off']"));
logoutButton.click();
}
} catch (Exception e) {
e.printStackTrace();
}
Thread.sleep(1000);
driver.close();
}
}
4)配置testng.xml文件
创建res文件夹,分别创建testng.xml,testng2.xml以便于分别执行测试。
只需要在pom.xml配置对应的testng即可执行。
testng2.xml文件配置如下:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite2">
<test name="测试登录">
<classes>
<class name="com.test.practice.maventest.LoginPar" />
</classes>
</test>
<listeners>
<listener class-name="org.uncommons.reportng.HTMLReporter" />
<listener class-name="org.uncommons.reportng.JUnitXMLReporter" />
</listeners>
</suite>
- 配置对应的pom.xml。
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- groupId:组织 id(类似包名), artifactId:project name , version:default development
version , packaging:default jar -->
<groupId>com.test.practice</groupId>
<artifactId>maventest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>maventest</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<xmlFileName>testng.xml</xmlFileName>
</properties>
<!-- 依赖的jar包可从百度搜索,例如:maven testng -->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.14.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.uncommons/reportng 依赖ReportNg,关联TestNg -->
<dependency>
<groupId>org.uncommons</groupId>
<artifactId>reportng</artifactId>
<version>1.1.4</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 依赖Guice -->
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>3.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi 依赖poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/net.sourceforge.jexcelapi/jxl 依赖jxl -->
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 添加插件 关联testNg.xml -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
<suiteXmlFiles>
<!-- <suiteXmlFile>res/${testng2.xml}</suiteXmlFile> -->
<suiteXmlFile>res/testng2.xml</suiteXmlFile>
</suiteXmlFiles>
<!-- 加入编码设置,否则生成的报告会中文乱码 -->
<argLine>-Dfile.encoding=UTF-8</argLine>
</configuration>
</plugin>
<!-- 添加插件,添加ReportNg的监听器,修改最后的TestNg的报告 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<properties>
<property>
<name>usedefaultlisteners</name>
<value>false</value>
</property>
<property>
<name>listener</name>
<value>org.uncommons.reportng.HTMLReporter</value>
</property>
</properties>
<workingDirectory>target/</workingDirectory>
<!-- <forkMode>always</forkMode> -->
</configuration>
</plugin>
</plugins>
</build>
</project>
- 执行测试:选中pom.xml右键run as 》Maven test
- 查看执行结果,在D:\eclipse-workspace\maventest\target\surefire-reports查看
测试报告结果:
*注意:控制台出现中文乱码时,需要设置如下,Run As》Run Configuration》Common》Encoding 》Other 选择"UTF-8"
设置之后的效果:
测试报告中文乱码时,需要在pom.xml文件中添加:
<argLine>-Dfile.encoding=UTF-8</argLine>
即显示正常,参考上面的pom.xml文件配置。