一、testng具体使用
案例一
package selenium.testCase;
import org.testng.annotations.*;
/**
* @Auther: ljm
* @Date: 2021/09/26/17:01
* @Description:
*/
public class testngTest {
@BeforeClass
public void test01(){
System.out.println("@BeforeClass====test01");
}
@BeforeMethod
public void test02(){
System.out.println(" @BeforeMethod====test02");
}
@AfterMethod
public void test03(){
System.out.println("@AfterMethod====test03");
}
@Test
public void test(){
System.out.println("@Test====test");
}
@Test
public void test8(){
System.out.println("@Test====test8");
}
@AfterClass
public void test04(){
System.out.println("@AfterClass====test04");
}
}
结果:

testNG参数化
配置账号密码和我们需要做的事情
testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All Test Suite"><test verbose="2" preserve-order="true" name="E:/cxzr_java_seleium/src/test/java/selenium">
<parameter name="username" value="130818****"/>
<parameter name="password" value="*****"/>
<classes>
<class name="selenium.testCase.SuiteTestLogin">
<methods>
<include name="testLogin"/>
</methods>
</class>
<class name="selenium.testCase.login">
<methods>
<include name="getLoginHome"/>
</methods>
</class>
<!-- <class name="selenium.testCase.testCourseList">-->
<!-- <methods>-->
<!-- <include name="CourseList"/>-->
<!-- </methods>-->
<!-- </class>-->
<!-- <class name="selenium.testCase.testngTest">-->
<!-- <methods><include name="test"/>-->
<!-- <include name="test8"/>-->
<!-- <include name="test04"/>-->
<!-- </methods>-->
<!-- </class>-->
</classes>
</test>
</suite>
src/test/java/selenium/testCase/login.java
@Test
public void getLoginHome(){
driver.get("http://www.imooc.com");
driver.max();
driver.findElement(By.id("js-signin-btn")).click();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//testng参数化
@Test(dependsOnMethods = {"getLoginHome"})
@Parameters({"username","password"})
public void testLogin(String username, String password) {
loginPro.login(username,password);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
右键运行testng.xml即可看到test的运行结果
企业中case运用登陆
在企业生产中case关联性尽量少用依赖
应该是一条case一条的写
登陆测试
判断登陆是否成功,就是看登陆名是否正确来确定
src/test/java/com/mushishi/selenium/testCase/SuiteTestLogin.java
里面用到了HomePagePro这个类需要对其进行函数构造AssertLogin方法
package com.mushishi.selenium.testCase;
import java.util.concurrent.TimeUnit;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import com.mushishi.selenium.base.DriverBase;
import com.mushishi.selenium.business.HomePagePro;
import com.mushishi.selenium.business.LoginPro;
import com.mushishi.selenium.util.HandleCookie;
import com.mushishi.selenium.util.ProUtil;
public class SuiteTestLogin extends CaseBase{
public DriverBase driver;
public LoginPro loginpro;
public HomePagePro homepagepro;
public ProUtil pro;
public HandleCookie handcookie;
@BeforeClass
public void beforeClass(){
this.driver = InitDriver("chrome");
pro = new ProUtil("loginTest.properties");
//等待时间
driver.driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
loginpro = new LoginPro(driver);
handcookie = new HandleCookie(driver);
homepagepro = new HomePagePro(driver);
driver.get(pro.getPro("url"));
}
@Test
public void testLogin(){
String username = pro.getPro("username");
String password = pro.getPro("passwd");
loginpro.login(username, password);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//判断是不是相等,很类似于springboot中的处理方法,业务层什么的分开了
if(homepagepro.AssertLogin(pro.getPro("yq"))){
System.out.println("登陆成功"+username);
handcookie.writeCookie();
}
}
@AfterClass
public void afterClass(){
driver.close();
}
}
配置文件

读取配置文件的方法,因为要在SuiteTestLogin.java中读取loginTest.properties文件所以构造方法
src/test/java/com/mushishi/selenium/util/ProUtil.java
package com.mushishi.selenium.util;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class ProUtil {
private Properties prop;
private String filePaht;
/**
* 构造方法
* */
public ProUtil(String filePath){
this.filePaht = filePath;
this.prop = readProperties();
}
/**
* 读取配置文件
* */
private Properties readProperties(){
Properties properties = new Properties();
try {
InputStream inputStream = new FileInputStream(filePaht);
BufferedInputStream in = new BufferedInputStream(inputStream);
properties.load(in);
} catch (IOException e) {
e.printStackTrace();
}
return properties;
}
/**
* 根据key读取关键字内容
* */
public String getPro(String key){
if(prop.containsKey(key)){
String username = prop.getProperty(key);
return username;
}else{
System.out.println("你获取key值不对");
return "";
}
}
/**
* 写入内容
* */
public void writePro(String key,String value){
Properties pro = new Properties();
try {
FileOutputStream file = new FileOutputStream(filePaht);
pro.setProperty(key, value);
pro.store(file, key);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
src/test/java/com/mushishi/selenium/page/HomePage.java
中的getUserNameElement函数获取用户名信息
package com.mushishi.selenium.page;
import org.openqa.selenium.WebElement;
import com.mushishi.selenium.base.DriverBase;
import com.mushishi.selenium.util.getByLocator;
public class HomePage extends BasePage{
public HomePage(DriverBase driver){
super(driver);
}
/**
* 获取点击登陆element
* */
public WebElement getLoginElement(){
return element(getByLocator.getLocator("login"));
}
/**
* 获取实战element
* */
public WebElement getCodingElement(){
return nodeElement(getByLocator.getLocator("tophead"),getByLocator.getLocator("coding"));
}
/**
* 获取用户名信息element
* */
public WebElement getUserNameElement(){
//移动到header上
action(element(getByLocator.getLocator("header")));
//获取用户名
return element(getByLocator.getLocator("nameInfo"));
}
}
对element.properties文件查看

src/test/java/com/mushishi/selenium/util/getByLocator.java方法读取文件element.properties文件进行处理
package com.mushishi.selenium.util;
import org.openqa.selenium.By;
public class getByLocator {
public static By getLocator(String key){
ProUtil pro = new ProUtil("element.properties");
String locator = pro.getPro(key);
String locatorType = locator.split(">")[0];
String locatorValue = locator.split(">")[1];
if(locatorType.equals("id")){
return By.id(locatorValue);
}else if(locatorType.equals("name")){
return By.name(locatorValue);
}else if(locatorType.equals("tagName")){
return By.tagName(locatorValue);
}else if(locatorType.equals("linkText")){
return By.linkText(locatorValue);
}else if(locatorType.equals("className")){
return By.className(locatorValue);
}else{
return By.xpath(locatorValue);
}
}
}
action.moveToElement用法
src/test/java/com/mushishi/selenium/base/DriverBase.java封装了action方法
文件中需要的action函数
就是利用悬停的进行用户名获取

/**
* actionMoveElement
* */
public void action(WebElement element){
Actions action =new Actions(driver);
action.moveToElement(element).perform();
}
文件中需要的定位函数
element函数
src/test/java/com/mushishi/selenium/page/BasePage.java定位元素
/**
* 定位一组elements
* */
public List<WebElement> elements(By by){
return driver.findElements(by);
}
/**
* 通过父节点定位一组elements
* */
public List<WebElement> elements(WebElement element,By by){
return element.findElements(by);
}
src/test/java/com/mushishi/selenium/handle/HomePageHandle.java
getUserName函数获取用户名里面用到了HomePage的getUserNameElement方法
package com.mushishi.selenium.handle;
import com.mushishi.selenium.base.DriverBase;
import com.mushishi.selenium.page.HomePage;
public class HomePageHandle {
public DriverBase driver;
public HomePage hp;
public HomePageHandle(DriverBase driver){
this.driver = driver;
hp = new HomePage(driver);
}
/**
* 点击登陆按钮
* */
public void clickLogin(){
hp.click(hp.getLoginElement());
}
/**
* 点击实战按钮
* */
public void clickCoding(){
hp.click(hp.getCodingElement());
}
/**
* 获取用户名
* */
public String getUserName(){
//getText获取文字
String username = hp.getText(hp.getUserNameElement());
return username;
}
}
src/test/java/com/mushishi/selenium/business/HomePagePro.java业务层上加判断
AssertLogin函数 根据用户名判断登陆信息是否正确,里面用到了HomePageHandle的getUserName()方法
package com.mushishi.selenium.business;
import com.mushishi.selenium.base.DriverBase;
import com.mushishi.selenium.handle.HomePageHandle;
public class HomePagePro {
public HomePageHandle hph;
public HomePagePro(DriverBase driver){
hph =new HomePageHandle(driver);
}
/**
* 点击登陆按钮
* */
public void clickLoginButton(){
hph.clickLogin();
}
/**
* 点击实战按钮
* */
public void clickCodingLink(){
hph.clickCoding();
}
/**
* 根据用户名判断登陆信息是否正确
* */
public Boolean AssertLogin(String username){
if(hph.getUserName().equals(username)){
return true;
}
return false;
}
}
case在企业中运用购物
利用cookie来登陆这样就不用依赖于登陆测试了
通过网页可以查看到cookies在慕课网中cookie命名为apsid

src/test/java/selenium/testCase/SuiteTestBuy.java
package selenium.testCase;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import selenium.base.DriverBase;
import selenium.business.CoursePagePro;
import selenium.handle.HandleCookie;
/**
* @Auther: ljm
* @Date: 2021/10/16/16:34
* @Description:
*/
public class SuiteTestBuy extends CaseBase{
public DriverBase driver;
public CoursePagePro cpp;
public HandleCookie handleCookie;
@BeforeClass
public void beforeClass(){
this.driver =InitDriver("chrome");
cpp = new CoursePagePro(driver);
handleCookie =new HandleCookie(driver);
driver.get("https://coding.imooc.com/class/522.html?mc_marking=bb86c9071ed9b7cf12612a2a85203372&mc_channel=hk");
handleCookie.setCookie();
//写进cookie需要再次刷新才能生效
driver.get("https://coding.imooc.com/class/522.html?mc_marking=bb86c9071ed9b7cf12612a2a85203372&mc_channel=hk");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Test
public void testBuy() {
cpp.buyNow();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@AfterClass
public void afterClass(){
driver.close();
}
}
src/test/java/selenium/handle/HandleCookie.java
对cookie进行读和写
package selenium.handle;
import org.openqa.selenium.Cookie;
import selenium.base.DriverBase;
import selenium.util.ProUtil;
import java.util.Set;
/**
* @Auther: ljm
* @Date: 2021/10/16/16:47
* @Description:
*/
public class HandleCookie {
public DriverBase driver;
//读取文件的
public ProUtil pro;
public HandleCookie(DriverBase driver) {
this.driver = driver;
pro = new ProUtil("src/test/resources/cookie.properties");
}
public void setCookie() {
String value = pro.getPro("apsid");
//作用域是imooc.com
Cookie cookie = new Cookie("apsid",value,"imooc.com","/",null);
driver.setCookie(cookie);
}
/***
* @Param:
* @return:
* @Author: ljm
* @Date: 2021/10/16
* @Description: 获取cookie
*/
public void writeCookie(){
Set<Cookie> cookies = driver.getCookie();
for (Cookie cookie : cookies) {
if (cookie.getName().equals("apsid")){
pro.writePro(cookie.getName(),cookie.getValue());
}
}
}
}
src/test/resources/cookie.properties
直接将

src/test/java/selenium/base/DriverBase.java
用到driverbase的setcookie和getcookie方法
//设置cookie
public void setCookie(Cookie cookie) {
driver.manage().addCookie(cookie);
}
//获取cookcie
public Set<Cookie> getCookie() {
Set<Cookie> cookies = driver.manage().getCookies();
return cookies;
}
src/test/java/selenium/util/ProUtil.java
对cookie.properties进行读写
package selenium.util;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class ProUtil {
private Properties prop;
private String filePaht;
/**
* 构造方法
* */
public ProUtil(String filePath){
this.filePaht = filePath;
this.prop = readProperties();
}
/**
* 读取配置文件
* */
private Properties readProperties(){
Properties properties = new Properties();
try {
InputStream inputStream = new FileInputStream(filePaht);
BufferedInputStream in = new BufferedInputStream(inputStream);
properties.load(in);
} catch (IOException e) {
e.printStackTrace();
}
return properties;
}
/**
* 根据key读取关键字内容
* */
public String getPro(String key){
if(prop.containsKey(key)){
String username = prop.getProperty(key);
return username;
}else{
System.out.println("你获取key值不对");
return "";
}
}
/**
* 写入内容
* */
public void writePro(String key,String value){
Properties pro = new Properties();
try {
FileOutputStream file = new FileOutputStream(filePaht);
pro.setProperty(key, value);
pro.store(file, key);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
对上文登陆页面进行修改
登陆成功就自动存入cookies实现自动化
在testng.xml中进行修改,这样就对两个文件进行测试,具体流程可以理解为先登陆,登陆成功并且将cookies存入cookies.properties中用于后面购买测试的登陆,再进行购买测试

二、selenium框架面试实战
TestNG如何管理case?
先建主键,之后才有了我们的case
testng配置文件,class代表配置文件

多线程实现 testng多线程机制

企业case的应用,比如说登陆的时候保存cookies,测试时候运行cookies即可。
本文详细介绍了如何在企业环境中使用TestNG进行参数化测试,包括登录验证与cookie管理,以及结合Selenium框架实现购物场景的自动化测试。涉及配置文件、多线程、cookies应用和常见技术栈如WebDriver和Page Object Model。

487

被折叠的 条评论
为什么被折叠?



