python+selenium自动化软件测试(第6章):selenium phantomjs页面解析使用

我们都知道Selenium是一个Web的自动化测试工具,可以在多平台下操作多种浏览器进行各种动作,比如运行浏览器,访问页面,点击按钮,提交表单,浏览器窗口调整,鼠标右键和拖放动作,下拉框和对话框处理等,我们抓取时选用它,主要是Selenium可以渲染页面,运行页面中的JS,以及其点击按钮,提交表单等操作。

from selenium import webdriver
driver = webdriver.PhantomJS()
driver.get("http://www.xxxxxx.com")
data = driver.title
print data

我们为什么要用phantomjs呢?

介绍

PhantomJS是一个基于webkit的JavaScript API。任何你可以在基于webkit浏览器做的事情,它都能做到。它不仅是个隐形的浏览器(没有UI界面的浏览器),提供了诸如CSS选择器、支持Web标准、DOM操作、JSON、HTML5、Canvas、SVG等,同时也提供了处理文件I/O的操作,从而使你可以向操作系统读写文件等。PhantomJS的用处可谓非常广泛,诸如前端无界面自动化测试(需要结合Jasmin)、网络监测、网页截屏等。

windows下进行安装:

pip install selenium

phantomjs使用简单的使用方式:

from selenium import webdriver
browser = webdriver.PhantomDS(‘D:\phantomjs.exe‘) #浏览器初始化;Win下需要设置phantomjs路径,linux下置空即可
url = ‘http://www.xxxxxx.com‘ # 设置访问路径地址
browser.get(url) # 打开网页
title = browser.find_elements_by_xpath(‘xxxxxx‘) #用xpath获取元素
for t in title: # 遍历输出
  print t.text #输出其中文本
  print t.get_attribute(’class’)# 输出属性值
browser.qiiit() #关闭浏览器。当出现异常时记得在任务浏览器中关闭

我们进行一个简单的对比操作,首先请回顾一下selenium webdriver的操作

from selenium import webdriver
driver = webdriver.Firefox()
driver.get("https: //www.xxxxxx.com/")
dniver.find_element_by_id(‘xxxxxxxx‘).send_keys("nxxxxxx")
dniver.find_element_by_id("xxxxxxxx").click()
driver.quit()

使用phantomjs

from selenium import webdriver
driver = webdriver.PhantomJS()
driver.set_window_size(xxx,xxx) #浏览器大小
driver.get ("https: //www.xxx.com/")
dniver.find_element_by_id(‘xxxx‘).send_keys("xxxx")
dniver.find_element_by_id("xxxxxx").click()
print driver.current_url
driver.quit()

通过以上两个案例大家应该可以看出相关的一个区别所在!!
编写一个简单的断言来判断phantomjs获取得到的URL是否正确的呢:

import unittest
from selenium import webdriver
class TestOne(unittest.TestCase):
    def setUp(self):
        self.driver = webdniver.PhantomDS()
        self.driver.set_window_size(xxx, xxx)
    def test_url(self):
        self.driver.get("https://www.xxx.com")
        self.driver.find_element_by_id(‘xxxxxx‘).send_keys("xxxx")
        self.driver.find_element_by_id("xxxxx").click()
        self.assentln("https://www.xxx.com", self.driver.current_url)
    def tearDown(self):
        self.driver.quit()

if __name__ == "__main__":
    unittest.main()                  

那么你会发现通过以上的单元测试进行断言后是完全可以通过的。
使用PhantomJS在浏览器的一个主要优点是测试通常要快得多。

import unittest
from selenium import webdriver
import time

class TestThree(unittest.TestCase):
  def setUp(self):
    self.startTime = time.time()
  def test_unl_fire(self):
    time.sleep(2)
    self.driver = webdniver.Firefox()
    self.driver.get("https://www.xxx.com")
    button = self.driver.find_element_by_id("xxx").get_attribute("xxxx")
    self.assentEquals(‘xxxxx‘, button)
  def test_unl_phantom(self):
    time.sleep(l)
    self.driver = webdniver.PhantomDS()
    self.driver.get("https://www.xxx.com")
    button = self.driver.find_element_by_id("xxxx").get_attribute("xxxx")
    self.assentEquals(‘xxxxx‘, button)
  def tearDown(self):
    t = time.time() - self.startTime
            print "%s: %.3f"% (self.id(), t)
            self.driver.quit()

if __name__== ‘__main__‘:
    suite = unittest.TestLoader().loadTestsFromTestCase(TestThree)
    unittest.TextTestRunner(verbosity=0).run(suite)                

通过两个时间上的一个对比你会发现使用phantomjs速度有多快
内容拓展:

# coding:utf-8
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui
import WebDriverWait
from selenium.webdriver.support
import expected_conditions as ec
import nose.tools as nose

#帐户
email = ‘user‘
password = ‘password‘

# phantomjs

# user agent
user_agent = ‘Mozilla/5.0 (Windows NT 5.1)
AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/29.0.1547.66 Safari/537.36‘

# PhantomUS的路径
pjs_path = ‘xx/node_modules/phantomjs/bin/phantomjs
dcap = {"phantomjs.page.settings.userAgent":
user_agent,
‘marionette‘ : True
}

