Python SnakeSQL Table not found error - python-2.7

I am learning Python and SnakeSQL for my database operations. I am following the examples of both web.py and SnakeSQL's. I combined both codes from their web sites and tried to make the below code worked but I am getting this error:
Traceback (most recent call last):
File ".\app.py", line 9, in
cursor.execute("INSERT INTO Test (dateColumn, numberColumn) VALUES ('2004->11-8', 4)")
File "D:\Python\virtualenvs\new4\textweb\bin\SnakeSQL\driver\base.py", line >1552, in execute
self.info = self.connection._insert(parsedSQL['table'], >parsedSQL['columns'], parsedSQL['sqlValues'], parameters)
File "D:\Python\virtualenvs\new4\textweb\bin\SnakeSQL\driver\base.py", line >1040, in _insert
raise SQLError("Table '%s' not found."%(table))
error.SQLError: Table 'Test' not found.
And here my app.py code:
import web
import SnakeSQL
render = web.template.render('templates/')
connection = SnakeSQL.connect(database='test', autoCreate=True)
connection = SnakeSQL.connect(database='test')
cursor = connection.cursor()
cursor.execute("INSERT INTO Test (dateColumn, numberColumn) VALUES ('2004-11-8', 4)")
urls = (
'/', 'index'
)
class index:
def GET(self, name):
cursor.execute("SELECT * FROM Test")
results = cursor.fetchall()
return render.index(results)
if __name__ == "__main__":
app = web.application(urls, globals())
app.run()
What could be wrong in here?

Related

scrapy TypeError: object() takes no parameters

