selenium之Cookie的应用-取消Case耦合,依赖关系

来源:互联网 发布:win7回收站恢复数据 编辑:程序博客网 时间:2024/06/01 07:43

引言:

项目中我们为了不出现多个case有较强的依赖,产生前面的case出现bug导致后面的case也无法运行的情况的出现,比如购买商品的时候一般用户都得登录,而购买商品就是依赖于登录,为了解决这个问题,我们需要引进Cookie这个用户的唯一登录识别信息来解决!

Cookie原理解读:Cookie的差别在于登录前后页面的显示情况:

登录前:

这里写图片描述

登录后:

这里写图片描述

下面结合购物车实例讲解,步骤:

首先创建一个名为cookie.properties的配置文件,用于存放我们的cookie

这里写图片描述

创建一个操作Cookie的工具类:HandleCookie

package com.selenium.wushuaiTest.util;import java.util.Set;import org.openqa.selenium.Cookie;import com.selenium.wushuaiTest.base.DriverBase;public class HandleCookie {    public DriverBase driverBase;    //要读取cookie.properties配置文件,所以需要Proutil工具类    public ProUtil proUtil;    public HandleCookie(DriverBase driverBase) {        this.driverBase=driverBase;        proUtil=new ProUtil("cookie.properties");    }    /**     * 设置cookie方法     *      * */    public void setCookie() {        String value=proUtil.getPro("apsid");        /**         * 五个参数的         * 第一个参数:cookie的名称,任意取         * 第二个参数:cookie的值         * 第三个参数:path:域名地址-代表我们只要是我们所测网站的域名,都表示有效         * 第四个代表全部路径下         * 第五个参数:日期         * */        Cookie cookie=new Cookie("apsid", value, "imooc.com", "/",null);        //把当前的cookie成功添加到Set<Cookie>集合中,在DriverBase类中封装了setCookie方法        driverBase.setCookie(cookie);    }    /**     * 获取cookie方法     * 原理:     *      * */    public void writeCookie() {        Set<Cookie> cookies=driverBase.getCookie();        for(Cookie cookie:cookies) {            if(cookie.getName().equals("apsid")) {                proUtil.writePro(cookie.getName(),cookie.getValue());            }        }    }}

创建一个测试立即购买的测试类SuiteTestBuy

package com.selenium.wushuaiTest.util;import java.util.Set;import org.openqa.selenium.Cookie;import com.selenium.wushuaiTest.base.DriverBase;public class HandleCookie {    public DriverBase driverBase;    //要读取cookie.properties配置文件,所以需要Proutil工具类    public ProUtil proUtil;    public HandleCookie(DriverBase driverBase) {        this.driverBase=driverBase;        proUtil=new ProUtil("cookie.properties");    }    /**     * 设置cookie方法     *      * */    public void setCookie() {        String value=proUtil.getPro("apsid");        /**         * 五个参数的         * 第一个参数:cookie的名称,任意取         * 第二个参数:cookie的值         * 第三个参数:path:域名地址-代表我们只要是我们所测网站的域名,都表示有效         * 第四个代表全部路径下         * 第五个参数:日期         * */        Cookie cookie=new Cookie("apsid", value, "imooc.com", "/",null);        //把当前的cookie成功添加到Set<Cookie>集合中,在DriverBase类中封装了setCookie方法        driverBase.setCookie(cookie);    }    /**     * 获取cookie方法     * 原理:     *      * */    public void writeCookie() {        Set<Cookie> cookies=driverBase.getCookie();        for(Cookie cookie:cookies) {            if(cookie.getName().equals("apsid")) {                proUtil.writePro(cookie.getName(),cookie.getValue());            }        }    }}

在登录测试类中引入HandleCookid工具类,便于登录完成后写入我们的用户的相应的Cookie值

package com.selenium.wushuaiTest.testCase;import java.util.concurrent.TimeUnit;import org.openqa.selenium.Cookie;import org.testng.annotations.AfterClass;import org.testng.annotations.BeforeClass;import org.testng.annotations.Parameters;import org.testng.annotations.Test;import com.selenium.wushuaiTest.base.DriverBase;import com.selenium.wushuaiTest.business.HomePagePro;import com.selenium.wushuaiTest.business.LoginPro;import com.selenium.wushuaiTest.util.HandleCookie;import com.selenium.wushuaiTest.util.ProUtil;public class SuiteTestLogin extends CaseBase{    public DriverBase driver;    public LoginPro loginPro;    public ProUtil proUtil;    public HomePagePro homePagePro;    public HandleCookie handleCookie;    //相当于构造方法    @BeforeClass    public void beforeClass() {        this.driver=InitDriverBase("chrome");        proUtil=new ProUtil("loginTest.properties");        handleCookie=new HandleCookie(driver);        //设置系统响应时间为10秒钟        driver.driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);        loginPro=new LoginPro(driver);        homePagePro=new HomePagePro(driver);        driver.get(proUtil.getPro("url"));        driver.getWindowMax();    }    @Test    @Parameters({"username","password"})    public void testLogin(String username,String password) {        loginPro.login(username, password);        driver.sleep(5000);        if(homePagePro.AssertLogin(proUtil.getPro("yq"))){            System.out.println("登陆成功"+username);            //确定登录成功后,写入我们当前用户的Cookie            handleCookie.writeCookie();        }    }    @AfterClass    public void afterClass() {        driver.getDriver().close();    }}

testNG中配置:

这里写图片描述

<?xml version="1.0" encoding="UTF-8"?><suite name="Suite" parallel="false">    <parameter name ="username" value="m13031155057@163.com"/>    <parameter name ="password" value="qw150831"/>  <test name="Test1">    <classes>      <class name="com.selenium.wushuaiTest.AppTest"/>      <class name="com.selenium.wushuaiTest.testCase.SuiteTestLogin"/>      <class name="com.selenium.wushuaiTest.testCase.SuiteTestBuy"/>    </classes>  </test></suite>

整体效果,首先执行登录,登录完毕后cookie.properties中会自动记录用户的cookie,当我们下单商品时直接用我们之前的Cookie登录下单


总结:

1.先网页,后设置cookie,最后再网页刷新效果

/**         *          *易错点:先执行handleCookie.setCookie();         *再进入网页driverBase.get("http://coding.imooc.com/class/149.html");         * 错误原因:典型的执果索因,如果我们提前设置cookie,猛一看上去没毛病,         * 但仔细一想,你都不知道进哪个网页,你把cookie设置给谁呢?我们的大前提弄错了         *          * driverBase.get("http://coding.imooc.com/class/149.html");         * handleCookie.setCookie();         * 进入页面后紧接着设置完cookie后,网页不会自动刷新,所以我们要自己重新加载一下网页         *          * */

2.在很多时候我们登录之后系统是会保存之前的一个用户的cookie,为了登录另一个账号,所以我们应该学会删除之前的一个Cookie,删除实在下次登录前执行

阅读全文
0 0
原创粉丝点击