aboutsummaryrefslogtreecommitdiff
path: root/test/test_digikey.py
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2019-02-24 21:51:38 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2019-02-24 21:51:38 +0100
commit80e0623913e87c6480049520590e424a831e0401 (patch)
treeff27e1d269cac886dd06ab4f9924719f84794e38 /test/test_digikey.py
parent8aae5d032dd30118b6d992018391a8bd5be759e4 (diff)
downloadee-python-80e0623913e87c6480049520590e424a831e0401.tar.gz
ee-python-80e0623913e87c6480049520590e424a831e0401.tar.bz2
ee-python-80e0623913e87c6480049520590e424a831e0401.tar.xz
ee-python-80e0623913e87c6480049520590e424a831e0401.zip
Digikey: replacing requests-based code with selenium.
Adding new tools: digikey-import-parts and digikey-refresh-parts.
Diffstat (limited to 'test/test_digikey.py')
-rw-r--r--test/test_digikey.py55
1 files changed, 10 insertions, 45 deletions
diff --git a/test/test_digikey.py b/test/test_digikey.py
index 97cd943..ccdc083 100644
--- a/test/test_digikey.py
+++ b/test/test_digikey.py
@@ -3,7 +3,6 @@ from itertools import groupby
from pathlib import Path
import pytest
-from selenium import webdriver
import ee.digikey as dk
@@ -11,16 +10,16 @@ basedir = Path(__file__).parent
static_copies = basedir / "digikey" / "static-copies" # type: Path
digikey = dk.Digikey()
-client = dk.DigikeyClient(digikey, cache_dir=basedir / "digikey" / "http_cache", on_download=print)
-driver: webdriver.Chrome = None
+parser = dk.DigikeyParser(digikey)
+client = dk.DigikeyClient(cache_dir=basedir / "digikey" / "static-copies", on_download=print)
force_refresh = False # Set to True to always fetch the updated html files
@pytest.mark.digikey
def test_digikey_1(tmpdir):
- content = cache_file("https://www.digikey.com/products/en?lang=en&site=us&pageSize=10", "TCR2LF18LM(CTTR-ND")
+ content = client.search("TCR2LF18LM(CTTR-ND")
- res = client.parse_string(content)
+ res = parser.parse_string(content)
assert res.response_type == dk.SearchResponseTypes.SINGLE
p = res.products[0]
assert p.part_number == "TCR2LF18LM(CTTR-ND"
@@ -43,9 +42,9 @@ def test_digikey_1(tmpdir):
@pytest.mark.digikey
def test_digikey_2():
- content = cache_file("https://www.digikey.com/products/en?lang=en&site=us&&pageSize=500", "TCR2LF")
+ content = client.product_search("TCR2LF", page_size=500)
- res = client.parse_string(content)
+ res = parser.parse_string(content)
assert res.response_type == dk.SearchResponseTypes.MANY
[print("dpn={}, mpn={}".format(p.part_number, p.mpn)) for p in res.products]
assert len(res.products) > 10
@@ -57,9 +56,9 @@ def test_digikey_2():
dpn = parts[0].part_number
print("Downloading {} as {}".format(mpn, dpn))
- content = cache_file("https://www.digikey.com/products/en?lang=en&site=us&pageSize=500", dpn)
+ content = client.product_search(dpn)
- res = client.parse_string(content)
+ res = parser.parse_string(content)
assert res.response_type == dk.SearchResponseTypes.SINGLE
p = res.products[0]
@@ -69,9 +68,9 @@ def test_digikey_2():
@pytest.mark.digikey
def test_digikey_3():
- content = cache_file("https://www.digikey.com/products/en?lang=en&site=us&pageSize=10", "RS1MTR")
+ content = client.product_search("RS1MTR")
- res = client.parse_string(content)
+ res = parser.parse_string(content)
assert res.response_type == dk.SearchResponseTypes.MANY
[print("dpn={}, mpn={}".format(p.part_number, p.mpn)) for p in res.products]
assert len(res.products) > 0
@@ -80,37 +79,3 @@ def test_digikey_3():
assert p.mpn == "RS1MTR"
assert p.url == "/product-detail/en/smc-diode-solutions/RS1MTR/1655-1501-1-ND/6022946"
-
-def cache_file(url, keyword):
- path = static_copies / "search-{}.html".format(keyword)
-
- if force_refresh and path.is_file():
- path.unlink()
-
- if not path.is_file():
- path.parent.mkdir(parents=True, exist_ok=True)
- from urllib.parse import quote
- url = url + "&keywords=" + quote(keyword)
- print("GET {}".format(url))
-
- global driver
- if driver is None:
- options = webdriver.ChromeOptions()
- driver = webdriver.Chrome(chrome_options=options)
-
- driver.get(url)
-
- with open(str(path), "w") as f:
- f.write(driver.page_source)
- assert path.stat().st_size > 0
-
- with open(str(path), "r") as f:
- content = f.read()
-
- return content
-
-
-try:
- driver.close()
-except Exception:
- pass