引言:

  做自动化测试,尤其是通过page object模式做UI自动化测试,登陆是个很麻烦的事情,比如你想对某个页面进行测试,一般直接链接到那个页面是不可能的,总是需要先登陆,然后刷新页面才能到想要的页面;而登录总不能每次都通过输入用户名、密码、验证码等登录,这就比较不智能,不自动化了;之前都是通过cookie来绕过登录,driver.add_cookie({'name': 'cookie_key', 'value': 'cookie_value'});但是HTML5发布后,慢慢不在通过cookie来记录用户的信息,而是使用localstorage,什么是localstorage??请自己百度一下;

正文:

  先上代码,这是我们公司内网的系统,估计看这文章的朋友,直接用我的代码会执行不了,可以看下基本思想,然后融会贯通到自己的系统中;

#coding:utf-

from selenium import webdriver
import time
import requestsimport json

def get_token():
    api_url = 'http://192.168.0.212:808/api/gwy/user/login.json'
    request_data = 'data={"param":{"mobile":"18800080008","password":"96e79218965eb72c92a549dd5a330112"},"sign":"2b78538442997ea1b47eda413c91bf95"}'
    HEADER = {
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
    }

    res = requests.post(api_url, data=request_data, headers=HEADER)
    return json.dumps(res.json()['data']['token'])

driver = webdriver.Chrome()
driver.get('http://192.168.0.212:808/html/marketTypeManagement.html')

time.sleep()
# driver.add_cookie({'name': 'token', 'value': 'f53fe0f1-504b-4cac-adde-eb17e42fde72'})
driver.execute_script('localStorage.setItem("token", %s);'% get_token())

time.sleep()
driver.quit()

解读一下代码:

  跟开发确认,是通过localstorage中的token字段来验证用户登录信息;所以首先一步是拿到登录的token,我这里是通过定义一个get_token()函数来调登录的接口,从接口返回值中拿到token值;

  用的是requests模块模拟发起http请求,获取到返回值后,提取token值;

  然后进行测试,主要是用执行一段js语法的脚本代码来向localStorage中注入获取到的token值,就能绕过登录,直接到你指定的页面了;

driver.execute_script('localStorage.setItem("token", arguments["token"]);', get_token()['data'])

selenium自动化测试通过localstorage绕过登陆的更多相关文章

  1. selenium自动化测试工具模拟登陆爬取当当网top500畅销书单

    selenium自动化测试工具可谓是爬虫的利器,基本动态加载的网页都能抓取,当然随着大型网站的更新,也出现针对selenium的反爬,有些网站可以识别你是否用的是selenium访问,然后对你加以限制 ...

  2. Selenium自动化测试框架介绍

    Selenium自动化测试框架介绍 1.测试架构作用 a.可维护性 b.提高编写脚本效率 c.提高脚本的可读性 2.框架的几大要素: Driver管理,脚本,数据,元素对象,LOG,报告,运行机制,失 ...

  3. Selenium自动化测试,接口自动化测试开发,性能测试从入门到精通

    Selenium自动化测试,接口自动化测试开发,性能测试从入门到精通Selenium接口性能自动化测试基础部分:分层自动化思想Slenium介绍Selenium1.0/2.0/3.0Slenium R ...

  4. 添加cookie绕过登陆

    添加cookie绕过登陆 #!/usr/bin/env python # -*- coding: utf-8 -*- from selenium import webdriver import tim ...

  5. 《手把手教你》系列技巧篇(三十)-java+ selenium自动化测试- Actions的相关操作下篇(详解教程)

    1.简介 本文主要介绍两个在测试过程中可能会用到的功能:Actions类中的拖拽操作和Actions类中的划取字段操作.例如:需要在一堆log字符中随机划取一段文字,然后右键选择摘取功能. 2.拖拽操 ...

  6. 《手把手教你》系列技巧篇(六十四)-java+ selenium自动化测试 - cookie -中篇(详细教程)

    1.简介 今天按照原计划宏哥要用实例来给小伙伴或童鞋们来演示一下,如何利用cookie实现跳过验证码进行登录.这个场景是自动登陆.有很多系统的登陆信息都是保存在cookie里的,因此只要往cookie ...

  7. JavaScript(Node.js)+ Selenium自动化测试

    Selenium is a browser automation library. Most often used for testing web-applications, Selenium may ...

  8. Eclipse+Selenium自动化测试脚本设计V1.0

    Eclipse+Selenium自动化测试脚本设计V1.0 http://www.docin.com/p-803032251.html

  9. Selenium自动化测试实践 公开班(广州)

    Selenium自动化测试实践 公开班(广州) http://gdtesting.com/product.php?id=115

随机推荐

  1. liunx的磁盘管理的基本命令

    df     查看磁盘占用率 du -sh    查看磁盘多大 sudo fdisk -l   查看硬盘信息 sudo mkfs -t ext3 /dev/sdb1    建立文件系统(相当于格式化) ...

  2. [LeetCode&Python] Problem 867. Transpose Matrix

    Given a matrix A, return the transpose of A. The transpose of a matrix is the matrix flipped over it ...

  3. js中准确判断数据类型的方法

    一   通用的typeof 方法 typeof  ture    输出   Boolean typeof  123   输出     number ..... 但是   typeof 无法判断  nu ...

  4. 谈ObjC对象的两段构造模式

    前言 Objective-c语言在申请对象的时,需要使用两段构造(Two Stage Creation)的模式.一个对象的创建,需要先调用alloc方法或allocWithZone方法,再调用init ...

  5. [转载]PHP 连接 Rabbitmq 实例代码

    转自 http://www.dahouduan.com/2017/11/23/php-rabbitmq-demo/ 接下来我们用 php 连接 rabbitmq 玩一玩.还没有安装 rabbitmq ...

  6. 《DSP using MATLAB》Problem 4.14

    代码: %% ---------------------------------------------------------------------------- %% Output Info a ...

  7. hdu2079 选课时间(题目已修改,注意读题) 母函数

    计算数的和的种类,母函数裸题 #include<stdio.h> #include<string.h> ],c2[],a,b; int main(){ int T; while ...

  8. graphql elasticsearch 集成试用

    graphql 是很方便的api 查询语言,elasticsearch 可以方便的进行全文检索的应用开发 有一个方便的npm 包graphql-compose-elasticsearch 可以进行es ...

  9. cmd下查看当前登陆用户

    cmd下查看当前登陆用户 终端下,自然可以用quser这个命令了.但是在其它如专业版2k下如何查看在线用户呢? C:\Documents and Settings\Administrator>n ...

  10. vue-cli 下的 webpack 优化

    app bundle 可以通过组件懒加载优化 vender 优化方法: 使用 cdn,然后在 webpack 配置中指定 externals. 利用 webpack 的插件 DllPlugin 和 D ...