I am new to Scrapy and trying to crawl a couple of links as a test using Scrapy. Whenever I run scrapy crawl tier1, I get "TypeError: object() takes no parameters" as the following:
Traceback (most recent call last):
File "/Users/btaek/TaeksProgramming/adv/crawler/lib/python2.7/site-packages/twisted/internet/defer.py", line 653, in _runCallbacks
current.result = callback(current.result, *args, **kw)
File "/Users/btaek/TaeksProgramming/adv/crawler/adv_crawler/adv_crawler/spiders/tier1_crawler.py", line 93, in parse
mk_loader.add_xpath('title', 'h1[#class="top_title"]') # Title of the article
File "/Users/btaek/TaeksProgramming/adv/crawler/lib/python2.7/site-packages/scrapy/loader/__init__.py", line 167, in add_xpath
self.add_value(field_name, values, *processors, **kw)
File "/Users/btaek/TaeksProgramming/adv/crawler/lib/python2.7/site-packages/scrapy/loader/__init__.py", line 77, in add_value
self._add_value(field_name, value)
File "/Users/btaek/TaeksProgramming/adv/crawler/lib/python2.7/site-packages/scrapy/loader/__init__.py", line 91, in _add_value
processed_value = self._process_input_value(field_name, value)
File "/Users/btaek/TaeksProgramming/adv/crawler/lib/python2.7/site-packages/scrapy/loader/__init__.py", line 150, in _process_input_value
return proc(value)
File "/Users/btaek/TaeksProgramming/adv/crawler/lib/python2.7/site-packages/scrapy/loader/processors.py", line 28, in __call__
next_values += arg_to_iter(func(v))
TypeError: object() takes no parameters
2017-08-23 17:25:02 [tier1-parse-logger] INFO: Entered the parse function to parse and index: http://news.mk.co.kr/newsRead.php?sc=30000001&year=2017&no=535166
2017-08-23 17:25:02 [tier1-parse-logger] ERROR: Error (object() takes no parameters) when trying to parse <<date>> from a mk article: http://news.mk.co.kr/newsRead.php?sc=30000001&year=2017&no=535166
2017-08-23 17:25:02 [tier1-parse-logger] ERROR: Error (object() takes no parameters) when trying to parse <<author>> from a mk article: http://news.mk.co.kr/newsRead.php?sc=30000001&year=2017&no=535166
2017-08-23 17:25:02 [scrapy.core.scraper] ERROR: Spider error processing <GET http://news.mk.co.kr/newsRead.php?sc=30000001&year=2017&no=535166> (referer: None)
Traceback (most recent call last):
File "/Users/btaek/TaeksProgramming/adv/crawler/lib/python2.7/site-packages/twisted/internet/defer.py", line 653, in _runCallbacks
current.result = callback(current.result, *args, **kw)
File "/Users/btaek/TaeksProgramming/adv/crawler/adv_crawler/adv_crawler/spiders/tier1_crawler.py", line 93, in parse
mk_loader.add_xpath('title', 'h1[#class="top_title"]') # Title of the article
File "/Users/btaek/TaeksProgramming/adv/crawler/lib/python2.7/site-packages/scrapy/loader/__init__.py", line 167, in add_xpath
self.add_value(field_name, values, *processors, **kw)
File "/Users/btaek/TaeksProgramming/adv/crawler/lib/python2.7/site-packages/scrapy/loader/__init__.py", line 77, in add_value
self._add_value(field_name, value)
File "/Users/btaek/TaeksProgramming/adv/crawler/lib/python2.7/site-packages/scrapy/loader/__init__.py", line 91, in _add_value
processed_value = self._process_input_value(field_name, value)
File "/Users/btaek/TaeksProgramming/adv/crawler/lib/python2.7/site-packages/scrapy/loader/__init__.py", line 150, in _process_input_value
return proc(value)
File "/Users/btaek/TaeksProgramming/adv/crawler/lib/python2.7/site-packages/scrapy/loader/processors.py", line 28, in __call__
next_values += arg_to_iter(func(v))
TypeError: object() takes no parameters
And, my spider file (tier1_crawler.py):
# -*- coding: utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import os
sys.path.append(os.path.abspath('..'))
import logging
import scrapy
from scrapy.loader import ItemLoader
from adv_crawler.items import AdvCrawlerItem
from datetime import datetime, date, time
t1_parse_logger = logging.getLogger("tier1-parse-logger")
t1_parse_logger.LOG_FILE = "Tier1-log.txt"
content_type_dic = {
'news': 'news',
}
class Tier1Crawler(scrapy.Spider):
name = "tier1"
def start_requests(self):
urls = ['http://news.mk.co.kr/newsRead.php?sc=30000001&year=2017&no=535982',
'http://news.mk.co.kr/newsRead.php?sc=30000001&year=2017&no=535166',
]
for url in urls:
yield scrapy.Request(url=url, callback=self.parse)
def parse(self, response):
t1_parse_logger.info("Entered the parse function to parse and index: %s" % response.url) # Log at the beginning of the parse function
item_loader = ItemLoader(item=AdvCrawlerItem(), response=response)
if 'mk.co.kr' in response.url:
mk_loader = item_loader.nested_xpath('//div[#id="top_header"]/div[#class="news_title"]/div[#class="news_title_text"]')
try:
mk_loader.add_xpath('date', 'div[#class="news_title_author"]/ul/li[#class="lasttime"]')
except AttributeError: # if the date is not in "lasttime" li tag
mk_loader.add_xpath('date', 'div[#class="news_title_author"]/ul/li[#class="lasttime1"]')
except Exception as e: # in case the error is not AttributeError
t1_parse_logger.error("Error "+"("+str(e)+")"+" when trying to parse <<date>> from a mk article: %s" % response.url)
try:
mk_loader.add_xpath('author', 'div[#class="news_title_author"]/ul/li[#class="author"]')
except AttributeError: # in case there is no author (some mk articles have no author)
item_loader.add_value('author', "None") # ir error, replace with the line below
# item['author'] = "None" # if the above gives any error, replace the above with this line
except Exception as e: # in case the error is not AttributeError
t1_parse_logger.error("Error "+"("+str(e)+")"+" when trying to parse <<author>> from a mk article: %s" % response.url)
item_loader.add_xpath('content', '//div[#id="Content"]/div[#class="left_content"]/div[#id="article_body"]/div[#class="art_txt"]') # Content of the article (entire contents)
mk_loader.add_xpath('title', 'h1[#class="top_title"]') # Title of the article
item_loader.add_value('content_type', content_type_dic['news'])
item_loader.add_value('timestamp', str(datetime.now())) # timestamp of when the document is being indexed
item_loader.add_value('url', response.url) # url of the article
t1_parse_logger.info("Parsed and indexed: %s" % response.url)
return item_loader.load_item()
And, my items.py file:
# -*- coding: utf-8 -*-
import scrapy
from scrapy.loader.processors import Join, MapCompose, TakeFirst
from w3lib.html import remove_tags
def filter_date(value):
if isinstance(value, unicode):
(year, month, day) = str(value.split(" ")[-2]).split(".")
return year+"-"+month+"-"+day
def filter_utf(value):
if isinstance(value, unicode):
return value.encode('utf-8')
class AdvCrawlerItem(scrapy.Item):
author = scrapy.Field(input_processor=MapCompose(remove_tags, TakeFirst, filter_utf),) # Name of the publisher/author
content = scrapy.Field(input_processor=MapCompose(remove_tags, Join, filter_utf),) # Content of the article (entire contents)
content_type = scrapy.Field()
date = scrapy.Field(input_processor=MapCompose(remove_tags, TakeFirst, filter_date),)
timestamp = scrapy.Field() # timestamp of when the document is being indexed
title = scrapy.Field(input_processor=MapCompose(remove_tags, TakeFirst, filter_utf),) # title of the article
url = scrapy.Field() # url of the article
And, pipelines.py file:
import json
from scrapy import signals
from scrapy.exporters import JsonLinesItemExporter
class AdvCrawlerJsonExportPipeline(object):
def open_spider(self, spider):
self.file = open('crawled-articles1.txt', 'w')
def close_spider(self, spider):
self.file.close()
def process_item(self, item, spider):
line = json.dummps(dict(item)) + "\n"
self.file.write(line)
return item
I am aware that "TypeError: object() takes no parameters" error is usually thrown when __init__ method of a class is not defined at all or not defined to take in parameter(s).
However, in the case above, how can i fix the error? Am I doing something wrong using the item loader or nested item loader??
When using scrapy processors you should use the classes to create objects that do the processing:
# wrong
field = Field(output_processor=MapCompose(TakeFirst))
# right
field = Field(output_processor=MapCompose(TakeFirst()))
^^

