diff --git a/chapter13/1-wikiUnitTest.py b/chapter13/1-wikiUnitTest.py index c3e05dd..4bf37ab 100644 --- a/chapter13/1-wikiUnitTest.py +++ b/chapter13/1-wikiUnitTest.py @@ -1,52 +1,44 @@ +import re +import random +import unittest from urllib.request import urlopen from urllib.parse import unquote -import random -import re + from bs4 import BeautifulSoup -import unittest + class TestWikipedia(unittest.TestCase): - bsObj = None url = None - def test_PageProperties(self): - global bsObj - global url - url = "http://en.wikipedia.org/wiki/Monty_Python" - #Test the first 100 pages we encounter + # Test the first 100 pages we encounter for i in range(1, 100): bsObj = BeautifulSoup(urlopen(url)) - titles = self.titleMatchesURL() + titles = self.titleMatchesURL(bsObj, url) self.assertEquals(titles[0], titles[1]) - self.assertTrue(self.contentExists()) - url = self.getNextLink() + self.assertTrue(self.contentExists(bsObj)) + url = self.getNextLink(bsObj) print("Done!") - def titleMatchesURL(self): - global bsObj - global url + def titleMatchesURL(self, bsObj, url): pageTitle = bsObj.find("h1").get_text() urlTitle = url[(url.index("/wiki/")+6):] urlTitle = urlTitle.replace("_", " ") urlTitle = unquote(urlTitle) return [pageTitle.lower(), urlTitle.lower()] - def contentExists(self): - global bsObj - content = bsObj.find("div",{"id":"mw-content-text"}) - if content is not None: - return True - return False - - def getNextLink(self): - global bsObj - links = bsObj.find("div", {"id":"bodyContent"}).findAll("a", href=re.compile("^(/wiki/)((?!:).)*$")) - link = links[random.randint(0, len(links)-1)].attrs['href'] - print("Next link is: "+link) - return "http://en.wikipedia.org"+link + def contentExists(self, bsObj): + content = bsObj.find("div", {"id": "mw-content-text"}) + return content is not None + + def getNextLink(self, bsObj): + links = bsObj.find("div", {"id": "bodyContent"}) \ + .findAll("a", href=re.compile("^(/wiki/)((?!:).)*$")) + link = links[random.randint(0, len(links) - 1)].attrs['href'] + print("Next link is: " + link) + return "http://en.wikipedia.org" + link if __name__ == '__main__': unittest.main() diff --git a/chapter13/6-combinedTest.py b/chapter13/6-combinedTest.py index 9ced109..b634bb0 100644 --- a/chapter13/6-combinedTest.py +++ b/chapter13/6-combinedTest.py @@ -1,27 +1,31 @@ +import unittest + from selenium import webdriver -from selenium.webdriver.remote.webelement import WebElement from selenium.webdriver import ActionChains -import unittest + class TestAddition(unittest.TestCase): driver = None + + @classmethod + def setUpClass(cls): + cls.driver = webdriver.PhantomJS(executable_path='') + def setUp(self): - global driver - driver = webdriver.PhantomJS(executable_path='') url = 'http://pythonscraping.com/pages/javascript/draggableDemo.html' - driver.get(url) + self.driver.get(url) def tearDown(self): print("Tearing down the test") def test_drag(self): - global driver - element = driver.find_element_by_id("draggable") - target = driver.find_element_by_id("div2") - actions = ActionChains(driver) + element = self.driver.find_element_by_id("draggable") + target = self.driver.find_element_by_id("div2") + actions = ActionChains(self.driver) actions.drag_and_drop(element, target).perform() - self.assertEqual("You are definitely not a bot!", driver.find_element_by_id("message").text) + self.assertEqual("You are definitely not a bot!", + self.driver.find_element_by_id("message").text) if __name__ == '__main__': - unittest.main() \ No newline at end of file + unittest.main()