driver = webdriver.PhantomJS(executable_path=pjs_path,
desired_capabilities=dcap)
# 5秒
wait = WebDriverWait(driver, 5)
#获取html登录页面
login_page_url = ‘http://xxx‘
driver.get(login_page_url)
#等到页面加载
wait.until(ec.presence_of_all_elements_located)
#检查当前网址
nose.eq_(‘http://xxx‘, driver.current_url)

# login

# button click
show_signin = driver.find_element_by_id(‘xxx‘)
show_signin.click()

# email
login_xpath = ‘xxx"]‘

#等待对象元素
wait.until(ec.visibility_of_element_located((By.XPATH, login_xpath)))

login_id_form =driver.find_element_by_xpath(login_xpath)
login_id_form.clean()
login_id_form.send_keys(email)

# password
password_xpath = ‘xxxx‘
#等待对象元素
wait.until(ec.visibility_of_element_located((By.XPATH, password_xpath)))
# password
password_form = driver.find_element_by_xpath(passwond_xpath)
password_form.clean()
password_form.send_keys(password)
# submit
submit_xpath = ‘xxxx‘
dniver.find_element_by_xpath(submit_xpath).click()
# result
driver.get(‘http://xxx‘)
#等到页面加载
wait.until(ec.presence_of_all_elements_located)
#检查当前网址
nose.eq_(‘http://xxx‘, driver.current_url)
user_email = driver.find_element_by_xpath(‘xxx‘).get_attribute(
"XXX")
nose.eq_(email, user_email)
时间: 2024-08-01 21:40:03

python+selenium自动化软件测试(第6章):selenium phantomjs页面解析使用的相关文章

python+selenium自动化软件测试(第10章):测试驱动TDD

测试驱动开发模式,要求开发在写业务代码的时候,先写出测试代码,同时单元测试例子决定了如何来写产品的代码,并且不断的成功的执行编写的所有的单元测试例子,不断的完善单元测试例子进而完善产品代码, 这样随着功能的开发完成,测试代码也会对应的完成, 很显然,这是一个全新的开发模式, 在一定程度上,可以完全的提高软件的质量,以及开发可以对自己写的代码进行一个全面的评估和测试. TDD 模式是一个很大的概念,在这里, 我重点介绍下测试驱动模式与自动化的融合以及精简自动化的测试代码.下面我们来看一个登录的案例

python+selenium自动化软件测试(第9章) :Logging模块

9.1 Logging模块 什么是日志记录?记录是跟踪运行时发生的事件的一种手段.该软件的开发人员将记录调用添加到其代码中,以指示某些事件已发生.事件由描述性消息描述,该消息可以可选地包含可变数据(即,对于事件的每次出现可能不同的数据).事件也是开发人员对事件的重视; 重要性也可以称为级别 或严重性.记录功能logging.debug('此功能提供详细信息')logging.warning('意外发生')logging.error('用于存储异常跟踪')logging.info('确认事情正在按

python+selenium自动化软件测试(第8章) 多线程

前戏:线程的基础 运行多个线程同时运行几个不同的程序类似,但具有以下优点:进程内共享多线程与主线程相同的数据空间,如果他们是独立的进程,可以共享信息或互相沟通更容易.线程有时称为轻量级进程,他们并不需要多大的内存开销,他们关心的不是过程便宜.一个线程都有一个开始,执行顺序,并得出结论.它有一个指令指针,保持它的上下文内正在运行的跟踪.(1).它可以是抢占(中断)(2).它可以暂时搁置(又称睡眠),而其他线程正在运行看一下以下的小案例: import thread from time import

python+selenium自动化软件测试(第13章):selenium面试题

前言最近看到群里有小伙伴贴出一组面试题,最近又是跳槽黄金季节,小编忍不住抽出一点时间总结了下 一.selenium中如何判断元素是否存在?expected_conditions模块提供了16种判断方法,以下方法是判断元素存在DOM中:presence_of_element_located    """ An expectation for checking that an element is present on the DOM of a page. This does n

python+selenium自动化软件测试(第11章):持续集成jenkins和GitHub的使用

11.1 jenkins持续集成环境 相关安装包下载链接:http://pan.baidu.com/s/1qYhmlg4 密码:dcw2赠送jenkins集成selenium环境视频链接http://pan.baidu.com/s/1qXAHwg0 密码:juy7 11.2 tomcat+jenkins *******************************************************************************相关安装包下载链接:http://p

python+selenium自动化软件测试(第14章):基础实战(1)

#coding=utf-8 from selenium import webdriven from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import Select from selenium.common.exceptions import NoSuchElementException im

python+selenium自动化软件测试(第16章):基础实战(3)

#coding:utf-8 from time import sleep from selenium import webdriver class cloudedge_register(object): def __init__(self,mailaddr,passwd,url): self.mailaddr = mailaddr self.passwd = passwd self.url = url self.browser = webdriver.Finefox() def register

python+selenium自动化软件测试(第7章):Page Object模式

什么是Page ObjectModel模式Page Objects是selenium的一种测试设计模式,主要将每个页面看作是一个class.class的内容主要包括属性和方法,属性不难理解,就是这个页面中的元素对象,比如输入用户名的输入框,输入登陆密码的输入框,登陆按钮,这个页面的url等,而方法,主要是指这个页面可以提供的具体功能.为什么选择POM?我们先看一段简单的代码如下: from selenium import webdriver import time driver = webdri

python+selenium自动化软件测试(第5章):Selenium Gird

5.1 分布式(Grid) Selenium grid是用来分布式执行测试用例脚本的工具,比如测试人员经常要测试多浏览器的兼容性,那就可以用到grid了.下面就来介绍如何在多个浏览器上运行同一份脚本.使用grid所需要的文件:1.Selenium server(即selenium-server-standalone-x.xx.x.jar):2.grid配置文件(该文件负责提供主机和浏览器信息):3.测试脚本.一.先来看看grid配置文件的内容:def grid():    d={'http://