File writing in Django keeps having IOError

I'm running my app locally and I'm currently having an IOError during my file creation from the database. I am using Django 1.10, MongoDB as my database, and Celery 4.0.2 for my background tasks. The problem occurs in the tasks.py since that is where I access the db then store it in my django subfolder 'analysis_samples'.
Here is the traceback:
[2017-04-15 15:31:08,798: ERROR/PoolWorker-2] Task tasks.process_sample_input[0619194e-4300-4a1d-91b0-20766e048c4a] raised unexpected: IOError(2, 'No such file or directory')
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/celery/app/trace.py", line 367, in trace_task
R = retval = fun(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/celery/app/trace.py", line 622, in __protected_call__
return self.run(*args, **kwargs)
File "/home/user/django_apps/myapp/analysis/tasks.py", line 218, in process_sample_input
with open(sample_file_path, "w+") as f:
IOError: [Errno 2] No such file or directory: u'/home/user/django_apps/myapp/myapp/analysis_samples/58f1cc3c45015d127c3d68c1'
And here is the snippet of tasks.py:
from django.core.files import File
sys.path.append(settings.ANALYSIS_SAMPLES)
import base64
import os, sys
#shared_task(name='tasks.process_sample_input')
def process_sample_input(instance_id):
instance = Sample.objects.get(pk=instance_id)
#many code here..
try:
conn=pymongo.MongoClient(settings.MONGO_HOST, settings.MONGO_PORT)
db = conn.thugfs #connect to GridFS db of thug
thugfs_db = GridFS(db)
except pymongo.errors.ConnectionFailure, e:
logger.error("Could not connect to ThugFS MongoDB: %s" % e)
sample_file_folder = settings.ANALYSIS_SAMPLES
for sample_fs_id in sample_fs_ids:
sample_file = thugfs_db.get(ObjectId(sample_fs_id)).read()
sample_file = base64.b64decode(sample_file) #decode file from database
sample_file_path = os.path.join(sample_file_folder, sample_fs_id)
with open(sample_file_path, "w+") as f:
fileOut = File(f)
fileOut.write(sample_file)
settings.py:
ANALYSIS_SAMPLES = os.path.join(BASE_DIR, 'myapp/analysis_samples')
Can anyone see the point that caused the error? Any help will be appreciated.

ValueError: Missing scheme in request url: h

I am a beginner in scrapy, python. I tried to deploy the spider code in scrapinghub and I encountered the following error. Below is the code.
import scrapy
from bs4 import BeautifulSoup,SoupStrainer
import urllib2
from scrapy.selector import Selector
from scrapy.http import HtmlResponse
import re
import pkgutil
from pkg_resources import resource_string
from tues1402.items import Tues1402Item
data = pkgutil.get_data("tues1402","resources/urllist.txt")
class SpiderTuesday (scrapy.Spider):
name = 'tuesday'
self.start_urls = [url.strip() for url in data]
def parse(self, response):
story = Tues1402Item()
story['url'] = response.url
story['title'] = response.xpath("//title/text()").extract()
return story
is my spider.py code
import scrapy
class Tues1402Item(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
title = scrapy.Field()
url = scrapy.Field()
is the items.py code and
from setuptools import setup, find_packages
setup(
name = 'tues1402',
version = '1.0',
packages = find_packages(),
entry_points = {'scrapy': ['settings = tues1402.settings']},
package_data = {'tues1402':['resources/urllist.txt']},
zip_safe = False,
)
is the setup.py code.
The following is the error.
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/scrapy/core/engine.py", line 126, in _next_request
request = next(slot.start_requests)
File "/usr/local/lib/python2.7/site-packages/scrapy/spiders/init.py", line 70, in start_requests
yield self.make_requests_from_url(url)
File "/usr/local/lib/python2.7/site-packages/scrapy/spiders/init.py", line 73, in make_requests_from_url
return Request(url, dont_filter=True)
File "/usr/local/lib/python2.7/site-packages/scrapy/http/request/init.py", line 25, in init
self._set_url(url)
File "/usr/local/lib/python2.7/site-packages/scrapy/http/request/init.py", line 57, in _set_url
raise ValueError('Missing scheme in request url: %s' % self._url)
ValueError: Missing scheme in request url: h
Thank you in Advance
Your error means that url h is not a valid url. You should print out your self.start_urls and see what urls you have there, you most likely have a string h as your first url.
Seems like your spider iterates through text instead of a list of urls here:
data = pkgutil.get_data("tues1402","resources/urllist.txt")
class SpiderTuesday (scrapy.Spider):
name = 'tuesday'
self.start_urls = [url.strip() for url in data]
Assuming that you store your urls with some separator in urllist.txt file you should split that:
# assuming file has url in every line
self.start_urls = [url.strip() for url in data.splitlines()]

Python- Assertion error when performing nosetests

I am new to web.py . I was following ex52 LPTHW , and I had to perform nosetests on all the tests in tests folder. But I am getting 1 failed test due to assertion error. I tried reading different assertion errors and why they occur , but I cant figure out this one. I tried expanding the errors which the server could possibly show up like 500 Internal , 400 but its still failing the test. I have created the other code just like mentioned in the book: http://learnpythonthehardway.org/book/ex52.html
Here is my traceback:
C:\lpthw\gothonweb>cd tests
C:\lpthw\gothonweb\tests>nosetests
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\web\application.py", line 239, in process
return self.handle()
File "C:\Python27\lib\site-packages\web\application.py", line 230, in handle
return self._delegate(fn, self.fvars, args)
File "C:\Python27\lib\site-packages\web\application.py", line 420, in _delegate
return handle_class(cls)
File "C:\Python27\lib\site-packages\web\application.py", line 396, in handle_class
return tocall(*args)
File "C:\lpthw\gothonweb\bin\app.py", line 29, in GET
return render.hello_form()
File "C:\Python27\lib\site-packages\web\template.py", line 1017, in __getattr__
t = self._template(name)
File "C:\Python27\lib\site-packages\web\template.py", line 1014, in _template
return self._load_template(name)
File "C:\Python27\lib\site-packages\web\template.py", line 1001, in _load_template
raise AttributeError, "No template named " + name
AttributeError: No template named hello_form
F...
======================================================================
FAIL: tests.app_tests.test_index
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\nose\case.py", line 197, in runTest
self.test(*self.arg)
File "C:\lpthw\gothonweb\tests\app_tests.py", line 12, in test_index
assert_response(resp)
File "C:\lpthw\gothonweb\tests\tools.py", line 5, in assert_response
assert status in resp.status, "Expected response %r not in %r" % (status , resp.status)
AssertionError: Expected response '200' not in '500 Internal Server Error'
----------------------------------------------------------------------
Ran 4 tests in 0.562s
FAILED (failures=1)
My tests code: app_tests.py
from nose.tools import *
from bin.app import app
from tests.tools import assert_response
def test_index():
#check that we get a 404 on the / URL
resp = app.request("/")
assert_response(resp, status= "404")
#test our first GET request to /hello
resp = app.request("/hello")
assert_response(resp)
#make sure default values work for the form
resp = app.request("/hello" , method="POST")
assert_response(resp , contains="Nobody")
#test that we get expected values
data = {'name':'Tejas','greet':'Ola!'}
resp = app.request("/hello " , method= "POST", data=data)
assert_response(resp , contains="Zed")
tools.py :
from nose.tools import *
import re
def assert_response(resp, contains=None, matches=None, headers=None, status= "200"):
assert status in resp.status, "Expected response %r not in %r" % (status , resp.status)
if status == "200":
assert resp.data , "Response data is empty."
if contains:
assert contains in resp.data, "Response does not contain %r" % contains
if matches:
reg = re.compile(matches)
assert reg.matches(resp.data), "Response does not match %r" % matches
if headers:
assert_equal(resp.headers , headers)
app.py code:
import web
from gothonweb import map
urls = (
'/game' , 'GameEngine' ,
'/' , 'Index',
)
app = web.application(urls, globals())
#little hack so that debug mode works with sessions
if web.config.get('_session') is None:
store= web.session.DiskStore('sessions')
session= web.session.Session(app, store, initializer={'room':None})
web.config._session = session
else:
session= web.config._session
render = web.template.render('templates/', base="layout")
class Index(object):
def GET(self):
#this is used to "setup" the session with starting values
session.room= map.START
web.seeother("/game")
class GameEngine(object):
def GET(self):
if session.room:
return render.show_room(room= session.room)
else:
#why is there here? do you need it?
return render.you_died()
def POST(self):
form= web.input(action=None)
if session.room and form.action:
session.room= session.room.go(form.action)
else:
session.room= None
if __name__ == "__main__" :
app.run()
After going ahead with the exercise now it gives me 2 import errors:
PS C:\lpthw\gothonweb\tests> nosetests
EE
======================================================================
ERROR: Failure: SyntaxError (invalid syntax (app.py, line 2))
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\nose\loader.py", line 418, in loadTestsFromName
addr.filename, addr.module)
File "C:\Python27\lib\site-packages\nose\importer.py", line 47, in importFromPath
return self.importFromDir(dir_path, fqname)
File "C:\Python27\lib\site-packages\nose\importer.py", line 94, in importFromDir
mod = load_module(part_fqname, fh, filename, desc)
File "C:\lpthw\gothonweb\tests\app_tests.py", line 2, in <module>
from bin.app import app
File "C:\lpthw\gothonweb\bin\app.py", line 2
from gothonweb import map.py
^
SyntaxError: invalid syntax
======================================================================
ERROR: Failure: ImportError (cannot import name map)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\nose\loader.py", line 418, in loadTestsFromName
addr.filename, addr.module)
File "C:\Python27\lib\site-packages\nose\importer.py", line 47, in importFromPath
return self.importFromDir(dir_path, fqname)
File "C:\Python27\lib\site-packages\nose\importer.py", line 94, in importFromDir
mod = load_module(part_fqname, fh, filename, desc)
File "C:\lpthw\gothonweb\tests\map_tests.py", line 2, in <module>
from gothonweb import map
ImportError: cannot import name map
----------------------------------------------------------------------
Ran 2 tests in 0.002s
FAILED (errors=2)
directory structure:
C:/lpthw/
gothonweb/
bin build dist doc sessions Gothonweb templates tests map.py app.py
tests/
map_tests.py
app_tests.py
__init__.py
tools.py
What should I do to fix the error? Thanks for the suggestions.
For this error,
"AttributeError: No template named hello_form"
use an absolute path and see if that works, instead of
"render = web.template.render('templates/', base="layout")
try:
"render = web.template.render('C:/lpthw/gothonweb/bin/templates', base="layout")
Amazingly, your test has nothing to do with app.py.
from nose.tools import *
from bin.app import app
from tests.tools import assert_response
def test_index():
#check that we get a 404 on the / URL
resp = app.request("/")
assert_response(resp, status= "404")
#test our first GET request to /hello
resp = app.request("/hello")
assert_response(resp)
#make sure default values work for the form
resp = app.request("/hello" , method="POST")
assert_response(resp , contains="Nobody")
#test that we get expected values
data = {'name':'Tejas','greet':'Ola!'}
resp = app.request("/hello " , method= "POST", data=data)
assert_response(resp , contains="Zed")
App.py
import web
from gothonweb import map
urls = (
'/game' , 'GameEngine' ,
'/' , 'Index',
)
app = web.application(urls, globals())
#little hack so that debug mode works with sessions
if web.config.get('_session') is None:
store= web.session.DiskStore('sessions')
session= web.session.Session(app, store, initializer={'room':None})
web.config._session = session
else:
session= web.config._session
render = web.template.render('templates/', base="layout")
class Index(object):
def GET(self):
#this is used to "setup" the session with starting values
session.room= map.START
web.seeother("/game")
class GameEngine(object):
def GET(self):
if session.room:
return render.show_room(room= session.room)
else:
#why is there here? do you need it?
return render.you_died()
def POST(self):
form= web.input(action=None)
if session.room and form.action:
session.room= session.room.go(form.action)
else:
session.room= None
if __name__ == "__main__" :
app.run()
Can you see, how they are not related
Your first test says you should get a 404 on the '/' url but that would only happen if '/' didn't exist. Your app.py code clearly shows that '/' calls on GET in the Index class.
Second test, resp = app.request("/hello") now that would give you a 404 error because that url doesn't exist on app.py
The same goes for the third test as '/hello' doesn't exist in your tuple of urls in app.py
And the fifth test as well
Although your tests are invalid, the primary problem you have though is trying to perform the automated test from inside the tests directory; that's wrong.
C:\lpthw\gothonweb\tests> nosetests
This is not the right thing to do, you need to be a directory below for all your imports to work e.g In app_tests you are trying to import bin/app.py but that's not in the 'tests' directory
Do this instead
C:\lpthw\gothonweb> nosetests
This way all the files that you need would be imported
Back to app_tests.py. I'll write out a very simple test that's actually related to app.py
from bin.app import app
from test.tools import assert_response
def test_index():
#check that we get a 303 on the / url
#this is because web.seeother() will always send the browser this http code
resp = app.request("/")
assert_response(resp, status= "303")
def test_game():
#check that we get a 200 on /game
resp = app.request("/game")
assert_response(resp, status = '200')
#check that we have response data on a form sumbit
resp = app.request("/game", method = 'POST')
aseert_response(resp.data)
The assertion_error you getting because your ran nosetests in this directory
/gothroweb/test
you should run it in
/gothroweb
bin docs gothonweb templates tests
./bin:
app.py app.pyc __init__.py __init__.pyc
./docs:
./gothonweb:
__init__.py __init__.pyc
./templates:
hello_form.html index.html layout.html
./tests:
app_tests.py __init__.py test.html tools.pyc
app_tests.pyc __init__.pyc tools.py
cheers

