zoukankan      html  css  js  c++  java
  • 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)
  • 相关阅读:
    北京燃气IC卡充值笔记
    随机分析、随机控制等科目在量化投资、计算金融方向有哪些应用?
    量化交易平台大全
    Doctor of Philosophy in Computational and Mathematical Engineering
    Institute for Computational and Mathematical Engineering
    Requirements for the Master of Science in Computational and Mathematical Engineering
    MSc in Mathematical and Computational Finance
    万字长文:详解多智能体强化学习的基础和应用
    数据处理思想和程序架构: 使用Mbedtls包中的SSL,和服务器进行网络加密通信
    31-STM32+W5500+AIR202/302基本控制篇-功能优化-W5500移植mbedtls库以SSL方式连接MQTT服务器(单向忽略认证)
  • 原文地址:https://www.cnblogs.com/zidonghua/p/7435904.html
Copyright © 2011-2022 走看看