selenium二次封装

使用到相关技术:selenium+testng+log4j+maven

selenium主要生成driver、获取页面元素等。

testng主要Run程序,生成测试报告、管理用例执行顺序、断言等。

log4j主要用来打印log日志。

maven主要统一管理外部引入的jar包,其中就包括selenium+testng+log4j jar包的管理。maven能自动处理jar包的依赖关系。

开始动手

1、eclipse中创建maven项目(就不具体讲解了,网上很多教程)

2、maven项目创建成功后,修改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>atf</groupId>
	<artifactId>autoTest</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>autoTest</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>
	
	<dependencies>
	
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.14</version>
		</dependency>
		
		<dependency>
			<groupId>org.seleniumhq.selenium</groupId>
			<artifactId>selenium-java</artifactId>
			<version>2.45.0</version>
		</dependency>
		
		<dependency>
			<groupId>org.testng</groupId>
			<artifactId>testng</artifactId>
			<version>6.9.9</version>
		</dependency>
		
	</dependencies>

</project>

3、项目结构如下:


解读:

src/main/java目录下源代码,本例子中有三个类,分别是LoginPage.java LoginCace.java ProFileUtil.java

src/main/resources目录下放配置文件LoginPage.properties

src/test/resources目录下放chromedriver.exe(网上下载即可)

4、直接上代码

LoginPage.properties

loginPage.userName=class,username
loginPage.userPassword=name,password
loginPage.submit=xpath,//button[@type='submit' and @class='btn btn-primary']

ProFileUtil.java

package com.atf.utils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import org.openqa.selenium.By;

/**
 * 加載和解析配置文件
 * 
 * @author alzhang
 */
public class ProFileUtil {

	Properties proerties;
	
	public ProFileUtil(){
		
	}
	// 加載配置文件方法
	public  ProFileUtil(String propFile) {
		proerties = new Properties();
		FileInputStream in = null;
		try {
			in = new FileInputStream(propFile);
			proerties.load(in);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	// 解析配置文件
	public By parseProFile(String elementName) {
		// 根据变量elementName,从配置文件中读取对应的配置对象。
		String by = proerties.getProperty(elementName);
		// 将配置文件中的定位类型存到byType变量中。将定位表达式的值存到byValue变量中。
		String byType = by.split(",")[0];
		String byValue = by.split(",")[1];
		
		switch (byType) {
		case "id":
			return By.id(byValue);
		case "name":
			return By.name(byValue);
		case "class":
			return By.className(byValue);
		case "tag":
			return By.className(byValue);
		case "link":
			return By.linkText(byValue);
		case "partiallinktext":
			return By.partialLinkText(byValue);
		case "css":
			return By.cssSelector(byValue);
		case "xpath":
			return By.xpath(byValue);
		default:
			throw new RuntimeException("输入的定位类型未在程序中定义,类型为:" + byValue);
		}

	}

}

LoginPage.java

package com.atf.pageElement;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import com.atf.utils.ProFileUtil;

/**
 * 登陆页类。返回登陆页面元素对象
 * 
 * @author alzhang
 *
 */
public class LoginPage {
	private WebElement element = null;

	// 指定页面元素定位表达式配置文件的绝对文件路径
	private ProFileUtil pfu = new ProFileUtil(
			"src/main/resources/LoginPage.properties");

	private WebDriver driver;

	public LoginPage() {

	}

	public LoginPage(WebDriver driver) {
		this.driver = driver;
	}

	// 获取页面源代码的封装方法
	public String getPageSource() {
		return driver.getPageSource();
	}

	// 返回登陆页面的用户名输入框元素对象
	public WebElement userName() throws Exception {
		element = driver.findElement(pfu.parseProFile("loginPage.userName"));
		return element;
	}

	// 返回登陆页面的用户密码输入框元素对象
	public WebElement userPassword() throws Exception {
		element = driver
				.findElement(pfu.parseProFile("loginPage.userPassword"));
		return element;
	}

	// 返回登陆页面的登陆按钮元素对象
	public WebElement submit() throws Exception {
		element = driver.findElement(pfu.parseProFile("loginPage.submit"));
		return element;
	}

}

LoginCace.java

package com.atf.testCase;

import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import com.atf.pageElement.LoginPage;

public class LoginCase {
	public WebDriver driver;
	private static Logger logger = Logger.getLogger(LoginCase.class);

	@Test
	public void testLogin() throws Exception {

		driver.get("此处填写登陆页面的URL:http://XXXXXX");
		LoginPage loginPage = new LoginPage(driver);
		logger.info("登陆用例开始");
		logger.info("不输入用户名和密码,点击登陆");
		loginPage.submit().click();
		Thread.sleep(1000);
		Assert.assertTrue(driver.getPageSource().contains("请输入账户名"));
		logger.info("输入用户名,不输入密码,点击登陆");
		loginPage.userName().sendKeys("18678945247");
		loginPage.submit().click();
		Thread.sleep(1000);
		Assert.assertTrue(driver.getPageSource().contains("请输入密码"));
		logger.info("输入错误的密码,点击登陆");
		loginPage.userPassword().sendKeys("1234567");
		loginPage.submit().click();
		Thread.sleep(1000);
		Assert.assertTrue(driver.getPageSource().contains("用户名或密码错误"));
		logger.info("输入正确的密码,点击登陆");
		loginPage.userPassword().clear();
		loginPage.userPassword().sendKeys("12345678");
		loginPage.submit().click();
		Thread.sleep(1000);
		Assert.assertTrue(driver.getPageSource().contains("查看所有创业公司"));
		logger.info("登陆用例结束");
	}

	@BeforeTest
	public void beforeMethod() {
		System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver.exe");
		// 去掉浏览器中的“--ignore-certificate-errors”
		DesiredCapabilities capabilities = DesiredCapabilities.chrome();
		capabilities.setCapability("chrome.switches",
				Arrays.asList("--incognito"));
		ChromeOptions options = new ChromeOptions();
		options.addArguments("--test-type");
		capabilities.setCapability("chrom.binary",
				"src/ucBrowserDrivers/chromedriver.exe");
		capabilities.setCapability(ChromeOptions.CAPABILITY, options);
		driver = new ChromeDriver(capabilities);
		driver.manage().window().maximize();
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
	}

	@AfterTest
	public void afterMethod() {
		driver.quit();
	}

}

今天就到此结束了,这是最最基本的selenium web自动化测试的框架封装。此框架可以看出,如果登陆页面的元素改变,我们只需维护LoginPage.properties这个配置文件即可。大大降低维护成本。

此框架还不完善,后续还会继续封装,添加driver工厂类,增加用例容错性,使用testng.xml组织用例,数据驱动,用例结束后的测试数据处理等等。也是在一步一步摸索中。很多想法尚不成熟,如果框架中有任何不足或是有更好的方法或意见欢迎留言分享。谢谢大家。


  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值