Log warning from Selenium on Django [duplicate]

Whenever I try to construct a string based on self.live_server_url, I get python TypeError messages. For example, I've tried the following string constructions (form 1 & 2 below), but I experience the same TypeError. My desired string is the Live Server URL with "/lists" appended. NOTE: the actual test does succeed to create a server and I can manually access the server, and more specifically, I can manually access the exact URL that I'm trying to build programmatically (e.g. 'http://localhost:8081/lists').
TypeErrors occur with these string constructions.
# FORM 1
lists_live_server_url = '%s%s' % (self.live_server_url, '/lists')
# FORM 2
lists_live_server_url = '{0}{1}'.format(self.live_server_url, '/lists')
self.browser.get(lists_live_server_url)
There is no python error with this form (nothing appended to string), albeit my test fails (as I would expect since it isn't accessing /lists).
self.browser.get(self.live_server_url)
Here is the python error that I'm getting.
/usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/bin/python3.4 /Applications/PyCharm.app/Contents/helpers/pycharm/django_test_manage.py test functional_tests.lists_tests.LiveNewVisitorTest.test_can_start_a_list_and_retrieve_it_later /Users/myusername/PycharmProjects/mysite_proj
Testing started at 11:55 AM ...
Creating test database for alias 'default'...
Traceback (most recent call last):
File "/usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/wsgiref/handlers.py", line 137, in run
self.result = application(self.environ, self.start_response)
File "/usr/local/lib/python3.4/site-packages/django/test/testcases.py", line 1104, in __call__
return super(FSFilesHandler, self).__call__(environ, start_response)
File "/usr/local/lib/python3.4/site-packages/django/core/handlers/wsgi.py", line 189, in __call__
response = self.get_response(request)
File "/usr/local/lib/python3.4/site-packages/django/test/testcases.py", line 1087, in get_response
return self.serve(request)
File "/usr/local/lib/python3.4/site-packages/django/test/testcases.py", line 1099, in serve
return serve(request, final_rel_path, document_root=self.get_base_dir())
File "/usr/local/lib/python3.4/site-packages/django/views/static.py", line 54, in serve
fullpath = os.path.join(document_root, newpath)
File "/usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/posixpath.py", line 82, in join
path += b
TypeError: unsupported operand type(s) for +=: 'NoneType' and 'str'
Am I unknowingly attempting to modify the live_server_url, which is leading to these TypeErrors? How could I programmatically build a string of live_server_url + "/lists"?
Here is the test that I am attempting...
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from django.test import LiveServerTestCase
class LiveNewVisitorTest(LiveServerTestCase):
def setUp(self):
self.browser = webdriver.Chrome()
self.browser.implicitly_wait(3)
def tearDown(self):
self.browser.close()
def test_can_start_a_list_and_retrieve_it_later(self):
#self.browser.get('http://localhost:8000/lists')
#self.browser.get('http://www.google.com')
#lists_live_server_url = '%s%s' % (self.live_server_url, '/lists')
#lists_live_server_url = '{0}{1}'.format(self.live_server_url, '/lists')
lists_live_server_url = self.live_server_url
self.browser.get(lists_live_server_url)
self.assertIn('To-Do', self.browser.title)
header_text = self.browser.find_element_by_tag_name('h1').text
self.assertIn('To-Do', header_text)
See this discussion on Reddit featuring the same error Traceback.
Basically, this is not a problem with anything within the Selenium tests but rather with your project's static file configuration.
From your question, I believe the key line within the Traceback is:
File "/usr/local/lib/python3.4/site-packages/django/views/static.py", line 54, in serve
fullpath = os.path.join(document_root, newpath)
This line indicates that an unsuccessful os.path.join is being attempted within django.views.static.
Set STATIC_ROOT in your project's settings.pyfile and you should be good.
Use StaticLiveServerTestCase instead may help