网站设计第一步怎么做,建网站怎么备案,怎么找做网站平台公司,网站承接广告宣传方案#x1f525; 交流讨论#xff1a;欢迎加入我们一起学习#xff01; #x1f525; 资源分享#xff1a;耗时200小时精选的「软件测试」资料包 #x1f525; 教程推荐#xff1a;火遍全网的《软件测试》教程 #x1f4e2;欢迎点赞 #x1f44d; 收藏 ⭐留言 #x1… 交流讨论欢迎加入我们一起学习 资源分享耗时200小时精选的「软件测试」资料包 教程推荐火遍全网的《软件测试》教程 欢迎点赞 收藏 ⭐留言 如有错误敬请指正 自动化测试模型可以看作自动化测试框架与工具设计的思想。随着自动化测试技术的发展演化为以下几种模型
线性测试模块化驱动侧式数据驱动测试关键字驱动测试
数据驱动测试
前一篇所讲的模块化驱动测试能够很好的解决脚本重复的问题但是在针对同一个功能进行不同数据的测试从而检测测试结果的变化时仍然需要重复地编写测试脚本。于是数据驱动测试的概念就为解决这类问题而被提出。 我们可以通过读取定义的数组、字典或者是外部文件excel、csv、txt、xml等都可以看作是数据驱动从而实现数据与脚本的分离进一步增强脚本的复用性。 读取txt文件 Java
(ノへ、)这段代码存在一个问题txt文件中有四组数据但运行时只执行了三组数据运行时忽略了一条密码为空的数据。 data.javapackage PublicMethods;import java.io.*;
import java.util.*;public class data {//读取txt文件public static Map txtData(String fileName) throws IOException {MapString, String map new HashMapString , String(); //存放多个键值对String[] arryTemp null;String username null;String password null;String s null;File file new File(fileName);FileReader fr new FileReader(file);BufferedReader br new BufferedReader(fr);while((s br.readLine()) ! null){arryTemp s.split(,); //将一行数据存入数组username arryTemp[0]; //获取账户password arryTemp[1]; //获取密码map.put(username, password); //存储一组账号密码}return map;}
} share.javapackage PublicMethods;import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebDriver;
import java.util.Map;
import java.util.Set;
import org.openqa.selenium.firefox.FirefoxDriver;public class share {private static WebDriver driver;public static WebDriver OpenDriver(String url){driver new FirefoxDriver();driver.get(url);return driver;}public static void UserLogin(WebDriver driver, Map map) throws InterruptedException {SetString keyset map.keySet(); //获取Map的值for(String count : keyset) {Thread.sleep(2000);driver.findElement(By.xpath(//*[idusername])).sendKeys(count);System.out.println(count);driver.findElement(By.xpath(//*[idpassword])).sendKeys(map.get(count).toString());Thread.sleep(2000);driver.findElement(By.xpath(//*[idlogin_button])).click();Thread.sleep(2000);try{driver.switchTo().alert().accept();Thread.sleep(2000);driver.findElement(By.xpath(//*[idusername])).clear();driver.findElement(By.xpath(//*[idpassword])).clear();}catch(NoAlertPresentException NofindAlert){UserLogout(driver);} }}public static void UserLogout(WebDriver driver) throws InterruptedException{driver.findElement(By.xpath(//*[idlogout_button])).click();Thread.sleep(2000);}
} LoginTest.javapackage Test;import java.io.IOException;
import java.util.*;
import PublicMethods.*;import org.openqa.selenium.WebDriver;public class LoginTest {public static void main(String[] args) throws InterruptedException, IOException {// TODO Auto-generated method stubWebDriver driver PublicMethods.share.OpenDriver(file:///D:/%E7%99%BB%E5%BD%95.html);String filename D:\\app_tool\\eclipse-workspace\\AutoTest\\TestData\\user_info.txt;Map map txtData(filename);UserLogin(driver, map);driver.quit();}
} Python data.pyclass data():# 读取txt文件def txtData(self, fileName):file open(fileName, r)lines file.readlines()file.close()return lines share.pyfrom time import *
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ECclass share():# 启动浏览器def open_driver(self, url):driver webdriver.Firefox()driver.implicitly_wait(10)driver.get(url)return driver# 登录账号检测def user_login(self, driver, lines):for line in lines:sleep(2)driver.find_element(By.XPATH, //*[idusername]).send_keys(line.split(,)[0])driver.find_element(By.XPATH, //*[idpassword]).send_keys(line.split(,)[1])sleep(2)driver.find_element(By.XPATH, //*[idlogin_button]).click()sleep(2)result EC.alert_is_present()(driver)# 判断是否有弹窗if result:result.accept()driver.find_element(By.XPATH, //*[idusername]).clear()driver.find_element(By.XPATH, //*[idpassword]).clear()# 退出账号def user_logout(self, driver):driver.find_element(By.XPATH, //*[idlogout_button]).click()sleep(2) LoginTest.pyfrom public import share,datadriver share.share().open_driver(file:///D:/%E7%99%BB%E5%BD%95.html)filename user_info.txt
lines data.data().txtData(filename)share.share().user_login(driver, lines)
share.share().user_logout(driver)
driver.quit() Ruby 读取csv文件
该方法同样适用于读取txt文件
Java data.javapackage PublicMethods;import java.io.*;
import java.util.*;public class data {//读取csv文件public static ArrayListString[] csvData(String fileName){ArrayListString[] list new ArrayListString[](); //创建保存数据集合CsvReader cReader null;try{cReader new CsvReader(fileName);//是否跳过表头cReader.readHeaders();while(cReader.readRecord()){list.add(cReader.getValues());}}catch(Exception e) {e.printStackTrace();}finally{cReader.close();}//如果使用testng的DataProvider,可以返回一个二维数组Object data[][] new Object[list.size()][];for(int i0;ilist.size();i){data[i]list.get(i);}return list;}
} share.javapackage PublicMethods;import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebDriver;
import java.util.ArrayList;
import org.openqa.selenium.firefox.FirefoxDriver;public class share {private static WebDriver driver;public static WebDriver OpenDriver(String url){driver new FirefoxDriver();driver.get(url);return driver;}public static void UserLogin(WebDriver driver, ArrayListString[] list) throws InterruptedException {for(int i0;ilist.size();i) {Thread.sleep(2000);driver.findElement(By.xpath(//*[idusername])).sendKeys(list.get(i)[1]);driver.findElement(By.xpath(//*[idpassword])).sendKeys(list.get(i)[2]);Thread.sleep(2000);driver.findElement(By.xpath(//*[idlogin_button])).click();Thread.sleep(2000);try{driver.switchTo().alert().accept();Thread.sleep(2000);driver.findElement(By.xpath(//*[idusername])).clear();driver.findElement(By.xpath(//*[idpassword])).clear();}catch(NoAlertPresentException NofindAlert){UserLogout(driver);} }}public static void UserLogout(WebDriver driver) throws InterruptedException{driver.findElement(By.xpath(//*[idlogout_button])).click();Thread.sleep(2000);}
} LoginTest.javapackage Test;import java.io.IOException;
import java.util.*;
import PublicMethods.*;import org.openqa.selenium.WebDriver;public class LoginTest {public static void main(String[] args) throws InterruptedException, IOException {// TODO Auto-generated method stubWebDriver driver PublicMethods.share.OpenDriver(file:///D:/%E7%99%BB%E5%BD%95.html);String filename D:\\app_tool\\eclipse-workspace\\AutoTest\\TestData\\user_info.csv;ArrayListString[] list csvData(filename);UserLogin(driver, list);driver.quit();}
} Python data.pyimport csvclass data():# 读取CSV文件def csvData(self, fileName):lines csv.reader(open(fileName, r))return lines share.pyfrom time import *
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ECclass share():# 启动浏览器def open_driver(self, url):driver webdriver.Firefox()driver.implicitly_wait(10)driver.get(url)return driver# 登录账号检测def user_login(self, driver, lines):for line in lines:sleep(2)driver.find_element(By.XPATH, //*[idusername]).send_keys(line[0])driver.find_element(By.XPATH, //*[idpassword]).send_keys(line[1])sleep(2)driver.find_element(By.XPATH, //*[idlogin_button]).click()sleep(2)result EC.alert_is_present()(driver)# 判断是否有弹窗if result:result.accept()driver.find_element(By.XPATH, //*[idusername]).clear()driver.find_element(By.XPATH, //*[idpassword]).clear()# 退出账号def user_logout(self, driver):driver.find_element(By.XPATH, //*[idlogout_button]).click()sleep(2) LoginTest.pyfrom public import share,datadriver share.share().open_driver(file:///D:/%E7%99%BB%E5%BD%95.html)filename user_info.csv
lines data.data().csvData(filename)share.share().user_login(driver, lines)
share.share().user_logout(driver)
driver.quit() Ruby 读取excel文件
Excel文件数据必须时文本格式
Java
进入http://poi.apache.org/download.html下载POI的Jar包 问题一 解决方法 进入http://mvnrepository.com/artifact/org.apache.xmlbeans/xmlbeans/2.6.0下载jar包
问题二 解决方法 进入http://mvnrepository.com/artifact/org.apache.commons/commons-collections4/4.1下载jar包
问题三 在遇到Excel单元值为空时sheet.getRow(i).getCell(j).getStringCellValue()会报错 解决方法 在Excel中把空值改为空格然后在代码中获取该值后去空格。 data.javapackage PublicMethods;import java.io.*;
import java.util.*;public class data {//读取Excel文件public static XSSFSheet excelData(String fileName) throws IOException{File file new File(fileName);FileInputStream is new FileInputStream(file);XSSFWorkbook wb new XSSFWorkbook(is); //加载workbookXSSFSheet sheet wb.getSheetAt(0); //加载sheetreturn sheet;}
} share.javapackage PublicMethods;import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;public class share {private static WebDriver driver;public static WebDriver OpenDriver(String url){driver new FirefoxDriver();driver.get(url);return driver;}public static void UserLogin(WebDriver driver, XSSFSheet sheet) throws InterruptedException {for(int i0;isheet.getLastRowNum();i) {Thread.sleep(2000);driver.findElement(By.xpath(//*[idusername])).sendKeys(sheet.getRow(i).getCell(1).getStringCellValue().toString().trim());driver.findElement(By.xpath(//*[idpassword])).sendKeys(sheet.getRow(i).getCell(2).getStringCellValue().toString().trim());Thread.sleep(2000);driver.findElement(By.xpath(//*[idlogin_button])).click();Thread.sleep(2000);try{driver.switchTo().alert().accept();Thread.sleep(2000);driver.findElement(By.xpath(//*[idusername])).clear();driver.findElement(By.xpath(//*[idpassword])).clear();}catch(NoAlertPresentException NofindAlert){UserLogout(driver);} }}public static void UserLogout(WebDriver driver) throws InterruptedException{driver.findElement(By.xpath(//*[idlogout_button])).click();Thread.sleep(2000);}
} LoginTest.javapackage Test;import java.io.IOException;
import PublicMethods.*;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.openqa.selenium.WebDriver;public class LoginTest {public static void main(String[] args) throws InterruptedException, IOException {// TODO Auto-generated method stubWebDriver driver PublicMethods.share.OpenDriver(file:///D:/%E7%99%BB%E5%BD%95.html);String filename D:\\app_tool\\eclipse-workspace\\AutoTest\\TestData\\user_info.xlsx;XSSFSheet sheet excelData(filename);UserLogin(driver, sheet);driver.quit();}
} Python data.pyimport xlrdclass data():# 读取excel文件def execelData(self, fileName, sheetName):data xlrd.open_workbook(fileName)# 通过索引顺序获取# table data.sheets()[0]# table data.sheet_by_index(0)table data.sheet_by_name(sheetName)# 获取一行或一列的值参数是第几行# table.row_values(0) 获取第一行的值# table.col_values(0) 获取第一列的值return table share.pyfrom time import *
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ECclass share():# 启动浏览器def open_driver(self, url):driver webdriver.Firefox()driver.implicitly_wait(10)driver.get(url)return driver# 登录账号检测def user_login(self, driver, table):rows table.nrowsfor i in range(rows):sleep(2)driver.find_element(By.XPATH, //*[idusername]).send_keys(table.cell(i, 1).value)driver.find_element(By.XPATH, //*[idpassword]).send_keys(table.cell(i, 2).value)sleep(2)driver.find_element(By.XPATH, //*[idlogin_button]).click()sleep(2)result EC.alert_is_present()(driver)# 判断是否有弹窗if result:result.accept()driver.find_element(By.XPATH, //*[idusername]).clear()driver.find_element(By.XPATH, //*[idpassword]).clear()# 退出账号def user_logout(self, driver):driver.find_element(By.XPATH, //*[idlogout_button]).click()sleep(2) LoginTest.pyfrom public import share,datadriver share.share().open_driver(file:///D:/%E7%99%BB%E5%BD%95.html)filename TestData/user_info.xlsx
sheetname test
table data.data().execelData(filename, sheetname)share.share().user_login(driver, table)
share.share().user_logout(driver)
driver.quit() Ruby 浅谈关键字驱动测试 在数据驱动的基础上我们把“数据”转化为“关键字”后通过关键字的改变从而引起测试结果的变化。 为何我要在这里说明是“浅谈”呢在关键字驱动测试中我们可以将测试的对象、满足条件、传输值、断言等甚至是所需要读取的外部文件以及外部类库所有的相关条件存储在文件中典型的关键字驱动工具UFT。我们可以将关键字以“填表格”形式写入文件中从而降低脚本的编写难度。 正因如此采用关键字驱动测试来编写同样的脚本需要较高的学习成本。同样这样的框架越到后期越难维护可靠性也会变差。所以暂时不深入研究关键字驱动测试。 最后我邀请你进入我们的【软件测试学习交流群785128166】 大家可以一起探讨交流软件测试共同学习软件测试技术、面试等软件测试方方面面还会有免费直播课收获更多测试技巧我们一起进阶Python自动化测试/测试开发走向高薪之路 作为一个软件测试的过来人我想尽自己最大的努力帮助每一个伙伴都能顺利找到工作。所以我整理了下面这份资源现在免费分享给大家有需要的小伙伴可以关注【公众号程序员二黑】自提