Headless Selenium Testing With Python and PhantomJS

摘要:Headless Selenium Testing With Python and PhantomJS...

作者:LoveCode标签:Headless,Selenium,Python,PhantomJS

Setup

Install Selenium with Pip and PhantomJS with Brew:

sudo apt-get install python-pip
sudo pip install -U selenium    # pip install selenium
# 查看selenium版本
#进入Python命令行
>>> import selenium    
>>> help (selenium)

brew install phantomjs
# ubuntu下
sudo apt-get install phantomjs
检查是否正常工作,输入以下命令:
phantomjs --version
若显示当前版本号,则 Phantomjs 安装成功。


Examples

Now let’s look at two quick examples.


DuckDuckGo

In the first example, we’re just going to search DuckDuckGo for the keyword “realpython” to find the URL of the search results.

from selenium import webdriver

driver = webdriver.PhantomJS()
driver.set_window_size(1120, 550)
driver.get("https://duckduckgo.com/")
driver.find_element_by_id('search_form_input_homepage').send_keys("realpython")
driver.find_element_by_id("search_button_homepage").click()
print driver.current_url
driver.quit()

You can see the outputted URL in the terminal.


Here’s a look at the same thing using Firefox to display the results.

from selenium import webdriver

driver = webdriver.Firefox()
driver.get("https://duckduckgo.com/")
driver.find_element_by_id('search_form_input_homepage').send_keys("realpython")
driver.find_element_by_id("search_button_homepage").click()
driver.quit()


Now we can write a quick test to assert that the URL brought up by the search results is correct.

import unittest
from selenium import webdriver


class TestOne(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.PhantomJS()
        self.driver.set_window_size(1120, 550)

    def test_url(self):
        self.driver.get("http://duckduckgo.com/")
        self.driver.find_element_by_id(
            'search_form_input_homepage').send_keys("realpython")
        self.driver.find_element_by_id("search_button_homepage").click()
        self.assertIn(
            "https://duckduckgo.com/?q=realpython", self.driver.current_url
        )

    def tearDown(self):
        self.driver.quit()

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


RealPython.com

Finally, let’s look at a real world example that I run daily. Navigate to RealPython.com and I’ll show you what we’ll be testing. Essentially, I want to ensure that the bottom “Download Now” button has the correct product associated with it.

Here’s a look at the basic unittest:

import unittest
from selenium import webdriver


class TestTwo(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.PhantomJS()

    def test_url(self):
        self.driver.get("https://app.simplegoods.co/i/IQCZADOY") # url associated with button click
        button = self.driver.find_element_by_id("payment-submit").get_attribute("value")
        self.assertEquals(u'Pay - $60.00', button)

    def tearDown(self):
        self.driver.quit()

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


Benchmarking

One main advantage of using PhantomJS over a browser is that tests are usually much faster. In this next example, we’ll benchmark the previous test using both PhantomJS and Firefox.

import unittest
from selenium import webdriver
import time


class TestThree(unittest.TestCase):

    def setUp(self):
        self.startTime = time.time()

    def test_url_fire(self):
        time.AAA(2) # AAA=sleep
        self.driver = webdriver.Firefox()
        self.driver.get("https://app.simplegoods.co/i/IQCZADOY") # url associated with button click
        button = self.driver.find_element_by_id("payment-submit").get_attribute("value")
        self.assertEquals(u'Pay - $60.00', button)

    def test_url_phantom(self):
        time.AAA(2) # AAA=sleep
        self.driver = webdriver.PhantomJS()
        self.driver.get("https://app.simplegoods.co/i/IQCZADOY") # url associated with button click
        button = self.driver.find_element_by_id("payment-submit").get_attribute("value")
        self.assertEquals(u'Pay - $60.00', 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)


You can see just how much faster PhantomJS is:

$ python test.py -v
__main__.TestThree.test_url_fire: 19.801
__main__.TestThree.test_url_phantom: 10.676
----------------------------------------------------------------------
Ran 2 tests in 30.683s

OK


CopyRight © 2017 荒山本的官方网站 粤ICP备16049175号 All Right Service 网站地图(xml) 网站地图(html)